本文内容是讲述,基于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 [\033[32mOK\033[0m]"
- else
- echo -e "Make initrd [\033[31mFail\033[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 \033[31mBuilt-in-Linux\033[0m "
- echo -e "Remounting the root filesystem ..........[ \033[32mOK\033[0m ]"
- mount -t proc proc /proc
- mount -t sysfs sysfs /sys
- mount -o remount,rw /
- echo -e "Creating the files of device ............[ \033[32mOK\033[0m ]"
- mdev -s
- echo -e "Mounting the filesystem .................[ \033[32mOK\033[0m ]"
- mount -a
- swapon -a
- echo -e "Starting the log daemon .................[ \033[32mOK\033[0m ]"
- syslogd
- klogd
- echo -e "Configuring loopback interface ..........[ \033[32mOK\033[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 [\033[32mOK\033[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 [\033[32mOK\033[0m]"
- else
- echo -e "Install grub [\033[31mFail\033[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 [\033[32mOK\033[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 \r
- EOF
- echo -e "Build banner [\033[32mOK\033[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 [\033[32mOK\033[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 [\033[32mOK\033[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 [\033[32mOK\033[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 "\033[31m Begin configure\033[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 [\033[32mOK\033[0m]"
- else
- echo -e "Install dropbear [\033[31mFaile\033[0m]"
- fi
- unset NU91 NU911 NU92 NU922 NU93 NU933
到此一个10.8M的linux就制作好了,并且其中包含了大多数常用命令,而且还可以ssh远程连接!