寫在前面,這篇博文只能讓裁剪的系統可以使用簡單的bash命令vim
在裁剪系統以前,先了解能啓動系統時,所必需的文件和程序。bash
一、開機所必需的的bootloader(grub)ide
二、內核文件、內核鏡像文件ui
三、能執行/sbin/init、有一個能執行命令的bash環境this
四、能執行命令時所需的庫文件spa
五、有inittab文件、rc.sysinit執行腳本文件3d
RHEL 5.x的裁剪orm
先添加一塊硬盤,把裁剪的系統放到新添加的硬盤上,而後用這塊新添加的硬盤當啓動盤,可以實現裁剪系統的正常啓動,則表示裁剪成功。本實驗是用vmworkstation虛擬機完成的。blog
1)添加硬盤,分兩個區,一個boot,一個根文件(rootfs)ip
添加硬盤步驟: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 <<EOF default=0 timeout=6 title My Litter Linux (RHEL 5.x) root (hd0,0) kernel /vmlinuz initrd /initrd.gz EOF #inittab文件 cat >> /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 <<EOF #!/bin/bash echo -e "\033[31m Welcome My Little RHEL 5.x\033[0m" /bin/bash EOF chmod +x /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 <<EOF default=0 timeout=6 title My Litter Linux (RHEL 5.x) root (hd0,0) kernel /vmlinuz initrd /initrd.gz EOF printf "=========Grub configuration file is OK========\n" mkdir -p /mnt/sysroot/{proc,sys,dev,etc/rc.d,lib/modules,bin,sbin,home,var,tmp} echo -e "\033[31m=======Sixth:Copy system running program=====\033[0m" echo -e "\033[31m You can input this command\033[0m" cat << EOF init bash lsmod insmod cp mv ls vim cat EOF 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" echo -e "\033[31m=======Seventh:Create inittab file and rc.sysinit script file======\033[0m" cat >> /mnt/sysroot/etc/inittab << EOF id:3:initdefault: si::sysinit:/etc/rc.d/rc.sysinit EOF cat >> /mnt/sysroot/etc/rc.d/rc.sysinit <<EOF #!/bin/bash echo -e "\033[31m Welcome My Little RHEL 5.x\033[0m" /bin/bash EOF chmod +x /mnt/sysroot/etc/rc.d/rc.sysinit sync sync sync sync
運行腳本的過程
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 <<EOF default=0 timeout=6 title My Litter Linux (RHEL 6.x) root (hd0,0) kernel /vmlinuz ro root=/dev/sda2 initrd /initrd.gz EOF printf "=========Grub configuration file is OK========\n" mkdir -p /mnt/sysroot/{proc,sys,dev,etc/rc.d,lib/modules,bin,sbin,home,var,tmp} echo -e "\033[31m=======Sixth:Copy system running program=====\033[0m" echo -e "\033[31m You can input this command\033[0m" cat << EOF init bash lsmod insmod cp mv ls vim cat EOF 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" echo -e "\033[31m=======Seventh:Create inittab file and rc.sysinit script file======\033[0m" cat >> /mnt/sysroot/etc/inittab << EOF id:3:initdefault: EOF cat >> /mnt/sysroot/etc/rc.d/rc.sysinit <<EOF #!/bin/bash echo -e "\033[31m Welcome My Little RHEL 6.x\033[0m" /bin/bash EOF chmod +x /mnt/sysroot/etc/rc.d/rc.sysinit sync sync sync sync