After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 685032 - Boxes should compactify VM images over time
Boxes should compactify VM images over time
Status: RESOLVED DUPLICATE of bug 738444
Product: gnome-boxes
Classification: Applications
Component: general
unspecified
Other Linux
: Normal normal
: --
Assigned To: GNOME Boxes maintainer(s)
GNOME Boxes maintainer(s)
Depends on:
Blocks:
 
 
Reported: 2012-09-28 10:34 UTC by Alexander Larsson
Modified: 2016-03-31 13:54 UTC
See Also:
GNOME target: ---
GNOME version: 3.7/3.8



Description Alexander Larsson 2012-09-28 10:34:27 UTC
We currently create "sparse" qcow2 images for VMs that start out empty, and grow only as needed. This is great as it allows us to avoid creating too small images that later "fill up" and have to be resized, while still not wasting space.

However, currently once diskspace has been used once we never release it, even if it is no longer used inside the guest. I.e. if you removed some files this will not shrink the disk image, although in some cases (it depends on the guest fs details) the space might be reused by new files in the guest.

It would be nice if we could somehow handle the shrinking of the image too. And related to this we might want to support general "compactification" of the images including reordering the image to be in-line with the guest disk order, as well as possibly compressing disk data.

There are various approaches here.

One interesting approach is to let the guest tell us that some disk block is not used by the filesystem anymore. This is known as the TRIM operation in ata/scsi. In qemu this can be enabled with the -device option discard_granularity. This should let the guest use the trim operation, although there might have to be some configuration of the guest to get it to use this. For instance, ext4 filesystems must be mounted with the "discard" mount option atm. With this activated unused blocks in the qcow image will be marked as unused, and may be reused without growing the image. However, it doesn't *shrink* the image.

Another approach is offline compaction. At regular times we could check the image data to see how much space is unused (in case TRIM is used as per the above) and how much fragmented the image is. When things pass over a certian treshold we just do a copy of the image to a new file using qmeu-img convert, which will compact the disk, and even optionally compress it. We could then switch to the new image and start the vm.

In case TRIM doesn't work for a guest we could use the virt-sparsify tool, which works similar to TRIM but in batch form. It parses the filesystem looking for unused space on the disk and creates a new disk containing only the data the filesystem uses. This is slighly more risky as it involves parsing the guest fs data, but works without TRIM support.

Its also possible to combine the offline models with the qcow snapshot support in order to do things "online" by creating a snapshot of a running vm image, compress that, and then shutdown the vm and apply the final delta to the new image.
Comment 1 Alexander Larsson 2012-09-28 11:27:14 UTC
For optimal reuse of TRIMed blocks we should use the same discard_granularity as the qcow cluster size, which by default is 64k.
Comment 2 Marc-Andre Lureau 2012-10-25 12:37:54 UTC
See also:
https://lists.gnu.org/archive/html/qemu-devel/2012-08/msg02111.html

"qcow2 marks the discarded blocks as free and will reuse them in future
allocations.  It does *not* discard at the OS level."

"The problem with it is that passing discards through to the file system,
or even handling them at all isn't always wanted. It is already a slow
operation for itself, but it also makes the next write access slower and
can cause fragmentation."

Imho, there might be other interesting ways of saving space, by using common base image / snapshots, building over time across images, does that exists?
Comment 3 Alexander Larsson 2012-10-25 15:10:42 UTC
Sure, it doesn't discard on the OS level. How could it? it still space in the file. However, with the offline compaction mention aboved, such space could be reclaimed (whereas it couldn't without magic parsing of the guest FS without it).

Base image + snapshots works. You can even rebase a snapshot.

According to:
https://bugzilla.redhat.com/show_bug.cgi?id=861375
upstream qemu-img now has support to calculate fragmentation in some images. This could let us know when its time to do a offline compaction.
Comment 4 Zeeshan Ali 2014-10-13 15:27:15 UTC
OK, this bug is very generic and thats one reason I never even looked into fixing this. bug#738444 has the same goal as this one and is very specific so its much more likely to get fixed.

*** This bug has been marked as a duplicate of bug 738444 ***