知识基础:
系统启动流程:POST-->BIOS(boot sequence)-->GRUB(bootloder(stage1:MBR;stage2:grub目录中))-->kernel(initrd)-->SHELL
制作步骤:
1、给目标磁盘分区
两个:
宿主机:/dev/sdb1 /dev/sdb2
将/dev/sdb1挂载至/mnt/boot,/dev/sdb2挂载至/mnt/sysroot
mount /dev/sdb1 /mnt/boot mount /dev/sdb2 /mnt/sysroot
2、安装grub至目标磁盘
grub-install --root-directory=/mnt /dev/sdb
3、复制内核和initrd文件
1 cp /boot/vmlinuz-2.6.32-279.el6.i686 /mnt/boot/vmlinuz 2 cp /boot/initramfs-2.6.32-279.el6.i686.img /mnt/boot/initramfs.img
4、创建目标文件的根系统文件
mkdir -pv /mnt/sysroot/{/etc/rc.d,usr,var,media,home,root,src,sys,boot,tmp,lib,lib64,dev,proc,bin,sbin}
利用bincp脚本移植bash,ls等至目标根系统文件
bincp.sh如下:
1 #!/bin/bash 2 # 3 DEST=/mnt/sysroot 4 libcp(){ 5 LIBPATH=${1%/*} 6 [ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH 7 [ ! -e $DEST${1} ] && cp $1 $DEST$LIBPATH && echo "copy lib $1 finished" 8 } 9 bincp(){ 10 CMDPATH=${1%/*} 11 [ ! -d $DEST$CMDPATH ] && mkdir -p $DEST$CMDPATH 12 [ ! -e $DEST${1} ] && cp $1 $DEST$CMDPATH 13 14 for LIB in `ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`;do 15 libcp $LIB 16 done 17 } 18 read -p "Your Command: " CMD 19 until [ $CMD == ‘q‘ ]; do 20 ! which $CMD &> /dev/null && echo "Wrong command" && read -p "Input again: " CMD && continue 21 COMMAND=`which $CMD | grep -v "^alias" | grep -o "[^[:space:]]\{1,\}"` 22 bincp $COMMAND 23 echo "copy $COMMAND finished." 24 read -p "Continues: " CMD 25 done
5、编写/mnt/sysroot/sbin/init文件
1 #!/bin/bash 2 # 3 echo -e "Welcome to \033[34m My Tiny\033[0m Linux" 4 mount -n -t proc proc /proc 5 mount -n -t sysfs sysfs /sys 6 insmod /lib/modules/e1000.ko 7 [ $? -eq 0 ] && echo -e "Load e1000 modules succeed [ \033[32mOK\033[0m ]" 8 ifocnfig lo 127.0.0.1/8 9 ifconfig eth0 192.168.0.225/16 10 mount -n -o remount,rw /dev/sda2 / 11 /bin/bash
6、为grub提供配置文件
1 default=0 2 timeout=5 3 title Test Linux 4 root (hd0,0) 5 kernel /vmlinuz ro root=/dev/sda2 selinux=0 init=/sbin/init 6 initrd /initramfs.img
到此,一个带有网络功能的linux便制作完成了。
时间: 2024-11-05 22:30:13