Creating a qcow2 disk image file on another partition
This guide was tested on Ubuntu 22.04 LTS, 22.10, 23.04, 23.10.
Automatically linking a directory to a block (automatic partition mounting)
1. Make new directories
$ sudo mkdir /media/username
$ sudo mkdir /media/username/Shared
$ sudo mkdir /media/username/Shared/kvm
Important! Directory name kvm with lower case letters.
2. Run blkid command to display information about available block devices. You will need universally unique identifier (UUID)
$ sudo blkid
The result should look something like:
/dev/sda1: LABEL="Ubuntu" UUID="azazazaz-azaz-azaz-azaz-azazazazazaz" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="Ubuntu" PARTUUID="a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1"
/dev/sda2: LABEL="Shared" UUID="abababab-abab-abab-abab-abababababab" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="Shared" PARTUUID="a2a2a2a2-a2a2-a2a2-a2a2-a2a2a2a2a2a2"
3. Edit the fstab file.
$ sudo nano /etc/fstab
Add the following two lines with the correct UUID in fstab file:
# Shared
UUID=abababab-abab-abab-abab-abababababab /media/username/Shared ext4 defaults 0 2
The end result should look something like:
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/nvme0n1p3 during installation
UUID=azazazaz-azaz-azaz-azaz-azazazazazaz / ext4 errors=remount-ro 0 1
# Shared
UUID=abababab-abab-abab-abab-abababababab /media/username/Shared ext4 defaults 0 2
4. Run mount -a
command
$ sudo mount -a
Possible issues:
- The emulator may not have search permissions for the path
'/media/username/Shared/kvm/Disk.qcow2'
- Error starting domain: Cannot access storage file
'/media/username/Shared/kvm/Disk.qcow2' (as uid:10101, gid:101):
Permission denied
5. Fix permission issues
$ sudo chown root:root '/media'
$ sudo chmod 775 '/media'
$ sudo chown root:root '/media/username'
$ sudo chmod 775 '/media/username'
$ sudo chown username:username '/media/username/Shared'
$ sudo chmod 775 '/media/username/Shared'
$ sudo chown username:username '/media/username/Shared/kvm'
$ sudo chmod 777 '/media/username/Shared/kvm'
Changing the default storage pool for libvirt
6. List the current pools
$ virsh pool-list
You should get the following text:
Name State Autostart
-------------------------------------------
default active yes
7. Destroy the default pool.
$ virsh pool-destroy default
You should get the following text: Pool default destroyed
8. Undefine the pool
$ virsh pool-undefine default
You should get the following text: Pool default has been undefined
9. Define a new pool named "default"
$ virsh pool-define-as --name default --type dir --target /media/username/Shared/kvm
You should get the following text: Pool default defined
10. Set the pool to start when the libvirt daemons start
$ virsh pool-autostart default
You should get the following text: Pool default marked as autostarted
11. Start the pool
$ virsh pool-start default
You should get the following text: Pool default started
12. Check the state of the pool
$ virsh pool-list
You should get the following text:
Name State Autostart
-------------------------------------------
default active yes
Now, when creating virtual machines, Virtual Machine Manager will save qcow2 files to the /media/username/Shared/kvm directory.
Creating a new guest machine or qcow2 file
13. Run virt-manager
$ virt-manager
14. Create a disk image for the virtual machine or create a new machine.