Tip

Mounting VHD files in Hyper-V environments

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

    Requires Free Membership to View

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.
 

This was first published in February 2009

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.