创建一个MiniLinux:
通过对RamDisk(Initrd)分析,现在了解了initrd的主要原理及工作流程,为了加深对该过程的理解,下面使用initrd创建一个微型的linux系统,也更加深对linux开机过程的原理性了解:并用脚本实现该过程;
环境:宿主机:RedHat 5.4 内核:2.6.18
要求:实现最基本的启动,正常启动后给用户一个shell;后期拓展,需要实现将根文件系统挂载为RW,实现命令:mount、umount、vi、ls、touch、mkdir等.也可以后期按照需求添加各项功能,逐渐丰富;
1. 配置所需的环境:新增硬盘/dev/sdb(大小512M~1G绰余)、建立所需目录(这里假定新增的硬盘为sdb)
- # mkdir -v /mnt/sysroot
- # mkdir -v /mnt/boot
- # mkdir -v /tmp/init
-
注:在虚拟机里面添加硬盘时将硬盘放到自己容易找到的位置,最好不用默认位置;
2. 对新硬盘分区,格式化后挂载到/mnt目录下的适当位置
- # fdisk /dev/sdb (将硬盘分为两个区,第一个100M作为boot分区,第二个作为根分区)
- # partproble /dev/sdb
- # mkfs -t ext3 /dev/sdb1
- # mkfs -t ext3 /dev/sdb2
- # mount /dev/sdb1 /mnt/boot
- # mount /dev/sdb2 /mnt/sysroot
- # mount (查看挂载是否成功)
-
3. 拷贝内核文件到相应的位置,并且解包initrd
- # cp /boot/vmlinuz-`uname -r`.img /mnt/boot/vmlinuz
- # cd /tmp/init
- # zcat /boot/initrd.2.6.18-164.el5.img | cpio -id &> /dev/null
-
4. 修改RamDisk内容:
- # sed -i s/(mkrootdev.*ro[[:space:]]).*/1sda2/g /tmp/init
-
上步骤中将RanDisk解压出来init文件中的”mkrootdev -t ext3 -o defaults,ro
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
/dev/VolGroup00/LogVol00”项修改为”mkrootdev -t ext3 -o defaults,ro sda2”;并且可以根据实际需求,将该文件中不需要加载的dm-mirror、dm-zero、dm-snapshrot项注释或删掉;当然为了更加精简镜像文件的大小,可以到对应的/lib文件夹下删除相应的文件;
initrd文件修改后示例:
- #!/bin/nash
- mount -t proc /proc /proc
- setquiet
- echo Mounting proc filesystem
- echo Mounting sysfs filesystem
- mount -t sysfs /sys /sys
- echo Creating /dev
- mount -o mode=0755 -t tmpfs /dev /dev
- mkdir /dev/pts
- mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts
- mkdir /dev/shm
- mkdir /dev/mapper
- echo Creating initial device nodes
- mknod /dev/null c 1 3
- mknod /dev/zero c 1 5
- mknod /dev/systty c 4 0
- mknod /dev/tty c 5 0
- mknod /dev/console c 5 1
- mknod /dev/ptmx c 5 2
- mknod /dev/rtc c 10 135
- mknod /dev/tty0 c 4 0
- mknod /dev/tty1 c 4 1
- mknod /dev/tty2 c 4 2
- mknod /dev/tty3 c 4 3
- mknod /dev/tty4 c 4 4
- mknod /dev/tty5 c 4 5
- mknod /dev/tty6 c 4 6
- mknod /dev/tty7 c 4 7
- mknod /dev/tty8 c 4 8
- mknod /dev/tty9 c 4 9
- mknod /dev/tty10 c 4 10
- mknod /dev/tty11 c 4 11
- mknod /dev/tty12 c 4 12
- mknod /dev/ttyS0 c 4 64
- mknod /dev/ttyS1 c 4 65
- mknod /dev/ttyS2 c 4 66
- mknod /dev/ttyS3 c 4 67
- echo Setting up hotplug.
- hotplug
- echo Creating block device nodes.
- mkblkdevs
- echo "Loading uhci-hcd.ko module"
- insmod /lib/uhci-hcd.ko
- echo "Loading ohci-hcd.ko module"
- insmod /lib/ohci-hcd.ko
- echo "Loading ehci-hcd.ko module"
- insmod /lib/ehci-hcd.ko
- mount -t usbfs /proc/bus/usb /proc/bus/usb
- echo "Loading jbd.ko module"
- insmod /lib/jbd.ko
- echo "Loading ext3.ko module"
- insmod /lib/ext3.ko
- echo "Loading scsi_mod.ko module"
- insmod /lib/scsi_mod.ko
- echo "Loading sd_mod.ko module"
- insmod /lib/sd_mod.ko
- echo "Loading scsi_transport_spi.ko module"
- insmod /lib/scsi_transport_spi.ko
- echo "Loading mptbase.ko module"
- insmod /lib/mptbase.ko
- echo "Loading mptscsih.ko module"
- insmod /lib/mptscsih.ko
- echo "Loading mptspi.ko module"
- insmod /lib/mptspi.ko
- echo "Loading dm-mod.ko module"
- insmod /lib/dm-mod.ko
- echo Waiting for driver initialization.
- stabilized --hash --interval 250 /proc/scsi/scsi
- echo Making device-mapper control node
- mkdmnod
- mkblkdevs
- echo Creating root device.
- mkrootdev -t ext3 -o defaults,ro /dev/sda2 (这里一定要修改好)
- echo Mounting root filesystem.
- mount /sysroot
- echo Setting up other filesystems.
- setuproot
- echo Switching to new root and running init.
- switchroot
-
|