本文內容是講述,基於busybox構建嵌入式linux,並且結合Dropbrae打造提供ssh的10.8Mlinux。
國際慣例,廢話不多說,步驟如下:
準備工作:
下載linux-2.6.34.1,下載地址:
http://www.kernel.org/pub/linux/kernel/v2.6/longterm/v2.6.34/linux-2.6.34.11.tar.bz2
下載busybox-1.16.1,下載地址:
http://www.busybox.net/downloads/binaries/1.16.1/busybox-i686
下載dropbear-2012.55,下載地址:
http://down.51cto.com/data/357545
注:由於busybox原生支援IDE硬碟,並不支援SCSI的硬碟,本人實驗SCSI硬碟還未成功,將SCSI的驅動編譯進核心仍然不行,希望博友們知道的不吝賜教,以下以IDE硬碟為基礎。
由於需要編譯核心,以及Busybox,不同的需要具有不同的選項,無法寫進指令碼,所以前面的步驟還是使用命令列完成:
1、新增IDE硬碟:
分割槽hda1作為boot分割槽
分割槽hda2作為根分割槽
分割槽hda3作為swap分割槽
並將其格式化為ext3,這裡不在給出詳細步驟。
2、建立掛載點:
#mkdir -pv /mnt/{boot,sysroot}
#mount /dev/hda1 /mnt/boot/
#mount /dev/hda2 /mnt/sysroot/
3、編譯核心:
#tar xvf linux-2.6.34.1.tar.bz2 -C /usr/src/
#cd /usr/src
#ln -sv linux-2.6.34.1 linux
#cd linux
#cp /root/busybox/kernel-2.6.30.5-i686.cfg ./.config
#make menuconfig
注:kernel-2.6.30.5-i686.cfg為編譯核心的選項模版,可以在附件中下載。
提示:為了實現後面的功能,請務必將檔案系統中的ext3和網路卡的驅動程式直接編譯進核心;因為作者使用的是vmware Workstation虛擬機器,所以,所需的網上驅動是pcnet32的,其它的均可按需要進行選擇。
網路卡驅動:pcnet32
Device Drivers —> Network device support —> Ethernet (10 or 100Mbit) —> EISA, VLB, PCI and on board controllers AMD PCnet32 PCI support
AMD 8111 (new PCI lance) support
ext3檔案系統:
File systems —> Ext3 journalling file system support Default to `data=ordered` in ext3
IDE硬碟驅動:
Device Drivers —> Block devices —> Very old hard disk (MFM/RLL/IDE) driver
SCSI硬碟驅動:
Device Drivers —> SCSI device support —> -*- SCSI device support
[*] SCSI target support
[*] SCSI generic support
[*] SCSI low-level drivers —>
[*] BusLogic SCSI support
[*] SCSI Device Handlers —> [*] LSI RDAC Device Handler
#make SUBDIR=arch/
#cp arch/x86/boot/bzImage /mnt/boot
4、安裝busybox
#tar xvf busybox-1.16.1.tar.bz2 -C /usr/src
#cd /usr/src/busybox-1.16.1
#make menuconfig
Busybox Settings –> Build Options –>Build BusyBox as a static binary (no shared libs)
Installation Options —> (./_install) BusyBox installation prefix,修改其值為/mnt/sysroot
Busybox Settings –> Busybox Library Tuning —> Username completion
Fancy shell prompts
Support for /etc/networks
Query cursor position from terminal
#make install
安裝後的檔案均位於/mnt/sysroot目錄中;但為了建立initrd,並實現讓其啟動以後將真正的檔案系統切換至目標系統分割槽上的rootfs,您還需要複製一份剛安裝在/mnt/sysroot下的busybox至另一個目錄,以實現與真正的根檔案系統分開製作。我們這裡選擇使用/mnt/temp目錄;
#mkdir -pv /mnt/temp
#cp -r /mnt/sysroot/* /mnt/temp/
執行指令碼buildlinux
#chmod +x buildlinux
#./buildlinux
提示:在指令碼執行過程有兩處和使用者互動
1.要求使用者為目標主機設定root使用者的密碼。
2.要求使用者輸入dropbear的原始碼包的完整路徑。
由於初學,指令碼可能寫的不完善,只能接受.tar.bz2的dropbear原始碼包,還請博友們見諒。
指令碼程式碼如下:
- #!/bin/bash
- # Name: buildlinux
- # Description:
- # Author: CZ
- # Version: 1.1.0
- # Datatime: 2012-03-26 12:25:25
- # Usage: buildlinux
- ########################### 1 Make initrd ###########################
- echo -e "[1] Make initrd....."
- cd /mnt/temp
- #[1]Build rootfs
- mkdir -p proc sys etc/init.d tmp dev mnt/sysroot
- #[2]Build equipment file
- mknod dev/console c 5 1
- mknod dev/null c 1 3
- #[3]Make init for initrd
- rm -rf linuxrc
- cat > init << EOF
- #!/bin/sh
- mount -t proc proc /proc
- mount -t sysfs sysfs /sys
- mdev -s
- mount -t ext3 /dev/hda2 /mnt/sysroot
- exec switch_root /mnt/sysroot /init
- EOF
- chmod +x init
- #[5]Make initrd
- find . | cpio --quiet -H newc -o | gzip -9 -n > /mnt/boot/initrd.gz
- if [ $? -eq 0 ];then
- echo -e "Make initrd [ 33[32mOK 33[0m]"
- else
- echo -e "Make initrd [ 33[31mFail 33[0m]"
- fi
- ########################### 2 Make reality filesystem ###########################
- echo -e "[2] Make reality filesystem....."
- cd /mnt/sysroot
- #[1]Build rootfs
- mkdir -p proc sys etc/init.d tmp dev/pts boot var/log boot home root
- chmod -R 700 root/
- #[2]Build equipment file
- mknod dev/console c 5 1
- mknod dev/null c 1 3
- #[3]Build initialization system script file
- cd /mnt/sysroot/etc/init.d
- cat > rcS <<EOF
- #!/bin/sh
- echo -e " Welcome to 33[31mBuilt-in-Linux 33[0m "
- echo -e "Remounting the root filesystem ..........[ 33[32mOK 33[0m ]"
- mount -t proc proc /proc
- mount -t sysfs sysfs /sys
- mount -o remount,rw /
- echo -e "Creating the files of device ............[ 33[32mOK 33[0m ]"
- mdev -s
- echo -e "Mounting the filesystem .................[ 33[32mOK 33[0m ]"
- mount -a
- swapon -a
- echo -e "Starting the log daemon .................[ 33[32mOK 33[0m ]"
- syslogd
- klogd
- echo -e "Configuring loopback interface ..........[ 33[32mOK 33[0m ]"
- ifconfig lo 127.0.0.1/8
- ifconfig eth0 172.16.4.6/16
- # END
- EOF
- chmod +x rcS
- #[4]Build inittab file
- cd /mnt/sysroot
- mv linuxrc init
- cd /mnt/sysroot/etc
- cat > inittab << EOF
- ::sysinit:/etc/init.d/rcS
- ::respawn:/sbin/getty 9600 tty1
- ::respawn:/sbin/getty 9600 tty2
- ::respawn:/sbin/getty 9600 tty3
- ::respawn:/sbin/getty 9600 tty4
- ::respawn:/sbin/getty 9600 tty5
- ::respawn:/sbin/getty 9600 tty6
- ::shutdown:/bin/umount -a -r
- ::ctrlaltdel:/sbin/reboot
- EOF
- #[5]Build fstab file
- cat > fstab << EOF
- sysfs /sys sysfs defaults 0 0
- proc /proc proc defaults 0 0
- devpts /dev/pts devpts mode=622 0 0
- /dev/hda1 /boot ext3 defaults 1 2
- /dev/hda2 / ext3 defaults 1 1
- /dev/hda3 swap swap defaults 0 0
- EOF
- #[6]Build syslog configure file
- cat > syslog.conf << EOF
- *.info /var/log/messages
- EOF
- echo -e "Make reality filesystem [ 33[32mOK 33[0m]"
- ########################### 3 Install grub ###########################
- echo -e "[3] Install grub....."
- grub-install --root-directory=/mnt /dev/hda
- NU31=`echo $?`
- #[1]Build grub.conf file
- cd /mnt/boot/grub
- cat > grub.conf << EOF
- default 0
- timeout 3
- color light-green/black light-green/black
- title Busybox--Linux--Dropbear (2.6.34.1)
- root (hd0,0)
- kernel /bzImage ro root=/dev/hda2 quiet
- initrd /initrd.gz
- EOF
- NU32=`echo $?`
- if [ $NU31 -eq 0 -a $NU32 -eq 0 ];then
- echo -e "Install grub [ 33[32mOK 33[0m]"
- else
- echo -e "Install grub [ 33[31mFail 33[0m]"
- fi
- unset NU31 NU32
- ########################### 4 Build user files ###########################
- echo -e "[4] Build user files....."
- cd /mnt/sysroot
- #[1]Build root passwd
- head -1 /etc/passwd > /mnt/sysroot/etc/passwd
- sed -i "s@/bin/bash@/bin/sh@" /mnt/sysroot/etc/passwd
- chmod 400 /mnt/sysroot/etc/passwd
- #[2]Build root group
- head -1 /etc/group > /mnt/sysroot/etc/group
- #[3]Build root shadow
- read -p "Input a passwd for root: " PASS
- while [ -z $PASS ];do
- read -p "Input a passwd for root" PASS
- done
- NUMBER=`echo $RANDOM$RANDOM`
- PASSWD=`echo $PASS | openssl passwd -1 -salt $NUMBER -stdin`
- echo "root:$PASSWD:15408:0:99999:7:::" > /mnt/sysroot/etc/shadow
- chmod 400 /mnt/sysroot/etc/shadow
- echo -e "Build user files [ 33[32mOK 33[0m]"
- unset PASS NUMBER PASSWD
- ########################### 5 Build banner ###########################
- echo -e "[5] Build banner....."
- cd /mnt/sysroot/etc
- cat > issue << EOF
- Welcome to Busybox-linux(grass51.blog.51cto.com)
- Kernel
- EOF
- echo -e "Build banner [ 33[32mOK 33[0m]"
- ########################### 6 Set Hostname ###########################
- echo -e "[6]Set Hostname config files"
- mkdir /mnt/sysroot/etc/sysconfig
- cd /mnt/sysroot/etc/sysconfig
- echo > network << EOF
- HOSTNAME=grass51.example.com
- EOF
- echo -e "Set Hostname config files [ 33[32mOK 33[0m]"
- ########################### 7 Build profile file ###########################
- echo -e "[7] Build profile file....."
- cd /mnt/sysroot/etc
- cat > profile << EOF
- export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
- export PS1=`[u@h W]$`
- EOF
- echo -e "Build profile file [ 33[32mOK 33[0m]"
- ########################### 8 OutPut PATH,Hostname ###########################
- echo -e "[8] OutPut PATH,HOSTNAME....."
- cd /mnt/sysroot/etc/init.d
- cat >> rcS << EOF
- [ -r /etc/sysconfig/network ] && source /etc/sysconfig/network
- [ -z $HOSTNAME ] && /bin/hostname localhost || /bin/hostname $HOSTNAME
- source /etc/profile
- EOF
- echo -e "OutPut PATH,HOSTNAME [ 33[32mOK 33[0m]"
- ########################### 9 Install dropbear ###########################
- echo -e "[9] Install dropbear....."
- read -p "Input the dropbear Package path[/tmp/dropbear-2012.55.tar.bz2]:" DROP_FILE
- until [ -e $DROP_FILE ];do
- echo -e "$DROP_FILE not extis"
- read -p "Input a right Package path:" DROP_FILE
- done
- tar xf $DROP_FILE
- FILEPATH=`echo ${DROP_FILE##*/}`
- TARPATH=`echo $FILEPATH | sed `s@(.*).tar.bz2@1@``
- cd $TARPATH
- echo -e " 33[31m Begin configure 33[0m"
- sleep 5
- ./configure
- NU91=`echo $?`
- if [ ! $NU91 -eq 0 ];then
- read -p "Sorry,configure error,Do you continue[N/y]" NU911
- case $NU911 in
- y|Y)
- echo ""
- ;;
- *)
- exit
- ;;
- esac
- fi
- make PROGRAMS="dropbear dbclient dropbearkey dropbearconvert scp"
- NU92=`echo $?`
- if [ ! $NU92 -eq 0 ];then
- read -p "Sorry,make error,Do you continue[N/y]" NU922
- case $NU922 in
- y|Y)
- echo ""
- ;;
- *)
- exit
- ;;
- esac
- fi
- make install
- NU93=`echo $?`
- if [ ! $NU93 -eq 0 ];then
- read -p "Sorry,make install error,Do you continue[N/y]" NU933
- case $NU933 in
- y|Y)
- echo ""
- ;;
- *)
- exit
- ;;
- esac
- fi
- cp -a scp /usr/local/bin
- cd ../
- rm -rf $TARPATH
- #[1]Build a explant lib file script
- mkdir -p /mnt/sysroot/{usr/lib,lib}
- for COMM in dropbear dropbearkey dropbearconvert dbclient scp;do
- TARGET=/mnt/sysroot
- COMMAND=`which $COMM | grep -o "/.*"`
- CMDPATH=${COMMAND%/*}
- [ -d $TARGET$CMDPATH ] || mkdir -p $TARGET$CMDPATH
- [ -e $TARGET$COMMAND ] || cp $COMMAND $TARGET$CMDPATH
- for LIBFILE in `ldd $COMMAND | grep -o "/.*lib[^[:space:]]*"`; do
- LIBPATH=${LIBFILE%/*}
- [ -d $TARGET$LIBPATH ] || mkdir -p $TARGET$LIBPATH
- [ -e $TARGET$LIBFILE ] || cp $LIBFILE $TARGET$LIBPATH
- done
- done
- unset COMM TARGET COMMAND CMDPATH LIBPATH
- #[2]Build shells for Dropbear
- cd /mnt/sysroot/etc
- cat > shells << EOF
- /bin/sh
- /bin/ash
- /bin/hush
- EOF
- #[3]Explant nsswitch
- cat > nsswitch.conf << EOF
- passwd: files
- shadow: files
- group: files
- hosts: files dns
- EOF
- #[4]Explant nsswith libfiles
- cd /usr/lib
- cp -a libnss_compat.so libnss_db.so libnss_files.so libnss_dns.so libnssutil3.so
- libnss3.so libnssckbi.so /mnt/sysroot/usr/lib/
- cd /lib
- cp -a libnss_compat* libnss_db* libnss_dns* libnss_files* /mnt/sysroot/lib/
- #[5]Build private key
- mkdir /mnt/sysroot/etc/dropbear
- export PATH=$PATH:/usr/local/bin:/usr/local/sbin
- dropbearkey -t rsa -s 2048 -f /mnt/sysroot/etc/dropbear/dropbear_rsa_host_key
- dropbearkey -t dss -f /mnt/sysroot/etc/dropbear/dropbear_dss_host_key
- if [ $NU91 -eq 0 -a $NU92 -eq 0 -a $NU93 -eq 0 ];then
- echo -e "Install dropbear [ 33[32mOK 33[0m]"
- else
- echo -e "Install dropbear [ 33[31mFaile 33[0m]"
- fi
- unset NU91 NU911 NU92 NU922 NU93 NU933
到此一個10.8M的linux就製作好了,並且其中包含了大多數常用命令,而且還可以ssh遠端連線!