Windows 8 Storage and Hyper-V – Part 3: The Art of Creating a VHD

In the last blog in my series on Windows 8 storage I already touched upon creating a VHDX with PowerShell 3.0 in Windows Server 8. In this blog I will focus on the subject a little bit more, showing the myriad ways of creating VHDs and show you some powerful commands to create multiple VHD files in Windows Server 8.

Here are a number of methods for creating a VHD:

  • WMI
  • Diskpart
  • Disk2VHD
  • VHD Tool
  • Disk Management
  • Hyper-V Manager
  • PowerShell 3.0

The WMI Method

Three years ago Taylor Brown in his blog “Hyper-V WMI Using PowerShell Scripts” demonstrated how we could create a VHD using a Hyper-V WMI method. Back then we did not have a Hyper-V PowerShell module as we now have in Windows 8. Borrowing from his work this is how we could accomplish our goal:

clip_image002

clip_image004

The WMI route certainly was powerful, but nowhere as intuitive as some of the other methods.

The Diskpart Method

Diskpart.exe first showed up in Windows 2000 and all subsequent Windows operating systems. It is still part of Windows 8. The creation and mounting of a VHD was not possible until Windows 7/Windows Server 2008. This is how you created a VHD by means of Diskpart:

clip_image006

Because we are running Diskpart in Windows Server 8, we can create a VHDX up to 64TB just by using the VHDX file extension.

The Disk2VHD Method

If you have an existing machine running some version of Windows (Windows XP, Server 200 and higher), you can convert the physical disks to the VHD format using Disk2VHD.exe which is a utility created by Mark Russinovich and Bryce Cogswell. I have even put it to good use within a virtual machine which was unable to merge snapshot files as a result of disk space limitations.

The VHD Tool Method

There are many more (free) tools available for creating VHD’s. After all the VHD format is a publicly-available image format specification. One that is interesting worth mentioning is VhdTool.exe which is capable of instantly creating large fixed-sized VHD’s. It also has a repair function to undo an expand operation on a base VHD when difference disks are present. This is useful in cases where an admin accidentally expands a base VHD when Hyper-V snapshots are present. It does not support VHDX however.

clip_image008

The Disk Management Method

Since Windows 7 and Windows Server 2008 you can also use Disk Management to Create and Attach VHD’s and it introduced Native VHD or Boot from VHD. I have written several blogs on how to Boot from VHD although the VHD creation method can easily be replaced by most of the methods described in this blog.

clip_image014

The Hyper-V Manager Method

Another much easier method is to use Hyper-V Manager, click on the Action menu and simply hit New | Hard Disk which starts the New Virtual hard Disk Wizard.

clip_image010

clip_image012

In the Hyper-V Manager GUI you can create a 2,040GB VHD or a 64TB VHDX file.

Although the instruction that goes with the VHDX option still refers to a supported maximum of 16TB, it is possible to create a 64TB VHDX via this method. The text is probably a remainder of the Developer Preview of Windows Server 8.

clip_image016

The PowerShell 3.0 Method

Windows 8 has brought us PowerShell 3.0 and very conveniently allows us to create and configure storage, networking, Hyper-V and many more.

If you just run Get-Command –Module Hyper-V *vhd* you get quick list of possible commands related to VHD(X). We are dealing with Creating VHD(X) in this blog, so let’s focus on the New-VHD command.

clip_image018

My preferred method of working with PowerShell 3.0 in Windows 8 is using Windows PowerShell Integrated Scripting Environment (ISE). ISE allows you to run PowerShell commands and cmdlets interactively and assists you by syntax-coloring, tab completion, visual debugging and context-sensitive help. It is a great way to learn PowerShell.

It is easy to find help by selecting a PowerShell module and supplying a keyword such as VHD in the following example:

clip_image020

If you click on the command itself, it gives you the full syntax. You could also have issued a command to get this information: New-VHD -? Or Get-Help New-VHD -Full

clip_image022

You can update help via this command:

clip_image024

Because the regional settings on my computer are set to non-English, the Update-Help command may fail. Just set it to en-US by means of this command: Set-Culture en-US

Demonstrating the Power of PowerShell for Creating VHD(X) files

Get-Command –Noun VHD shows you all the possible commands which use VHD. As you probably know by now, PowerShell uses a Verb-Noun syntax. So Get-Command –Verb New shows you all the commands starting with New.

Variations for creating a VHD in PowerShell 3.0

Creating a new Fixed-sized VHD with a size of 1GB:

New-VHD –Path DynamicVHD.vhd –Dynamic –VHDFormat VHD –SizeBytes 10GB

This cmdlet can be shorted in several ways:

New-VHD DynamicVHD.vhd –Size 10GB creates a file of type VHD automatically and is Dynamic by default. You don’t need to type –Path and you can shorten –SizeBytes to –Size

New-VHD DynamicVHD.vhdx –Size 10GB creates a file of type VHDX automatically and is Dynamic by default.

The following two commands clearly show that both the VHD and VHD format have a LogicalSectorSize of 512 bytes but the VHDX has a four times larger PhysicalSectorSize, nicely aligning at 1MB boundaries.

.clip_image028

Large VHDX files are created with a 32MB BlockSize. You can influence the blocksize with this command:

New-VHD LargeSector1TBVHDX.vhdx –Dynamic –BlockSizeBytes 256MB
–LogicalSectorSizeBytes 4KB –SizeBytes 1TB

clip_image030

An interesting command for rapidly creating VM’s based off a Parent VHDX using Differencing VHDX files looks like this:

New-VHD DiffBasedOnParent.VHDX –Differencing –ParentPath E:ParentParent.VHDX

I actually build a new base lab in just a few seconds by using this method:

clip_image032

If you need to create a number of VHD(X) files very rapidly, this single line is very powerful:

(1..5) | % { New-VHD DISK$_.vhdx -Size 10GB }

You can also mount these virtual hard disks in one line:

Mount-VHD DISK?.vhdx

As a final example, you can also convert a physical disk using the Disk ID. In this example I convert a USB thumbdrive to a new Dynamic VHD of type VHDX with a maximum size of 1TB.

New-VHD VHDfromUSB.vhdx –Dynamic –VHDFormat VHDX –SourceDisk 15

clip_image034

The Mount and Eject in the GUI can of course also be accomplished in PowerShell:

clip_image036

This last example is by no means everything there is. As we will see in a future blog, the VHD format is also used for the Microsoft iSCSI Target Server which is now an in-box Feature in Windows Server 8 and can be put to great use for creating virtual guest clusters or quickly provisioning raw LUNs to a Data Protection Manager 2012 server for instance.

You will probably agree, there are endless possibilities. Hope you enjoyed it!

/Hans

You can follow me on Twitter with @hvredevoort

My next gig will be at The Experts Conference 2012 in San Diego where I will be presenting Cool Storage Tricks with Hyper-V in Windows Server 8

clip_image038

No Comments Yet

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">