Boot From VHD

1 Mar

http://www.tsaysoft.com/knowhow/BootFromVHD/

Boot From VHD

Windows 7 provides a unique feature called Boot From VHD. Essentially it is a storage virtualization. Usually a virtual machine consists of a virtual CPU, virtual devices, and virtual storages. Boot From VHD lets you use a real CPU, real devices, but a virtual storage. It doesn’t require Intel VT or AMD-V on the machine. After setting up Boot From VHD, what you have is a multi-boot machine. The boot is not from a partition, but from a VHD file.

You can follow the steps below to create a dual-boot machine. One boot from a real drive and the other boot from a VHD file.

  • Put Windows 7 DVD in the DVD drive and boot from DVD/CD. In the welcome screen, you type Shift-F10 to start a command prompt.
  • Locate you drive and folder to be used to store the VHD file. If you have only one hard drive, it ususally becomes drive D when in Windows Preinstallation Environment (WinPE).
  • Run diskpart, and then use the command create vdisk and attach vdisk to mount the VHD file. For example, D:\VM\Win7.vhd is the desired VHD file, and the desired size is 25GB.
D:\VM>diskpart

Microsoft DiskPart version 6.1.7600
Copyright (C) 1999-2008 Microsoft Corporation.
On computer: MINWINPC

DISKPART> create vdisk file=D:\VM\Win7.vhd type=expandable maximum=25000

 100 percent completed

DiskPart successfully created the virtual disk file.

DISKPART> attach vdisk

 100 percent completed

DiskPart successfully attached the virtual disk file.
  • To make sure the VHD file is mounted correctly. Use the command list disk to verify.
DISKPART> list disk

  Disk ###  Status         Size     Free     Dyn  Gpt
  --------  -------------  -------  -------  ---  ---
  Disk 0    Online          232 GB      0 B
* Disk 1    Online           24 GB    24 GB
  • Use the command exit to exit diskpart and command prompt.
DISKPART> exit

Leaving DiskPart...

D:\VM>exit
  • Start the installation of Windows 7. Select the VHD disk. In this case, it is Disk 1. Setup will show a warning for  this disk, but you can click Next to continue and the setup.
  • Once the setup completes, it should create a new boot loader entry in Boot Manager, and use it as the default. The default boot loader is for VHD and the second one is for the real drive. Use the command bcdedit /v to see Boot Manager and Loader settings. Each boot loader is identified by a GUID. You may get different GUIDs.
C:\>bcdedit /v

Windows Boot Manager
--------------------
identifier              {9dea862c-5cdd-4e70-acc1-f32b344d4795}
device                  partition=\Device\HarddiskVolume1
description             Windows Boot Manager
locale                  en-US
inherit                 {7ea2e1ac-2e61-4728-aaa3-896d9d0a9f0e}
default                 {51f3be40-8088-11de-ace6-0014223d6008}
resumeobject            {51f3be3f-8088-11de-ace6-0014223d6008}
displayorder            {51f3be40-8088-11de-ace6-0014223d6008}
                        {51f3be3c-8088-11de-ace6-0014223d6008}
toolsdisplayorder       {b2721d73-1db4-4c62-bf78-c548a880142d}
timeout                 30

Windows Boot Loader
-------------------
identifier              {51f3be40-8088-11de-ace6-0014223d6008}
device                  vhd=[D:]\VM\Win7.vhd,locate=custom:12000002
path                    \Windows\system32\winload.exe
description             Windows 7
locale                  en-US
inherit                 {6efb52bf-1766-41db-a6b3-0ee5eff72bd7}
recoverysequence        {51f3be41-8088-11de-ace6-0014223d6008}
recoveryenabled         Yes
osdevice                vhd=[D:]\VM\Win7.vhd,locate=custom:22000002
systemroot              \Windows
resumeobject            {51f3be3f-8088-11de-ace6-0014223d6008}
nx                      OptIn

Windows Boot Loader
-------------------
identifier              {51f3be3c-8088-11de-ace6-0014223d6008}
device                  partition=D:
path                    \Windows\system32\winload.exe
description             Windows 7
locale                  en-US
inherit                 {6efb52bf-1766-41db-a6b3-0ee5eff72bd7}
recoverysequence        {51f3be3d-8088-11de-ace6-0014223d6008}
recoveryenabled         Yes
osdevice                partition=D:
systemroot              \Windows
resumeobject            {51f3be3b-8088-11de-ace6-0014223d6008}
nx                      OptIn
  • Use the command bcdedit /set to change the description of a boot loader. For example, to change the description of the boot loader for VHD to “Windows 7 on VHD”
C:\>bcdedit /set {51f3be40-8088-11de-ace6-0014223d6008} description "Windows 7 on VHD"
  • Use the command bcdedit /displayorder to change the list order of boot loaders. For exmaple, to move the boot loader for VHD to the last one in the list.
C:\>bcdedit /displayorder {51f3be40-8088-11de-ace6-0014223d6008} /addlast
  • Use the command bcdedit /default to change the default boot loader. For example, to make the boot loader for the real drive as the default boot.
C:\>bcdedit /default {51f3be3c-8088-11de-ace6-0014223d6008}

Now the machine has become a dual-boot machine. The first and default boot loader is for the real drive, and the second boot loader is for the VHD file.

So what’s the difference between the two? When you boot from the real drive, the real drive will get drive letter C. If you want to read from or write to Win7.vhd, you can use Disk Management (diskmgmt.msc) to attach VHD. When you boot from VHD, Win7.vhd will get drive letter C, and the real drive get the letter D. You can read from or write to both of them. One interesting thing is that when you check the size of Win7.vhd on Drive D, you will find the size is what you defined for the VHD, even the type is expandable. You might think you could create an expandable VHD with a big drive size. When the VHD drive size is too big, bigger than the free space on the real drive, you will get a blue screen when booting from VHD.