首页 > 学院 > 操作系统 > 正文

LFS Bash脚本小试牛刀 一

2024-06-28 13:27:41
字体:
来源:转载
供稿:网友
LFS Bash脚本小试牛刀 一
题目:将给定的一块硬盘制作为可启动硬盘,操作全程使用二进制程序或者脚本自动完成:  1.系统为一个裁剪后的GNU/linux操作系统;  2.使用init脚本初始化系统,要求能装载网卡驱动,并配置ip地址;  3.最后进入bash shell交互环境。

解:

处理过程鸟瞰

第一步:格式化此磁盘,创建两个主分区,并格式化。此工作在之前的文章《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 do12   if [ "$dev" = "quit" ]13   then14     exit 915   fi16   echo "input a device path like /dev/sd[a-z]"17   read -p "input the device path:" dev18 done19 20 echo "the next step will format the whole disk $dev"21 read -p "input ENTER to quit,input y or yes to continue:" flag22 23 if ! [ "$flag" = "y" -o "$flag" = "yes" ]24 then25   exit 826 fi27 28 echo "delete MBR"29 dd if=/dev/zero of=$dev bs=512 count=130 fdisk -l $dev31 echo "auto partition"32 echo "n33 p34 135 36 +100M37 n38 p39 240 41 +512M42 w43 q44 " | fdisk $dev &>/dev/null45 fdisk -l $dev46 mkfs -t ext3 "$dev"147 mkfs -t ext3 "$dev"248 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/boot55 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 step310         root (hd0,0)11         kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/sda2 selinux=0 init=/sbin/init12         initrd /initramfs-2.6.32-431.el6.x86_64.img13 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/null13   then14     echo "Usage:  cpy_bin_and_so iptables /mnt/root "15     echo "your input cmd:$cmd not found"16     return 117   fi18 19   if ! [ -d "$dest_dir" ]20   then21     echo "Usage:  cpy_bin_and_so iptables /mnt/root "22     echo "your input dir:$dest_dir not exist or not a dir"23     return 224   fi25   26   fullpath=`which "$cmd" | egrep -o "/[^[:space:]]+"` 27   #dirpath=`dirname $fullpath`28   29   #cpy bin & *.so30   for i in "$fullpath" `ldd $fullpath | egrep -o "/[^[:space:]]+"`31   do32     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   done39   40   return 041 }
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 << EOF12 #!/bin/bash13 mount -n -t proc proc /proc14 mount -n -t sysfs sysfs /sys15 insmod /lib/modules/e1000.ko16 ifconfig eth0 172.16.31.98/1617 ifconfig18 busybox clear19 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 Nietzsche23         '24 bash25 EOF26 27 chmod +x /mnt/sysroot/sbin/init28 29 30 sync31 umount ${dev}132 umount ${dev}233 echo "done :)"

附录Ⅰ 实验结果

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

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

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

4.boot:

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

6.重设eth0,检测:

附录Ⅱ 全部代码

  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 $dev105 106 cat > /mnt/boot/grub/grub.conf <<EOF107 default=0108 timeout=5109 title step3110         root (hd0,0)111         kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/sda2 selinux=0 init=/sbin/init112         initrd /initramfs-2.6.32-431.el6.x86_64.img113 EOF114 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 busybox118 do119   cpy_bin_and_so $i /mnt/sysroot120 done121 122 mkdir /mnt/sysroot/lib/modules123 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 << EOF126 #!/bin/bash127 mount -n -t proc proc /proc128 mount -n -t sysfs sysfs /sys129 insmod /lib/modules/e1000.ko130 ifconfig eth0 172.16.31.98/16131 ifconfig132 busybox clear133 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 Nietzsche137         '138 bash139 EOF140 141 chmod +x /mnt/sysroot/sbin/init142 143 sync144 umount ${dev}1145 umount ${dev}2146 echo "done :)"
View Code


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表