Can not access VMs on external usb device - QEMU/KVM - Virtual Machine Manager #4524
Replies: 2 comments 1 reply
|
When using libvirt VMs (via virsh or virt-manager) I created custom volumes (e.g., from within virt-manager) that live in my ~/.local/virt dir. I have an iso sub-dir (for install media) and vm sub-dir (for qcow2 files). It is a little fiddly to keep the file ownership but it does work. This is primarily because virt-manager wants sudo auth to access the default volume locations. But I didn't dig into it further. By keeping the dirs separate I can easily choose which of the large files I need on my local hd at any given time - e.g., the iso's got removed once not needed; and so did the qcow2 files when my work on that project was done for the moment. And ~/.local/virt is backed up as part of my standard $HOME backup regimen. I can only assume you can do something similar with USB external drives as well. But may need some udev and/or automount config to get that custom volume to work reliably. Personally, I would just copy the files to/from the drive as needed if they do not need to be accessed from the ext drive. But I do realize that there are use cases where leaving them on the ext drive might be preferred. I have not tried that though. Good luck. But do try the custom volume approach to see if that gets you farther. |
SummaryWorkaround: mounting usb drive via fstab to a folder in my home directory Sources
Stepsqemu.conf
Local dir for mounting usb device via fstab entry
Setting access rights
SELinux context
sudo ls -laZ ~/mnt/VM
drwxrwx---@ - root qemu system_u:object_r:svirt_image_t:s0 2026-04-25 19:32 images
drwxrwx---@ - root qemu system_u:object_r:svirt_image_t:s0 2023-06-07 17:44 qemu
sudo ls -laZ ~/mnt/VM/images
...
-rwxrwx---. 1 root qemu system_u:object_r:svirt_image_t:s0 53695545344 26. Apr 11:24 debian.qcow2
...Adjusting path in the xml files for the VM
<domain type="kvm">
<name>debian</name>
..
<devices>
...
<disk type="file" device="disk">
...
<source file="/home/myUserID/mnt/VM/images/debian.qcow2"/>
...
</disk>
...
</devices>
</domain>Adjusting path for the VM snapshots in their xml files
Create virtual machines
Redefining snapshots
Adjusting backing file paths in snapshot filesI have external snapshots. Example-rwxrwx---. 1 root qemu 214171648 1. Feb 11:02 debian-102.20260123_2016
-rwxrwx---. 1 root qemu 1679294464 17. Apr 01:04 debian-102.20260201_1102
-rwxrwx---. 1 root qemu 10613358592 1. Feb 11:02 debian-102.qcow2sudo qemu-img info ~/mnt/VM/images/debian-102.20260201_1102
image: /var/home/myUserID/mnt/VM/images/debian-102.20260201_1102
file format: qcow2
...
backing file: /run/media/myUserID/1TB-M2NVME/01_VM/images/debian-102.20260123_2016
backing file format: qcow2
...
Child node '/file':
filename: /var/home/myUserID/mnt/VM/images/debian-102.20260201_1102
...sudo qemu-img info ~/mnt/VM/images/debian-102.20260123_2016
image: /var/home/myUserID/mnt/VM/images/debian-102.20260123_2016
file format: qcow2
...
backing file: /run/media/myUserID/1TB-M2NVME/01_VM/images/debian-102.qcow2
backing file format: qcow2
...
Child node '/file':
filename: /var/home/myUserID/mnt/VM/images/debian-102.20260123_2016
...Script to set backing file paths in snapshot filesTo change the backing file path in the snapshot files, i had to temporary create symlinks for the old paths, since Script (re)created / optimized by KI. #!/usr/bin/env bash
VM_IMAGES_DIR_CURRENT="/home/${USER}/mnt/VM/images/" # folder containing qcow2 files, including the (external) snapshots
VM_IMAGES_DIR_OLD="/run/media/${USER}/1TB-M2NVME/01_VM/images/" # old backing file path
# all VM qcow2 files incl. snaphosts are in VM_IMAGES_DIR_CURRENT
# check if path ${VM_IMAGES_DIR_CURRENT} exists:
if [[ ! -d "${VM_IMAGES_DIR_CURRENT}" ]]; then
echo "Error: Folder '${VM_IMAGES_DIR_CURRENT}' does not exist, exit."
exit 1
fi
# 1. recreate old paths
# - To change the backing file path in the snapshot files, i had to temporary create symlinks for the old paths, since `qemu-img rebase ...` checks for the old paths.
# - But they do not exist anymore, since my external usb drive is now mounted via fstab in a different path.
sudo mkdir -p "${VM_IMAGES_DIR_OLD}"
sudo chown "${USER}:${USER}" "${VM_IMAGES_DIR_OLD}"
# 2. array for the backing file names, which have to be "created" as symlinks
declare -a backing_files_to_link=()
# 3. collect all backing file names # search all files in folder ${VM_IMAGES_DIR_CURRENT}:
while IFS= read -r -d $'\0' file; do
backing_file=$(sudo qemu-img info "$file" | grep "backing file:" | sed "s/backing file: //")
if [ -n "$backing_file" ]; then
backing_file_name=$(basename "$backing_file")
if [[ ! " ${backing_files_to_link[*]} " =~ ${backing_file_name} ]]; then
backing_files_to_link+=("$backing_file_name")
fi
fi
done < <(sudo find "${VM_IMAGES_DIR_CURRENT}" -maxdepth 1 -type f -print0)
# 4. temporary create symlinks for all backing file names to the old path
for file in "${backing_files_to_link[@]}"; do
target="${VM_IMAGES_DIR_CURRENT}${file}"
link="${VM_IMAGES_DIR_OLD}${file}"
if sudo test -f "${target}"; then
echo "Creating symlink: ${link} -> ${target}"
sudo ln -sf "${target}" "${link}"
else
echo "ERROR: Backing file '${target}' does NOT exist at new path!"
exit 1
fi
done
# 5. adjust backing file path in snapshot file
while IFS= read -r -d $'\0' file; do
backing_file=$(sudo qemu-img info "$file" | grep "backing file:" | sed "s/backing file: //")
# check if backing file exists and if it is not the backing file itself:
if [ -n "$backing_file" ] && [ "$backing_file" != "$file" ]; then
# adjust backing file path to the new one (all occurances of VM_IMAGES_DIR_OLD):
new_backing_file="${backing_file//${VM_IMAGES_DIR_OLD}/${VM_IMAGES_DIR_CURRENT}}"
# show which file will be adjusted and execute rebase:
echo "Adjust: ${file} -> Backing File: $(basename "$new_backing_file")"
if ! sudo qemu-img rebase -F qcow2 -b "${new_backing_file}" "${file}"; then
echo "Failed to adjust backing file for '${file}'"
continue
fi
else
echo "Skipping file '${file}': contains no 'backing file' or it is the backing file itself."
fi
done < <(sudo find "${VM_IMAGES_DIR_CURRENT}" -maxdepth 1 -type f -print0)
# 6. delete temporary created symlinks
for file in "${backing_files_to_link[@]}"; do
echo "Deleting temporary symlink '${VM_IMAGES_DIR_OLD}${file}'"
sudo rm -f "${VM_IMAGES_DIR_OLD}${file}"
done
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi,
before switching to bluefin (developer mode enabled), i dumped config for my vms, networks, snapshots (via virsh dumpxml/net-dumpxml/snapshot-dumpxml), which are on an external usb device (/run/media/myUserID/1TB-M2NVME/01_VM/images/).
I was able to restore (except for the ones with uefi secureboot #4550 but thats another thing).
But when tried to start a VM I get the error message, e.g.:
So I tried changing ownership (
root,qemu,libvirt,kvm) and access rights, currently:sudo ls -lad /run/media/myUserID/1TB-M2NVME/01_VM/imagesdrwxrwxrwx+ 2 qemu qemu 4096 21. Apr 10:17 /run/media/myUserID/1TB-M2NVME/01_VM/imagessudo ls -la /run/media/myUserID/1TB-M2NVME/01_VM/images-rwxrwxrwx 1 qemu qemu 8689418240 17. Apr 01:15 debianLAMP.qcow2mount | grep 1TB-M2NVME/dev/sdb1 on /run/media/myUserID/1TB-M2NVME type ext4 (rw,nosuid,nodev,relatime,seclabel,errors=remount-ro,uhelper=udisks2)iduid=1000(myUserID) gid=1000(myUserID) groups=1000(myUserID),10(wheel),18(dialout),952(docker),954(incus-admin),957(libvirt)sudo usermod -aG qemu,kvm myUserIDdoes not seem to have worked / survived the reboot.When creating a new VM with Virtual Machine Manager in standard path
/var/lib/libvirt/images, it works fine.Copying the qcow img to
/var/lib/libvirt/imagesand adjusting path to/var/lib/libvirt/images/debianLAMP.qcow2in config of VM, I can start VMs (but not the ones with a current snapshot set, but that's also another step).In https://docs.projectbluefin.io/bluefin-dx/#virtualization-and-container-runtimes i could not find a tip.
SELinux Context IssuesDisabled SELinux temp. but no effect.
ls -laZ /run/media/myUserID/1TB-M2NVME/01_VM/images~~
~~
remark: but before i tried without effect:
sudo semanage fcontext -a -t virt_image_t "/run/media/myUserID/1TB-M2NVME/01_VM/images(/.*)?"sudo restorecon -Rv /run/media/myUserID/1TB-M2NVME/01_VM/imagesvirt_image_tcontextLooking for problems regarding access rights / mount options next...
All reactions