How to create dynamic disks .qcow2 as it happens with VDI?

5,151

You can convert the existing disk in a new disk (that uses preallocation=off) with the following command:

qemu-img convert -f qcow2 -O qcow2 -o preallocation=off vol.qcow2 newdisk.qcow2

where:

  • the -f format flag specifies the format of the input disk
  • the -O format flag specifies the format of the output disk
  • the -o flag is used to specify some options for the output file, such as the way data is allocated (in this case, no data preallocation)

Then use the new disk in place of the original disk:

  • rename vol.qcow2 as vol.qcow2.bak just to backup it. Delete it only when you are sure that newdisk.qcow2 works as expected.
  • rename newdisk.qcow2 as vol.qcow2

If you want to create a dynamic virtual disk from scratch, you can run this command:

qemu-img create -f qcow2 -o preallocation=off <disk-name> <disk-size>

where:

  • disk-name is the name of the dynamic virtual disk
  • disk-size is the maximum disk size (you can use k, M, G, T, P or E as suffixes)
Share:
5,151

Related videos on Youtube

acgbox
Author by

acgbox

Updated on September 18, 2022

Comments

  • acgbox
    acgbox over 1 year

    I have created a virtual disk with QEMU / KVM in format .qcow2 enter image description here

    I already know they are virtual sparce disks, however, the problem is that when I try to copy & paste virtual disk (vol.qcow2) in another path (removable drive), it is still 20GB (not 3.32 MiB), something that does not happen with dynamic disks in Virtualbox.

    I have tried editing the .xml file to put

    sparce = true # supposedly this option is the one that defines if the disk is dynamic or fixed
    

    But I have not been successful.

    What should I do to create a virtual disk with QEMU / KVM / virt-manager in .qcow2 or RAW format and keep it similar size of virtualbox dynamic virtual disks (VDI nd other formats like a VHD, etc)?

    PD: Please with QEMU/KVM virt-manager" in linux

    thanks in advanced

  • acgbox
    acgbox over 3 years
    Your answer is correct. I already tried it and it works (and I am grateful), but it only explains how to do it by command line and not by GUI with the "QEMU / KVM virtual machine manager" in linux
  • Lorenz Keel
    Lorenz Keel over 3 years
    I don't know if it's possible to do it via GUI. That's the reason why I typically create the virtual disk by command line, and then, when creating a VM using virt-manager, I use the option to use an existing virtual disk.
  • acgbox
    acgbox over 3 years
    ok. I will investigate this topic further to see if this is possible, although we agree, as in virtualbox, creating or resizing disks by command line is safer and better. thanks