<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://wiki.karlsbakk.net/index.php?action=history&amp;feed=atom&amp;title=Libvirt_TRIM%2FUNMAP</id>
	<title>Libvirt TRIM/UNMAP - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.karlsbakk.net/index.php?action=history&amp;feed=atom&amp;title=Libvirt_TRIM%2FUNMAP"/>
	<link rel="alternate" type="text/html" href="https://wiki.karlsbakk.net/index.php?title=Libvirt_TRIM/UNMAP&amp;action=history"/>
	<updated>2026-06-14T20:19:28Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.0</generator>
	<entry>
		<id>https://wiki.karlsbakk.net/index.php?title=Libvirt_TRIM/UNMAP&amp;diff=9&amp;oldid=prev</id>
		<title>Roy: Moved from https://wiki.malinux.no/index.php/Libvirt_TRIM/UNMAP</title>
		<link rel="alternate" type="text/html" href="https://wiki.karlsbakk.net/index.php?title=Libvirt_TRIM/UNMAP&amp;diff=9&amp;oldid=prev"/>
		<updated>2020-11-17T18:49:41Z</updated>

		<summary type="html">&lt;p&gt;Moved from https://wiki.malinux.no/index.php/Libvirt_TRIM/UNMAP&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;KVM/Libvirt supports UNMAP, similar to TRIM. That way you can free space used by a VM on a qemu2 vdisk not currently in use. &lt;br /&gt;
&lt;br /&gt;
First of all, your disk images need to be in QCOW2 format. If they are raw, [https://unixblogger.com/2016/06/13/convert-img-raw-to-qcow2/ convert] them first. To allow for deallocation of unused space, you&amp;#039;ll need to use UNMAP, similar to TRIM, except that the latter is the ATA counterpart to SCSI&amp;#039;s UNMAP. KVM/Libvirt doesn&amp;#039;t support the ATA version, so we&amp;#039;ll change the driver to SCSI. Usually drives are added as &amp;#039;&amp;#039;&amp;#039;VirtIO&amp;#039;&amp;#039;&amp;#039;. If they are, change this, using virt-manager. Obviously, you can do this on the commandline, but I&amp;#039;m lazy so I just use the GUI. On my box (Debian Jessie), I can&amp;#039;t change &amp;#039;&amp;#039;&amp;#039;VirtIO&amp;#039;&amp;#039;&amp;#039; to &amp;#039;&amp;#039;&amp;#039;VirtIO SCSI&amp;#039;&amp;#039;&amp;#039;, so I&amp;#039;ll have to remove and re-add the drives. If your version of KVM/Libvirt support changing this, skip steps 2 and 3 and just change it. Otherwise, do as follows&lt;br /&gt;
&lt;br /&gt;
# Shut down the VM&lt;br /&gt;
# Start virt-manager and remove the disk(s) from the VM&lt;br /&gt;
# Add them in the same order, but as &amp;#039;&amp;#039;VirtIO SCSI&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Now, you need to add a flag to the driver to make it support UNMAP. Changing the XML config file itself may not work too well, so in my experience, I find it easier to just dump the config, change it, undefine (that is, delete, not touching the data) the old VM and recreating one.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
virsh dumpxml myvm &amp;gt; myvm.xml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then change myvm.xml, adding the discard part&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;driver name=&amp;#039;qemu&amp;#039; type=&amp;#039;qcow2&amp;#039; discard=&amp;#039;unmap&amp;#039;/&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Undefine and redefine the VM&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
virsh undefine myvm&lt;br /&gt;
virsh define myvm.xml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start the vm with &amp;#039;&amp;#039;&amp;#039;virsh start myvm&amp;#039;&amp;#039;&amp;#039; or with virt-manager and wait for it to start. When done, try &amp;#039;&amp;#039;&amp;#039;du -sh /var/lib/libvirt/images/myvm.qcow2&amp;#039;&amp;#039;&amp;#039; and run &amp;#039;&amp;#039;&amp;#039;fstrim /&amp;#039;&amp;#039;&amp;#039; inside the VM and check the &amp;#039;&amp;#039;&amp;#039;du&amp;#039;&amp;#039;&amp;#039; output again. If there are a lot of wasted space, it should be freed by now.&lt;br /&gt;
&lt;br /&gt;
[mailto:roy@karlsbakk.net roy]&lt;/div&gt;</summary>
		<author><name>Roy</name></author>
	</entry>
</feed>