知识基础:
系统启动流程: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文件
cp /boot/vmlinuz-2.6.-.el6.i686 /mnt/boot/vmlinuz
cp /boot/initramfs-2.6.-.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如下:
#!/bin/bash
#
DEST=/mnt/sysroot
libcp(){
LIBPATH=${%/*}
[ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH
[ ! -e $DEST${1} ] && cp $1 $DEST$LIBPATH && echo "copy lib $1 finished"
}
bincp(){
CMDPATH=${1%/*}
[ ! -d $DEST$CMDPATH ] && mkdir -p $DEST$CMDPATH
[ ! -e $DEST${1} ] && cp $1 $DEST$CMDPATH for LIB in `ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`;do
libcp $LIB
done
}
read -p "Your Command: " CMD
until [ $CMD == 'q' ]; do
! which $CMD &> /dev/null && echo "Wrong command" && read -p "Input again: " CMD && continue
COMMAND=`which $CMD | grep -v "^alias" | grep -o "[^[:space:]]\{1,\}"`
bincp $COMMAND
echo "copy $COMMAND finished."
read -p "Continues: " CMD
done
5、编写/mnt/sysroot/sbin/init文件
#!/bin/bash
#
echo -e "Welcome to \033[34m My Tiny\033[0m Linux"
mount -n -t proc proc /proc
mount -n -t sysfs sysfs /sys
insmod /lib/modules/e1000.ko
[ $? -eq ] && echo -e "Load e1000 modules succeed [ \033[32mOK\033[0m ]"
ifocnfig lo 127.0.0.1/
ifconfig eth0 192.168.0.225/
mount -n -o remount,rw /dev/sda2 /
/bin/bash
6、为grub提供配置文件
default=
timeout=
title Test Linux
root (hd0,)
kernel /vmlinuz ro root=/dev/sda2 selinux= init=/sbin/init
initrd /initramfs.img
到此,一个带有网络功能的linux便制作完成了。