Working with VHD Files

Hopefully, every single technology professional in the entire Windows world knows that a VHD file is what Hyper-V uses as a virtual hard drive for Hyper-V guests. But, what else can be done with them?

Be aware that “VHD” was the original extension for Hyper-V virtual hard disks. The more recent format is “VHDX”. Unless otherwise noted, I will use “VHD” to mean both types throughout this post.

Revised January 3, 2015

Working Directly with the Bits

At its simplest, a VHD is just a file. It has a header and it has data. It can be manipulated just like any other file. What’s really interesting about it is that Microsoft has published the complete specifications for the format. Visit http://www.microsoft.com/downloads and search for “VHD specification” or “VHDX specification” (I didn’t include a direct link so that any subsequent changes and updates aren’t missed). What that means is that any software vendor, or well, anyone really, can write utilities that read, write, and manipulate VHD files.

If binary processing isn’t really your thing, that doesn’t mean you’re excluded from the beautiful simplicity of hard-drive-in-a-file. The real gift of VHD is that it is portable — highly portable. This is a gift that gives in many ways.

Attach an Existing VHD to a Rebuilt VM

In Hyper-V Server 2008 R2 and earlier, it didn’t take much to really wreck a virtual machine because the XML descriptor files were… fragile, to say the least. I read many posts on some very elaborate methods to salvage virtual machines that had been damaged in some way. Someone tried just copying the files from one machine to another, someone didn’t understand the export/import process, someone opened the VM descriptor file in a text editor and put in an extra CRLF, the list goes on and on. Whatever happened, the result was wrecked configuration files with good VHD files. There were some really clever fixes out there for these problems. But all of them left me with the same question: why?

With whatever tool you use, Hyper-V Manager or SCVMM or something else I don’t know about, make a new VM. Somewhere in the wizard, there’s a spot that asks you about hard drives. You can make a new one or… attach an existing one! So, as long you can sort of remember how the virtual machine was configured, you can just build a brand new virtual machine from scratch and connect its VHD files. What’s the point of going through those clever but agonizing steps to save some configuration files? Granted, 2012 has toughened the configuration files up substantially so this problem has sorted itself out for the vast majority of users that have gone through the upgrade. I’m sure there are also at least some instances where it made sense, but how many, really? You may say, “Well, it had software on it recognized when the VM config changed and wanted to reactivate (or something).” OK… so, if it was important, you backed it up, right? So why not just restore the configuration files and then attach the VHD file?

Move a VHD from One Bus to Another

Does this sound familiar?: You tried moving a Generation 1 virtual machine’s C: drive to the virtual SCSI chain because SCSI is faster. Or, you made another drive, attached it to the SCSI chain, and put a Windows page file on it, also because SCSI is faster. It’s OK to admit it. We’ve all been there (the support group meets on Thursdays). It didn’t work, but it was really easy to move the VHD from one chain to the other, right? And then, when you figured out your error, it was just as easy to move it back, right? Go ahead, move drives between IDE and SCSI. VHD doesn’t care. Hyper-V doesn’t care. Unless… it’s the system drive or contains a Windows page file, anyway. Data volumes, though, they’re happy wherever they land.

Read a VHD File from Your Desktop

Moving a VHD file between virtual buses isn’t the best part about the independence of VHD, though. Have you ever had a hard drive partially fail in a system or had the system fail around a hard drive, so you attached it to another computer to read the contents? Well, you can do the exact same thing with VHD files, only faster and with much less effort. All you have to is access Disk Management (you’re probably used to finding that under Computer Management). Right-click on the Computer Management tree node and click Attach VHD, browse to the location of the file, and poof! You have mounted the VHD in the operating system where you can manipulate it like any other disk. You can even mount it as read-only so you don’t accidentally damage its contents. This works on Windows Server/Hyper-V Server 2008 and onward and Windows Vista and onward for VHD. You’ll need Windows 8 or Windows/Hyper-V Server 2012 or later to mount VHDX files, though.

Mounting a VHD file in Windows Server 2012

Mount a VHD in Windows Server 2012

Running Hyper-V Server or a Core installation of Windows? No problem. Computer/Disk Management runs remotely (right-click on the root node and click “Connect to another computer…”). Don’t want to use GUI tools? That’s not a problem either. Mount-VHD to the rescue! Not running a version of Windows or Hyper-V that supports Mount-VHD? DISKPART has your back!

Copy a Physical Disk to a VHD

Computer dying? Have an old hunk of rust with a ridiculously outdated operating system whose data must be kept until the third week of eternity? Physical-to-virtual conversions (P2V) are perilous for many reasons, but data preservation is at least possible. Use these tools:

  • Disk2Vhd runs in even fairly old operating systems and makes them into VHDs. End results for P2V conversions are hit and miss, but the file can always be mounted as talked about in the previous section so that data can be recovered.
  • If the Hyper-V host can see the disk, it can probably convert it directly to a virtual disk.
  • If P2V is your goal, the Microsoft Virtual Machine Converter might do the trick.

Cool Sysop Trick: Save That Old Computer Forever

The last thing I’m going to tell you about today may not be all that practical, but the guy in the cubicle next to hasn’t read this blog, so he can’t do it. That should be reason enough. I’ll explain it by way of scenario. Let’s say your boss just got a new desktop computer, but he’s really worried about losing all the stuff on his current desktop computer. As usual, the help desk staff has done a data transfer for him, but he’s just not convinced. After all, there was that shortcut he lost that one time. And then there was that time somebody forgot to bring the Outlook nickname file over and the world almost ended. The normal process for this is to back up everything, make sure the backup is duplicated to all remote facilities and a Swiss safe deposit box, and put its contents in a ridiculously over-secured folder on a file share. And you’ll leave it there. For eternity. Micro-managing it.

Not this time.

This time around, you’re going to do something a little awesome. First, you’re going to follow one of the procedures in the preceding section to convert the original computer’s disk to VHD. You’ll put that file in a not-quite-so-ridiculously-over-secured folder on a server that you can make a bit of a configuration change to. You’ll enable/install the Microsoft iSCSI Target Software on that system (built-in to Windows Server 2012, downloadable for 2008 R2). You’ll set up an iSCSI target and LUN pointing to that VHD file. Finally, you’ll run iscsicpl.exe on your boss’s new desktop to connect to it and use it as a D: drive. Really cool part? File security is already set and you can reassure your boss that no one else is looking at his things. You’ll probably want to put some initiator security on the target, of course, but it still won’t be the micro-management issue that the old file share method is.

Overkill? Absolutely. Worth it? Totally. Of course, this scenario is just one little thing that I dreamed up. I’m sure someone out there will come up with a more practical application.

Backup VHD Files

I will always advocate for a proper backup solution for your virtual machines. That might seem like a biased statement, since I write for Altaro, but the truth is that I was a backup evangelist long before I ever heard of Altaro. That might be why I get along so well around these parts. I’ve heard many people complain that backup solutions are too expensive — they’re lucky enough to have never seen the cost of recovery without one. I have been present for that. I have also had to tell someone, “Sorry, all of your business’s data is permanently lost.” It does happen. So, my public service announcement is: get a real backup solution, learn it, use it, test it.

However, there are times when you just need a copy. Or you need to understand the VHD system well enough to understand what’s going on. So, let’s talk about that.

A VHD is just a file. It’s. Just. A. File. You can copy it like any other file. Of course, if the owning virtual machine is in use at the time, that might pose a problem. A pure read-only operation will succeed, but you can’t be certain that the contents won’t change in the interim. For that scenario, Hyper-V 2012 R2+ can export a virtual machine while it’s running. If you only want the VHD, you can always keep that and get rid of everything else. Another option is to take a checkpoint of the running virtual machine. That leaves its VHD in a read-only state that’s even more suited to copying. Just don’t forget to delete the VHD when you’re done.

Restore VHD Files

Ditto the above on the proper backup application. Just so we’re clear, I meant that.

Otherwise, a VHD file is… yep, just a file. If you have a copy of a VHD, you can copy it over the top of the one you want to replace. The virtual machine that it’s part of needs to be off, of course, and I would recommend renaming the file that you think is bad instead of overwriting it, just in case. What you might run into here is a security block. More on that…

VHD File Security

A big issue with VHDs is file security. I have one recommendation that will make your life easy: use Hyper-V Manager (Failover Cluster Manager for highly-available virtual machines). If a VHD file is manually changed in any way, whether by tinkering or by copying or what-have-you, go into Hyper-V Manager, disconnect the disk from the virtual machine, and then re-attach it.

The Remove button is on the VHD’s page in the virtual machine’s property sheet:

Remove VHD

Remove VHD

You’ll have to use the Apply button so that the VHD is removed from the VM. Don’t worry; the file is unharmed. Now, just use the Add button as shown in the following screenshot:

Add VHD

Add VHD

This will return the dialog to the same condition as in the preceding screenshot, except that the disk data won’t be populated. Use the Browse button to locate it. Upon pressing OK or Apply, the proper security will be re-applied to the file.

For highly available virtual machines, ensure that the storage folder and its share, if applicable, have the proper security so that all nodes have Full Control. You do not not need to set permissions for the cluster name object.

 

Altaro Hyper-V Backup
Share this post

Not a DOJO Member yet?

Join thousands of other IT pros and receive a weekly roundup email with the latest content & updates!

11 thoughts on "Working with VHD Files"

  • Gary Gemmell says:

    As far as I was aware there isnt much difference between scsi or ide in hyperv unless that changed in the latest release.

    Microsoft state :

    Although the I/O performance of physical SCSI and IDE devices can differ significantly, this is not true for the virtualized SCSI and IDE devices in Hyper-V. Hyper-V. IDE and SCSI devices both offer equally fast I/O performance when integration services are installed in the guest operating system.

  • Gary Gemmell says:

    Some good tips though for sysadmins that didnt know about them – the iscsi one is new to me!

    Thanks
    Always enjoyable reading this blog!

    Short sweet and to the point!

  • Eric Siron says:

    That’s correct. The SCSI virtual controller is basically just as fast as the IDE virtual controller. Some people just really, really, really have to have that extra millisecond or whatever it works to be. Or, they’re at least convinced that they really need it.

    And thank you!

  • Gerald Nell says:

    I have a unique situation and google is not helping at all with this one.

    We have a NAS with a .vhd on it that I can attach to a 2008 R2 server but not on 2012. Is there a security setting that does not allow this?

    Thanks

  • Jojo says:

    I have a VHDX on an IDE connection, can I remove it from the IDE and then attach it to SCSI? the VHDX contains the exchange server program files and mail store.

    After attaching it to the SCSI , I plan to expand/resize the VHDX.

    Is this safe to do?

Leave a comment or ask a question

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

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

Notify me of follow-up replies via email

Yes, I would like to receive new blog posts by email

What is the color of grass?

Please note: If you’re not already a member on the Dojo Forums you will create a new account and receive an activation email.