Changes for page libvirt

Last modified by Sebastian Marsching on 2023/05/16 20:12

From version 9.1
edited by Sebastian Marsching
on 2022/07/17 14:20
Change comment: There is no comment for this version
To version 10.1
edited by Sebastian Marsching
on 2023/05/16 20:06
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -56,3 +56,27 @@
56 56  ```
57 57  
58 58  This will create an XML file with the VM configuration and a QCOW2 file for each disk in `/path/to/destination/dir`. The XML file can be adjusted and then imported using `virsh define`.
59 +
60 +# Recovering unused space from virtual disk images
61 +
62 +libvirt offers a utility called virt-sparsify, which helps with recovering unused space from virtual disk images, reducing their size on disk. This is particularly useful with images in the QCOW2 format.
63 +
64 +The utility can either be used in-place or create a new image. To use the in-place variant, use the following command:
65 +
66 +```bash
67 +virt-sparsify --in-place /path/to/image.qcow2
68 +```
69 +
70 +The advantage of this method is that you don’t need a lot of free space on the partition storing the image. The disadvantages are that it does not always recover as much space as the second method, and if something goes wrong, the image might be damaged.
71 +
72 +The second variant creates a new image, copying data from the original one. This method has the disadvantage that it does not only require enough free space for the new image but also a lot of temporary space. As the default location for temporary files often does not offer enough free space, it can make sense to set `TMPDIR` to point to a path on a partition with a sufficient amount of free space.
73 +
74 +When using this methods, options can be passed to the `qemu-img` command. For example, setting the `preallocation` option might be useful:
75 +
76 +```bash
77 +mkdir /path/to/partition/with/free/space/tmp
78 +TMPDIR=/path/to/partition/with/free/space/tmp virt-sparsify /path/to/original/image.qcow2 -o preallocation=metadata /path/to/new/image.qcow2
79 +rm -r /path/to/partition/with/free/space/tmp
80 +```
81 +
82 +This will create an entirely new image, not touching the original one. Typically, the only useful values for the prellocation option or `none` or `metadata`. Using `falloc` or `full` will defeat the purpose of `virt-sparsify`, because instead of creating a sparse image, the unused space in the image is still going to be allocated, thus not freeing up any space.