How do you create a qcow2 file that is small yet commodious on a Linux server?

59,173

To create a qcow2 image for a virtual disk size of 100MB do:

qemu-img create -f qcow2 foobar.qcow2 100M

The created foobar.qcow2 will be about 200KB.

You can easily create a very big virtual disk with this which will take only very small space on the host filesystem, e.g. 100TB virtual disk would take only about 2MB on the host, but when the disk is being used on the guest the file will grow on the host. That will eventually fill the host filesystem and prevent guest doing further writes to any location that is not already allocated. Allocating more resources for guest that is really available on the host is called thin provisioning, see https://en.wikipedia.org/wiki/Thin_provisioning for more info.

Simplest way to avoid problems, when disk is being thin provisioned, is to monitor the disk usage of the host system (with automated alarms being sent to admins) and add more disk space before the host system filesystem fills up.

Share:
59,173

Related videos on Youtube

Catbird55
Author by

Catbird55

Updated on September 18, 2022

Comments

  • Catbird55
    Catbird55 almost 2 years

    I believe that there are qcow2 files that displace a small amount of gigabytes on a server (as verified with a df -h on the physical server) yet allow for lots of space when you log into the virt-manager VM and issue a df -h. How do you create such a file? How does the physical server know that there won't be a problem if/when the VMs grow to their maximum size and potentially fill up the physical server?

    The truncate command doesn't create small qcow2 files. Virtual Machine Manager's GUI allows the creation of "hardware" via the GUI. Both of these methods of creating .qcow2 files create files that are the maximum size.

    • Michael Hampton
      Michael Hampton over 8 years
      They're sparse files. It's your job to add more disk space when necessary.
  • Catbird55
    Catbird55 over 8 years
    That worked. It seems like I can use foobar.qcow2 just as easily as the .img file type. Is there a disadvantage to qcow2?
  • Tuomas
    Tuomas over 8 years
    The file extension -- whether it's foobar.img or foobar.qcow2 -- doesn't matter. The file format is not determined by the extension, but by the contents of the file. I.e. if you use "-f qcow2" the file will be qcow2 even if it's named foobar.img. But since this seems to cause confusion, I'll edit my answer and change the extension from .img to .qcow2.
  • Salem F
    Salem F over 5 years
    Why file manger in Linux show me actual v disk size 10.7 GB even before I write anything to it !
  • F.Raab
    F.Raab almost 5 years
    @SalemF You are likely using a filesystem (ext4, …) which supports ‘holes’ in ‘sparse’ files. The host filesystem knows the ‘appearent-size‘ (e.g. virtual size of the image) of the file but leaves out zero-blocks – the holes – when storing the file. Linux tools including ls -l and du --apparent-size -h will report the virtual size of image (e.g. the maximum possible size). The physical size on disk can be displayed with du -h and qemu-img info.