qemu-img是一个功能强大磁盘镜像管理工具。
qemu-img --help 包括以下功能:
示例:
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格式选项:
预分配策略:
# 每种预分配格式创建的磁盘镜像文件初始占用的磁盘空间 -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指定后端镜像盘:指定后端镜像盘来创建新的镜像盘
[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的磁盘镜像文件
[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