How to Change libvirt’s Default Storage Pool Location for KVM Virtual Disks and ISOs


1 views

When working with libvirt on Ubuntu-based systems, you'll notice that Virtual Machine Manager (virt-manager) defaults to /var/cache/libvirt for storing virtual disks and ISOs, regardless of other configured storage pools. This behavior persists even when you've created and activated additional storage pools.

The default location has several limitations:

  • Limited space on root partition
  • Potential performance issues with system files
  • Difficulties in backup management
  • No separation between system and VM storage

Here's how to permanently change the default storage location:

1. Create the New Storage Pool

First, define a new directory-based pool in your preferred location (/media/work/kvm in your case).

sudo virsh pool-define-as --name kvm_storage --type dir --target /media/work/kvm
sudo virsh pool-build kvm_storage
sudo virsh pool-start kvm_storage
sudo virsh pool-autostart kvm_storage

2. Modify libvirt Configuration

Edit the libvirt daemon configuration file:

sudo nano /etc/libvirt/libvirtd.conf

Find and modify these parameters:

default_image_format = "qcow2"
default_storage_pool = "kvm_storage"

3. Restart libvirt Services

Apply the changes by restarting the services:

sudo systemctl restart libvirtd
sudo systemctl restart virtlogd.socket

Confirm the changes took effect:

virsh pool-list --all
virsh pool-dumpxml kvm_storage

When creating a new VM in virt-manager, the default path should now point to your custom location.

For more complex setups, consider these additional adjustments:

Set Default Volume Format

sudo virsh pool-edit kvm_storage

Add the following to the XML configuration:

<target>
  <path>/media/work/kvm</path>
  <format type='qcow2'/>
</target>

Modify Permissions for Non-root Usage

sudo chown -R :libvirt /media/work/kvm
sudo chmod -R g+rw /media/work/kvm
  • Permission denied errors: Ensure SELinux contexts are correct if enabled
  • Storage pool not persistent: Verify you ran pool-autostart
  • virt-manager still shows old location: Clear the application cache or restart the GUI

If you can't modify system configurations, consider creating a symbolic link:

sudo mv /var/lib/libvirt/images /var/lib/libvirt/images.old
sudo ln -s /media/work/kvm /var/lib/libvirt/images

This maintains compatibility while redirecting storage.


Many KVM users encounter situations where Virtual Machine Manager persistently defaults to /var/cache/libvirt despite creating alternative storage pools. This occurs because libvirt maintains an internal default configuration that isn't automatically updated when new pools are added.

First, verify your current storage pools:

virsh pool-list --all

To permanently set your preferred storage pool (e.g., /media/work/kvm) as default, edit libvirt's configuration:

sudo nano /etc/libvirt/libvirtd.conf

Add or modify these directives:

default_pool = "kvm_pool"
storage_pool_dir = "/media/work/kvm"

For your specific case with /media/work/kvm, here's how to properly define it:


<pool type='dir'>
  <name>kvm_pool</name>
  <target>
    <path>/media/work/kvm</path>
    <permissions>
      <mode>0711</mode>
      <owner>libvirt-qemu</owner>
      <group>libvirt</group>
    </permissions>
  </target>
</pool>

Save this as kvm_pool.xml and activate it:

virsh pool-define kvm_pool.xml
virsh pool-start kvm_pool
virsh pool-autostart kvm_pool

After restarting libvirtd (sudo systemctl restart libvirtd), check the active default:

virsh pool-dumpxml kvm_pool | grep -i default

For Virtual Machine Manager to respect these changes, edit QEMU's configuration:

sudo nano /etc/libvirt/qemu.conf

Add these parameters:

dynamic_ownership = 1
user = "libvirt-qemu"
group = "libvirt"

Common permission problems can be resolved with:

sudo chown -R libvirt-qemu:libvirt /media/work/kvm
sudo chmod -R 771 /media/work/kvm
sudo restorecon -Rv /media/work/kvm