本片文章介绍在宿主机机上面通过bash脚本实现自动化构建Min Linux,从而了解linux系统的启动过程,当系统无法启动时可以有效定位出现错误的原因,在阅读本片文章之前建议先阅读这篇文章: http://urchin.blog.51cto.com/4356076/788394

BASH 脚本代码如下:

首先您需要在宿主机上面添加一块磁盘IDE或者SCSI,它们分别被linux系统识别为hd和sd,并根据您的实际情况修改脚本中的磁盘设备参数,我的实验环境是在Vmware  workstation上实现而且宿主机(RedHat 5.4 OS)上有两块SCSI的磁盘,新添加的磁盘设备是SDB

主代码

 
  
  1. #!/bin/bash 
  2. # Author: urchin, 
  3. # Version:1.0.0 
  4. # Date: 20120309 
  5. # Mail:shouwei19870814@163.com 
  6. ## clear the MBR co
  7. dd if=/dev/zero of=/dev/sdb bs=512 count=1 
  8. ## create partitions 
  9. echo ' 
  10.  
  11. +100M 
  12.  
  13. +128M 
  14. 82 
  15.  
  16. +4G 
  17. w' | fdisk /dev/sdb >/dev/null 2>&1 
  18. ## kernel reread  partition table 
  19. partprobe /dev/sdb 
  20. partprobe /dev/sdb 
  21. sleep 3 
  22. ## format partitions 
  23. mkfs -j /dev/sdb1 >/dev/null 2>&1 
  24. mkswap /dev/sdb2 >/dev/null 2>&1 
  25. mkfs -j /dev/sdb3 >/dev/null 2>&1 
  26. ## create diretory 
  27. mkdir -pv /mnt/{boot,sysroot} 
  28. ## mount partitions 
  29. mount /dev/sdb1 /mnt/boot 
  30. mount /dev/sdb3 /mnt/sysroot 
  31. ## install grub 
  32. grub-install --root-directory=/mnt /dev/sdb 
  33. cd /mnt/sysroot 
  34. mkdir -pv ./{bin,sbin,usr/lib,lib/modules,etc/{rc.d,sysconfig},tmp,mnt,media,var/run,opt,dev,home,root,proc,sys,srv} 
  35. cd /mnt/boot 
  36. ## copy kernel  
  37. cp /boot/vmlinuz-2.6.18-164.el5 vmlinuz 
  38. ## modify and order initrd  
  39. mkdir -pv /tmp/linux 
  40. cd /tmp/linux 
  41. zcat /boot/initrd-2.6.18-164.el5.img | cpio -id  
  42. sed -i "s@/dev/Vol0/LogVol00@/dev/sda3@g" /tmp/linux/init 
  43. find . | cpio -o -H newc --quiet | gzip -9 > /mnt/boot/initrd 
  44. cd /mnt/boot/grub && rm -rf /tmp/linux 
  45. ## create grub.conf  
  46. cat > grub.conf << EOF 
  47. default=0 
  48. timeout=3 
  49. title Min_linux 
  50.     root (hd0,0) 
  51.     kernel /vmlinuz ro root=/dev/sda3 
  52.     initrd /initrd 
  53. EOF 
  54. ## order issue file 
  55. cat > /mnt/sysroot/etc/issue << EOF 
  56. Welcome to Min_linux 
  57. the password of root is redhat 
  58. EOF 
  59. ## copy modules to init Ethernet  
  60. cp /lib/modules/2.6.18-164.el5/kernel/drivers/net/mii.ko /mnt/sysroot/lib/modules 
  61. cp /lib/modules/2.6.18-164.el5/kernel/drivers/net/pcnet32.ko /mnt/sysroot/lib/modules 
  62.  
  63. ## create inittab 
  64. cat > /mnt/sysroot/etc/inittab <<EOF 
  65. id:3:initdefault: 
  66. si::sysinit:/etc/rc.d/rc.sysinit 
  67. l0:0:wait:/etc/rc.d/rc.sysdone 
  68. l1:1:wait:/sbin/init -t1 S 
  69.  
  70. 1:2345:respawn:/sbin/mingetty tty1 
  71. 2:2345:respawn:/sbin/mingetty tty2 
  72. 3:2345:respawn:/sbin/mingetty tty3 
  73. 4:2345:respawn:/sbin/mingetty tty4 
  74. 5:2345:respawn:/sbin/mingetty tty5 
  75. 6:2345:respawn:/sbin/mingetty tty6 
  76. EOF 
  77. ## create link for sh 
  78. cd /mnt/sysroot/bin 
  79. ln -s bash sh 
  80. ## creating rc.sysinit and giving the executive right to it 
  81. cat > /mnt/sysroot/etc/rc.d/rc.sysinit << EOF 
  82. #!/bin/bash 
  83. echo -e "Welcome to My Min_linux" 
  84. [ -r /etc/sysconfig/network ] && source /etc/sysconfig/network 
  85. [ -z $HOSTNAME ] && /bin/hostname localhost || /bin/hostname $HOSTNAME  
  86. echo " insert pcnet32 module." 
  87. /sbin/insmod /lib/modules/mii.ko 
  88. /sbin/insmod /lib/modules/pcnet32.ko 
  89. echon " mount filesystme" 
  90. /bin/mount -n -o remount,rw /dev/sda3 
  91. echo "mount the extra fielsystem" 
  92. mount -n -a 
  93. echo " activing swap filesystem." 
  94. swapon /dev/sda2 
  95. echo "seting loopback" 
  96. ifconfig lo 127.0.0.1/8 
  97. echo "set the kernel parameters." 
  98. /sbin/sysctl -p 
  99.  
  100. EOF 
  101.  
  102. chmod +x /mnt/sysroot/etc/rc.d/rc.sysinit 
  103. ## creating shutdown file and give the executive right to it 
  104. cat > /mnt/sysroot/etc/rc.d/rc.sysdone << EOF 
  105. #!/bin/bash 
  106. /bin/sync 
  107. /bin/sleep 3 
  108. /bin/sync 
  109. exec /sbin/halt -p 
  110. EOF 
  111. ##  give the executive right to it 
  112. chmod +x /mnt/sysroot/etc/rc.d/rc.sysdone 
  113. ## copy login file 
  114. cp /root/login /mnt/sysroot/bin  
  115. ## create /etc/{passwd,group,shadow} 
  116. head -1 /etc/passwd > /mnt/sysroot/etc/passwd 
  117. head -1 /etc/group > /mnt/sysroot/etc/group 
  118. head -1 /etc/shadow > /mnt/sysroot/etc/shadow 
  119. ## change /etc/shadow's right 
  120. chmod 400 /mnt/sysroot/etc/shadow 
  121. ## copy libraries needed by Authentication 
  122. cd /usr/lib  
  123. cp -a libnss_files.so libnss3.so libnssckbi.so libnss_dns.so libnssutil3.so libnss_compat.so libnss_db.so /mnt/sysroot/usr/lib 
  124. cd /lib 
  125. cp -a libnss_compat* libnss_dns* libnss_db* libnss_files* /mnt/sysroot/lib/ 
  126. ## order cdrom facility 
  127. cd /dev 
  128. cp -a cdrom  /mnt/sysroot/dev 
  129. cp -a cdrom-hdc /mnt/sysroot/dev 
  130. cp -a hdc /mnt/sysroot/dev 
  131. ## change the right of /mnt/sysroot/root 
  132. chmod go=--- /mnt/sysroot/root -R 
  133. ## copy vim's configration file 
  134. cp /etc/vimrc /mnt/sysroot/etc 
  135. ## create /mnt/sysroot/etc/nsswitch.conf 
  136. cat > /mnt/sysroot/etc/nsswitch.conf << EOF 
  137. passwd:     files 
  138. shadow:     files 
  139. group:      files 
  140. hosts:      files dns 
  141. EOF 
  142. ## create fstab 
  143. cat >> /mnt/sysroot/etc/fstab << EOF 
  144. /dev/sda2   swap        swap            defaults 0  0 
  145. /dev/sda3  /           ext3            defaults 0  0 
  146. /dev/sda1  /boot       ext3            defaults 0  0 
  147. proc        /proc       proc            defaults 0  0 
  148. devpts      /devpts     devpts       defaults 0  0 
  149. sysfs       /sys        sysfs           defaults 0  0 
  150. tmpfs       /dev/shm    tmpfs           defaults 0  0 
  151. EOF 
  152. ## create /etc/sysconfig/network  file 
  153. cat > /mnt/sysroot/etc/sysconfig/network << EOF 
  154. NETWORKING=yes 
  155. HOSTNAME=www.urchin.org 
  156. EOF 
  157. ## create /etc/sysctl.conf 
  158. cat > /mnt/sysroot/etc/sysctl.conf << EOF 
  159. net.ipv4.ip_forward = 1 
  160. EOF 
  161. ## create /etc/profile 
  162. cat > /mnt/sysroot/etc/profile << EOF 
  163. # /etc/profile 
  164. PS1='[\u@\h \W]\$' 
  165. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin 
  166. export PS1 PATH 
  167. EOF 
  168. ## create /etc/bashrc 
  169. cat > /mnt/sysroot/etc/bashrc << EOF 
  170. # /etc/bashrc 
  171. if [ $UID -gt 99 ] && [ `id -gn` -eq `id -un` ]; then 
  172.     umask 002 
  173. else 
  174.     umask 022 
  175. fi 
  176. EOF 
  177. ## copy applications needed by min_linux 
  178. exec /root/bcp.sh 

复制应用程序以及对应程序运行所需的库文件

 
  
  1. #!/bin/bash  
  2. #Author:urchin  
  3. #Version:1.0.0  
  4. #Date:20120309  
  5. #E-mail:shouwei19870814@163.com  
  6.   
  7. ##define function to copy applications and libraries needed by applications  
  8. function BCP {  
  9.   TARGET=/mnt/sysroot    
  10.   
  11.   COMMAND=`which $1 | grep -o "/.*"`  
  12.   CMDPATH=${COMMAND%/*}  
  13.   
  14.   [ -d $TARGET$CMDPATH ] || mkdir -p $TARGET$CMDPATH  
  15.   [ -e $TARGET$COMMAND ] || cp $COMMAND $TARGET$CMDPATH  
  16.   
  17.   for LIBFILE in `ldd $COMMAND | grep -o "/.*lib[^[:space:]]*"`; do  
  18.     LIBPATH=${LIBFILE%/*}  
  19.     [ -d $TARGET$LIBPATH ] || mkdir -p $TARGET$LIBPATH  
  20.     [ -e $TARGET$LIBFILE ] || cp $LIBFILE $TARGET$LIBPATH  
  21.   done  
  22. }  
  23.   
  24.  
  25. #  
  26. for I in {du,df,dd,kill,fdisk,mkfs,e2fsck,tune2fs,clear,sysctl, \  
  27. chmod,chgrp,chown,ftp,swapoff,swapon,free,init,bash,sync,grep,cat, \  
  28. less,halt,hostname,autoconf,umount,automake,gcc,vim,vi,sleep,yum, \  
  29. ls,insmod,ping,ifconfig,reboot,mount,mingetty,passwd,route,uname, \  
  30. mkdir,ps,tree,rm,cp,mv,netstat,id,tar,ln,touch}; do  
  31.    BCP $I  
  32. done  

当您在扩展您的Min linux时需要用到其他的应用程序及其程序所需的库文件时,您可以使用一下的脚本复制程序及其所需的库文件

 
  
  1. #!/bin/bash 
  2. #Author:urchin 
  3. #Version:1.0.0 
  4. #Date:20120309 
  5. #E-mail:shouwei19870814@163.com 
  6. function BCP { 
  7.   TARGET=/mnt/sysroot   
  8.  
  9.   COMMAND=`which $1 | grep -o "/.*"` 
  10.   CMDPATH=${COMMAND%/*} 
  11.  
  12.   [ -d $TARGET$CMDPATH ] || mkdir -p $TARGET$CMDPATH 
  13.   [ -e $TARGET$COMMAND ] || cp $COMMAND $TARGET$CMDPATH 
  14.  
  15.   for LIBFILE in `ldd $COMMAND | grep -o "/.*lib[^[:space:]]*"`; do 
  16.     LIBPATH=${LIBFILE%/*} 
  17.     [ -d $TARGET$LIBPATH ] || mkdir -p $TARGET$LIBPATH 
  18.     [ -e $TARGET$LIBFILE ] || cp $LIBFILE $TARGET$LIBPATH 
  19.   done 
  20.  
  21. while true; do 
  22.   read -p "A Command: " MYCMD 
  23.   case $MYCMD in 
  24.   q|Q)  
  25.      echo "Quit..." 
  26.      exit 0 
  27.      ;; 
  28.   *) 
  29.     ! which $MYCMD &> /dev/null && echo "Wrong command..." && continue 
  30.     BCP $MYCMD 
  31.     ;; 
  32.   esac 
  33. done 

  注:请确保各个脚本有执行权限,特别是复制application及其运行所需的Libraries的脚本有执行权限,否则会出现错误