|
做完CLFS后,如何部署到未装任何操作系统的裸机上呢?CLFS手册推荐用NFS方式,但是如果目标机不能网络启动,或者不方便与NFS联网,那么用光盘方式部署则是更好的选择。目前已有很多文章描述了Live CD的制作,可以用来做CLFS的部署光盘,不过我倒是想尝试完全用已做好的CLFS系统来完成部署光盘的制作,即“自力更生”型的CLFS部署方式。前几天将好完成了x86_64 multilib CLFS的构建,这次就拿它来做试验。
1、思路及可行性分析
已完成的CLFS本身是一个完整的系统,我们可以考虑把它做成initramfs,直接在目标机上启动,然后完成目标机的硬盘分区、创建文件系统、复制文件,最终将CLFS部署到目标机上。
把CLFS系统做成initramfs,我最担心的是系统太大,initramfs无法容纳。那么就先看CLFS的各个目录的大小吧:
$ du -sh /mnt/clfs/*
3.7M /mnt/clfs/bin
1.8M /mnt/clfs/boot
175M /mnt/clfs/cross-tools
4.0K /mnt/clfs/dev
1.1M /mnt/clfs/etc
4.0K /mnt/clfs/home
3.5M /mnt/clfs/lib
7.6M /mnt/clfs/lib64
du: `/mnt/clfs/lost+found': Permission denied
12K /mnt/clfs/media
4.0K /mnt/clfs/mnt
4.0K /mnt/clfs/opt
4.0K /mnt/clfs/proc
du: `/mnt/clfs/root': Permission denied
4.4M /mnt/clfs/sbin
490M /mnt/clfs/sources
4.0K /mnt/clfs/srv
4.0K /mnt/clfs/sys
12K /mnt/clfs/tmp
299M /mnt/clfs/tools
314M /mnt/clfs/usr
344K /mnt/clfs/var
可以看到,除usr 和 tools目录外,其他目录都不大。tools目录是构建的临时系统,可以不用部署到目标机,而usr中的内容并不是部署CLFS时必须的,可以不用包含在initramfs中,而可以打包后作为数据文件,在目标机启动后复制到目标机中。
看来,这个思路是可行的。
2、制作initramfs
在 /mnt/clfs下创建一个名为 clfs-ins的目录,用于制作CLFS的initramfs。下面命令用root用户执行,并假定CLFS分区已挂载在host的 /mnt/clfs 目录下。
cd /mnt/clfs/clfs-ins
mkdir rootfs
cp -a ../{bin,boot,dev,etc,home,lib,lib64,media,mnt,opt,proc,root,sbin,srv,sys,tmp,var} rootfs/
cd rootfs
ln -s /sbin/init init
find . | cpio -o -H newc |gzip -9 > ../initramfs.cpio.gz
生成的initramfs有9M多,而rootfs的大小是23M多,都不是很大。(关于构建initramfs的详细资料,可参考:http://linuxman.blog.ccidnet.com/blog-htm-do-showone-uid-60710-type-blog-itemid-1100709.html)
现在,把 usr目录打包压缩。
cd /mnt/clfs
tar cjf usr.tar.bz2 usr
生成的压缩包有67M多,并没有我原来想的那么大。
用qemu试验一下刚刚生成的initramfs
cd /mnt/clfs/clfs-ins
qemu-system-x86_64 -kernel rootfs/boot/clfskernel-2.6.17.13 -initrd initramfs.cpio.gz -hda /dev/null
虚拟机正常启动,有3个初始化操作失败,但并不影响系统功能,可以忽略。
突发奇想,把 usr.tar.bz2 也打到initramfs中,看看是否也能正常运行?
cp /mnt/clfs/usr.tar.bz2 /mnt/clfs/clfs-ins/rootfs
cd /mnt/clfs/clfs-ins/rootfs
find . | cpio -o -H newc |gzip -9 > ../initramfs.cpio.gz
生成的initramfs有74M多,感觉应该不会有问题。
再用qemu试验一下刚刚生成的initramfs(注意虚拟机的内存已由缺省的128M,用-m 参数设置成256M了):
cd /mnt/clfs/clfs-ins
qemu-system-x86_64 -kernel rootfs/boot/clfskernel-2.6.17.13 -initrd initramfs.cpio.gz -hda /dev/null -m 256
虚拟机正常启动!
3、向目标机部署CLFS
以虚拟机为目标机,部署CLFS。
(1)先创建虚拟机的硬盘映像文件
cd /mnt/clfs/clfs-ins
qemu-img create -f qcow2 hda.img 2G
(2)启动虚拟机
qemu-system-x86_64 -kernel rootfs/boot/clfskernel-2.6.17.13 -initrd initramfs.cpio.gz -hda hda.img -m 256
(3)向目标机部署CLFS系统
参照在Windows上构建Linux系统——后传一:用NFS部署目标系统第6节的内容,在目标机上(虚拟机)部署CLFS系统,具体的命令这里略去。
还有另外的命令要执行:
cp -a /lib64 /mnt
cd /mnt
tar xf /usr.tar.bz2 (这个命令运行的时间很长,需要耐心等待。)
部署完毕后,输入
halt
命令,关闭虚拟机。
(4)重新启动目标机,配置grub
qemu-system-x86_64 -kernel rootfs/boot/clfskernel-2.6.17.13 -hda hda.img -m 256 -append "root=/dev/hda1"
注意:去掉了 -initrd 参数,增加了 -append 参数。
目标机启动后,执行
grub
命令,按CLFS手册上的说明配置grub。
4、部署光盘的制作
前面用虚拟机成功验证initramfs的功能和部署方法,现在就看看如何做成部署光盘的iso映像文件了。
制作光盘iso文件的方法,参考了http://imghch.com/bootcd/index.html,这里只列出我的具体命令,进一步的说明,请查阅上面的文章。
cd /mnt/clfs/clfs-ins
mkdir -p iso/boot/grub
cp ../usr/lib/grub/x86_64-unknown/stage2_eltorito iso/boot/grub
vi iso/boot/gurb/menu.lst
default 0
timeout 10
color green/black light-green/black
title CLFS install environment
root (cd)
kernel /boot/clfskernel-2.6.17.13
initrd /boot/initramfs.cpio.gz (斜体字为 menu.lst文件的内容)
cp rootfs/boot/clfskernel-2.6.17.13 iso/boot
cp initramfs.cpio.gz iso/boot
mkisofs -R -b iso/boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o clfs-ins.iso iso
用qemu验证刚刚生成的 clfs-ins.iso 文件:
qemu-system-x86_64 -cdrom clfs-ins.iso -hda hda.img -boot d -m256
虚拟机启动后,可以看到经典的LFS式的GRUB配色菜单。按回车后,系统正常启动,可以用前面第3节的方法向目标机部署CLFS系统了。 |
|