裁剪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