Linux系统裁剪笔记之四busybox

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux系统裁剪笔记之四busybox相关的知识,希望对你有一定的参考价值。

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 /


[[email protected] tmp]# tar -xf linux-2.6.38.5.tar.bz2
[[email protected] mtd]# cd /tmp/busybox-1.20.2/

[[email protected] include]# cp /tmp/linux-2.6.38.5/include/mtd/ubi-user.h ./mtd/

[[email protected] include]# cp /tmp/linux-2.6.38.5/include/mtd/mtd-abi.h ./mtd/


[[email protected] busybox-1.20.2]# make install   #

[[email protected] 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
[[email protected] busybox-1.20.2]# du -sh _install

3.0M    _install     #initrd,fsfiles

[[email protected] busybox-1.20.2]# cp _install  /tmp/busybox -a

[[email protected] busybox-1.20.2]# cp _install  /tmp/busybox2 -a

[[email protected] busybox-1.20.2]# cd /tmp/busybox
[[email protected] 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
[[email protected] busybox]# rm linuxrc
rm: remove symbolic link `linuxrc‘? y

[[email protected] busybox]# mkdir proc sys etc dev tmp mnt/sysroot lib/modules -pv

[[email protected] busybox]# modinfo ext3
filename:       /lib/modules/2.6.23.1-42.fc8/kernel/fs/ext3/ext3.ko
...
depends:        jbd

[[email protected] busybox]# modinfo jbd
filename:       /lib/modules/2.6.23.1-42.fc8/kernel/fs/jbd/jbd.ko

[[email protected] busybox]# cp /lib/modules/2.6.23.1-42.fc8/kernel/fs/ext3/ext3.ko lib/modules/

[[email protected] busybox]# cp /lib/modules/2.6.23.1-42.fc8/kernel/fs/jbd/jbd.ko lib/modules/

[[email protected] 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

[[email protected] busybox]# chmod +x init

[[email protected] busybox]# mknod dev/console c 5 1
[[email protected] busybox]# mknod dev/null c 1 3
[[email protected] busybox]# ls
bin  dev  etc  init  lib  mnt  proc  sbin  sysroot  usr
[[email protected] busybox]# tree dev
dev
|-- console
`-- nullmdev -s


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

[[email protected] boot]# cp /boot/vmlinuz-2.6.18-164.el5PAE ./vmlinuz #复制内核

[[email protected] 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
[[email protected] 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
[[email protected] boot]# ls
grub  initrd.gz  lost+found  vmlinuz

[[email protected] 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


[[email protected] busybox-1.20.2]# cd   /tmp/busybox2

[[email protected] busybox2]# ls  
bin  linuxrc  sbin  usr

[[email protected] busybox2]# rm -f linuxrc

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

[[email protected] busybox2]# vim etc/inittab

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

console::respawn:-/bin/sh

::ctrlaltdel:/sbin/reboot

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

[[email protected] 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


[[email protected] 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


[[email protected] 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

[[email protected] busybox2]# mknod dev/console c 5 1
[[email protected] busybox2]# mknod dev/null c 1 3

[[email protected] 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


[[email protected] busybox2]# vim etc/rc.d/rc.sysinit
[[email protected] 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[31mOK033[0m  ]"
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -o  remount,rw  /

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

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



---end---

以上是关于Linux系统裁剪笔记之四busybox的主要内容,如果未能解决你的问题,请参考以下文章

Linux系统裁剪笔记之二insmod

Linux系统裁剪笔记之三user

Linux系统裁剪笔记

Linux 系统裁剪--制作一个最小化的Linux iso镜像

Linux 系统裁剪笔记 软盘2

linux-裁剪Linux功能,编译/bin/login, busybox编译linux