跳转至

两种类型的镜像文件

有时我们发现 qemu-img 查看镜像大小不一样

如下:

Bash
1
2
3
# ll -h test*.img
-rw------- 1 root root  11G 10月 21 16:05 test1.img    # 创建时勾选了metadata
-rw------- 1 root root 193K 10月 21 16:06 test2.img    # 创建时没有勾选metadata

这两个镜像同样是10G空间,但是在系统显示test2就要小很多

可以查看两个镜像对比信息

Bash
# qemu-img info test1.img 
image: test1.img
file format: qcow2
virtual size: 10 GiB (10737418240 bytes)
disk size: 20.8 MiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    extended l2: false
Child node '/file':
    filename: test1.img
    protocol type: file
    file length: 10 GiB (10739318784 bytes)
    disk size: 20.8 MiB
    Format specific information:
        extent size hint: 1048576

# qemu-img info test2.img 
image: test2.img
file format: qcow2
virtual size: 10 GiB (10737418240 bytes)
disk size: 196 KiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    extended l2: false
Child node '/file':
    filename: test2.img
    protocol type: file
    file length: 192 KiB (197120 bytes)
    disk size: 196 KiB
    Format specific information:
        extent size hint: 1048576

注意不同在于file length:

通过webvirtmgr管理工具创建镜像时,勾选metadata , 会自动配置preallocation=metadata,这样创建的镜像就是完整大小的

如果不勾选,则使用preallocation=off,创建的镜像属于稀疏文件格式,起初镜像很小,随着使用增大

preallocation 预分配镜像大小

  • preallocation=off (默认,稀疏文件),初始镜像小,随着使用增长,性能会低
  • preallocation=metadata (预分配元数据),创建固定大小的镜像,提前占用宿主机磁盘大小,提高使用性能

预分配镜像的转换

将稀疏文件转换为预分配( 此时镜像增大)

Bash
1
2
3
# 将 test.img 转换为预分配
qemu-img convert -f qcow2 -O qcow2 -o preallocation=metadata test.img test-prealloc.img
mv test-prealloc.img test.img

将预分配转换为稀疏文件(此时镜像减小)

Bash
1
2
3
# 将 test1.img 转换为稀疏文件
qemu-img convert -f qcow2 -O qcow2 test1.img test1-sparse.img
mv test1-sparse.img test1.img