我的linux版本:redhat 5.4
虚拟机客户端:Vmware Workstation 6.5
我们分步骤来实现这个小型linux
第一步:在虚拟机上添加一块新硬盘到linux,大小20G (可以自定)
2)重读下分区表
格式化刚建立的分区为ext3文件系统
第四步:1)安装grub到/dev/sdb
深入浅出:linux启动流程刨析
1)拷贝kernel文件,并且重命名方便记忆。
2)拷贝initrd文件,并且重命名方便记忆。
第七步:创建小型linux的文件目录结构
第八步:拷贝命令到小型linux里去
1)怎么样拷贝命令?
第十步:创建/mnt/boot/grub/grub.conf 和 /etc/rc.d/rc.sysinit,并给rc,sysyinit 赋执行权限
启动完成的效果,很简陋吧,注意Little
虚拟机客户端:Vmware Workstation 6.5
我们分步骤来实现这个小型linux
第一步:在虚拟机上添加一块新硬盘到linux,大小20G (可以自定)
- [root@server69 ~]# fdisk /dev/sdb
- The number of cylinders for this disk is set to 2610.
- There is nothing wrong with that, but this is larger than 1024,
- and could in certain setups cause problems with:
- 1) software that runs at boot time (e.g., old versions of LILO)
- 2) booting and partitioning software from other OSs
- (e.g., DOS FDISK, OS/2 FDISK)
- Command (m for help): p
- Disk /dev/sdb: 21.4 GB, 21474836480 bytes
- 255 heads, 63 sectors/track, 2610 cylinders
- Units = cylinders of 16065 * 512 = 8225280 bytes
- Device Boot Start End Blocks Id System
- /dev/sdb1 1 7 56196 83 Linux
- /dev/sdb2 8 70 506047+ 83 Linux
- /dev/sdb3 71 102 257040 82 Linux swap / Solaris
- Command (m for help): n
- Command action
- e extended
- p primary partition (1-4)
- p
- Selected partition 4
- First cylinder (103-2610, default 103):
- Using default value 103
- Last cylinder or +size or +sizeM or +sizeK (103-2610, default 2610): +50M
- Command (m for help): w
2)重读下分区表
- [root@server69 ~]# partprobe /dev/sdb
- [root@server69 ~]# mke2fs -j /dev/sdb1
- [root@server69 ~]# mke2fs -j /dev/sdb2
- [root@server69 LX]# mount /dev/sdb1 /mnt/boot
- [root@server69 LX]# mount /dev/sdb2 /mnt/sysroot
第四步:1)安装grub到/dev/sdb
- [root@server69 mnt]# grub-install --root-directory=/mnt/ /dev/sdb
- Installation finished. No error reported.
- This is the contents of the device map /mnt//boot/grub/device.map.
- Check if this is correct or not. If any of the lines is incorrect,
- fix it and re-run the script `grub-install'.
- (fd0) /dev/fd0
- (hd0) /dev/sda
- (hd1) /dev/sdb
- 2)修改/mnt/boot/grub/device.map
- [root@server69 mnt]# vim /mnt/boot/grub/device.map
- (fd0) /dev/fd0 //去掉这一行 fd指的是软盘
- (hd0) /dev/sda
- (hd1) /dev/sdb //去掉这一行
深入浅出:linux启动流程刨析
1)拷贝kernel文件,并且重命名方便记忆。
- [root@server69 LX]# cp /boot/vmlinuz-2.6.18-164.el5 /mnt/boot/vmlinuz
2)拷贝initrd文件,并且重命名方便记忆。
- [root@server69 LX]# cp /boot/initrd-2.6.18-164.el5.img /mnt/boot/initrd
- [root@server69 mnt]# mv initrd initrd.gz //刚其改名,否则gzip不识别
- [root@server69 mnt]# gzip -d initrd.gz //解压
- [root@server69 mnt]# cpio -id < initrd.gz //释放归档
- 14731 blocks
- [root@server69 mnt]# ls
- bin boot dev etc init lib proc sbin sys sysroot
- //看到了吗?initrd几乎具有所有的linux文件目录,简直就是一个小linux
- [root@server69 mnt]# vim init
- 99 mkrootdev -t ext3 -o defaults,ro /dev/vol0/root // 修改为 mkrootdev -t ext3 -o defaults,ro /dev/sda2
- [root@server69 mnt]#find. | cpio -H newc -o quiet | gzip -9 //重新归档并压缩
第七步:创建小型linux的文件目录结构
- [root@server69 mnt]# cd /mnt/sysroot/
- [root@server69 sysroot]# mkdir -pv root bin sbin usr/{bin,sbin} lib dev tmp proc sys home mnt media boot
第八步:拷贝命令到小型linux里去
1)怎么样拷贝命令?
- [root@server69 sysroot]# which bash //使用which命令查看命令所在路径
- /bin/bash
- [root@server69 sysroot]# ldd /bin/bash //ldd 加命令路径可以显示出这个命令所依赖的库文件
- linux-gate.so.1 => (0x008e6000) //这个文件不用拷贝
- libtermcap.so.2 => /lib/libtermcap.so.2 (0x0041e000)
- libdl.so.2 => /lib/libdl.so.2 (0x003ef000)
- libc.so.6 => /lib/libc.so.6 (0x002a7000)
- /lib/ld-linux.so.2 (0x00284000)
- [root@server69 sysroot]# cp /lib/lidkl.so.2 /mnt/sysroot/lib/ //将上面的库文件都复制到小linux对应的目录
2)拷贝init 和bash ,mount , ls 这几个个命令去linux 注意,init和bash必须拷贝。
- [root@server69 ~]# vim /mnt/sysroot/etc/fstab
- /dev/sda2 / ext3 defaults 0 0
- /dev/sda1 /boot ext3 defaults 0 0
- sysfs /sys sysfs defaults 0 0
- proc /proc proc defaults 0 0
- [root@server69 ~]# vim /mnt/sysroot/etc/inittab
- id:3:initdefault:
- si::sysinit:/etc/rc.d/rc.sysinit
第十步:创建/mnt/boot/grub/grub.conf 和 /etc/rc.d/rc.sysinit,并给rc,sysyinit 赋执行权限
- [root@server69 ~]# vim /mnt/boot/grub/grub.conf
- default=0
- timeout=10
- title Little Linux //grub的标题,可以自己写
- kernel /vmlinuz ro root=/dev/sda2 quiet //加载内核,只读方式挂载根文件系统
- initrd /initrd.gz //加载内存文件系统
- [root@server69 ~]# vim /mnt/sysroot/etc/rc.d/rc.sysinit
- #!/bin/bash
- echo -e "\t\tWelcome to \033[31mLittle\033[0m Linux" //这里可以写自己想写的欢迎界面 ^^
- mount -n -o remount.rw / //以读写方式重启挂载根文件系统
- mount -n -a //挂载所有设备,并不记录
- /bin/bash //加载bash shell
- <pre name="code" class="cpp" style="margin-top: 4px; margin-right: 0px; margin-bottom: 4px; margin-left: 0px; background-color: rgb(240, 240, 240); ">[root@server69 ~]# chmod +x /etc/rc.d/rc.sysinit //给rc.sysinit赋执行权限
好了,大功告成,新建一个虚拟机,选择linux2.6xx ,挂载上这块硬盘看下效果吧~
这是我的grub界面很朴素吧~你可以自己更改背景哈


##更新:第十步最后要给rc.sysinit赋执行权限,否则可能系统加载时无法执行!