Trinix Quick Tip!
Mounting & Accessing a qcow2 Virtual Disk Image Contents
Mounting & Accessing a qcow2 Virtual Disk Image Contents
This quick tip will show you how to mount and access the files within a qcow2 Virtual Disk Image format. I basically wanted to have this functionality because throughout my my virtualization testing, I managed to break at least 1 VM & wanted to check out the bash-history & syslogs to see if I can manage to see what went wrong.
During some of my initial attempts to get GPU passthrough working, there was a bit of trial and error. Some of those errors resulted with the VM being in a state where it won’t boot. So seeing what I did that didn’t work will help me in the future the next time I setup a up virtual machine with PCI host device passthrough. You know the saying…I’m just trying to learn from my mistakes.
Prerequisites
Obviously you gonna need access to a qcow2 disk image, but other than that, the qemu-utils package should be the only software you may need to install.
If you already have KVM/QEMU virtualization setup on your system, you more than likely already have this installed, but running apt install for a package that’s already installed won’t cause an error. It will just report that you have it installed if that’s the casee & install it if it’s not. There are those cases where you may need to access qcow2 disk image contents on a platform that isn’t used for virtualization. Either way, run the following command to make sure you have the packages needed for this procedure:
/your/current/pathsudo apt install qemu-utils
Once you have install qemu-utils, we need to make sure the nbd (Network Block Device) kernel module is loaded. Run the following command todo so:
/your/current/pathsudo modprobe nbd
After running the modprobe command you should now see 16 nbdXX device paths in the /dev directory. To check, run a ls /dev/nbd* and you should see something similar to the following output:
/your/current/pathls /dev/nbd*
/dev/nbd0 /dev/nbd10 /dev/nbd12 /dev/nbd14 /dev/nbd2 /dev/nbd4 /dev/nbd6 /dev/nbd8
/dev/nbd1 /dev/nbd11 /dev/nbd13 /dev/nbd15 /dev/nbd3 /dev/nbd5 /dev/nbd7 /dev/nbd9
Attaching & Mounting the qcow2 Virtual Disk Image
The next thing we want todo is connect our virtual disk image to one of the 16 available Network Block Devices (/dev/nbdXX). To do this, run the following command:
/your/current/pathqemu-nbd -c /dev/nbd0 /srv/nfs/vm-pools/xubuntu18-VM001-disk-01.qcow2
Once this is done, any volumes or partitions inside the disk image will be mountable. You can view the available partitions using a number of common disk utilities in linux such as: fdisk or gparted. So something like the following, that will give you a list of available partitions/volumes to mount:
/your/current/pathsudo fdisk /dev/nbd0 -l
Disk /dev/nbd0: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x7b7daf85
Device Boot Start End Sectors Size Id Type
/dev/nbd0p1 * 2048 41940991 41938944 20G 83 Linux
Once we figure out which partition we want to mount and it’s path, make sure you have a directory created for the mount point. Something like this will suffice:
/your/current/pathsudo mkdir /mnt/vmImgMnt
Once you have the mount target created, we’ll take the information that we got from the output of which ever disk utility you used to get the paths to the images partitions and use it to construct out mount command like so:
/your/current/pathsudo mount /dev/nbd0p1 /mnt/vmImgMnt
Unmounting & Detaching Image
After we’re done doing what we need to do with the virtual machine disk image, unmount the virtual machine disk image by running the following command, replacing the path with the path you used during the initial mount. In my case, the path to my mount point is: /mnt/vmImgMnt, so my command looks like this:
/your/current/pathsudo umount /mnt/vmImgMnt
Once we have the disk image unmounted, we are ready to detatch is from the Network Block Device (nbdXX) that we assigned it to earlier with qemu-nbd. This time we run the same command, but with the detach -d switch, followed by the path of the device you originally used when connecting the image to the Network Block Device.
Using the command I used to connect as reference, my detach command looks like this:
/your/current/pathsudo qemu-nbd -d /dev/nbd0
If you don’t have any more images to mount you can also remove the the Network Block Device kernel module. Make sure you detached all the virtual machine disk images from /dev/nbdXX before removing the kernel module.
To remove the module, run the following command:
/your/current/pathsudo rmmod nbd