Using VHDs on Bare Metal

Yesterday I learned that you can use Virtual Hard Disks (VHD/VHDX) on bare metal. I’d always associated them with Virtual Machines, but I learned that you can create and manage them using DiskPart. And you can even install Windows directly to them on bare metal, without any Windows install already existing. Lastly, if you do already have a copy of Windows installed (or even if you don’t) you can use them to create multi-boot system without having to repartition physical disks.

Using DiskPart to Make VHDs

Open an Administrative Command Prompt or PowerShell window. If you’re doing this during a Windows installation, hit SHIFT+F10 to open an Administrative Command Prompt. Also, you’ll need to have a location to store the VHD file if you don’t aleady, so that might mean simply making a folder, or creating and formatting a partition if you’re on completely bare metal.

Creating VHDs

> diskpart

Microsoft DiskPart version 10.0.10586

Copyright (C) 1999-2013 Microsoft Corporation.
On computer: DESKTOP-5JPGP1D

DISKPART> create vdisk file="F:\VHDs\example.vhd" maximum=60000 type=expandable

  100 percent completed

DiskPart successfully created the virtual disk file.

In this example I’m creating an expandable VHD on F: in a folder called VHDs with a size of 60,000 MB, or roughly 59 GB.

The extension you choose here (.vhd/.vhdx) changes the VHD version. VHDX supports larger disk sizes and is more resilient in the event of a power failure, however is not supported in versions of Windows prior to Windows 8 or Server 2012.

The type can be either fixed or expandable. Fixed VHDs will allocate all the space specified in the maximum parameter immediately. Where expandable will dynamically allocate space as data is written. I personally prefer expandable VHDs, however you can easily overprovision space and encounter stability issues if you don’t monitor carefully.

Using VHDs

To actually use the VHD you just created you’ll need to attach it to the system. If you’ve done the above command or one similar the virtual disk will have been automatically selected. If you’ve created multiple VHDs or are using a VHD that already exists you’ll need to select it.

Selecting a VHD

DISKPART> select vdisk file="F:\VHDs\example.vhd"

DiskPart successfully selected the virtual disk file.

Attaching a VHD

After you’ve selected a VHD you can now attach it to the system.

DISKPART> attach vdisk

  100 percent completed

DiskPart successfully attached the virtual disk file.

DISKPART> list disk

  Disk ###	Status		Size		Free		Dyn	Gpt
  --------	--------------	----------	----------	---	---
  Disk 0	Online		    465 GB	       0 B		 *
* Disk 1	Online		     59 GB	     59 GB

DISKPART> list vdisk

  VDisk ###	Disk ###	State			Type		File
  ---------	--------	--------------------	----------	----
* VDisk 0	Disk 1		Attached not open	Expandable	F:\VHDs\example.vhd

As you can see the VHD shows up as a physical disk under the list disk command as Disk 1, and also shows up under the list vdisk command as VDisk 0 but also showing as Disk 1. Conveniently the asterisk to the left shows you which disk you’ve selected in both cases.

Now that the VHD is attached it acts like any other physical drive, you can partition and format it however you like, install a copy of Windows to it, dual boot to it. You can even install Linux on to it! (With some tinkering)

Detaching a VHD

If you want to remove a VHD using DiskPart you’ll need to select the VHD and detach it. The process is similar to attaching a VHD.

DISKPART> select vdisk file="F:\VHDs\example.vhd"

DiskPart successfully selected the virtual disk file.

DISKPART> detach vdisk

DiskPart successfully detached the virtual disk file.

DISKPART> list vdisk

  VDisk ###	Disk ###	State			Type		File
  ---------	--------	--------------------	----------	----
* VDisk 0	Disk ---	Added			Unknown		F:\VHDs\example.vhd

The disk will continue to be listed until you exit DiskPart, but you can see that it no longer has a physical disk number, it’s state is no longer listed as attached, and it’s type is no longer known.

Installing Windows

If you created a VHD/VHDX during the install process select the Custom option so that you’ll be able to select the drive you created for the install. Remember the drive number from DiskPart or the drive size to be sure you’re selecting the correct drive.

One thing to note, when creating a VHD on bare metal and performing a Windows Installation, the installation process will need to create some files on the physical drive to allow the machine to boot from the VHD. These are the same files it would create had you installed directly onto the physical drive that Windows would use to boot. Here’s a directory listing from the drive showing the hidden files.

> dir F:\ /A:H
11/22/2016	10:53 AM	<DIR>		$RECYCLE.BIN
11/22/2016	01:42 PM	<DIR>		Boot
10/30/2015	02:18 AM		400,228	bootmgr
10/30/2015	02:18 AM		      1 BOOTNXT
11/22/2016	01:42 PM		  8,192 BOOTSECT.BAK
11/22/2016	12:15 PM	  1,207,959,552	pagefile.sys
11/22/2016	10:45 AM	<DIR>		System Volume Information

The total file size is around 18 MB, however you’ll also have to take into account the pagefile that will be created here as well. This will relate to the machine you choose to use and whether you allow Windows to automatically manage the file. I wasn’t able to move the pagefile within the VHDs, Windows would have an issue upon reboot and create a temporary pagefile anyway in the same location. I’m not sure what the limitation here is, but be aware that it exists.

A bonus however is that because the page file will be shared, you wont be consuming additional space per Windows install for each of their pagefiles.

Differential Disks

Another neat feature of VHDs is the ability to use differential disks with them. These can be used for creating snapshots of the state of your machine, in much the same way you would with a virtual machine. You can even chain these child VHDs to other child VHDs. Example:

base.vhd
├─ base-child-01.vhd
│	└─ base-child-01-child.vhd
├─ base-child-02.vhd
│	├─ base-child-02-child-01.vhd
│	│	└─ base-child-02-child-01-child.vhd
│	└─ base-child-02-child-02.vhd
└─ base-child-03.vhd

I wouldn’t really recommend doing this sort of thing though unless you really need to. It becomes pretty unwieldy and you don’t get a nice visual representation like the above. If you’re interested though, this post titled Windows 8 Boot to Differencing VHD by Stephen Cleary discusses using this very concept as well as his use case.

Hope you found that as interesting as I did when I first learned it yesterday.

-Spencer