Mounting VHD files in Hyper-V environments

Accessing files from virtual machines that aren't powered on can be time-consuming. But you can use the PowerShell script or the VHDMount tool in Microsoft Virtual Server to mount VHD files on your desktop. An expert explains these not-altogether-seamless processes.

Greg Shields

As a virtual administrator, you may sometimes need to access a file or folder from a virtual machine (VM). For VMs that are up and running, you can just log in to the virtual machine or connect to it over the network and get the files. But accessing files from VMs that aren't powered on can be a complicated process. But with Microsoft Hyper-V, you can access files by mounting the Virtual Hard Disk (VHD) files of VMs onto your desktop. To do so, you can use the Windows PowerShell script or the VHDMount tool in Microsoft Virtual Server (MVS). I'll explain both of these methods in this tip.

Mounting VHD files with the VHDMount tool
Booting an external VHD file in Hyper-V is somewhat difficult. If you have a VHD file on a backup drive and need to access a file from its VM, Hyper-V requires that you import the virtual machine. This process can be time-consuming and irritating if you need to quickly grab a few files.

Another way of accessing the hard drive is by mounting the VHD directly on your desktop. Much like mounting a DVD's ISO file, or the Windows Imaging Format file of an OS image, it's possible to mount a VM's VHD file through the command line. The result is that the virtual machine's disk appears as its own separate disk on the desktop with a specific drive letter.

The VHDMount tool can accomplish this task, and it is available in Microsoft Virtual Server 2005. VHDMount provides a command line interface (CLI) for mounting and accessing VHDs. For first-timers, however, the process required to place it on your desktop or server, is a bit odd.

Once you've downloaded MVS 2005 begin the installation, but install only its VHDMount feature and nothing else. This process installs the necessary components for the VHDMount tool without installing MVS 2005 in total. If you want to extract VHDMount even faster, you can use the following two commands against its setup file. They obtain the MSI from the setup file and then run a targeted installation for only the VHDMount components:

setup.exe /c /t c:\{targetFolder} msiexec /i "c:\{targetFolder}\Virtual Server 2005 Install.msi" /qn ADDLOCAL=VHDMount

Once VHDMount is installed, use the command vhdmount /m {targetVHD} to mount the VHD on an available drive letter. After mounting the VHD, any changes made will be written to a differencing disk until the VHD is unmounted. This allows you to revert back to the pristine VHD if you make a mistake. Use the command vhdmount /u {targetVHD} to unmount the VHD. If you want to eliminate changes –- and the contents of the differencing disk –- as you unmount it, use the command vhdmount /u /d {targetVHD}. These commands should work with Windows Vista, but there are some problems with VHDMount and Windows XP.

Using PowerShell to mount VHD files
If the hassle of installing VHDMount doesn't appeal to you, you can use the PowerShell tool to achieve the same results. Unfortunately, with PowerShell the process isn't much easier. To begin, the following two commands will mount a VHD:

$objVHDService = get-wmiobject -class "Msvm_ImageManagementService" -namespace "root\virtualization" -computername "." $objVHDService.Mount("{targetVHD}")

The first line above uses PowerShell to establish a connection to the local computer's Windows Management Instrumentation store and grabs an instance of the Msvm_ImageManagementService class. This class is necessary to use .Mount method, which does the work of the script.

After completing this step, the mounted drive arrives as an offline disk. You'll need to bring that disk online by using Windows Disk Management or the diskpart.exe command line tool. Accomplishing this through PowerShell is complicated though. If you want a straight PowerShell solution, review this blog post on using PowerShell to mount VHDs for all the gory details.

Once you've finished working with the virtual disk, you'll need to return the disk to an offline state by running the unmount script:

$objVHDService = get-wmiobject -class "Msvm_ImageManagementService" -namespace "root\virtualization" -computername "." $objVHDService.Unmount("{targetVHD}")

If you use Hyper-V in a small environment, you may also use Microsoft's internal Windows Server Backup utility for backing up your VMs. Since this tool can back up only at the individual volume level –- and, hence, at the individual VM level –- these backups get stored at the VHD level. This means that individual file and folder restores from within a Hyper-V machine require this tricky mounting and unmounting.

ABOUT THE AUTHOR: Greg Shields, MVP, is a co-founder and IT guru at Concentrated Technology (www.concentratedtechnology.com) with nearly 15 years of IT architecture and enterprise administration experience. He is an IT trainer and speaker on such IT topics as Microsoft administration, systems management and monitoring, and virtualization. His recent book, Windows Server 2008: What's New/What's Changed is available from SAPIEN Press.

And check out our Server Virtualization blog.

Dig Deeper on Microsoft Hyper-V and Virtual Server