LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 956|回复: 4

自力更生型的CLFS部署方式

[复制链接]
发表于 2009-4-11 00:29:37 | 显示全部楼层 |阅读模式
做完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系统了。
发表于 2009-4-11 10:32:33 | 显示全部楼层
还是太麻烦,并且费张盘。

不拆机取硬盘的话:
只要把目标机自举(引导)起来,之后随便怎么折腾。

如果目标机无网络启动功能,
那么最简单的方法引导一个临时系统,live cd、u盘、或干脆安装一个发行版的最小系统都可以,配置好网络,然后随便怎么折腾。

况且,真要按您的方式做,步骤还可以进一步简化。
编译内核,制作 initrd 或 initramfs:
方法随便,关键是加入目标机光驱、硬盘支持、fdisk、还有 tar gzip bzip2 dd 或 其它适合您解包的东西。
打包目标机 rootfs:
直接打包整个 rootfs 即可(没必要分的那么麻烦),
cd rootfs
tar -jcvf ../rootfs.tar.bz2 *
制作光盘:
准备 bootloader,料有了,之后方法随意。
部署:
用光盘启动,挂载光盘,给目标机分区、制作文件系统、挂载目标根文件系统,解包,完成。

如果想更简单点,可以使用 debian bootcd 软件包制作光盘,其直接使用光盘的文件系统做 rootfs。
您可以编译合适的内核(省掉 initrd initramfs)。
您甚至不用打包,直接用 cp 也可部署系统。
回复 支持 反对

使用道具 举报

发表于 2009-4-11 10:43:36 | 显示全部楼层
http://packages.debian.org/sid/bootcd
http://ftp.debian.org/debian/poo ... /bootcd_3.14.tar.gz
Package: bootcd (3.14)

run your system from cd without need for disks

Build an image of your running Debian System with the command bootcdwrite. You can also build a bootcd ISO image via NFS on a remote System. When you run your system from CD you do not need any disks. All changes will be done in ram. To reuse this changes at next boot time you can save them on FLOPPY with the command bootcdflopcp. If booting from your CD-drive is not supported, booting from FLOPPY is possible. It is possible to install a new system from the running CD with the command bootcd2disk. Bootcd2disk can also find a target disk, format it and make it bootable automatically. Bootcd also supports lilo, grub, initrd, udev, lvm, transparent-compression ISO 9660 fs and syslinux/isolinux. The included FAQ describes how bootcd can do online or offline backups of other Linux distributions, resulting in restore-bootcds. The bootcdbackupwizard helps to create online backups.
瞧,这简直是专门为懒人订做的。

懒人最好选择 debian 作宿主,方便制作光盘。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-4-11 17:37:25 | 显示全部楼层

生命的乐趣在于折腾,呵呵!

确实,用liveCD启动目标机完成CLFS的部署最方便。不过,我想试试完全依靠已完成的CLFS系统本身的能力,来完成这事,也就是命名为“自力更生”原因。生命的乐趣在于折腾,呵呵!

bootcd不错,抽空研究研究。
回复 支持 反对

使用道具 举报

发表于 2009-4-13 20:08:00 | 显示全部楼层
最近 Distrowatch 大 hit 的 TinyCore 似乎用来做 bootcd 的骨干也不错
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表