LFS Bash脚本小试牛刀 一

题目:
将给定的一块硬盘制作为可启动硬盘,操作全程使用二进制程序或者脚本自动完成:
  1.系统为一个裁剪后的GNU/Linux操作系统;
  2.使用init脚本初始化系统,要求能装载网卡驱动,并配置IP地址;
  3.最后进入bash shell交互环境。

 

解:

  处理过程鸟瞰

LFS Bash脚本小试牛刀 一_第1张图片

  第一步:格式化此磁盘,创建两个主分区,并格式化。此工作在之前的文章《LFS Bash脚本热身任务 一》中已经完成,直接使用其代码。

 1 #!/bin/bash
 2 #kernel_step3.sh
 3 #author: Sen Han
 4 #date:   21:29 2014/3/2
 5 
 6 
 7 echo "input a device path like /dev/sd[a-z]"
 8 read -p "input the device path:" dev
 9 
10 while ! [ -b "$dev" ]
11 do
12   if [ "$dev" = "quit" ]
13   then
14     exit 9
15   fi
16   echo "input a device path like /dev/sd[a-z]"
17   read -p "input the device path:" dev
18 done
19 
20 echo "the next step will format the whole disk $dev"
21 read -p "input ENTER to quit,input y or yes to continue:" flag
22 
23 if ! [ "$flag" = "y" -o "$flag" = "yes" ]
24 then
25   exit 8
26 fi
27 
28 echo "delete MBR"
29 dd if=/dev/zero of=$dev bs=512 count=1
30 fdisk -l $dev
31 echo "auto partition"
32 echo "n
33 p
34 1
35 
36 +100M
37 n
38 p
39 2
40 
41 +512M
42 w
43 q
44 " | fdisk $dev &>/dev/null
45 fdisk -l $dev
46 mkfs -t ext3 "$dev"1
47 mkfs -t ext3 "$dev"2
48 
49 [ -e /mnt/boot ] || mkdir /mnt/boot 
50 [ -d /mnt/boot ] || ( echo "error:/mnt/boot isn't a directory "; exit 1 )
51 [ -e /mnt/sysroot ] || mkdir /mnt/sysroot 
52 [ -d /mnt/sysroot ] || ( echo "error:/mnt/sysroot isn't a directory "; exit 1 )
53 
54 mount "$dev"1 /mnt/boot
55 mount "$dev"2 /mnt/sysroot
View Code

  第二步:将这两个分区挂载到两个宿主机目录,分别是/mnt/boot与/mnt/sysroot,在磁盘上安装grub系统启动管理程序,复制内核压缩映像与initramfs至boot分区,配置grub。

 1 cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /mnt/boot/
 2 cp /boot/initramfs-2.6.32-431.el6.x86_64.img /mnt/boot/
 3 
 4 grub-install --root-directory=/mnt $dev
 5 
 6 cat > /mnt/boot/grub/grub.conf <<EOF
 7 default=0
 8 timeout=5
 9 title step3
10         root (hd0,0)
11         kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/sda2 selinux=0 init=/sbin/init
12         initrd /initramfs-2.6.32-431.el6.x86_64.img
13 EOF

  第三步:初始化根目录,创建必要的目录文件,复制需要的命令与其所使用的库文件,复制网卡驱动,配置系统的初始化脚本init。此处用到了文章《LFS Bash脚本热身任务 二》中的一个函数。

 1 function cpy_bin_and_so() { # cmd=$1  : ls #dest_dir=$2  : /tmp
 2                             #usage:  cpy_bin_and_so iptables /mnt/root  
 3                             #usage:  cpy_bin_and_so cat      /root/mirror  
 4                             #              func      cmd       dir
 5   local cmd="$1"
 6   local dest_dir="$2"
 7   local fullpath=""
 8   local dirpath=""
 9   local filename=""
10   local i=""
11   
12   if ! which "$cmd" &> /dev/null
13   then
14     echo "Usage:  cpy_bin_and_so iptables /mnt/root "
15     echo "your input cmd:$cmd not found"
16     return 1
17   fi
18 
19   if ! [ -d "$dest_dir" ]
20   then
21     echo "Usage:  cpy_bin_and_so iptables /mnt/root "
22     echo "your input dir:$dest_dir not exist or not a dir"
23     return 2
24   fi
25   
26   fullpath=`which "$cmd" | egrep -o "/[^[:space:]]+"` 
27   #dirpath=`dirname $fullpath`
28   
29   #cpy bin & *.so
30   for i in "$fullpath" `ldd $fullpath | egrep -o "/[^[:space:]]+"`
31   do
32     echo "$i"
33     dirpath=`dirname $i`
34     filename=`basename $i`
35     [ -e "${dest_dir}${dirpath}" ] || mkdir -p ${dest_dir}${dirpath} 
36     [ -d "${dest_dir}${dirpath}" ] || ( echo "error:${dest_dir}${dirpath} isn't a directory "; return 2 )
37     cp -f ${i} ${dest_dir}${dirpath}/${filename}
38   done
39   
40   return 0
41 }
View Code
 1 mkdir /mnt/sysroot/{etc,usr,var,proc,sys,dev,lib,lib64,bin,sbin,boot,srv,mnt,media,home,root}
 2 declare i=''
 3 for i in `ls /bin` insmod modprobe ifconfig busybox
 4 do
 5   cpy_bin_and_so $i /mnt/sysroot
 6 done
 7 
 8 mkdir /mnt/sysroot/lib/modules
 9 cp /lib/modules/2.6.32-431.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib/modules/
10 
11 cat > /mnt/sysroot/sbin/init << EOF
12 #!/bin/bash
13 mount -n -t proc proc /proc
14 mount -n -t sysfs sysfs /sys
15 insmod /lib/modules/e1000.ko
16 ifconfig eth0 172.16.31.98/16
17 ifconfig
18 busybox clear
19 echo ' '
20 echo '  He who fights with monsters might take care lest he thereby become a monster. And if you gaze for long into an abyss, the abyss gazes also into you.
21 
22         --Friedrich Nietzsche
23         '
24 bash
25 EOF
26 
27 chmod +x /mnt/sysroot/sbin/init
28 
29 
30 sync
31 umount ${dev}1
32 umount ${dev}2
33 echo "done :)"

 

 附录Ⅰ  实验结果

   1.在宿主机上添加一新磁盘:

LFS Bash脚本小试牛刀 一_第2张图片

   2.启动宿主机,运行kernel.3.sh脚本,对新磁盘进行处理:

LFS Bash脚本小试牛刀 一_第3张图片

  3.关闭宿主机,新建一虚拟机,加入处理后的磁盘,调整好boot启动次序:

LFS Bash脚本小试牛刀 一_第4张图片  4.boot:

LFS Bash脚本小试牛刀 一_第5张图片

  5.启动完毕,检查网络设置: 

LFS Bash脚本小试牛刀 一_第6张图片

   6.重设eth0,检测:LFS Bash脚本小试牛刀 一_第7张图片

 

  附录Ⅱ  全部代码

  1 #!/bin/bash
  2 #kernel_step3.sh
  3 #author: Sen Han
  4 #date:   21:29 2014/3/2
  5 
  6 
  7 echo "input a device path like /dev/sd[a-z]"
  8 read -p "input the device path:" dev
  9 
 10 while ! [ -b "$dev" ]
 11 do
 12   if [ "$dev" = "quit" ]
 13   then
 14     exit 9
 15   fi
 16   echo "input a device path like /dev/sd[a-z]"
 17   read -p "input the device path:" dev
 18 done
 19 
 20 echo "the next step will format the whole disk $dev"
 21 read -p "input ENTER to quit,input y or yes to continue:" flag
 22 
 23 if ! [ "$flag" = "y" -o "$flag" = "yes" ]
 24 then
 25   exit 8
 26 fi
 27 
 28 echo "delete MBR"
 29 dd if=/dev/zero of=$dev bs=512 count=1
 30 fdisk -l $dev
 31 echo "auto partition"
 32 echo "n
 33 p
 34 1
 35 
 36 +100M
 37 n
 38 p
 39 2
 40 
 41 +512M
 42 w
 43 q
 44 " | fdisk $dev &>/dev/null
 45 fdisk -l $dev
 46 mkfs -t ext3 "$dev"1
 47 mkfs -t ext3 "$dev"2
 48 
 49 [ -e /mnt/boot ] || mkdir /mnt/boot 
 50 [ -d /mnt/boot ] || ( echo "error:/mnt/boot isn't a directory "; exit 1 )
 51 [ -e /mnt/sysroot ] || mkdir /mnt/sysroot 
 52 [ -d /mnt/sysroot ] || ( echo "error:/mnt/sysroot isn't a directory "; exit 1 )
 53 
 54 mount "$dev"1 /mnt/boot
 55 mount "$dev"2 /mnt/sysroot
 56 
 57 
 58 function cpy_bin_and_so() { # cmd=$1  : ls #dest_dir=$2  : /tmp
 59                             #usage:  cpy_bin_and_so iptables /mnt/root  
 60                             #usage:  cpy_bin_and_so cat      /root/mirror  
 61                             #              func      cmd       dir
 62   local cmd="$1"
 63   local dest_dir="$2"
 64   local fullpath=""
 65   local dirpath=""
 66   local filename=""
 67   local i=""
 68   
 69   if ! which "$cmd" &> /dev/null
 70   then
 71     echo "Usage:  cpy_bin_and_so iptables /mnt/root "
 72     echo "your input cmd:$cmd not found"
 73     return 1
 74   fi
 75 
 76   if ! [ -d "$dest_dir" ]
 77   then
 78     echo "Usage:  cpy_bin_and_so iptables /mnt/root "
 79     echo "your input dir:$dest_dir not exist or not a dir"
 80     return 2
 81   fi
 82   
 83   fullpath=`which "$cmd" | egrep -o "/[^[:space:]]+"` 
 84   #dirpath=`dirname $fullpath`
 85   
 86   #cpy bin & *.so
 87   for i in "$fullpath" `ldd $fullpath | egrep -o "/[^[:space:]]+"`
 88   do
 89     echo "$i"
 90     dirpath=`dirname $i`
 91     filename=`basename $i`
 92     [ -e "${dest_dir}${dirpath}" ] || mkdir -p ${dest_dir}${dirpath} 
 93     [ -d "${dest_dir}${dirpath}" ] || ( echo "error:${dest_dir}${dirpath} isn't a directory "; return 2 )
 94     cp -f ${i} ${dest_dir}${dirpath}/${filename}
 95   done
 96   
 97   return 0
 98 }
 99 
100 
101 cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /mnt/boot/
102 cp /boot/initramfs-2.6.32-431.el6.x86_64.img /mnt/boot/
103 
104 grub-install --root-directory=/mnt $dev
105 
106 cat > /mnt/boot/grub/grub.conf <<EOF
107 default=0
108 timeout=5
109 title step3
110         root (hd0,0)
111         kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/sda2 selinux=0 init=/sbin/init
112         initrd /initramfs-2.6.32-431.el6.x86_64.img
113 EOF
114 
115 mkdir /mnt/sysroot/{etc,usr,var,proc,sys,dev,lib,lib64,bin,sbin,boot,srv,mnt,media,home,root}
116 declare i=''
117 for i in `ls /bin` insmod modprobe ifconfig busybox
118 do
119   cpy_bin_and_so $i /mnt/sysroot
120 done
121 
122 mkdir /mnt/sysroot/lib/modules
123 cp /lib/modules/2.6.32-431.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib/modules/
124 
125 cat > /mnt/sysroot/sbin/init << EOF
126 #!/bin/bash
127 mount -n -t proc proc /proc
128 mount -n -t sysfs sysfs /sys
129 insmod /lib/modules/e1000.ko
130 ifconfig eth0 172.16.31.98/16
131 ifconfig
132 busybox clear
133 echo ' '
134 echo '  He who fights with monsters might take care lest he thereby become a monster. And if you gaze for long into an abyss, the abyss gazes also into you.
135 
136         --Friedrich Nietzsche
137         '
138 bash
139 EOF
140 
141 chmod +x /mnt/sysroot/sbin/init
142 
143 sync
144 umount ${dev}1
145 umount ${dev}2
146 echo "done :)"
View Code

 

 

 

你可能感兴趣的:(bash)