写在前面,这篇博文只能让裁剪的系统能够使用简单的bash命令
在裁剪系统之前,先了解能启动系统时,所必需的文件和程序。
1、开机所必需的的bootloader(grub)
2、内核文件、内核镜像文件
3、能执行/sbin/init、有一个能执行命令的bash环境
4、能执行命令时所需的库文件
5、有inittab文件、rc.sysinit执行脚本文件
RHEL 5.x的裁剪
先添加一块硬盘,把裁剪的系统放到新添加的硬盘上,然后用这块新添加的硬盘当启动盘,能够实现裁剪系统的正常启动,则表示裁剪成功。本实验是用vmworkstation虚拟机完成的。
1)添加硬盘,分两个区,一个boot,一个根文件(rootfs)
添加硬盘步骤:VM--->Settings--->Add--->Hard Disk--->next--->Create a new virtual disk--->next---SCSI--->next--->
添加磁盘后重启电脑,然后对磁盘分区。
[root@localhost ~]# fdisk /dev/sdb Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610): +30M Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (6-2610, default 6): Using default value 6 Last cylinder or +size or +sizeM or +sizeK (6-2610, default 2610): +200M Command (m for help): w [root@localhost ~]#partprobe [root@localhost ~]#mkfs -t ext3 /dev/sdb1 &>/dev/null [root@localhost ~]#mkfs -t ext3 /dev/sdb2 &>/dev/null
2)为新添加的磁盘的第一个分区创建grub文件
3)copy内核文件和生成内核镜像文件
[root@localhost opt]# pwd /opt [root@localhost opt]#find . | cpio -H newc --quiet -o |gzip -9 > /mnt/boot/initrd.gz #把opt目录的所有文件利用cpio打包,然后用gzip命令压缩后,输出到/mnt/boot目录下,文件名为initrd.gz
4)复制init和bash等其它命令以及命令的库文件到新添加硬盘的sysroot目录
思路:复制命令时,命令如果要执行,需要依赖该命令所需的库文件,所有在复制命令时,必须把命令依赖的库文件也复制到相应的目录,init的命令位于/sbin/init,库文件位于/lib64(系统为x86_64)下,要将/sbin/init 复制到新硬盘的/mnt/sysroot/sbin目录下,init所依赖的库,也应该复制/mnt/sysroot/lib64目录下
#!/bin/bash DestDir=/mnt/sysroot bincp() { [ -d ${DestDir}${CmdDir} ] || mkdir -p ${DestDir}${CmdDir} [ ! -f ${DestDir}${Cmd} ] && cp $Cmd ${DestDir}${Cmd} && echo "copy $Cmd command to $DestDir finished." } libcp() { for Lib in `ldd $Cmd | grep -o "[^[:space:]]*/lib[^[:space:]]*"`;do LibDir=${Lib%/*} [ -d ${DestDir}${LibDir} ] || mkdir -p ${DestDir}${LibDir} [ ! -f ${DestDir}${Lib} ] && cp $Lib ${DestDir}${Lib} && echo "copy $Lib to $DestDir finished." done } read -p "input a command:" Cmd while [ $Cmd != 'q' -a $Cmd != 'Q' ];do Cmd=`which $Cmd | grep -v "alias" | sed 's@[[:space:]]@@g'` CmdDir=${Cmd%/*} bincp libcp read -p "input a command:" Cmd done printf "=======Copy command is OK=========\n"
脚本的内容
5)建立系统所需的文件和脚本 grub配置文件、inittab文件和rc.sysinit脚本文件
#grub配置文件 cat >> /mnt/boot/grub/grub.conf <> /mnt/sysroot/etc/inittab << EOF id:3:initdefault: si::sysinit:/etc/rc.d/rc.sysinit EOF #rc.sysinit脚本文件,/bin/bash,表示开机自动运行bash,生成一个bash环境 cat >> /mnt/sysroot/etc/rc.d/rc.sysinit < 把系统关机,然后把新添加的硬盘作为一个裁剪的系统看能否启动
在虚拟机里面新建一个虚拟机,安装之前裁剪的系统,看是否能正常运行
RHEL 5.x的裁剪的脚本,该脚本能自动化的完成上述的操作
#!/bin/bash echo -e "\033[31m=======Frist:Formatting a disk partition======\033[0m" echo "Inital a disk..." echo -e "\033[31m Warning:\033[0m" fdisk -l 2>/dev/null | grep -o "^Disk /dev/[sh]d[a-z]" read -p "Pls choice a disk:" DiskPart if [ "$DiskPart" == 'quit' ];then echo "you choice quit." exit 3 fi until fdisk -l 2>/dev/null | grep -o "^Disk /dev/[sh]d[a-z]" | grep "^Disk $DiskPart"&>/dev/null ;do read -p "you choice is wrong,choice again:" DiskPart done read -p "Your choice will destory all data,continue:" Choice until [ $Choice == 'y' -o $Choice == 'n' ];do read -p "Your choice will destory all data,continue:" Choice done if [ $Choice == 'n' ];then echo "you Choice quit." exit 4 else dd if=/dev/zero of=$DiskPart bs=512 count=1 &>/dev/null sync sync sleep 3 echo 'n p 1 #后面有一行空格 +30M n p 2 #后面有一行空格 +200M w'| fdisk $DiskPart &>/dev/null fi partprobe mkfs -t ext3 ${DiskPart}1 &>/dev/null mkfs -t ext3 ${DiskPart}2 &>/dev/null printf "===============Partition is OK=========\n" #以上是分区的脚本 echo -e "\033[31m=======Second:Mount the Partition======\033[0m" [ -d /mnt/boot ] || mkdir -p /mnt/boot [ -d /mnt/sysroot ] || mkdir -p /mnt/sysroot if mount | grep "/dev/sdb1" &>/dev/null;then echo "/dev/sdb1 is mounted..." else mount /dev/sdb1 /mnt/boot fi if mount | grep "/dev/sdb2" &>/dev/null;then echo "/dev/sdb2 is mounted...." else mount /dev/sdb2 /mnt/sysroot fi printf "===============Mount is OK=========\n" echo -e "\033[31m=======Third: Create boot partition======\033[0m" [ -d /mnt/boot/grub ]|| grub-install --root-directory=/mnt /dev/sdb &>/dev/null;echo "grub is ok" printf "========Boot partition is OK============\n" echo -e "\033[31m=======Fourth:Copy the kernel and make the kernel p_w_picpath file======\033[0m" cp /boot/vmlinuz-2.6.18-308.el5 /mnt/boot/vmlinuz rm -rf /opt/* cd /opt mkinitrd /opt/initrd-`uname -r`.img `uname -r` zcat /opt/initrd-`uname -r`.img | cpio -id &>/dev/null rm -rf /opt/initrd-`uname -r`.img sed -i '/resume/s@resume@#resume@g' /opt/init #把resume那行的前面添加#,即注释掉resume开头的行 sed -i 's/mkrootdev/# &/' /opt/init #先把mkrootdev开头的行注释 sed -i '/mkrootdev/a\mkrootdev -t ext3 -o defaults,ro /dev/sda2' /opt/init #然后在mkrootdev那行下面添加一行mkrootdev -t ext3 -o defaults,ro /dev/sda2 find . | cpio -H newc --quiet -o |gzip -9 > /mnt/boot/initrd.gz printf "========Kernel and kernel p_w_picpath file is OK========\n" echo -e "\033[31m=======Fifth:Edit grub configuration file======\033[0m" cat >> /mnt/boot/grub/grub.conf <> /mnt/sysroot/etc/inittab << EOF id:3:initdefault: si::sysinit:/etc/rc.d/rc.sysinit EOF cat >> /mnt/sysroot/etc/rc.d/rc.sysinit < 运行脚本的过程
RHEL 6.x系统的裁剪步骤就详细描述了,直接写脚本
#!/bin/bash echo -e "\033[31m=======Frist:Formatting a disk partition======\033[0m" echo "Inital a disk..." echo -e "\033[31m Warning:\033[0m" fdisk -l 2>/dev/null | grep -o "^Disk /dev/[sh]d[a-z]" read -p "Pls choice a disk:" DiskPart if [ "$DiskPart" == 'quit' ];then echo "you choice quit." exit 3 fi until fdisk -l 2>/dev/null | grep -o "^Disk /dev/[sh]d[a-z]" | grep "^Disk $DiskPart"&>/dev/null ;do read -p "you choice is wrong,choice again:" DiskPart done read -p "Your choice will destory all data,continue:" Choice until [ $Choice == 'y' -o $Choice == 'n' ];do read -p "Your choice will destory all data,continue:" Choice done if [ $Choice == 'n' ];then echo "you Choice quit." exit 4 else dd if=/dev/zero of=$DiskPart bs=512 count=1 &>/dev/null sync sync sleep 3 echo 'n p 1 +30M n p 2 +200M w'| fdisk $DiskPart &>/dev/null fi partx -a ${DiskPart}1 $DiskPart partx -a ${DiskPart}2 $DiskPart mkfs -t ext4 ${DiskPart}1 &>/dev/null mkfs -t ext4 ${DiskPart}2 &>/dev/null printf "===============Partition is OK=========\n" echo -e "\033[31m=======Second:Mount the Partition======\033[0m" [ -d /mnt/boot ] || mkdir -p /mnt/boot [ -d /mnt/sysroot ] || mkdir -p /mnt/sysroot if mount | grep "/dev/sdb1" &>/dev/null;then echo "/dev/sdb1 is mounted..." else mount /dev/sdb1 /mnt/boot fi if mount | grep "/dev/sdb2" &>/dev/null;then echo "/dev/sdb2 is mounted...." else mount /dev/sdb2 /mnt/sysroot fi printf "===============Mount is OK=========\n" echo -e "\033[31m=======Third: Create boot partition======\033[0m" [ -d /mnt/boot/grub ]|| grub-install --root-directory=/mnt /dev/sdb &>/dev/null;echo "grub is ok" printf "========Boot partition is OK============\n" echo -e "\033[31m=======Fourth:Copy the kernel and make the kernel p_w_picpath file======\033[0m" cp /boot/vmlinuz-2.6.32-358.el6.x86_64 /mnt/boot/vmlinuz mkinitrd /opt/initramfs-`uname -r`.img `uname -r` cp /opt/initramfs-2.6.32-358.el6.x86_64.img /mnt/boot/initrd.gz printf "========Kernel and kernel p_w_picpath file is OK========\n" echo -e "\033[31m=======Fifth:Edit grub configuration file======\033[0m" cat >> /mnt/boot/grub/grub.conf <> /mnt/sysroot/etc/inittab << EOF id:3:initdefault: EOF cat >> /mnt/sysroot/etc/rc.d/rc.sysinit <