Tuesday, August 30, 2011

Create & Boot VHD

This is just quick list for creating and booting from a Virtual Hard Disk (VHD) in Windows 7.

1a. Creating VHD from scratch


If you’re creating a VHD from scratch (that is not a child-disk based on another VHD) this is the commands you need to run from command prompt:
:\> diskpart

Microsoft DiskPart version 6.1.7601
Copyright (C) 1999-2008 Microsoft Corporation.
On computer: xxxxxxxx


DISKPART> create vdisk file=[FILEPATH] type=expandable maximum 50000

  100 percent completed

DiskPart successfully created the virtual disk file.

DISKPART> exit

The ‘:\>’ means command prompt, typically it would be ‘c:\>’ or something similar, but the actual directory you’re in doesn’t matter for the command.

Running the command ‘Diskpart’ will start the DOS-utility that we’ll use for creating the VHD. When diskpart starts, the command prompt will change to ‘DISKPART>’. Now you can type the command ‘create vdisk’ to create the actual VHD.

The command has only two required arguments; ‘file’ and ‘maximum’. The ‘file’-argument specifies where to output the vhd and the name of the vhd-file. So [FILEPATH] in the example above will typically be ‘D:\VHD\MyNewVhd.vhd’.

The ‘maximum’ argument specifies the size of the VHD in megabytes. You can choose between to types of disks (the ‘type’ argument); either fixed, meaning that the VHD file will be created with the size specified by the ‘maximum’ argument, or expandable, which means it will only be as big as the data on the disk requires but not larger than ‘maximum’. The default is fixed and if you have lots of space available it’s the recommended setting as it’s a bit faster than expandable disks. But on my laptop I do not have unlimited storage, so here I’ll go with the expandable option and setting the maximum file size to about 50 GB. It’s also a bit cheaper to do backups of smaller files, so expandable has it’s advantage there.

1b. Creating a differencing VHD


If you already have a base-VHD that you want to use as a parent-disk, this is the commands you need to run from command prompt:

:\> diskpart

Microsoft DiskPart version 6.1.7601
Copyright (C) 1999-2008 Microsoft Corporation.
On computer: xxxxxxxx


DISKPART> create vdisk file=[FILEPATH] parent=[PARENT_FILEPATH]

100 percent completed

DiskPart successfully created the virtual disk file.

DISKPART> exit

As with the 1a) option above, the [FILEPATH] specifies the path and name of the disk to create. The ‘parent’ argument should be pretty obvious and [PARENT_FILEPATH] is the fully qualified name of the existing VHD you want to use as base-image.

Note that you cannot specify ‘maximum’ or ‘type’ as arguments for a differencing disk because the size of the child disk is set from the parent. It’s also possible to merge a diff-disk with it’s parent at a later point, to create a new VHD.

Remember to make your parent-disk read-only before you use it for differencing disk. Failing to do so and then accidentally starting up or booting directly in to your parent VHD, will render your differencing disks useless (and yes; I learned that the hard way).

Another gotcha to be aware of; if you want to boot in to the new differencing disk, it needs to be located on the same disk volume as the parent disk. They can be in different folders, but they cannot reside on another volume – not even if the volumes is on the same physical disk.

2. Boot it up!


When the VHD is ready to use, issue these commands to add them to your boot menu;
:\>bcdedit /copy {current} /d “My virtual boot entry”

The entry was successfully copied to {df399c86-f723-11df-85b6-8d1c50594e14}.

:\>bcdedit /set {df399c86-f723-11df-85b6-8d1c50594e14} device vhd=[locate]\Path\To\Disk.vhd

The operation completed successfully.

:\>bcdedit /set {#GUID#} osdevice vhd=[locate]\Path\To\Disk.vhd

The operation completed successfully.

:\>bcdedit /set {#GUID#} detecthal on

The operation completed successfully.

If you run the ‘bcdedit’ command without any arguments, you should be seeing your new boot entry at the bottom. And it should look something like this;

Windows Boot Loader
-------------------
identifier              {df399c86-f723-11df-85b6-8d1c50594e14}
device                  vhd=[locate]\Path\To\Disk.vhd
path                    \Windows\system32\winload.exe
description             My virtual boot entry
locale                  en-US
inherit                 {bootloadersettings}
recoverysequence        {df399c79-f723-11df-85b6-8d1c50594e14}
recoveryenabled         Yes
osdevice                vhd=[locate]\Path\To\Disk.vhd
systemroot              \Windows
resumeobject            {df399c77-f723-11df-85b6-8d1c50594e14}
nx                      OptIn
detecthal               Yes


The first command, ‘bcdedit /copy’ will copy the default boot entry and create a new with the name specified by the /d argument. Then you’ll use the /set argument to modify the new entry. You’ll need to copy the id that will be displayed after the initial copy command, and input this as the first argument for the /set command.

There’s 3 things that need to be set; the device, the osdevice and the detecthal. The first to are similar and takes the path to the VHD you want to boot from as input. Note the ‘[LOCATE]’ syntax in the path; this will tell the boot manager to figure out which drive to locate the VHD on. So instead of ‘vhd=d:\path\to\disk.vhd’, you  need to enter ‘vhd=[locate]\path\to\disk.vhd’.

A little tip if you have spaces in the path or filename for the VHD; surround the path with apostrophes (‘).

The last /set command will give some instructions to the kernel to detect certain hardware information (HAL = Hardware Abstraction Layer), which is needed on some x86-based system.
And that’s it; Ready to boot!

Resources


In some of my earlier posts I wrote a bit about how to compact VHDs, how to automate Windows install on VHDs and some pros and cons with virtual machines.

Also, David Loongnecker has some good tips in his blog post Tips for Booting/Using VHDs in Windows 7.

No comments: