一台虚拟机的核心就是一个磁盘镜像,这个镜像可以理解成虚拟机的磁盘,里面有虚拟机的操作系统和驱动等重要文件。本文主要介绍创建虚拟机的一般过程。
创建虚拟机镜像
要在一台host上跑起一个虚拟机一般需要两个步骤:
第一步:创建虚拟机镜像
1 |
<strong>qemu-img create</strong> -f raw /images/vm1.raw 8G |
qmeu-img创建的镜像是一个稀疏文件,也就是说刚创建出来的文件并没有8G,它会随着数据的增多慢慢增加,直到8G
第二步:启动虚拟机
1 |
<strong>kvm</strong> /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
-
增加或减小镜像大小
创建镜像
1 |
<strong>qemu-img create</strong> -f <<span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">fmt</span>> -o <options> <fname> <size> |
举例:
1 |
<strong>qemu-img create</strong> -f raw -o size=4G /images/vm2.raw |
1 |
<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">hzgatt@hzgatt:~/</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"><span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">images$</span> <strong>ll</strong> total </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">0</span>-rw-r--r-- <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">1</span> hzgatt hzgatt <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">4</span>.0G 6月 <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">29</span> <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">14</span>:<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">11</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> vm2.raw<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">hzgatt@hzgatt:</span></span><span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">~/images$</span> <strong>ll -</strong><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"><strong>s</strong> total </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">0</span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">0</span> -rw-r--r-- <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">1</span> hzgatt hzgatt <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">4</span>.0G 6月 <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">29</span> <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">14</span>:<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">11</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> vm2.raw</span> |
1 2 3 4 |
<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">hzgatt@hzgatt:~/images$</span> <strong>qemu-img <span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">info</span></strong><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"><strong> vm2.raw</strong> image: vm2.raw</span><span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">file</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> format: raw virtual size: </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">4</span>.0G (<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">4294967296</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> bytes) disk size: </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">0</span> |
虽然ls中看到文件的大小是4G,但是实际上磁盘大小是0。这就是稀疏文件
转化
将一个镜像文件转化为另外一种格式,qemu-img支持的格式可以看qemu-img -h最后一行。
1 |
Supported formats: vvfat vpc vmdk vdi sheepdog rbd raw host_cdrom host_floppy host_device <span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">file</span> qed qcow2 qcow parallels nbd dmg tftp ftps <span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">ftp</span> https http cow cloop bochs blkverify blkdebug |
转化命令:
1 |
<strong>qemu-img convert</strong> <strong>-c</strong> -f fmt <strong>-O</strong> out_fmt <strong>-o</strong> options <strong>fname out_fname</strong> |
-c:采用压缩,只有qcow和qcow2才支持
-f:源镜像的格式,它会自动检测,所以省略之
-O 目标镜像的格式
-o 其他选先
fname:源文件
out_fname:转化后的文件
看例子:
1 |
<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">hzgatt@hzgatt:~/images$</span> <strong>qemu-img convert</strong> -c -O qcow2 vm2.raw vm2.qcow2 |
1 2 3 4 |
<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">hzgatt@hzgatt:~/images$</span> <strong>ll -</strong><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"><strong>s</strong> total 136K </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">0</span> -rw-r--r-- <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">1</span> hzgatt hzgatt <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">5</span>.0G 6月 <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">29</span> <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">13</span>:<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">55</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> vm1.raw 136K </span>-rw-r--r-- <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">1</span> hzgatt hzgatt 193K 6月 <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">29</span> <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">14</span>:<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">22</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> vm2.qcow2 </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">0</span> -rw-r--r-- <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">1</span> hzgatt hzgatt <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">4</span>.0G 6月 <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">29</span> <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">14</span>:<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">11</span> vm2.raw |
1 2 3 4 5 |
<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">hzgatt@hzgatt:~/images$</span> <strong>qemu-img</strong> <span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">info</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> vm2.qcow2 image: vm2.qcow2</span><span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">file</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> format: qcow2 virtual size: </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">4</span>.0G (<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">4294967296</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> bytes) disk size: 136K cluster_size: </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">65536</span> |
如果想看要转化的格式支持的-o选项有哪些,可以在命令末尾加上 -o ?
1 2 3 4 5 6 7 |
<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;"><A href="mailto:hzgatt@hzgatt:~/images$2>hzgatt@hzgatt:~/images$</a></span> <strong>qemu-img convert</strong> -c -O qcow2 vm2.raw vm2.qcow2<strong> -o ?</strong><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> Supported options: size Virtual disk size backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size preallocation Preallocation mode (allowed values: off, metadata)</span> |
增加减少镜像大小
注意:只有raw格式的镜像才可以改变大小
1 |
<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">hzgatt@hzgatt:~/images$</span> <strong>qemu-img resize</strong> vm2.raw <strong>+2GB</strong> |
1 2 3 4 5 6 7 8 9 |
<strong><A href="mailto:hzgatt@hzgatt:~/images$2>hzgatt@hzgatt:~/images$</a></strong> ll -<span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;">s total 136K </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">0</span> -rw-r--r-- <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">1</span> hzgatt hzgatt <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">5</span>.0G 6月 <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">29</span> <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">13</span>:<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">55</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> vm1.raw 136K </span>-rw-r--r-- <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">1</span> hzgatt hzgatt 193K 6月 <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">29</span> <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">14</span>:<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">22</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> vm2.qcow2 </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">0</span> -rw-r--r-- <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">1</span> hzgatt hzgatt <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">6</span>.0G 6月 <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">29</span> <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">14</span>:<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">28</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> vm2.raw hzgatt@hzgatt:</span>~/images$ qemu-img <span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">info</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> vm2.raw image: vm2.raw</span><span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">file</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> format: raw virtual size: </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">6</span>.0G (<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">6442450944</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> bytes) disk size: </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">0</span> |
快照
查看快照
1 |
<strong>qemu-img snapshot -l</strong> /images/vm2.qcow2 |
注意:只有qcow2才支持快照
打快照
1 |
<strong>qemu-img snapshot -c</strong> booting vm2.qcow2 |
举例:
1 2 3 |
<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">hzgatt@hzgatt:~/images$</span> <strong>qemu-img snapshot</strong> -<span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;">c booting vm2.qcow2 <span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">hzgatt@hzgatt:</span></span><span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">~/images$</span> <strong>qemu-img snapshot</strong> -<span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;">l vm2.qcow2 Snapshot list: ID TAG VM SIZE DATE VM CLOCK</span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">1</span> booting <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">0</span> <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">2012</span>-<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">06</span>-<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">29</span> <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">14</span>:<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">35</span>:<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">04</span> <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">00</span>:<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">00</span>:<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">00.000</span> |
从快照恢复:
1 |
<strong>qemu-img snapshot</strong> <strong>-a</strong> <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">1</span> /images/vm2.qcow2 |
然后从kvm启动这个虚拟机,会发现虚拟机又在打快照时的状态了
删除快照:
1 |
<strong>qemu-img snapshot -d</strong> <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">2</span> /images/vm2.qcow |
使用派生镜像(qcow2)
当创建的虚拟机越来越多,并且你发现好多虚拟机都是同一个操作系统,它们的区别就是安装的软件不大一样,那么你肯定会希望把他们公共的部分提取出来,只保存那些与公共部分不同的东西,这样镜像大小下去了,空间变多了,管理也方便了。派生镜像就是用来干这事的!
首先看一个原始镜像
1 2 3 4 |
<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">hzgatt@hzgatt:~/images$</span> <strong>qemu-img <span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">info</span></strong><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> vm3_base.raw image: vm3_base.raw</span><span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">file</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> format: raw virtual size: </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">2</span>.0G (<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">2147483648</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> bytes) disk size: </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">2</span>.0G |
现在我们新建一个镜像,但是派生自它
1 |
<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">hzgatt@hzgatt:~/images$</span> <strong>qemu-img create</strong> <strong>-f qcow2 vm3_5.qcow2 -o backing_file=</strong><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"><strong>vm3_base.raw 5G</strong> Formatting </span><span style="font-size:12px;font-family:'Courier New';color:#800000;line-height:1.5;">'</span><span style="font-size:12px;font-family:'Courier New';color:#800000;line-height:1.5;">vm3_5.qcow2</span><span style="font-size:12px;font-family:'Courier New';color:#800000;line-height:1.5;">'</span>, <span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">fmt</span>=qcow2 size=<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">5368709120</span> backing_file=<span style="font-size:12px;font-family:'Courier New';color:#800000;line-height:1.5;">'</span><span style="font-size:12px;font-family:'Courier New';color:#800000;line-height:1.5;">vm3_base.raw</span><span style="font-size:12px;font-family:'Courier New';color:#800000;line-height:1.5;">'</span> encryption=off cluster_size=<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">65536</span> |
1 |
<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">hzgatt@hzgatt:~/</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"><span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">images$</span> <strong>ll</strong></span>-rw-r--r-- <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">1</span> hzgatt hzgatt 193K 6月 <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">29</span> <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">15</span>:<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">00</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> vm3_5.qcow2</span>-rw-r--r-- <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">1</span> hzgatt hzgatt <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">2</span>.0G 6月 <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">29</span> <span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">14</span>:<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">51</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> vm3_base.raw<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;"> </span></span> |
1 2 3 4 5 |
<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;"><A href="mailto:hzgatt@hzgatt:~/images$2>hzgatt@hzgatt:~/images$</a></span> <strong>qemu-img <span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">info</span></strong><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"><strong> vm3_5.qcow2</strong> image: vm3_5.qcow2</span><span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">file</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> format: qcow2 virtual size: </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">5</span>.0G (<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">5368709120</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> bytes) disk size: 136K cluster_size: </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">65536</span><span style="font-size:12px;font-family:'Courier New';line-height:1.5;background-color:#ccffcc;"><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> backing </span><span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">file</span>: vm3_base.raw (actual path: vm3_base.raw)</span> |
^_^,这个镜像才136K,够省了吧。DRY永远的真理啊!
现在我们在vm3_5.qcow2上打了很多安全补丁,然后发现我又想在vm3_5.qcow2上派生新的虚拟机,o(∩∩)o...哈哈,这下怎么办呢?
1 |
<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">hzgatt@hzgatt:~/images$</span> <strong>qemu-img convert</strong> -O raw vm3_5.qcow2 vm3_base2.raw |
1 2 3 4 |
<span style="font-size:12px;font-family:'Courier New';text-decoration:underline;line-height:1.5;">hzgatt@hzgatt:~/images$</span> <strong>qemu-img <span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">info</span></strong><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> vm3_base2.raw image: vm3_base2.raw</span><span style="font-size:12px;font-family:'Courier New';color:#0000ff;line-height:1.5;">file</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> format: raw virtual size: </span><span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">5</span>.0G (<span style="font-size:12px;font-family:'Courier New';color:#800080;line-height:1.5;">5368709120</span><span style="font-size:12px;font-family:'Courier New';color:#000000;line-height:1.5;"> bytes) disk size: 592M</span> |
这个转化将会将vm3_5和base合并,生成新的vm3_base2.raw,然后你就可以继续无穷无尽的派生之旅了!