Home KVM How To Change KVM Libvirt Default Storage Pool Location

How To Change KVM Libvirt Default Storage Pool Location

By sk
Published: Updated: 58.4K views

This guide explains what are storage pools and volumes in Libvirt and how to change KVM libvirt default storage pool location using Virsh program, Virt-manager and Cockpit in Linux.

Storage pools and volumes in Libvirt

Libvirt provides storage management on a KVM host system through storage pools and volumes.

A storage pool is a quantity of storage allocated on the KVM host for use by the virtual machines. A storage pool is divided into storage volumes and assigned to the VMs as block devices.

To put this in other words, a storage pool is storage resource on KVM host system that can be used for storing volumes. And the storage volume is a storage device that can be assigned to a virtual machine.

A storage volume can be a block device, for example a partition, logical volume, or a file. The virtual hard disks, cd/dvd/floppy devices, and ISO images attached to the guest machines are examples of storage volumes.

You can't have a storage volume without storage pool. First, you need to create a storage pool and then create the storage volumes inside the pool.

By default, all the virtual machines created with libvirt are stored under /var/lib/libvirt/images/ location. This is the default storage pool for Libvirt VM images.

Types of storage pools

A storage pool can be one of the following types:

  1. Directory pool - Use a directory as a pool for hosting image files. The files can be either one of the valid volume format types such as raw, qcow, qcow2, dmg, vmdk, vpc or ISO images.
  2. Filesystem pool - Use a block device (E.g. partition or LVM group) as pool instead of a using directory on an existing mounted filesystem.
  3. Network filesystem pool - Use a network filesystem (E.g. cifs, glusterfs, nfs etc.) as a pool to store storage volumes. Instead of directory or a block device, it requires a network host and the path of a shared directory.
  4. Logical volume pool - Use an LVM volume group as a pool for storing volumes.
  5. Disk pool - Use a physical disk as a pool. The volumes can be created by adding partitions to the disk.
  6. iSCSI pool - Use an iSCSI target to store volumes. All volumes should be pre-allocated on the iSCSI server.
  7. iSCSI direct pool - This is a variant of the iSCSI pool. Instead of using iscsiadm, it uses libiscsi. It requires a host, a path which is the target IQN, and an initiator IQN.
  8. SCSI pool - Use an SCSI host bus adapter in almost the same way as an iSCSI target.
  9. Multipath pool - At present, Multipath is not supported.
  10. RBD pool - This storage driver provides a pool which contains all RBD images in a RADOS pool. RBD (RADOS Block Device) is part of the Ceph distributed storage project.
  11. Sheepdog pool - Use Sheepdog Cluster as a pool to store volumes.
  12. Gluster pool - Use Gluster distributed file system as a pool.
  13. ZFS pool - Use ZFS filesystem as a pool.
  14. Vstorage pool - Use Virtuozzo distributed software-defined storage as a pool.

For the purpose of this guide, we will be using Directory pool.

1. Change KVM Libvirt Default Storage Pool Location

As stated already, all KVM/Libvirt images are stored in /var/lib/libvirt/images/ location by default. Since this location resides in the rootfs partition, you may want to move it to some other location, for example $HOME directory.

We can change the default location of KVM Libvirt VM images using virsh command line program and its graphical front-end called Virt-manager, and also Cockpit web console.

First, we will see how to change Libvirt default storage pool path using virsh program.

1.1. Change KVM Libvirt default storage pool location using virsh program

1.1.1. Login to your KVM host system and power off all running guests.

To find the list of running VMs, simply do:

$ virsh list --all

Shutdown if there any VM(s) are running.

$ virsh shutdown <vm-name>

1.1.2. List all the configured storage pools in your KVM host machine:

$ virsh pool-list 

Sample output:

 Name      State    Autostart
 default   active   no

As you can see, I have only one storage pool, which is the default. It is created automatically when we install kvm/libvirt.

1.1.3. Let us view the details of the default storage pool using command:

$ virsh pool-info default 

Sample output:

 Name:           default
 UUID:           76f47a3c-7656-4c34-9ab5-c87e5136d532
 State:          running
 Persistent:     yes
 Autostart:      no
 Capacity:       369.22 GiB
 Allocation:     65.60 GiB
 Available:      303.62 GiB
Display the details of the libvirt default storage pool
Display the details of the libvirt default storage pool

The above output lists the details such as name of the storage pool, UUID, state, persistent, autostart, capacity, allocated space of the storage pool and the remaining available space.

Since we are going to change the path of the storage pool, we need to find it as well.

1.1.4. To display the path of the default storage pool, we use grep command with virsh like below:

$ virsh pool-dumpxml default | grep -i path

Sample output:

<path>/var/lib/libvirt/images</path>

As you see in the above output, the default storage pool is /var/lib/libvirt/images/. We are going to change it in the subsequent steps.

1.1.5. List all the existing VM images that are stored in the default storage pool using virsh like below:

$ virsh vol-list default | grep "/var/lib/libvirt/images/"

Sample output:

archlinux.img   /var/lib/libvirt/images/archlinux.img

I have one Arch Linux virtual machine image in the default storage pool.

1.1.6. Stop and undefine the default storage pool with command:

$ virsh pool-destroy default
$ virsh pool-undefine default

1.1.7. Edit the default storage pool using command:

$ virsh pool-edit default 

This will open the VM's XML file in the your default editor.

Find the line that reads "<path>/var/lib/libvirt/images</path>" under the target directive and change it to a new path of your choice.

In my case, I have changed the <path> element from /var/lib/libvirt/images to /home/sk/.local/share/libvirt/images.

<pool type='dir'>
  <name>default</name>
  <uuid>76f47a3c-7656-4c34-9ab5-c87e5136d532</uuid>
  <capacity unit='bytes'>396452147200</capacity>
  <allocation unit='bytes'>71178977280</allocation>
  <available unit='bytes'>325273169920</available>
  <source>
  </source>
  <target>
    <path>/home/sk/.local/share/libvirt/images</path>
    <permissions>
      <mode>0755</mode>
      <owner>1000</owner>
      <group>1000</group>
      <label>unconfined_u:object_r:svirt_home_t:s0</label>
    </permissions>
  </target>
</pool>
Change KVM Libvirt default storage pool location using virsh command
Change KVM Libvirt default storage pool location using virsh command

Just make sure the new path is exists. If it exists, just create it and assign sufficient permission to the new path directory. Save the file and close it.

TIP:

If there is no default Storage pool exists for any reason, you can create one like below:

$ virsh pool-define-as --name default --type dir --target /home/sk/.local/share/libvirt/images/

Replace the storage path with your own.

1.1.8. Finally, start the default storage pool:

$ virsh pool-start default

1.1.9. Set storage pool to start automatically on system boot:

$ virsh pool-autostart default

1.1.10. Verify if the libvirt storage pool path has been changed or not with command:

$ virsh pool-dumpxml default | grep -i path

You should now see the new path.

     /home/sk/.local/share/libvirt/images

From now on, the virtual images will be saved in the new storage pool.

1.1.11. Check the storage pool state:

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

1.1.12. Restart libvirtd service:

$ sudo systemctl restart libvirtd

1.1.13. We need to do one last thing. Copy all VM images from old storage path to the new one:

$ sudo mv /var/lib/libvirt/images/archlinux.qcow2 /home/sk/.local/share/libvirt/images/

All done!

This is how we change KVM Libivrt default storage pool location to new one from command line using Virsh program. As you can see, changing default storage path for KVM/Libvirt guests is fairly easy!

Now let us look at how to change Libvirt default storage pool path using a graphical application named Virt-manager.

1.2. Change KVM Libvirt default storage pool location using Virt-manager

Virt-manager, short for Virtual Machine Manager, is a graphical user interface application used for managing virtual machines through libvirt. To put this in other words, Virt-manager is a just graphical front-end for libvirt.

1.2.1. Open Virt-manager application. Right click on QEMU/KVM and click Details option.

View KVM connection details
View KVM connection details

You can also click Edit-> Connection details from the Virt-manager interface.

1.2.2. Under the Storage section, you will see the default storage pool location.

KVM Libvirt default storage pool location
KVM Libvirt default storage pool location

1.2.3. Click Stop Pool and then Delete Pool options in the bottom left pane.

Stop and delete KVM Libvirt default storage pool
Stop and delete KVM Libvirt default storage pool

This will deactivate and delete the default pool.

1.2.4. Click the plus (+) sign on the bottom left pane to create a new storage pool for use by the virtual machines.

Enter the name for the storage pool (E.g. default in my case). Choose the type of the pool. In our case, I have selected Filesystem Directory. Specify the target location and click Finish.

Create new KVM Libvirt storage pool
Create new KVM Libvirt storage pool

1.2.5. Now the new Storage is active. Check the Autostart box to automatically start the new storage pool at system boot.

New KVM Libvirt storage pool location
New KVM Libvirt storage pool location

1.2.6. Move all the VM images from the old storage directory to the new one.

$ sudo mv /var/lib/libvirt/images/archlinux.qcow2 /home/sk/.local/share/libvirt/images/

1.2.7. Finally, restart libvirtd service:

$ sudo systemctl restart libvirtd

1.3. Change KVM Libvirt default storage pool location using Cockpit

Cockpit is a free web-based server administration tool to monitor and administer Linux servers via a web browser.

1.3.1 Open your web browser and log in to Cockpit console by navigating to http://localhost:9090 or http://IP-address:9090 from the address bar.

Log in to Cockpit web console
Log in to Cockpit web console

1.3.2. Click on the Virtual Machines tab on the left pane. On the right hand side, click Storage pool option.

Go to Storage pool section in Cockpit
Go to Storage pool section in Cockpit

1.3.3. Under the Storage pools section, You will see the default storage pool. Click the small arrow to expand storage pools tab and click Deactivate and Delete buttons.

Deactivate and delete default storage pool in Cockpit
Deactivate and delete default storage pool in Cockpit

Note: Since I already changed the default storage location, it shows the new path. In your system, it will show the old default path i.e. /var/lib/libvirt/images.

1.3.4. Enter the storage pool name, type, and the target path and click Create. Make sure the Startup check box is checked.

Create new KVM Libvirt storage pool in Cockpit
Create new KVM Libvirt storage pool in Cockpit

1.3.5. Click Activate button to start the pool.

Activate KVM Libvirt storage pool in Cockpit
Activate KVM Libvirt storage pool in Cockpit

1.3.6. Move all the VM images from the old storage directory to the new one.

$ sudo mv /var/lib/libvirt/images/archlinux.qcow2 /home/sk/.local/share/libvirt/images/

1.3.7. Restart libvirtd service:

$ sudo systemctl restart libvirtd

Troubleshooting

After Changing the storage pool path, you will probably encounter with the following error when starting a VM:

Failed to start domain 'Archlinux_default' error: Cannot access storage file '/home/sk/.local/share/libvirt/images/Archlinux_default.img' (as uid:107, gid:107): Permission denied.

This is because the qemu user may not have read permission to the storage directory. To fix it, refer the following link:

Conclusion

In this guide, we discussed an important KVM/Libvirt topic - Storage pools. We learned what are storage pools and volumes, and then how to change kvm libvirt default storage pool location using three programs, namely VIrsh, Virt-manager and Cockpit. Hope you find this useful.

Resource:

Featured image by mohamed Hassan from Pixabay.

You May Also Like

2 comments

chmedly November 12, 2024 - 11:19 pm

Can you elaborate on some of the permissions issues that might happen when moving the default pool?

Reply
sk November 14, 2024 - 2:51 pm

The most common permission issues that occur are:

1. Missing execute permissions on parent directories preventing libvirt from accessing the path
2. Incorrect SELinux contexts if SELinux is enabled
3. Wrong ownership preventing QEMU from writing to image files
4. Insufficient permissions for libvirt-qemu to manage the storage

By addressing these potential issues, you can able to smoothly move to the new storage pool location in your KVM/libvirt setup.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More