使用qmeu-img创建虚拟机(创建虚拟机,虚拟机快照)

来源:互联网 发布:新顶级域名不备案吗 编辑:程序博客网 时间:2024/05/21 10:45

本文主要介绍创建虚拟机的一般过程。

一台虚拟机的核心就是一个磁盘镜像,这个镜像可以理解成虚拟机的磁盘,里面有虚拟机的操作系统和驱动等重要文件。


创建虚拟机镜像



 

要在一台host上跑起一个虚拟机一般需要两个步骤:

第一步:创建虚拟机镜像

qemu-img create -f raw /images/vm1.raw 8G

qmeu-img创建的镜像是一个稀疏文件,也就是说刚创建出来的文件并没有8G,它会随着数据的增多慢慢增加,直到8G

 

第二步:启动虚拟机

kvm /imges/vm1.raw

运行结果: 因为镜像里面没有任何内容,所以提示找不到可引导设备。

 

使用qemu-img管理镜像



 

qemu-img基本命令


 

上节介绍了使用qemu-img创建镜像,这一节将会介绍qemu-img在镜像管理上的强大功能。

qemu-img有很多命令,包括下面常用的,当然qemu-img -h你懂得。

info

查看镜像的信息

create

创建镜像

check

检查镜像

convert

转化镜像的格式,(raw,qcow ……)

snapshot

管理镜像的快照

rebase

在已有的镜像的基础上创建新的镜像

resize

增加或减小镜像大小

 创建镜像


 

qemu-img create -f <fmt> -o <options> <fname> <size>

 举例:

qemu-img create -f raw -o size=4G /images/vm2.raw

 

hzgatt@hzgatt:~/images$ lltotal 0-rw-r--r-- 1 hzgatt hzgatt 4.0G  6月 29 14:11 vm2.rawhzgatt@hzgatt:~/images$ ll -stotal 00 -rw-r--r-- 1 hzgatt hzgatt 4.0G  6月 29 14:11 vm2.raw

 

hzgatt@hzgatt:~/images$ qemu-img info vm2.raw image: vm2.rawfile format: rawvirtual size: 4.0G (4294967296 bytes)disk size: 0

 

虽然ls中看到文件的大小是4G,但是实际上磁盘大小是0。这就是稀疏文件

 

 

转化


将一个镜像文件转化为另外一种格式,qemu-img支持的格式可以看qemu-img -h最后一行。

Supported formats: vvfat vpc vmdk vdi sheepdog rbd raw host_cdrom host_floppy host_device file qed qcow2 qcow parallels nbd dmg tftp ftps ftp https http cow cloop bochs blkverify blkdebug

 

转化命令:

qemu-img convert -c -f fmt -O out_fmt -o options fname out_fname

 

-c:采用压缩,只有qcow和qcow2才支持

-f:源镜像的格式,它会自动检测,所以省略之

-O 目标镜像的格式

-o 其他选先

fname:源文件

out_fname:转化后的文件

看例子:

hzgatt@hzgatt:~/images$ qemu-img convert -c -O qcow2 vm2.raw vm2.qcow2

 

hzgatt@hzgatt:~/images$ ll -stotal 136K   0 -rw-r--r-- 1 hzgatt hzgatt 5.0G  6月 29 13:55 vm1.raw136K -rw-r--r-- 1 hzgatt hzgatt 193K  6月 29 14:22 vm2.qcow2   0 -rw-r--r-- 1 hzgatt hzgatt 4.0G  6月 29 14:11 vm2.raw
hzgatt@hzgatt:~/images$ qemu-img info vm2.qcow2 image: vm2.qcow2file format: qcow2virtual size: 4.0G (4294967296 bytes)disk size: 136Kcluster_size: 65536

 


如果想看要转化的格式支持的-o选项有哪些,可以在命令末尾加上 -o ?

hzgatt@hzgatt:~/images$ qemu-img convert -c -O qcow2 vm2.raw vm2.qcow2 -o ?Supported options:size             Virtual disk sizebacking_file     File name of a base imagebacking_fmt      Image format of the base imageencryption       Encrypt the imagecluster_size     qcow2 cluster sizepreallocation    Preallocation mode (allowed values: off, metadata)

 


增加减少镜像大小


注意:只有raw格式的镜像才可以改变大小

hzgatt@hzgatt:~/images$ qemu-img resize vm2.raw +2GB
hzgatt@hzgatt:~/images$ ll -stotal 136K   0 -rw-r--r-- 1 hzgatt hzgatt 5.0G  6月 29 13:55 vm1.raw136K -rw-r--r-- 1 hzgatt hzgatt 193K  6月 29 14:22 vm2.qcow2   0 -rw-r--r-- 1 hzgatt hzgatt 6.0G  6月 29 14:28 vm2.rawhzgatt@hzgatt:~/images$ qemu-img info vm2.raw image: vm2.rawfile format: rawvirtual size: 6.0G (6442450944 bytes)disk size: 0

 

快照


查看快照

qemu-img snapshot -l /images/vm2.qcow2

注意:只有qcow2才支持快照

打快照

qemu-img snapshot -c booting vm2.qcow2

 

举例:

hzgatt@hzgatt:~/images$ qemu-img snapshot -c booting vm2.qcow2 hzgatt@hzgatt:~/images$ qemu-img snapshot -l vm2.qcow2 Snapshot list:ID        TAG                 VM SIZE                DATE       VM CLOCK1         booting                   0 2012-06-29 14:35:04   00:00:00.000

 

从快照恢复:

qemu-img snapshot -a 1 /images/vm2.qcow2

然后从kvm启动这个虚拟机,会发现虚拟机又在打快照时的状态了

 

删除快照:

qemu-img snapshot -d 2 /images/vm2.qcow

 

 

使用派生镜像(qcow2)


    当创建的虚拟机越来越多,并且你发现好多虚拟机都是同一个操作系统,它们的区别就是安装的软件不大一样,那么你肯定会希望把他们公共的部分提取出来,只保存那些与公共部分不同的东西,这样镜像大小下去了,空间变多了,管理也方便了。派生镜像就是用来干这事的!

首先看一个原始镜像

hzgatt@hzgatt:~/images$ qemu-img info vm3_base.raw image: vm3_base.rawfile format: rawvirtual size: 2.0G (2147483648 bytes)disk size: 2.0G

现在我们新建一个镜像,但是派生自它

hzgatt@hzgatt:~/images$ qemu-img create -f qcow2 vm3_5.qcow2 -o backing_file=vm3_base.raw 5GFormatting 'vm3_5.qcow2', fmt=qcow2 size=5368709120 backing_file='vm3_base.raw' encryption=off cluster_size=65536

 

hzgatt@hzgatt:~/images$ ll-rw-r--r-- 1 hzgatt hzgatt 193K  6月 29 15:00 vm3_5.qcow2-rw-r--r-- 1 hzgatt hzgatt 2.0G  6月 29 14:51 vm3_base.raw

 

hzgatt@hzgatt:~/images$ qemu-img info vm3_5.qcow2 image: vm3_5.qcow2file format: qcow2virtual size: 5.0G (5368709120 bytes)disk size: 136Kcluster_size: 65536backing file: vm3_base.raw (actual path: vm3_base.raw)

 

 ^_^,这个镜像才136K,够省了吧。DRY永远的真理啊!


现在我们在vm3_5.qcow2上打了很多安全补丁,然后发现我又想在vm3_5.qcow2上派生新的虚拟机,o(∩∩)o...哈哈,这下怎么办呢?

hzgatt@hzgatt:~/images$ qemu-img convert -O raw vm3_5.qcow2 vm3_base2.raw

 

hzgatt@hzgatt:~/images$ qemu-img info vm3_base2.raw image: vm3_base2.rawfile format: rawvirtual size: 5.0G (5368709120 bytes)disk size: 592M

 

这个转化将会将vm3_5和base合并,生成新的vm3_base2.raw,然后你就可以继续无穷无尽的派生之旅了!

0 0
原创粉丝点击