How to turn a physical disk to a bootable VHD

30 Nov

My 160GB hard disk is running out of free space. Seriously low… It’s amazing that I’ve managed to fill it up with so much stuff and it’s all work stuff too. Now normally I rebuild my laptop every 4 months or so (yes I really do) but this time, I don’t necessarily have the luxury of spending a couple of days because I’m up to my armpits on a project.

My Computer - creaking along

So my normal setup is to have my one physical disk partitioned into two: one for the native operating system and the other for my data. I’ve always favoured split partitions, since configuration means that if I ever need to do a rebuild, I don’t have to spend overnight backing up all my work data before the drive gets obliterated. Even though it only takes about 45-60minutes to reinstall Windows 7, it does take a while to install Microsoft Office, Visual Studio, SQL Server and a plethora of other programs and tools. It’s probably a couple of days before I’m comfortably back into a situation where everything is back to how I want it to be.

Thing is, I don’t have a couple of days and ultimately it’s not going to solve my space issue. So I have a cunning plan with Windows 7 because I’ve discovered that it’s now got the inbuilt capability that allows it to mount and boot from Virtual Hard Disks (VHDs). Effectively a portable, logical disk that runs natively on the hardware – not in a virtual machine.

Where does a VHD sit?

Basically, a VHD is a large contiguous file structure that can be recognised as a hard disk, yet it’s a virtual one sitting on your physical hard disk. I can backup my existing physical disk partitions to a VHD but it has to sit on a larger disk than the original, because there is an amount of hidden space used by the operating system to host the file. VHD’s are a bit clever in that they can be setup as “dynamic” and expand as necessary when data is added to it – there is no compression involved, but the free space is squeezed out in an attempt to minimise the overall footprint of the VHD – a 160GB partition with 50% free space will result in a VHD file with a 80GB footprint. Alternatively, VHDs can be set to “fixed” and remain that size regardless of its mounted state – a 160GB VHD remains 160GB. If you want faster performance then select “fixed”, if you are scrimping on space then select “dynamic” but beware that someday you’ll need that full expanded space available if you fill it up!

Seagate Hybrid TechnologySo, after crooning after a range of Solid State Disks (SSDs) which are blisteringly fast, very expensive but ultimately too small in capacity, I decided to try a 500GB Seagate Momentus XT Solid State Hybrid Drive. Best of both worlds really, optimised for speed, not so expensive and big capacity. Rather ironic, that EMC is one of the largest disk suppliers yet we don’t put high performance disks in our consultancy laptops!

 

DISCLAIMER:
THIS IS MY JOURNEY FROM DISK TO VHD.
IF YOU FOLLOW ANY OF THESE STEPS, THEN YOU DO IT AT YOUR OWN RISK!

 

Creating your VHD

First thing to get your hands on is Disk2VHD. This is a neat little SysInternals tool from Microsoft (just recently had a revamp) which will create you a VHD file for use in Microsoft Virtual PC or for an Hyper-V virtual machine. Unlike some other tools out there, Disk2VHD can backup a disk/volume that is currently operational. YES, that means that you can transpose that disk/volume that you’ve currently booted from!

  1. Download and install
  2. Run “Disk to VHD converter” as administrator.
  3. The following dialog will pop up, telling you what you can convert and how much space you need to complete the operation.
  4. Specify a destination file name for your VHD.
  5. Tick the partitions that you want to include into the VHD.
  6. Click “Create” and the tool will take a snapshot and proceed to transfer data.
  7. Depending on the sizes of the partitions you clicked, wait….

DISK2VHD

Couple of things to note:

  • If you are transferring relatively big partitions (>20GB) then be prepared to wait a while! When I Disk2VHD’ed my laptop, it estimated a 115GB VHD file so I left the tool running overnight!
  • Disk2VHD can backup your currently operational drive onto a disk in that archive – I could have cloned the C:\ and E:\ partitions to a file on the E:\ partition. BUT, for performance reasons, this is not optimal.  Either plug in a spare USB hard disk or connect to a network drive.
  • Don’t think that you have to keep all your partitions on the same VHD. If you want to share a partition amongst different instances, then just backup the minimum to each VHD.
  • Only the booting VHD is guaranteed to be mounted on restart; additional VHDs need to be mounted manually or by script since Windows 7 will not remember on reboot.

With my 160GB laptop hard disk backed up to VHD on a network drive, I can now swap my old physical hard disk for my new shiny 500GB Seagate Momentus XT Solid State Hybrid Drive. I decided to continue with my usual split of the drive, by creating a first partition with 80GB for my native booting operating system and a second partition with the remaining available space for my data. This way, if I ever end up needing to rebuild my laptop, I can leave the “data partition” alone and just blow away the “operating system partition”.

 

Mounting your VHD

In Windows 7, mounting a VHD could not be simpler. The VHD is better off located on a locally attached drive so if you used a network drive as the destination for Disk2VHD, you’re better off copying it back to either the native physical disk or to USB attached storage. Windows 7 won’t stop you attaching to a remote VHD but it’s much slower – I guess that it’s because dragging large chunks of a file across the network is a drain on performance.

So, from the “Start” taskbar, search for “Disk Management” or “Create and format hard disk partitions”. This will pull up the following control panel with details of your current system. Be careful with any of these options!

Disk Management console

Now before I go further… pay heed to the warning from DISK2VHD:

NOTE: do not attach to VHDs on the same system on which you created them if you plan on booting from them. If you do so, Windows will assign the VHD a new disk signature to avoid a collision with the signature of the VHD’s source disk. Windows references disks in the boot configuration database (BCD) by disk signature, so when that happens Windows booted in a VM will fail to locate the boot disk.

 

Attaching a VHDSince I’ve swapped physical disks and reinstalled Windows, I’m not in danger of that rule. Phew.

Mounting a VHD is very straightforward. From the menu bar, select Action | Attach VHD.

Your VHD will then appear as another row in the disk management matrix and all logical partitions will be identified and assigned a new drive letter for your current session. Once there are drive letters, you’ll get access to the drive through Windows Explorer and be able to access folders and files as you would any other drive. Of course, that also means that any tightened security that was applied (particularly if NTFS formatted) might be a bit of a problem since you’ll have to get permission (elevating to administrator normally helps) to just navigate the file system.

Once mounted, the Disk Management console now looks like below. Be careful, but right click the separate drives and take a look at various tasks – these allow you to change aspects of the the individual drives to customise your system.

Disk Management - with mounted VHD

 

Booting your VHD

The last step. Got a backed-up VHD of my original system and I’ve mounted it to check that it’s all there, however this is not good enough for actually running anything because it’s not the boot device. It’s a law of computing nature – all the system files and registry settings have to be on the booted partition. Enter the world of GUIDs and BCDEDIT. Pay heed to my disclaimer above and read all this section before you proceed!

Gone are the days of boot.ini, autoexec.bat and config.sys (remember them?). Windows now keeps the boot settings within Boot Configuration Data (BCD) stores and BCDEDIT allows you to configure these settings via command line. Since I’ve reinstalled Windows onto my physical drive, I’ll have a good BCD store already configured, otherwise Windows would not have booted up!

From the command line (run as Administrator) execute the following code to copy the existing/current BCD to a cloned BCD, giving it a new name of your choice:

bcdedit /copy {current} /d “My new VHD

That command will return a message containing a GUID that identifies the key of the new BCD created. Make a note of your GUID, copy it to notepad or something, but don’t lose it, it will be needed in the next couple of steps. Next, change the new BCD to point to the new VHD. Most of it is self explanatory, but note that the drive parameter is the currently assigned drive letter – the tool just wants to be sure of locating the file:

bcdedit /set {guid} device vhd=[drive:]\folder-path\vhd-filename.vhd
bcdedit /set {guid} osdevice vhd=[drive:]\folder-path\vhd-filename.vhd
bcdedit /set {guid} detecthal on

Two extra commands of note:

bcdedit /v    — this will display all the BCD configuration for your perusal.

bcdedit /delete {guid} /cleanup  — cleans out the BCD data for the GUID key supplied

 

Just to give you an example, my personal configuration used the following commands:

bcdedit /copy {current} /d “Windows 7 EMC VHD

bcdedit /set {f83d7c67-2b5c-11e0-8cda-e442eeb247bc} device vhd=[e:]\vhd\win7-emc.vhd
bcdedit /set {f83d7c67-2b5c-11e0-8cda-e442eeb247bc} osdevice vhd=[e:]\vhd\win7-emc.vhd
bcdedit /set {f83d7c67-2b5c-11e0-8cda-e442eeb247bc} detecthal on

bcdedit /v  

 

Now, reboot. Fingers crossed that if you followed all these steps, you’ll be marvelling at the extra boot option before Windows spins up. I now get to choose between my native Windows 7 partition or my new “Windows 7 EMC VHD” which is running from the VHD file. If you need to tweak the system start-up settings,  then open the “Startup and Recovery” – the new instance from the VHD will show up because of the updated BCD records.

Startup and Recovery options

In Summary

Does it seem any faster? Probably not, at least not noticeably but that has not been my concern, space is. Maybe my new Seagate Momentus XT Solid State Hybrid Drive has helped because of it’s newer electronics and bigger I/O cache, I’m not fussed. My laptop can breathe now, plus this was all done on a lazy weekend rather than spending a whole week of blowing away my existing Windows instance and reloading all that software and digging out all the licence keys that I’ve accumulated over time and that I actually need to do my job. By the way… this could be done faster than a weekend since much of the time was creating and copying the VHD up and down from my NAS and that was done overnight.

Hang on though, have I not just moved the problem into a VHD? Well no. I can turn up to work the next day and still be productive, but I’ll systematically remove those really old applications I no longer use and move my local archive folders either to one of my native data partitions or to NAS so that any future Windows instance can see them (native or VHD).

My Computer - now it can breathe

This has also proved to be a convenient means of backing up my work. Basically once this is done once, I just reboot into my native Windows instance find the VHD file and copy it to my NAS (with a timestamp in the filename). If I manage to corrupt anything, swap laptop, or run up that VHD in Virtual PC, I just copy it back and change the filename to that in the BCD record (so it always remains as “win7-emc.vhd” on my laptop). Gives me great flexibility… although VHDs also have the ability to have support differencing diskstoo, which might help to reduce the constant large footprint (why I have a big NAS!).

A mention about “booting VHDs from USB Hard Drives”. There’s been some mention of this on the forums and blogs that I came across while researching this topic. Seems to be some sort of holy grail for which I think it’s a stupid idea. Sure, it’s useful to stash VHDs to separate disks, but USB HDDs have one flaw for me… they are attached to the end of a cable – have you tried sitting on the sofa with a USB HDD dangling off the side? Tried moving around the airport or the office with your precious HDD swinging about? DON’T DO IT. BUY A BIGGER DISK. The techniques I’ve described above are there for convenience.. don’t bother tying yourself down again. Disk is cheap!