How safe is it to access qemu/kvm raw or qcow2 disk image files from host

disk-imagekvm-virtualizationqcow2qemu

I've read mixed opinions about how safe it is to modify qemu disk image files.

There are two distinct situations I'm interested in.

1) let's take a raw or qcow2 disk image file not used by kvm (i.e. the virtual machine is not running), mount it using losetup and kpartx and modify its contents (e.g. edit some files, remove some, add some, ….). While I've done this with no problem until now (provided I don't go beyond the maximum size of the image), I'm not sure this is perfectly riskless; I especially wonder how the space-saving features of those image formats behave in such cases – i.e. are they only activated when the VM is running or is a feature of the disk image file by itself.

2) same situation (mounted disk image) but the virtual machine is running now. Is is safe to access the image file contents read-only and even to edit/add some file provided for sure I'm absolutely certain this won't interfer with processes running in the VM ?

Thanks in advance,

phep

Best Answer

For scenario #1, everything should be fine if the guest isn't running. But I would use guestmount rather than losetup. Loop mounting is probably OK for raw volumes, but I am not sure how it handles qcow. guestmount does extra work to ensure things are loaded as they would be by a VM.

For scenario #2, read only will be fine, just make sure you really mount it read only. --ro will do this in guestmount. But read-write is guaranteed to cause corruption.