13

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.

Catbird55
  • 153
  • 1
  • 1
  • 6

1 Answers1

25

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.

Tuomas
  • 705
  • 7
  • 9
  • That worked. It seems like I can use foobar.qcow2 just as easily as the .img file type. Is there a disadvantage to qcow2? – Catbird55 Oct 25 '15 at 14:56
  • 1
    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. – Tuomas Oct 26 '15 at 17:05
  • Why file manger in `Linux` show me actual v disk size `10.7 GB` even before I write anything to it ! – Salem F Dec 21 '18 at 11:47
  • 2
    @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`. – F.Raab Jul 09 '19 at 13:07