Thursday, January 13, 2011

Windows 7: Introduction to Virtual Hard Disks (VHDs)

As described in another post in this blog, I was trying to figure out the best way to install Windows 7 Ultimate.  I had discovered native virtual hard disk (VHD) booting.  It sounded like I could use a native VHD to boot Win7.  I wasn't sure precisely how that worked, so I decided to give it a whirl.  This post describes that process.

I started with Win7 already installed and booted from a basic hard drive.  I had a two-drive software RAID0 array created by Win7.  Actually, it was a set of several RAID0 arrays, of which one was left empty and unformatted.  This, I hoped, would be the virtual partition on which I would install a bootable version of Windows 7.  I had not been successful in doing so natively (i.e., directly, without intermediary software).  I hoped to do so with a native VHD -- that is, a virtual drive that would create a Win7-boot-friendly world within the Win7 RAID0 partition.

The first step was to run Disk Management (Start > type "diskmgmt.msc").  (There were also command-line options.)  I could see my eagerly waiting empty RAID0 partition.  On the menu, I went into Action > Create VHD > Browse.  To my surprise, Disk Management seemed willing to create the VHD just anywhere.  It made sense -- the VHD was going to be contained in a single file that would function like other files -- but I had expected something like a partitioning program here.

I designated a location and filename:  D:\Current\Win7VHD.vhd.  Having heard nasty things about the dynamically expanding option, I went with the fixed size default setting.  Since I had the general idea that I was going to be installing Win7 inside this file, it seemed this file would have to be monstrously large, raising a question of backup:  would a single change anywhere in this file require another mega-backup?  A search led to a thread advising an allocation of 60GB to 100GB for a Win7 partition.  Informed posts in another thread echoed that; likewise a third.  I could spare 100GB on my existing setup without moving partitions, so I set it at 90GB (i.e., 92160MB) just in case there was some funky slippage that nobody was telling me about.  Win7 informed me that it was installing VHD device driver software.

I checked D:\Current.  There it sat, Win7VHD.vhd, size = 94,371,841KB = 92160MB.  Now what?  Apparently one option was to just treat this VHD like a data drive.  In Disk Management, I went into the Action menu pick, with the idea of clicking on the Attach VHD option.  But it was grayed out.  Apparently I was missing something there.  I figured the myster would be resolved eventually.  And then, right before my very eyes, Disk Management changed.  Now it was displaying a new, 90GB unallocated space that it described as Disk 3, Unknown, Not Initialized.  The minutes that had passed since I created the 90GB VHD file were apparently spent actually creating that file in D:\Current.  Evidently this was like the file copying process in Windows Explorer, where sometimes it would say that it had actually gone ahead and copied your 15GB video file to another drive, but what it actually meant was that it was just starting that process, and the new copy would not really exist for a while yet.

Alright.  So now I selected Disk 3, my VHD thing, and went into Action.  Now Attach VHD was not greyed -- it was ready to go.  I clicked on it, to see what would happen next.  Apparently selecting Disk 3 hadn't accomplished anything:  Attach VHD just opened up a dialog asking me where was the file that I wanted to attach.  That made sense -- but then why did Disk Management already show this 90GB unallocated partition?  It seemed like it would have been more logical to show it once I had designated the file that I wanted to attach.  Anyway, I browsed to and selected D:\Current\Win7VHD.vhd and clicked Open > OK.  This gave me an error message:  "The virtual disk is already attached."  So maybe I was right the first time:  as soon as I created the 90GB VHD file, Win7 set aside a logical partition for it, which I was seeing as Disk 3 in Disk Management.  Evidently the Attach VHD option had no real meaning for my present purposes; evidently it had been grayed out because Win7 was then in the VHD-creation process.

So did this mean that Windows Explorer would now display a 90GB unallocated disk?  No.  Well, alright then, could I right-click on the Unallocated area of Disk 3 and format it or something?  No, all options were grayed out except Properties.  Beggars can't be choosers:  I went into that.  There wasn't really anything to adjust there, unless I wanted to turn off write-caching.  Ah, but I could right-click on the left end of the Disk 3 bar, there in Disk Management, and there I did find options:  Initialize Disk, Offline, Detach VHD, Properties, or Help.  It looked all set to let me go ahead and initialize that disk.  So then apparently I would have a 90GB drive, identified with some Windows drive letter -- Q, maybe -- and this drive Q would exist within a 90GB file located in D:\Current.  Kind of an interesting idea.  Probably I could detach it, then, and move it around to some other folder, or maybe even move it without remembering to detach it and thereby crash the system and lose the file and everything in it.  But no, I had faith in Microsoft -- surely they had already anticipated that and would have various protections in place.

It then occurred to me that I had made the drive 90GB because I had an unused 100GB space on my hard drive.  But I was thinking wrong.  The VHD hadn't been created in that unused space.  It had been created in drive D.  I checked the properties for that drive in Windows Explorer.  Sure enough, drive D was a lot more full than it had been 15 minutes ago.  Wherever I was going to be creating VHDs, I needed to make sure I had enough space to play with.  Preferably I would have a space set aside for them, so that I would create and keep them in one place, instead of having to move these gigantic 100GB lumps around all the time, from one drive to another.  I guessed that they could be stored in much smaller spaces on a compressed backup drive, but that trying to run a VHD from a compressed drive would be an extremely slow affair, if such a thing was even possible.

Finally, detaching the VHD gave me the scary option of deleting the disk file.  I was surprised Microsoft put that there.  Oops, my mistake:  there goes 100GB of data.  In this case, I went ahead with it, since I had nothing in the VHD; and for that purpose it was convenient.

So we had the concept of a VHD.  Now, what if I wanted to use a VHD, not just as a storage unit, but as the home of my Windows 7 program files?  In other words, I wanted to boot the system from a VHD.  But how?  That is the topic of a separate post.