qcow2标头有多大?

时间:2015-09-16 11:18:13

标签: linux virtualization qemu kvm libvirt

我的原始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标头大小&#34; 256位等于总标头大小1&#39; 052&#39; 672字节和512位2MiB之一。&#34; (6.2 How do I backup a LUKS header?),但无法找到有关qcow2标头大小的任何可靠信息。它是固定的还是取决于虚拟磁盘大小? qcow2标题有多大?

1 个答案:

答案 0 :(得分:0)

qcow2标头大小为1个集群,默认集群大小为64kb,但除此之外,您需要添加可变大小的L1和L2表,以及集群引用表,其大小取决于虚拟映像尺寸。在进行预分配时,即使qcow2代码也不能计算出所需的确切大小 - 它会近似逼近,因为真正的计算非常困难。结果是没有真正好的方法来获得确切的数字。最好的办法是通过使用最大的预期图像大小进行预分解测试来确定开销。例如,假设您希望最大的图像大小为20 GB,

$ qemu-img create -f qcow2 -o preallocation = falloc demo.img 20G

格式化&#39; demo.img&#39;,fmt = qcow2 size = 21474836480 encryption = off cluster_size = 65536 preallocation = falloc lazy_refcounts = off refcount_bits = 16

$ ls -al demo.img -rw-R - R--。 1 berrange berrange 21478375424 12月21日23:11 demo.img

21478375424-21474836480 == 3538944

因此,20 GB的图像在qcow2中有大约3.5 MB的开销。