裁剪linux,在宿主機上安裝一個硬盤;創建/mnt/boot和/mnt/sysroot兩個目錄,通過grub-install安裝引導到指定的磁盤,編輯grub.conf,然後將bash和一些命令和庫文件移動到目標磁盤,將網卡模塊加載到目標系統,這樣可以實現啓動和簡單的網絡功能.
1.創建/mnt/boot和/mnt/sysroot
mkdir /mnt/boot/ /mnt/sysroot/ -pv
2.操作vmware添加一塊新硬盤,然後對其分區格式化和掛載
分區,格式和掛載
echo -e "n\np\n1\n\n+100M\nn\np\n2\n\n+512M\nw" |fdisk /dev/sdb
[root@localhost ~]# mkfs -t ext4 /dev/sdb1 [root@localhost ~]# mkfs -t ext4 /dev/sdb2
[root@localhost ~]# mount /dev/sdb1 /mnt/boot [root@localhost ~]# mount /dev/sdb2 /mnt/sysroot
3.通過腳本將bash,ls.cat,ps,vi,modinfo,insmod,cd,ifconfig,ip,route,lsmod,cp,ping 等命令複製到指定的命令
scripts
#!/bin/bash # target=/mnt/sysroot clearCmd() { if which $cmd &> /dev/null; then cmdPath=`which --skip-alias $cmd` else echo "No such command" return 5 fi } cmdCopy() { cmdDir=`dirname $1` [ -d ${target}${cmdDir} ] || mkdir -p ${target}${cmdDir} [ -f ${target}${1} ] || cp $1 ${target}${cmdDir} } libCopy() { for lib in `ldd $1 | grep -o "/[^[:space:]]\{1,\}"`; do libDir=`dirname $lib` [ -d ${target}${libDir} ] || mkdir -p ${target}${libDir} [ -f ${target}${lib} ] || cp $lib ${target}${libDir} done } while true; do read -p "Enter a command: " cmd if [ "$cmd" == 'quit' ] ;then echo "quit" exit 0 fi clearCmd $cmd [ $? -eq 5 ] && continue cmdCopy $cmdPath libCopy $cmdPath done
4.複製內核和initrd文件到/mnt/boot目錄下創建根文件系統
cp /boot/initramfs-2.6.32-431.el6.x86_64.img /mnt/boot/initramfs.img cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /mnt/boot/vmlinuz
5.通過grub-install安裝移動到指定的分區,這樣可以用來引導
grub-install --root-directory=/mnt /dev/sdb
6.配置grub.conf文件和init文件實現引導
grub.conf的配置
[root@localhost ~]# cat /mnt/boot/grub/grub.conf default=0 timeout=5 title limeizhi Mini Linux root (hd0,0) kernel /vnlinuz ro root=/dev/sda2 quiet selinux=0 init=/sbin/init initrd /initramfs.img
init的配置
#!/bin/sh echo -e "Welcome to \033[32limeizhi\033[0m Linux" mount -n -t proc /proc /proc mount -n -t sysfs /sysfs /sys insmod /lib/modules/e1000.ko ifconfig lo 127.0.0.1/8 ifconfig eth0 192.168.11.240/24 route add -net 0.0.0.0 gw 192.168.11.1 /bin/bash ~
7.目標機上啓動linux