为自己打造Linux小系统

 一、前言

Linux操作系统至1991.10.5号诞生以来,就源其开源性和自由性得到了很多技术大牛的青睐,每个Linux爱好者都为其贡献了自己的一份力,不管是在Linux内核还是开源软件等方面,都为我们后来人提供了一个良好的学习和研究环境。做为一个Linuxer,感谢各位前辈们为我们提供一个自由的空间,让我们也能够在学习的同时去研究Linux。

本文主要通过裁剪现有Linux系统,打造一个属于自己的Linux小系统,让其能够装载网卡驱动,并配置IP地址,实现网络功能。

二、原理

启动流程介绍:

制作Linux小系统之前,我们有必要再了解一下Linux的启动流程:

1、首先Linux要通过POST自检,检查硬件设备有没有故障

2、如果有多块启动盘的话,需要在BIOS中选择启动磁盘

3、启动MBR中的bootloader引导程序

4、加载内核文件

5、执行所有进程的父进程、老祖宗init

6、打印欢迎界面

在Linux的启动流程中,加载内核文件时还需要借助别外两个文件:

1)initrd,是CentOS5上用内存模拟的磁盘设备

2)initramfs,是CentOS6上用内存模拟的文件系统

在启程的流程中,init主要是用来做哪些操作的呢?

init通过调用/etc/inittab这个配置文件,然后再去执行/etc/rc.d/rc.sysinit的系统初始化脚本

启发

到Linux打印欢迎界面后,就说明系统已经启动成功,如果我们要制作一个Linux小系统,我们只需要把它在开机流程中用到的各文件都装载到一起,就可以点亮属于我们自己的系统了,而Linux是一个模块化的操作系统,好多功能组件都是通过模块化的工具来实现的,而且支持动态装载和卸载,我们要是想实现某种功能,只需加载相应的模块即可,就可以实现我们的Linux操作系统大瘦身了。

三、操作步骤

1、目标磁盘分区

在宿主机上挂一块新磁盘,命名为soft-Linux,此块磁盘是宿主机上的第二块磁盘,所以这里是/dev/sdb,而到时候挂载到目标主机的时候,因为那里只有这一块磁盘,所以在目标主机上的名称应该是/dev/sda,这个不能搞混了。首先,我们要在目标磁盘上分两个区,并进行格式化。第一个分区500M,用来装引导程序;第二个分区10G,用来装根文件系统。然后再进行挂载操作,将/dev/sdb1挂载到/mnt/boot下,将/dev/sdb2挂载到/mnt/sysroot下。

wKiom1MW2r3h9Ac-AABTTYsC2gk251.png

  1. [[email protected] ~]# mount /dev/sdb1 /mnt/boot 
  2. mount: mount point /mnt/boot does not exist 
  3. [[email protected] ~]# mkdir -p /mnt/boot /mnt/sysroot 
  4. [[email protected] ~]# mount /dev/sdb1 /mnt/boot 
  5. [[email protected] ~]# mount /dev/sdb2 /mnt/sysroot/ 
  6. [[email protected] ~]# 

2、安装grub至目标磁盘

一个系统能启动,就需要引导,所以我们首先要安装一个grub引导程序到我们的新磁盘上,安装grub引导程序主要有两个命令,一个是grub-install,另一个是setup,这里最好使用grub-install来安装。因为:

  1. ①grub-install会安装grub引导第二阶段的文件 
  2. ②setup不会安装第二阶段的引导程序,是安装引导信息到MBR 
  3. 第二个需要注意的地方就是--root-directory=后面接的路径应该是boot目录所在的地方,而不是/mnt/boot,因为boot目录在mnt下;目标磁盘是/dev/sdb 
  4. [[email protected] ~]# grub-install --root-directory=/mnt /dev/sdb 
  5. Probing devices to guess BIOS drives. This may take a long time. 
  6. Installation finished. No error reported. 
  7. This is the contents of the device map /mnt/boot/grub/device.map. 
  8. Check if this is correct or not. If any of the lines is incorrect, 
  9. fix it and re-run the script `grub-install'. 
  10. (fd0)   /dev/fd0 
  11. (hd0)   /dev/sda 
  12. (hd1)   /dev/sdb 
  13. [[email protected] ~]# cd /mnt/boot/ 
  14. [[email protected] boot]# ls 
  15. grub  lost+found 
  16. [[email protected] boot]# cd grub/ 
  17. [[email protected] grub]# ls 
  18. device.map  e2fs_stage1_5  fat_stage1_5  ffs_stage1_5  iso9660_stage1_5  jfs_stage1_5  minix_stage1_5  reiserfs_stage1_5  stage1  stage2  ufs2_stage1_5  vstafs_stage1_5  xfs_stage1_5 
  19. [[email protected] grub]# 

 

 

 

安装完grub后,进入grub目录,会发现没有grub.conf配置文件,这样就导致我们的引导程序是不健全的,所以我们需要手动写一个配置文件在里边,不过这得需要知道内核的版本,等移植完内核版本,再回过头来补充此步。

3、复制内核文件和initrd文件

init是系统中用来产生其它所有进程的程序。它以守护进程的方式存在,其进程号为1,init是所有进程的父进程,老祖宗,所以不移植是不行的。它通过调用/etc/inittab这个配置文件,然后再去执行/etc/rc.d/rc.sysinit的系统初始化脚本。

将内核文件和initrd文件复制到/dev/sdb下的boot目录中。

  1. [[email protected] grub]# cp /boot/vmlinuz-2.6.32-358.el6.x86_64 /mnt/boot/vmlinuz-soft 
  2. [[email protected] grub]# cp /boot/initramfs-2.6.32-358.el6.x86_64.img /mnt/boot/initramfs-soft.img 
  3. [[email protected] grub]# 

 

4、创建目标主机根文件系统

①使用命令行展开创建文件系统

  1. [[email protected] sysroot]# mkdir -pv /mnt/sysroot/{etc/rc.d,usr,var,proc,sys,dev,lib,lib64,bin,sbin,boot,srv,mnt,media,home,root} 
  2. mkdir: created directory `/mnt/sysroot/etc' 
  3. mkdir: created directory `/mnt/sysroot/etc/rc.d' 
  4. mkdir: created directory `/mnt/sysroot/usr' 
  5. mkdir: created directory `/mnt/sysroot/var' 
  6. mkdir: created directory `/mnt/sysroot/proc' 
  7. mkdir: created directory `/mnt/sysroot/sys' 
  8. mkdir: created directory `/mnt/sysroot/dev' 
  9. mkdir: created directory `/mnt/sysroot/lib' 
  10. mkdir: created directory `/mnt/sysroot/lib64' 
  11. mkdir: created directory `/mnt/sysroot/bin' 
  12. mkdir: created directory `/mnt/sysroot/sbin' 
  13. mkdir: created directory `/mnt/sysroot/boot' 
  14. mkdir: created directory `/mnt/sysroot/srv' 
  15. mkdir: created directory `/mnt/sysroot/mnt' 
  16. mkdir: created directory `/mnt/sysroot/media' 
  17. mkdir: created directory `/mnt/sysroot/home' 
  18. mkdir: created directory `/mnt/sysroot/root' 
  19. [[email protected] sysroot]# ls 
  20. bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  proc  root  sbin  srv  sys  usr  var 
  21. [[email protected] sysroot]# 

 

 

②移植bash命令和其库文件到根文件系统

 

  1. [[email protected] mnt]# sh ~/scripts/cporder.sh 
  2. Enter a command: bash 
  3. Enter a command: shutdown 
  4. Enter a command: reboot 
  5. Enter a command: vim 
  6. Enter a command: touch 
  7. Enter a command: mkdir 
  8. Enter a command: rm 
  9. Enter a command: ls 
  10. Enter a command: cat 
  11. Enter a command: less 
  12. Enter a command: ifconfig 
  13. Enter a command: ip 
  14. Enter a command: route 
  15. Enter a command: quit 
  16. quit 
  17. [[email protected] mnt]# sync 
  18. [[email protected] mnt]# sync 
  19. [[email protected] mnt]# ls 
  20. boot  sysroot 
  21. [[email protected] mnt]# cd sysroot/ 
  22. [[email protected] sysroot]# ls 
  23. bin  lib64  sbin  usr 
  24. [[email protected] sysroot]# cd bin/ 
  25. [[email protected] bin]# ls 
  26. bash  cat  ls  mkdir  rm  touch 
  27. [[email protected] bin]# ln -sv bash sh 
  28. `sh' -> `bash' 
  29. [[email protected] bin]# sync 
  30. [[email protected] bin]# 

 

 

附:命令移植脚本

 

  1. #!/bin/bash  
  2. #  
  3. target=/mnt/sysroot  
  4. clearCmd() {  
  5. if which $cmd &> /dev/null; then  
  6. cmdPath=`which --skip-alias $cmd`  
  7. else  
  8. echo "No such command"  
  9. return 5  
  10. fi  
  11. }  
  12. cmdCopy() {  
  13. cmdDir=`dirname $1`  
  14. [ -d ${target}${cmdDir} ] || mkdir -p ${target}${cmdDir}  
  15. [ -f ${target}${1} ] || cp $1 ${target}${cmdDir}  
  16. }  
  17. libCopy() {  
  18. for lib in `ldd $1 | grep -o "/[^[:space:]]\{1,\}"`; do  
  19. libDir=`dirname $lib`  
  20. [ -d ${target}${libDir} ] || mkdir -p ${target}${libDir}  
  21. [ -f ${target}${lib} ] || cp $lib ${target}${libDir}  
  22. done  
  23. }  
  24. while true; do  
  25. read -p "Enter a command: " cmd  
  26. if [ "$cmd" == 'quit' ] ;then  
  27. echo "quit"  
  28. exit 0  
  29. fi  
  30. clearCmd $cmd  
  31. [ $? -eq 5 ] && continue  
  32. cmdCopy $cmdPath  
  33. libCopy $cmdPath  
  34. done  

 

5、为grub提供配置文件

上面移植了内核和initrd文件,我们就可以根据内核版本和initrd版本来编写grub.conf配置文件了:

  1. [[email protected] grub]# vim grub.conf 
  2. default=
  3. timeout=
  4. title   nmshuishui   soft-Linux 
  5. root (hd0,0) 
  6. kernel /vmlinuz-soft ro root=/dev/sda2 quiet selinux=init=/bin/bash 
  7. initrd /initramfs-soft.img 

 

quiet是静默安装,不再显示安装时的一大堆信息。后面要把selinux关掉,而且init要使用/bin/bash,告诉内核不要再去找init程序了。如果不指定这一步,在启动过程中就会报kernel panic(内核恐慌),以为系统就它一个了,没有init进程,恐慌的不行。

6、启动测试

wKiom1MXCoyRcPGEAAAffbLO5Tw465.png

wKioL1MXCqiiMgMpAAAx801vyus733.png

7、特别提醒

如果在vmvare上做此实验,在新建虚拟机创建新磁盘的时候,一定要选“Store virtual disk as a single file”,否则,也会出现内核恐慌kennel panic。

wKiom1MXDPCSJRKsAACBEVw0vmI001.png

四、装载模块,实现网络功能

1、查看宿主机的网卡模块信息

  1. [[email protected] net]# lsmod | grep e1000 
  2. e1000                 170646  0 
  3. [[email protected] net]# 

 

 

2、查看网卡的详细信息

 

  1. [[email protected] net]# modinfo e1000 
  2. filename:       /lib/modules/2.6.32-358.el6.x86_64/kernel/drivers/net/e1000/e1000.ko 
  3. version:        7.3.21-k8-NAPI 
  4. license:        GPL 
  5. description:    Intel(R) PRO/1000 Network Driver 
  6. author:         Intel Corporation, <[email protected]
  7. srcversion:     1D4F1E82BB99EA36D320B1B 
  8. alias:          pci:v00008086d00002E6Esv*sd*bc*sc*i* 
  9. alias:          pci:v00008086d000010B5sv*sd*bc*sc*i* 
  10. alias:          pci:v00008086d00001099sv*sd*bc*sc*i* 
  11. alias:          pci:v00008086d0000108Asv*sd*bc*sc*i* 
  12. alias:          pci:v00008086d0000107Csv*sd*bc*sc*i* 

 

 

 

 

这里查询到了网卡模块的路径,把它复制到/dev/sdb的库文件下:

  1. [[email protected] net]# mkdir -pv /mnt/sysroot/lib64/modules 
  2. mkdir: created directory `/mnt/sysroot/lib64/modules' 
  3. [[email protected] net]# cp /lib/modules/2.6.32-358.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib64/modules/e1000.ko 

 

3、init程序

 现在虽然是模块复制过去了,但是还不能用,而且现在也不满足我们的流程需要,因为连最起码的init程序都没有,如果我们想要这个init,有两个选择,第一,移植宿主系统的,但是格式会复杂一些;所以我们还是先自己动手写脚本吧,把脚本当作init来用,能够让小系统跑起来。init一般在sbin目录下,所以我们要在/dev/sdb2这个分区上编写一个init脚本。

  1. [[email protected] ~]# cd /mnt/sysroot/sbin/ 
  2. [[email protected] sbin]# vim init 
  3. #!/bin/bash 
  4. #print Welcome info 
  5. echo -e "Welcome to \033[34m nmshuishui soft-Linux\033[0m" 
  6. #mount wei wenjian system when the system is running. 
  7. mount -n -t proc proc /proc 
  8. mount -n -t sysfs sysfs /sys 
  9. #mount ethernet driver autl when the system is start. 
  10. insmod /lib64/modules/e1000.ko 
  11. [ $? -eq 0 ] && echo -e "Load e1000 module succeeded                    [\033[32m0K\033[0m]" 
  12. ifconfig lo 172.0.0.1/8 
  13. ifconfig eth0 172.16.251.235/16 
  14. #mount the /dev/sda2 to make it can be write and read. 
  15. mount -n -o remount,rw /dev/sda2 / 
  16. #run /bin/bash 
  17. /bin/bash 

 

 

 

 

写完这个init脚本后,我们要把我们要给其一个执行权限,让其能够被执行;此脚本中还用到mount,insmod这些命令,所以要用上一个脚本把这些命令移植过去。最后还需要把/mnt/boot/grub/grub.conf中的init=/bin/bash换成init=/sbin/init,因为我现在要用这个init脚本来执行系统启动了,再也不需让/bin/bash来替换了。

4、实现网络功能的Linux小系统    

上面的步骤完成后,就可以把/dev/sdb挂到另一台主机上体验我们的私人订制小系统了。

wKioL1MXMwjxhM_qAAA3e897C_o133.png

wKioL1MXMxagfH5AAAArVMaw4I0926.png


https://www.cnblogs.com/liangxiaofeng/p/4958950.html