我们可以使用工具软件来对镜像做一些定制后,再上传到OpenStack云上,不过只能对离线的未在运行的镜像进行修改。
-
Guestfish
Guestfish 是libguestfs项目中的一个工具软件,提供修改虚机镜像内部配置的功能。它不需要把虚机镜像挂接到本地,而是为你提供一个shell接口,你可以查看、编辑和删除镜像内的文件。例如:
# guestfish --rw -a centos63_desktop.img
Welcome to guestfish, the libguestfs filesystem interactive shell forediting virtual machine filesystems. Type: 'help' for help on commands 'man' to read the manual 'quit' to quit the shell ><fs>
执行run命令,加载虚机镜像:
><fs> run
查看镜像中的文件系统:
><fs> list-filesystems/dev/vda1: ext4/dev/vg_centosbase/lv_root: ext4/dev/vg_centosbase/lv_swap: swap
手工把root分区挂接在本地:
><fs> mount /dev/vg_centosbase/lv_root /
现在可以对文件做增删改了:
><fs> rm /etc/udev/rules.d/70-persistent-net.rules
或者编辑:
><fs> edit /etc/sysconfig/network-scripts/ifcfg-eth0
如果你需要镜像加载8021q kernel,则需要为其创建一个可执行脚本文件:
><fs> touch /etc/sysconfig/modules/8021q.modules><fs> edit /etc/sysconfig/modules/8021q.modules
文件内容为:modprobe 8021q
><fs> chmod 0755 /etc/sysconfig/modules/8021q.modules
><fs> exit
Guestfish的更多使用说明,请参见 http://libguestfs.org/guestfs-recipes.1.html。
-
Guestmount
在一些使用场景中,直接把虚机镜像文件挂接在本地系统中,也是一个简便的办法。例如。
# guestmount -a centos63_desktop.qcow2 -m /dev/vg_centosbase/lv_root --rw /mnt
如果事先不知道虚机内部分区的路径信息,则可以使用参数-i来让guestmount自己决定使用哪一个挂接点:
# guestmount -a centos63_desktop.qcow2 -i --rw /mnt
接着使用# rpm -qa --dbpath /mnt/var/lib/rpm 查看该虚机安装了哪些rpm包
操作完成后,使用# umount /mnt退出。
-
virt-*tools
在libguestfs项目中有很多其它有用的工具,包括:
l virt-edit
l Virt-df
l Virt-resize
l Virt-sysprep
l Virt-sparsify
l Virt-p2v
l Virt-v2v
(1)virt-edit
# virsh shutdown instance-000000e1# virt-edit -d instance-000000e1 /etc/shadow# virsh start instance-000000e1
这样,就实现了对指定虚机中指定文件的编辑。
(2)Virt-resize
Virt-resize可以实现为虚机磁盘扩容。例如我们有下面这样一个虚机镜像。
# virt-filesystems --long --parts --blkdevs -h -a /data/images/win2012.qcow2Name Type MBR Size Parent/dev/sda1 partition 07 350M /dev/sda/dev/sda2 partition 07 16G /dev/sda/dev/sda device - 16G -
我们要给它的sda2分区增加几十GB的空间。可以先创建一个新分区,然后使用virt-resize命令把原来的sda2分区中的数据拷贝到新分区上来。这样就完成了。
# qemu-img create -f qcow2 /data/images/win2012-50gb.qcw2 50G# virt-resize --expand /dev/sda2 /data/images/win2012.qcow2 \ /data/images/win2012-50gb.qcow2Examining /data/images/win2012.qcow2 ...********** Summary of changes: /dev/sda1: This partition will be left alone. /dev/sda2: This partition will be resized from 15.7G to 49.7G. The filesystem ntfs on /dev/sda2 will be expanded using the 'ntfsresize' method. **********Setting up initial partition table on /data/images/win2012-50gb.qcow2 ...Copying /dev/sda1 ... 100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ 00:00Copying /dev/sda2 ... 100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ 00:00Expanding /dev/sda2 using the 'ntfsresize' method ... Resize operation completed with no errors. Before deleting the olddisk, carefully check that the resized disk boots and works correctly.