qemu-img是一个功能强大磁盘镜像管理工具。
qemu-img --help 包括以下功能:

  • check:检查完整性
  • create:创建镜像
  • commit:提交更改
  • compare:比较
  • convert:转换
  • info:获取信息
  • map:映射
  • snapshot:快照管理
  • rebase:在已有的的基础上创建新的镜像
  • resize:调整大小
  • amend:修订镜像格式选项

示例:

create创建磁盘:

[root@desktop example]# ls
[root@desktop example]# qemu-img create t1.img 1g    # 创建一个1G的磁盘文件
Formatting 't1.img', fmt=raw size=1073741824
[root@desktop example]# ls
t1.img
[root@desktop example]# qemu-img info t1.img         # 查看指定磁盘文件的信息
image: t1.img
file format: raw
virtual size: 1.0G (1073741824 bytes)
disk size: 0
[root@desktop example]#

磁盘镜像qcow2格式选项:

  1. backing_file:指定后端镜像文件
  2. backing_fmt:设置后端镜像的镜像格式。
  3. cluster_size:设置镜像中的簇大小,取值在512到2M之间,默认值为64K。
  4. preallocation:设置镜像文件空间的预分配模式
  5. encryption:用于设置密码。

预分配策略:

  • off:缺省策略,即不使用预分配策略
  • metadata:分配元数据,预分配后的虚拟磁盘仍然属于稀疏映像类型
  • full:分配所有磁盘空间并置零,预分配后的虚拟磁盘属于非稀疏映像类型
  • falloc:分配文件的块并标示它们的状态的未初始化,相对full模式来说,创建虚拟磁盘的速度要快得多
# 每种预分配格式创建的磁盘镜像文件初始占用的磁盘空间 -f qcow2指定创建所使用的格式
[root@desktop example] qemu-img create -f qcow2 test1.qcow2 1g
[root@desktop example] qemu-img create -f qcow2 test2.qcow2 1g  -o preallocation=off
[root@desktop example] qemu-img create -f qcow2 test3.qcow2 1g  -o preallocation=metadata
[root@desktop example] qemu-img create -f qcow2 test4.qcow2 1g  -o preallocation=falloc
[root@desktop example] qemu-img create -f qcow2 test5.qcow2 1g  -o preallocation=full
[root@desktop example]# ls -lh
total 2.1G
-rw-r--r--. 1 root root 1.0G Mar 18 09:11 t1.img
-rw-r--r--. 1 root root 193K Mar 18 09:30 test1.qcow2
-rw-r--r--. 1 root root 193K Mar 18 09:32 test2.qcow2
-rw-r--r--. 1 root root 1.1G Mar 18 09:32 test3.qcow2
-rw-r--r--. 1 root root 1.1G Mar 18 10:01 test4.qcow2
-rw-r--r--. 1 root root 1.1G Mar 18 10:01 test5.qcow2
[root@desktop example]# du -h *
0       t1.img
196K    test1.qcow2
196K    test2.qcow2
516K    test3.qcow2
1.1G    test4.qcow2
1.1G    test5.qcow2
[root@desktop example]#

backing_file指定后端镜像盘:指定后端镜像盘来创建新的镜像盘 

  • -f指定创建的磁盘镜像文件格式
  • backing_file指定所用的后端磁盘镜像文件
[root@desktop example]# qemu-img create -f qcow2 -o backing_file=test3.qcow2 new_disk.qcow2
Formatting 'new_disk.qcow2', fmt=qcow2 size=1073741824 backing_file='test3.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off
[root@desktop example]# qemu-img info new_disk.qcow2
image: new_disk.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 196K
cluster_size: 65536
backing file: test3.qcow2
Format specific information:
    compat: 1.1
    lazy refcounts: false

虚拟磁盘格式转换,把raw格式的镜像文件转成qcow2的磁盘镜像文件

  • -O 指定输出格式
[root@desktop example]# qemu-img convert -O qcow2 t1.img t1.qcow2
[root@desktop example]# qemu-img info t1.qcow2
image: t1.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@desktop example]#

调整虚拟磁盘大小:
语法格式:resize filename [+|-] size
操作之前,一定要做好数据备份
增加文件大小后,需要在客户机使用fdisk,parted等分区工具进行相应的操作才能真正让客户机使用到增加后的镜像空间。
缩小镜像之前,要在客户机中保证里面的文件系统有空余空间,否则会数据丢失。
qcow2不支持缩小镜像的操作。

[root@desktop example]# qemu-img info test5.qcow2
image: test5.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 1.0G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@desktop example]# qemu-img resize test5.qcow2 +2G
Image resized.
[root@desktop example]# qemu-img info test5.qcow2
image: test5.qcow2
file format: qcow2
virtual size: 3.0G (3221225472 bytes)
disk size: 1.0G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false