1

I'm looking for a way to mount a read only snapshot of a qcow2 disk image so I can back up files from it. Based on the manpages for qemu-nbd a command like the following ought to work:

qemu-nbd -x myimage --read-only --snapshot /var/lib/libvirt/images/myvm.qcow2

However, when I run it I get the warning:

Failed to blk_new_open '/var/lib/libvirt/images/myvm.qcow2': Failed to get shared "write" lock
Is another process using the image?

My understanding of the above command is that it would create a backing file to keep writes away from myvm.qcow2 and would mount the image in read only mode anyway so there's no reason it would need to obtain a write lock. I'm struggling to find any info on this other than the manpages so am hoping someone has come across this before and might know if there is a way around it. I know that qemu-img has a -U flag to force access to the image even if there is a write lock. Is there a similar flag for qemu-nbd?

I am running running Ubuntu 18.04 using QEMU 2.11.1 and libvirt 4.0.0 if there are any known issues with those specific versions

  • I think that you're confused about how advisory locks work. You cannot acquire a read lock while a write lock is set on a file. You can however acquire multiple read locks at the same time. – mosvy Sep 23 '19 at 4:50
  • In this case, you do NOT want to mount a disk image, hardware or virtual, which can be modified at the same time by something else in an uncoordinated way -- it will be like pulling the rug from the under the feet of the HOST kernel, and causing it to CRASH. You'll have to run both your qemu-nbd server and the virtual machine in snapshot mode, or create with qemu-img -B myvm.qcow2 ... two copy-on-write images having the same backing file, one for the host and another for the guest. In any case, this is not a way to share data between the guest and the host. – mosvy Sep 23 '19 at 4:50
  • Thanks @mosvy. As you say, I'm not looking to make my nbd export writeable so there shouldn't be a risk of corrupting the data if the guest has write access and the host only has read access. Have I misunderstood what the --snapshot flag does? Surely there wouldn't be a point to it if it still only works on images that aren't attached to a running guest? – David Hollings Sep 23 '19 at 5:47
  • It may crash the host if the guest modifies the data, even if the host is only accessing it for reading. The kernel doesn't expect the filesystem structures on the disk (either hardware or virtual) to change under its feet. – mosvy Sep 23 '19 at 5:50
  • @mosvy Thanks, I get it now. So if I make a backing file using virsh snapshot-create-as and then mount myvm.qcow2 as read only would that be safe or am I completely barking up the wrong tree in trying to do this? – David Hollings Sep 23 '19 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.