Mounting VHDs in Ubuntu

Virtual Hard Disk IconMany of the CAD tools that I need to work with on a daily basis are Windows-only tools. I have slowly attempted to minimize the number of those tools that I require and slowly ease into Linux. At this point, the only real tool that I continue to use that is solely for Windows is Altium Designer.

In part of my move to Linux, I decided to convert my Windows partition to a VHD file to use in a virtual machine in the future. Of course right after I did that, I found I needed to access it without the virtual machine. After piecing a few things together, I was able to mount and access the VHD file successfully.

Pre-requisites

Besides the main driver library, which I’ll get to later, there was one main prerequisite to install: The Sleuth Kit. This set of tools allow you to examine disk images and get information about them. Specifically, this provides the mmls tool that displays the partition layout we’ll use to mount the image’s partitions.

It’s very simple to install with Ubuntu:

sudo apt-get install sleuthkit

Finally, we’re going to need to build the main driver library, so we’ll need to install build-essential:

sudo apt-get install build-essential

Tool Installation

Most of my research on the Google machine has led me to utilizing libvhdi to access .vhd images. Luckily, this tool is hosted on GitHub! Unfortunately, it isn’t distributed as a binary, debian package or anything else like that. So, we have to build it ourselves. Just to jerk you back and forth again, there’s already a nice build tutorial here.

You should be able to gather most of the information you need from the tutorial, however, I’ll slim it down for you:

Retrieve the source package go to the downloads page and download the file named:

libvhdi-alpha-<version>.tar.gz

To extract the source package run:

tar xfv libvhdi-alpha-<version>.tar.gz

This will create the source directory:

libvhdi-<version>

To build the libvhdi source code change into the source directory and run the following commands:

./configure
make

You can install the binaries that were build by running:

sudo make install

Make sure libvhdi.so is in the library cache; normally it suffices to run:

sudo ldconfig

Finally, in order to use the vhdimount utility, we need to install libfuse-dev:

sudo apt-get install libfuse-dev

Use

This video shows the steps we’ll go through, but doesn’t explain anything, so I’ll go through the rest here.

First, we need to make sure we have root access for the whole operation by opening up a new shell:

sudo bash

In this new shell, we’ll first create two directories that we’ll mount the .vhd file to and then the partition to.

mkdir /path/to/mount/vhd
mkdir /path/to/mount/partition

Next, mount the disk image to a location by using this command:

vhdimount DISKIMAGE.vhd /path/to/mount/vhd

This will mount the .vhd image to the /path/to/mount/vhd location. If you cd to that location the contents would be something similar to this:

-r--r--r-- 1 root root 50G May 29 23:44 vhdi1

So, as you can see this doesn’t mount any partitions or anything. The partitions are shown as sub-folders here. So, in order for it to act like most other mounted partitions, we’ll need to mount it. The first step in this is to figure out what the partition information looks like so we can mount it correctly. This is why we installed the sleuth kit. We can use the command like this (using the partition name that we found in the last step):

mmls -aB vhdi1

There are two main things to draw from this output. First, the sector size; it will report something like Units are in 512-byte sectors. This is pretty obvious in meaning. The second piece of information is the starting location of the paritition – that is shown in the start column belonging to the partition you would like to mount:

     Slot   Start       End         Length      Size   Description
02:  00:00  0000002048  0100663295  0100661248  0047G  Linux (0x83)
06:  01:00  0100665344  0104855551  0004190208  0001G  Linux Swap / Solaris x86 (0x82)

Now it’s time to actually mount the partition to the location you’d like to browse it from. The offset we’ll use is the start position * sector size we got from the previous step. In the example that was 2048 * 512 = 1048676:

mount -o ro,noload,offset=1048576 vhdi1 /path/to/mount/parition

Now we can just exit the root shell and navigate to the path we’ve mounted the partition to in order to browse it. In addition, it should now show up as part of the list of ejectable hard disks.

Unfortunately, I can’t use any of the disk checking tools in this manner, but we’ll figure that out at a later date.

3 thoughts on “Mounting VHDs in Ubuntu

  1. You sir, are a lifesaver. I decided to do away with my Xenserver install in favor of straight CentOS 7. I was pretty much relying on a post like this to help me mount the old Xenserver VHD images.

    Just wanted to say Thank You!

  2. The last command, mount, gives me. “mount: mount/vhd/vhdi1: failed to setup loop device: Permission denied”. I do run the command with sudo. And there is nothing unusual about the folder I choose as mount point. I am able to mount iso files with no problems. Any idea, what is wrong?

Leave a Reply

Your email address will not be published. Required fields are marked *