我的原始vm映像当前存储在luks加密文件(例如/var/lib/libvirt/images/win7.img)中,并通过以下方式使用libvirt访问:

<disk type='block' device='disk'>
  <driver name='qemu' type='raw'/>
  <source dev='/dev/mapper/win7.img'/>
  <target dev='vda' bus='virtio'/>
</disk>

我在btrfs's snapshot functionality的帮助下支持他们:
# sample backup procedure
IMG_PATH="/var/lib/libvirt/images"
virsh shutdown win7
cryptsetup luksClose /dev/mapper/win7.img
DATE=$(date --utc +%Y-%m-%d-%H%M%S)
btrfs subvolume snapshot -r ${IMG_PATH}/ ${IMG_PATH}/${DATE}
cryptsetup luksOpen --key-file /root/libvirt.key ${IMG_PATH}/win7.img
virsh start win7
btrfs send ${IMG_PATH}/${DATE} | btrfs receive /mnt/backup/images/

# subsequent backup
virsh shutdown win7
cryptsetup luksClose /dev/mapper/win7.img
NEW_DATE=$(date --utc +%Y-%m-%d-%H%M%S)
btrfs subvolume snapshot -r ${IMG_PATH}/ ${IMG_PATH}/${NEW_DATE}
cryptsetup luksOpen --key-file /root/libvirt.key ${IMG_PATH}/win7.img
virsh start win7
btrfs send -p ${IMG_PATH}/${DATE} ${IMG_PATH}/${NEW_DATE} | btrfs receive /mnt/backup/images/
btrfs subvolume delete ${IMG_PATH}/${DATE}

现在,我打算通过converting the raw images to compressed qcow2节省一些空间:
# create a sparse and LUKS encrypted image
# the LUKS header is 2 MiB large
# therefore "seek=51202" instead of "seek=51200" for a 50 GiB virtual disk
IMG_PATH="/var/lib/libvirt/images"
dd bs=1048576 count=0 seek=51202 if=/dev/zero of=${IMG_PATH}/win7_new.img
cryptsetup luksFormat -c aes-xts-plain -h sha512 -s 512 ${IMG_PATH}/win7_new.img
cryptsetup luksAddKey ${IMG_PATH}/win7_new.img /root/libvirt.key
cryptsetup luksOpen --key-file /root/libvirt.key ${IMG_PATH}/win7_new.img win7_new.img

# copy the content from the old image to the new compressed image and delete the old one
qemu-img convert -f raw -O qcow2 -c /dev/mapper/win7.img /dev/mapper/win7_new.img
cryptsetup luksClose /dev/mapper/win7.img
cryptsetup luksClose /dev/mapper/win7_new.img
rm -f ${IMG_PATH}/win7.img
mv ${IMG_PATH}/win7_new.img ${IMG_PATH}/win7.img

新的libvirt xml代码如下:
<disk type='block' device='disk'>
  <driver name='qemu' type='qcow2'/>
  <source dev='/dev/mapper/win7.img'/>
  <target dev='vda' bus='virtio'/>
</disk>

我的问题是:
我知道luks头大小“256位等于总头大小1'052'672字节,512位等于2mib。”(6.2 How do I backup a LUKS header?),但找不到关于qcow2头大小的任何可靠信息。它是固定的还是取决于虚拟磁盘的大小?qcow2头有多大?

最佳答案

qcow2头的大小为1个集群,默认集群大小为64kb,但除此之外,还需要添加可变大小的l1和l2表以及集群refcount表,其大小取决于虚拟映像大小。当进行预分配时,即使QCOW2代码不计算所需的精确大小,它也会近似近似,因为真正的计算是相当困难的。结果是没有真正好的方法得到准确的数字。最好的办法就是用您期望的最大图像大小进行预分配测试来确定开销。比如说,你预计最大的图像大小为20GB,
$qemu img create-f qcow2-o预分配=falloc demo.img 20g
格式化“demo.img”,fmt=qcow2 size=21474836480 encryption=off cluster\u size=65536 preallocation=falloc lazy\u refcounts=off refcount\u bits=16
$ls-al demo.img
-rw-r--r--。1 Berrange Berrange 21478375424 12月21日23:11 demo.img
21478375424-21474836480==3538944
因此,20GB的图像在qcow2中有大约3.5MB的开销。

关于linux - qcow2 header 有多少?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32607114/

10-12 18:18