Linux系统裁剪笔记之四busybox

1.什么裁剪?

本篇文章的主要目的是让笔者和读者更深的认识Linux系统的运作方式,大致内容就是把Linux拆开自己一个个组件来组装,然后完成一个微型的Linux系统.下面,让我们来实现吧..写的不好的地方请指教.


2.原理

大家都知道,操作系统的启动流程是(主要是Linux):POST—>BIOS—>MBR—kernel-->initrd-->sbin/init,

POST,BIOS都不是我们管的,所以这里我们从MBR开始,Linux的系统引导先主要是用的grub这个软件,grub引导系统了,然后启动内核,内核调用initrd来实现最基本的操作系统.


3.实验 (所有操作均在虚拟机上实现,且定义硬盘时选IDE)


Kernel + initrd(busybox制作,提供ext3文件系统模块) + ROOTFS (busybox制作)

    硬件驱动:initrd

      initrd: 仅需要提供内核访问真正的根文件系统所在设备需要的驱动

           存储设备和文件系统相关的模块

      系统初始化rc.sysinit: 初始其它硬件的驱动程序;

       

    ROOTFS: busybox,init不支持运行级别

           /etc/inittab:格式也不尽相同 ash,hush  bash


   编译busybox:  (下载地址:https://busybox.net/)

   IDE:

      /dev/hda1: ext3 /boot

      /dev/hda2: ext3 /


  一、加电后系统初始化,以启动initrd。

[root@test tmp]# tar -xf linux-2.6.38.5.tar.bz2  #解压
[root@test mtd]# cd /tmp/busybox-1.20.2/

[root@test mtd]# mkdir /tmp/busybox-1.20.2/include/mtd

[root@test include]# cp /tmp/linux-2.6.38.5/include/mtd/ubi-user.h ./mtd/   #复制编译依赖的包

[root@test include]# cp /tmp/linux-2.6.38.5/include/mtd/mtd-abi.h ./mtd/


[root@JackTest busybox-1.20.2]# make install   #编译成功后生成_install文件

[root@JackTest busybox-1.20.2]# ll _install
total 28
drwxr-xr-x 2 root root 4096 2016-11-29 13:50 bin
lrwxrwxrwx 1 root root   11 2016-11-29 13:50 linuxrc -> bin/busybox
drwxr-xr-x 2 root root 4096 2016-11-29 13:51 sbin
drwxr-xr-x 4 root root 4096 2016-11-29 13:51 usr
[root@JackTest busybox-1.20.2]# du -sh _install

3.0M    _install     #initrd,fsfiles

[root@JackTest busybox-1.20.2]# cp _install  /tmp/busybox -a  #一份用于initrd

[root@JackTest busybox-1.20.2]# cp _install  /tmp/busybox2 -a #一份用于rootfs

[root@JackTest busybox-1.20.2]# cd /tmp/busybox
[root@JackTest busybox]# ll
total 28
drwxr-xr-x 2 root root 4096 2016-11-29 13:50 bin
lrwxrwxrwx 1 root root   11 2016-11-29 13:58 linuxrc -> bin/busybox
drwxr-xr-x 2 root root 4096 2016-11-29 13:51 sbin
drwxr-xr-x 4 root root 4096 2016-11-29 13:51 usr
[root@JackTest busybox]# rm linuxrc
rm: remove symbolic link `linuxrc'? y

[root@JackTest busybox]# mkdir proc sys etc dev tmp mnt/sysroot lib/modules -pv

[root@JackTest busybox]# modinfo ext3    #复制ext3依赖的模块
filename:       /lib/modules/2.6.23.1-42.fc8/kernel/fs/ext3/ext3.ko
...
depends:        jbd

[root@JackTest busybox]# modinfo jbd
filename:       /lib/modules/2.6.23.1-42.fc8/kernel/fs/jbd/jbd.ko

[root@JackTest busybox]# cp /lib/modules/2.6.23.1-42.fc8/kernel/fs/ext3/ext3.ko lib/modules/

[root@JackTest busybox]# cp /lib/modules/2.6.23.1-42.fc8/kernel/fs/jbd/jbd.ko lib/modules/

[root@JackTest busybox]# vim init
#!/bin/sh
#
mount -t proc proc /proc
mount -t sysfs sysfs /sys
insmod /lib/modules/jbd.ko
insmod /lib/modules/ext3.ko

mount -t ext3 /dev/hda2 /mnt/sysroot
exec switch_root  /mnt/sysroot   /sbin/init     #将sysroot权限转交给init

[root@JackTest busybox]# chmod +x init

[root@JackTest busybox]# mknod dev/console c 5 1    #新增console模块
[root@JackTest busybox]# mknod dev/null c 1 3
[root@JackTest busybox]# tree dev
dev
|-- console
`-- nullmdev -s

[root@JackTest busybox]# ls
bin  dev  etc  init  lib  mnt  proc  sbin  sys  tmp  usr
[root@JackTest busybox]# find . |cpio -H newc --quiet -o |gzip -9 > /mnt/boot/initrd.gz
[root@JackTest busybox]# ls -lh /mnt/boot/initrd.gz
-rw-r--r-- 1 root root 937K 2016-11-29 14:33 /mnt/boot/initrd.gz

[root@test boot]# cp /boot/vmlinuz-2.6.18-164.el5PAE ./vmlinuz #复制内核

[root@test boot]# ll
total 2773
-rw-r--r-- 1 root root  959088 Nov 29 14:33 initrd.gz
drwx------ 2 root root   12288 Nov 29 10:42 lost+found
-rw-r--r-- 1 root root 1852596 Nov 29 14:35 vmlinuz


[root@test boot]# grub-install --root-directory=/mnt /dev/hda
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /mnt/boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.

(fd0)    /dev/fd0
(hd0)    /dev/hda    #此系统创建所需的引导程序
(hd1)    /dev/hdb
(hd2)    /dev/sda
[root@test boot]# ls
grub  initrd.gz  lost+found  vmlinuz

[root@test boot]# vi grub/grub.conf
default=0
timeout=3
title Jack Linux(2.6.18)
    root(hd0,0)
    kernel /vmlinuz ro root=/dev/hda2
    initrd /initrd.gz


  二、系统初始化rc.sysinit: 初始其它硬件的驱动程序


[root@JackTest busybox-1.20.2]# cd   /tmp/busybox2

[root@test busybox2]# ls  
bin  linuxrc  sbin  usr

[root@test busybox2]# rm -f linuxrc

[root@test busybox2]# mkdir -pv proc sys dev tmp var/{log,lock,run} lib/modules etc/rc.d/init.d root boot mnt media

[root@test busybox2]# vim etc/inittab

::sysinit:/etc/rc.d/rc.sysinit

console::respawn:-/bin/bash       #/bin/sh改为/bin/bash

::ctrlaltdel:/sbin/reboot

::shutdown:/bin/umount -a -r

[root@test busybox2]# vim etc/fstab

sysfs           /sys    sysfs   defaults       0 0

proc            /proc  proc    defaults        0 0

/dev/hda1   /boot  ext3    defaults        0 0

/dev/hda2   /         ext3    defaults        1 1


[root@test busybox2]# ls bin   #busybox中已有97个命令

addgroup chown dmesg getopt iplink lsattr mt ps setarch

adduser conspy dnsdomainname grep  iproute lzop mv  pwd setseri

ash cp dumpkmap gunzip iprule makemime netstat reformime sh     

base64 cpio echo gzip iptunnel mkdir nice rev sleep  

busybox cttyhack ed  hostname kill mknod pidof rm  stat   

cat date egrep  hush linux32 mktemp ping rmdir stty   

catv dd  false iostat linux64 more ping6  rpm su   

chattr delgroup  fdflush ip ln  mount pipe_progress run-parts sync

chgrp deluser fgrep ipaddr login mountpoint powertop scriptreplay tar

chmod df  fsync ipcalc ls mpstat printenv sed touch


[root@test busybox2]# ls sbin/   #busybox中已有61个命令

acpid devmem getty ifup lsmod mkfs.minix poweroff slattach sy

adjtimex fbsplash halt init makedevs mkfs.vfat raidautorun start-stop-daemon tu

arp fdisk hdparm insmod man mkswap  reboot sulogin vc

blkid  findfs hwclock klogd mdev modinfo rmmod swapoff wa

blockdev freeramdisk  ifconfig loadkmap  mkdosfs modprobe route swapon zc

bootchartd fsck ifdown logread  mke2fs  nameif runlevel switch_root

depmod fsck.minix ifenslave losetup mkfs.ext2 pivot_root setconsole sysctl

[root@test busybox2]# mknod dev/console c 5 1
[root@test busybox2]# mknod dev/null c 1 3

[root@test busybox2]# ll dev/
total 0
crw-r--r-- 1 root root 5, 1 Nov 29 15:42 console
crw-r--r-- 1 root root 1, 3 Nov 29 15:42 null


[root@test busybox2]# vim etc/rc.d/rc.sysinit
[root@test busybox2]# cat etc/rc.d/rc.sysinit
#!/bin/sh
echo -e "\tWelcome to  \033[31mJack XXX XXX\033[0m Linux"

echo -e "Remounting the root filesystem ...[  \033[31mOK\033[0m  ]"
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -o  remount,rw  /

echo -e "Creating the files of device ...  [  \033[31mOK\033[0m  ]"
mdev -s

echo -e "Mounting the filesystem ...       [  \033[31mOK\033[0m  ]"
mount -a
swapon -a
[root@test busybox2]# chmod +x etc/rc.d/rc.sysinit

[root@test busybox2]# cp -a ./* /mnt/sysroot/
[root@test busybox2]# ls /mnt/sysroot/
bin  boot  dev  etc  lib  lost+found  media  mnt  root  sbin  sys  tmp  usr  var
[root@test sysroot]# du -sh
4.0M   

[root@test boot]# du -sh
2.9M   

[root@test ~]# ./bincopy.sh
Your command: bash

[root@test bin]# rm /mnt/sysroot/bin/sh
[root@test bin]# ln -sv bash sh

wKiom1g-glyBwOLNAAGNt_Bs8eQ932.png-wh_50


[root@test sysroot]# modinfo e1000    #新增启动网络模块,请确认网卡型号

[root@test sysroot]#cp /lib/modules/2.6.18-164.el5PAE/kernel/drivers/net/e1000/e1000.ko lib/modules

[root@test sysroot]# vim /mnt/sysroot/etc/rc.d/init.d/network  (参考:Linux系统裁剪笔记之二insmod)

[root@test sysroot]# vim etc/rc.d/rc.sysinit 

...  #新增以下内容

echo "Initalizing network device ..."
/sbin/insmod /lib/modules/e1000.ko
[ $? -eq 0 ] && success "Initializing network device" || failure "Initializing network device"
 
ifconfig lo 127.0.0.1/8
[ $? -eq 0 ] && success "Activating loopback network" || failure "Activating loopback network"

ifconfig eth0 10.119.131.208/22 up
[ $? -eq 0 ] && success "Activating eth0 network" || failure "Activating eth0 network"



4、将 etc/inittab文件改为如下内容:

::sysinit:/etc/init.d/rc.sysinit

#console::respawn:-/bin/bash

::respawn:/sbin/getty9600 tty1

::respawn:/sbin/getty9600 tty2

::respawn:/sbin/getty9600 tty3

::respawn:/sbin/getty9600 tty4

::respawn:/sbin/getty9600 tty5

::respawn:/sbin/getty9600 tty6

::shutdown:/bin/umount -a -r

::ctrlaltdel:/sbin/reboot


4、在系统登录时提供banner信息

# vi /mnt/sysroot/etc/issue

Welcome to Jack Linux XXXXX62 ...
Kernel \r

wKiom1g_x8PR5nNUAAFmc6Vki2M536.png-wh_50


5、在系统登录时显示用户信息

[root@test sysconfig]# cat /mnt/sysroot/etc/sysconfig/network
HOSTNAME=Jack.test.com

[root@test etc]# vim rc.d/rc.sysinit

... #新增以下内容

echo "Set the hostname..."
[ -f /etc/sysconfig/network ] && . /etc/sysconfig/network
[ -z $HOSTNAME -o "HOSTNAME" == '(none)' ] && HOSTNAME=localhost
/bin/hostname $HOSTNAME
[ $? -eq 0 ] && success "Set the hostname" || failure "Set the hostname"

wKiom1g_zDmAiwlfAAF2zV1UfGs577.png-wh_50


6、在系统登录时提供日志功能

[root@test sysroot]# vim etc/rc.d/rc.sysinit

... #新增以下内容

echo "Strat syslog..."
syslogd
[ $? -eq 0 ] && success "Start syslog" || failure "Start syslog"
klogd
[ $? -eq 0 ] && success "Start klog" || failure "Start klog"

[root@test sysroot]# cat etc/syslog.conf
*.info;auth.none        /var/log/messages
auth.*                /var/log/secure

[root@test sysroot]# ll var/log    #新建以下两个文件,且改为600权限
total 48
-rw------- 1 root root 47913 Dec  5 15:46 messages
-rw------- 1 root root     0 Dec  5 15:50 secure

wKioL1hFIaSBpMstAAITzxIZ1_8165.png-wh_50

7、在系统登录时提供SSH功能

[root@test sysroot]# cat etc/shells
/bin/sh
/bin/bash
/bin/hush
/bin/ash
[root@test sysroot]# vim etc/fstab
[root@test sysroot]# cat etc/fstab
sysfs       /sys   sysfs   defaults        0 0
proc        /proc  proc    defaults        0 0
devpts        /dev/pts devpts mode=620       0 0
/dev/hda1   /boot  ext3    defaults        0 0
/dev/hda2   /      ext3    defaults        1 1
[root@test sysroot]# ls
bin  boot  dev  etc  lib  lost+found  media  mnt  proc  root  sbin  sys  tmp  usr  var
[root@test sysroot]# mkdir dev/pts


[root@test sysroot]# cat etc/shells
/bin/sh
/bin/bash
/bin/hush
/bin/ash
[root@test sysroot]# cat etc/fstab
sysfs       /sys   sysfs   defaults        0 0
proc        /proc  proc    defaults        0 0
devpts        /dev/pts devpts mode=620       0 0
/dev/hda1   /boot  ext3    defaults        0 0
/dev/hda2   /      ext3    defaults        1 1

[root@test sysroot]# mkdir etc/dropbear
[root@test sysroot]# dropbearkey -t rsa -f /mnt/sysroot/etc/dropbear/dropbear_rsa_host_key -s 2048
Will output 2048 bit rsa secret key to '/mnt/sysroot/etc/dropbear/dropbear_rsa_host_key'
Generating key, this may take a while...
Public key portion is:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAnGoBr5U1wMb0bsNmBC5PsX+e0tTc9j+UY/T/9cG2SrBbUcLZ/+vlHFmmrxMr3N/hJZBEFHwDoOxffnx6v7GhVFXlcWhNajq7DuSdLqCmknKAd/BzYU/61iQKTqGECLW7cg0sF30NE65cquumEfctUVbIhZ4m33B6NqBBIRUz/3pujbsmOoIAXnpIyVMKpQOHcDEER9dLC2JzBJ5iM2ZE7eaSW/Ddw3SYB3UrbE43A1xHZbXPpVSbr9GQS3r6DvgwFesUd1PPCLl8+5vq2EkCwWd3wiJkzhcI0TALyNaZnftPr3qQ2Bq3XEPnYMeA85ZRe65JRvSAoGqLy7XcPvSrLLLRw== root@test
Fingerprint: md5 3c:04:cf:fd:8c:89:a5:a8:3e:a8:90:bc:22:56:91:1e

[root@test sysroot]# dropbearkey -t dss -f /mnt/sysroot/etc/dropbear/dropbear_dss_host_key
Will output 1024 bit dss secret key to '/mnt/sysroot/etc/dropbear/dropbear_dss_host_key'
Generating key, this may take a while...
Public key portion is:
ssh-dss AAAAB3NzaC1kc3MAAACBANqb58a9g4TENxtXPl4hjM2Gkgp+GHcDn8DYQRJQDdQLr0GwwoA3xJWrB1geAFUem9vDcpQ5/oa0tEr300IyXUhTaldGj9C5yqQXPsCoJZOLBr5FZCpbBJ9D7xljAVLMXJhL4yB4hDbR+U1keP9eHe2PuuamD7VN8obAfG0MP25XAAAAFQD/eG5E3tlk6wG7NXmGqKNrFTgbMwAAAIEAjBh2Omy+VwqMSp7K0X2LkzN95VYPK7HpWIzeVPscdKzU4USr+Z2axmYlztzk+BaFkdhh3GO8FitUNjLsjIoOoPtPBZ1u0mc3tetnoTtjjJTaRrXstnaW4tR0844PGYo8+CCMnN6tmVUTxefNuo68FhRVp14kjcS73nENm7prqtEAAACBAMfr2mi48f36s+Dl0g+o0TefmV5GpP7W4wEV3yXzN/2loJS8Sp1yL4JbdMykpJQeOc5vl/GWRveV1tuudW2wI30O22Srpb6KZaV8BnMRthJtfUo8IEOctu4dqZTazoIP02tHiUTps1nBxH1VF2ZI0Veun0oP9/FWEEgVbHsoLl5r root@test
Fingerprint: md5 bf:81:55:ea:64:b3:8d:80:b7:42:3a:2c:f0:0d:68:bc

[root@test sysroot]# ls etc/dropbear/
dropbear_dss_host_key  dropbear_rsa_host_key


[root@test sysroot]# mkdir usr/lib

[root@test sysroot]# cp -d /lib/libnss_files* /mnt/sysroot/lib/

[root@test sysroot]# cp -d /usr/lib/libnss3.so /usr/lib/libnss_files.so /mnt/sysroot/usr/lib/

[root@test sysroot]# cp /etc/nsswitch.conf ./etc/

[root@test sysroot]# ll usr/lib/
total 1242
-rwxr-xr-x 1 root root 1188804 Dec  6 10:23 libnss3.so
lrwxrwxrwx 1 root root      27 Dec  6 10:23 libnss_files.so -> ../../lib/libnss_files.so.2
-rwxr-xr-x 1 root root   75284 Dec  6 10:39 libz.so.1
[root@test sysroot]# ll lib/
total 1848
-rwxr-xr-x 1 root root  125736 Nov 30 15:11 ld-linux.so.2
-rwxr-xr-x 1 root root   45288 Dec  6 10:39 libcrypt.so.1
-rwxr-xr-x 1 root root 1611564 Nov 30 15:11 libc.so.6
-rwxr-xr-x 1 root root   16428 Nov 30 15:11 libdl.so.2
-rwxr-xr-x 1 root root   46680 Dec  6 10:22 libnss_files-2.5.so
lrwxrwxrwx 1 root root      19 Dec  6 10:22 libnss_files.so.2 -> libnss_files-2.5.so
-rwxr-xr-x 1 root root   13276 Nov 30 15:11 libtermcap.so.2
-rwxr-xr-x 1 root root   15164 Dec  6 10:39 libutil.so.1
drwxr-xr-x 2 root root    1024 Nov 30 16:29 modules

[root@test sysroot]# cat etc/nsswitch.conf
passwd:     files
shadow:     files
group:      files
hosts:      files dns

[root@test sysroot]# sync  

以下为启动监听出错时解决办法:

wKioL1hGQJKD2ef6AAGkBhmam38578.png-wh_50

[root@test sysroot]# vi /etc/rc.d/rc.sysinit

... #新增以下内容

echo "Activating SSH Service ..."                                                           
/sbin/dropbear                                                                              
[ $? -eq 0 ] && success "Activated SSH service" || failure "Activated SSH service" 

[root@fox ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address  Foreign Address State PID/Program name   
tcp    0  0 0.0.0.0:22  0.0.0.0:*     LISTEN      522/dropbear
[root@test sysroot]# /bin/dbclient -l root 110.119.131.204 #进入该主机后远程连接其它PC



---end---