虚拟化系列教程:磁盘格式及格式转换

格式:convert -c [-f fmt] [-O output_fmt] [-o options] filename [filename2 […]] output_filename

将fmt格式的filename镜像文件根据options选项转换为格式为output_fmt的名为output_filename的镜像文件。它支持不同格式的镜像文件之间的转换,比如可以用VMware用的vmdk格式文件转换为qcow2文件,这对从其他虚拟化方案转移到KVM上的用户非常有用。一般来说,输入文件格式fmt由qemu-img工具自动检测到,而输出文件格式output_fmt根据自己需要来指定,默认会被转换为与raw文件格式(且默认使用稀疏文件的方式存储以节省存储空间)。

其中,“-c”参数是对输出的镜像文件进行压缩,不过只有qcow2和qcow格式的镜像文件才支持压缩,而且这种压缩是只读的,如果压缩的扇区被重写,则会被重写为未压缩的数据。同样可以使用“-o options” 来指定各种选项,如:后端镜像、文件大小、是否加密等等。使用backing_file选项来指定后端镜像,让生成的文件是copy-on-write的 增量文件,这时必须让转换命令中指定的后端镜像与输入文件的后端镜像的内容是相同的,尽管它们各自后端镜像的目录、格式可能不同。

如果使用qcow2、qcow、cow等作为输出文件格式来转换raw格式的镜像文件(非稀疏文件格式),镜像转换还可以起到将镜像文件转化为更小的镜像,因为它可以将空的扇区删除使之在生成的输出文件中并不存在。

下面的命令行演示了两个转换:将VMware的vmdk格式镜像转换为KVM可以使用的qcow2镜像,将一个raw镜像文件转化为qcow2格式的镜像。

# qemu-img convert my-vmware.vmdkmy-kvm.img

(此处并无实际存在vmdk文件,仅演示其命令行操作)

# qemu-img convert -O qcow2 rhel6u3.imgrhel6u3-a.img

用例:

[root@centos8 opt]# qemu-img convert -f raw -O qcow2 centos.raw centos2.qcow2
[root@centos8 opt]# ll centos2.qcow2 centos.raw 
-rw-r--r-- 1 root root  1633353728 2月  27 11:25 centos2.qcow2
-rw------- 1 root root 10737418240 2月  27 11:25 centos.raw 
[root@centos8 opt]# qemu-img info centos2.qcow2 
image: centos2.qcow2
file format: qcow2
virtual size: 10 GiB (10737418240 bytes)
disk size: 1.52 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
extended l2: false
[root@centos8 opt]# qemu-img info centos.raw 
image: centos.raw
file format: raw
virtual size: 10 GiB (10737418240 bytes)
disk size: 1.53 GiB

在转换完格式后,如果想替换存储文件,需要执行以下命令:

[root@centos8 opt]# virsh edit centos7 
Domain 'centos7' XML configuration edited.
     61     <disk type='file' device='disk'>
     62       <driver name='qemu' type='qcow2'/>
     63       <source file='/opt/centos2.qcow2'/>
     64       <backingStore/>
     65       <target dev='vda' bus='virtio'/>
     66       <address type='pci' domain='0x0000' bus='0x04' slot='0x00' function=        '0x0'/>
     67     </disk>
[root@centos8 opt]# virsh reboot centos7

猜你喜欢

转载自blog.csdn.net/taoxicun/article/details/129423487