Skip to content

Instantly share code, notes, and snippets.

@JPalmerGithub
Forked from plembo/relocatelibvirtimages.md
Created November 3, 2022 12:08
Show Gist options
  • Save JPalmerGithub/d50d2dd2bbc903c914d217947d716fbb to your computer and use it in GitHub Desktop.
Save JPalmerGithub/d50d2dd2bbc903c914d217947d716fbb to your computer and use it in GitHub Desktop.
Relocate kvm images directory

Relocate an existing libvirtd (KVM) images directory

By default, KVM (libvirtd) images on Ubuntu and most other Linux distributions are found in /var/lib/libvirt/images. This can be inconvenient if you don't have a separate /var partition that can grow over time to accommodate multiple large images.

You can simply rename the images folder to something else and then symlink to a larger space with it (e.g. ln -s /data1/libvirt/images /var/lib/libvirt/images). That's what I used to do.

But that can lead to all sorts of unanticipated trouble. The right way to have your images on a bigger disk is to change the path for libvirt's default storage pool, which is logically where KVM is going to create them.

First check to make sure a default pool exists:

$ virsh pool-list --all
 Name      State    Autostart
-------------------------------
 default   active   yes

NOTE: If no default pool exists, follow the instructions in this gist to create one on your preferred storage volume.

Assuming a default pool exists, find the current default pool's path:

$ virsh pool-dumpxml default
<pool type='dir'>
  <name>default</name>
  <uuid>91c6d1b3-4e42-4a1f-989e-89b2127da056</uuid>
  <capacity unit='bytes'>133070688256</capacity>
  <allocation unit='bytes'>25177309184</allocation>
  <available unit='bytes'>107893379072</available>
  <source>
  </source>
  <target>
    <path>/var/lib/libvirt/images</path>
    <permissions>
      <mode>0711</mode>
      <owner>64055</owner>
      <group>108</group>
    </permissions>
  </target>
</pool>

Before going further it is best to shut down ("destroy") all existing virtual machines, and then use virsh edit to change the disk path to the default pool's new location. Assuming this will be "/data1/libvirt/images", you would proceed like this:

$ virsh-edit example
...
<disk type='file' device='disk'>
    <driver name='qemu' type='qcow2'/>
-   <source file='/var/lib/libvirt/images/example.qcow2'/>
+   <source file='/data1/libvirt/images/example.qcow2'/>

(the line will then read <source file='/data1/libvirt/images/example.qcow2')

Needless to say,those disk images should also be physically moved to that new location.

With that out of the way, a dump of the default pool should be saved:

$ virsh pool-dumpxml default >default-pool.xml

Next, the default pool should be completely shut down:

$ virsh pool-destroy default

Then, undefined:

$ virsh pool-undefine default

Now, taking the original dump of the pool, edit the target path so it now points to the new images location:

  <target>
-    <path>/var/lib/libvirt/images</path>
+    <path>/data1/libvirt/images</path>

(the line should then read <path>/data1/libvirt/images</path>)

Having done this, redefine and start the pool, and set it to autostart:

$ virsh pool-define default-pool.xml
$ virsh pool-start default
$ virsh pool-autostart default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment