Wednesday, 1 June 2011

Accessing a VM image from a QEMU host

Just a quick note of the commands needed to mount a VM disk image from the QEMU host server...
The following commands may be useful if you need to copy some files (SSH keys perhaps) to or from a VM disk image.   This assumes the QEMU VM environment, which is the one that's built in to Ubuntu Server 10.04 Linux.  You need to be root, of course.

It will only work safely for VM's that aren't running at the time!
-- Shut down our VM guest...
# virsh shutdown vm2
Domain vm2 is being shutdown

-- Load the required kernel module...
# lsmod | grep nbd
# modprobe nbd max-part=63
# lsmod | grep nbd
nbd                     9903  0 

-- Export the VM image file on /dev/nbd0, then mount that to a temp folder
# qemu-nbd -c /dev/nbd0 vm2.img
# ls -l /dev/nbd0*
brw-rw---- 1 root disk 43, 0 2011-06-01 15:42 /dev/nbd0
brw-rw---- 1 root disk 43, 1 2011-06-01 15:42 /dev/nbd0p1
brw-rw---- 1 root disk 43, 2 2011-06-01 15:42 /dev/nbd0p2
brw-rw---- 1 root disk 43, 5 2011-06-01 15:42 /dev/nbd0p5
# mount /dev/nbd0p1 /mnt/vm2

-- You can now access the VM disk folders under /mnt/vm2...
... do your stuff ...

-- To clean up...
# umount /mnt/vm2
# qemu-nbd -d /dev/nbd0
/dev/nbd0 disconnected
# rmmod nbd
# virsh start vm2
Domain vm2 started
I believe there's a command to take a snapshot of a running VM, but I haven't tested that myself.  "virsh snapshot-create VMname" apparently needs libvirt >= 0.8.1, but Ubuntu Server 10.04 LTS currently only runs libvirt 0.7.5.    I guess the LTS releases of Ubuntu Server must be a bit conservative, as they have five years of support, all free of charge.

No comments:

Post a comment

Spammers: please stop wasting my time. All comments are moderated before publication.