c o d i n g f r o g s

croaking about programming, programming languages, software engineering, and the business of software

21Oct/100

Automounting a VHD at system boot on Windows 7

I'm playing around a bit more with my test machine at work, and I've been thinking, as excellent as my last setup was, it could be even better.

One way to make it better was to avoid duplicating installation data and instead to put that kind of stuff on a shared VHD that all the other machines can access - physical and virtual.

To start out I recreated a multi-boot-from-VHD setup as I had before.  In addition, I created another VHD to be used in common among them; let's call this "shared.vhd".

Once I've installed the OS into the VHD, I can worry about trying to get the OS to mount the shared.vhd at boot time.  There's three main steps involved.

The first thing to do is create a diskpart script.  This is simply a text file that will be passed in as an argument later to the diskpart command.  The script simply selects my shared VHD and attaches it.  Here's the contents of my dpscript.txt file:

select vdisk file=D:\shared.vhd

attach vdisk

exit

In my case the shared.vhd file is located on D: — your mileage may vary.

The next thing to do is create a batch file that will run diskpart with the script we just created.  I made mountvhd.bat with a single line:

diskpart /s C:\dpscript.txt

Both dpscript.txt and mountvhd.bat are located in my C: root folder, although that also is just down to personal preference.

Anyway, at this point if I run my batch file from a command prompt (with administrative privileges) I can see it select and attach the virtual disk file, so I know the script is working.

The last thing to do is set this up to run at startup time.  There's a number of possible ways to do this but not all of them seemed to work well for me.  For example, using the Task Scheduler to create a task that runs at startup time should work, but it doesn't seem to.  However, adding this batch file as a startup script through Group Policy worked.  Do the following:

  • From an administrative command prompt, type "mmc" to bring up the management console.
  • Under the File menu, choose "Add/Remove Snap-In".
  • Choose "Group Policy Object Editor" in the left pane and click the "Add" button in the middle.
  • Accept the defaults in the dialog.
  • Click OK to dismiss the Add/Remove Snap-Ins dialog.
  • In the tree view on the left of the main window, expand Local Computer Policy, then Computer Configuration, then Windows Settings, and select "Scripts (Startup/Shutdown)".
  • In the main window, select "Startup".
  • Click the "Properties" link to bring up a dialog for managing startup properties.
  • Choose "Add" to add your new startup script.
  • Browse for the batch file you created, click OK, then click OK again to exit the Startup Properties window.
  • Close mmc.  If prompted to save, do so.

Now when you reboot your machine, that VHD should be mounted as an additional volume.

3Sep/100

Multi-booting Windows 7/2008R2 From VHD

One very useful feature of Windows 7 and Windows Server 2008 R2 is the ability to boot the OS directly from a virtual hard disk file (VHD).  This can make it pretty straightforward to be able to run a number of different versions of Windows on a single machine.

I just finished converting my test machine to a boot-from-VHD scenario; before that, I was using a native Windows Server 2008 R2 install with Hyper-V.  Here's some of my observations about the tradeoffs (remember, I DO NOT speak for Microsoft):

  • Boot-from-VHD is not virtualized; you are just running the OS out of a mounted VHD file
    • Because of this, you might expect boot-from-VHD to be a bit faster than a virtualized OS.
    • Since a lot of our work is based on virtualization technologies, it seems weird to test it in Hyper-V (a VM within a VM) and sometimes I wonder if issues I see are a result of my environment.
  • With Hyper-V, you can run all your test operating systems at once; boot-from-VHD is one OS at a time
  • VMs in Hyper-V have snapshotting, so it's easy to roll back to a pristine state

There's a few other minor tradeoffs in the boot-from-VHD scenario, but not enough to fret about too much.  So I thought I'd give it a try.

To start out, just plop in your Windows 7 media and start a clean new installation of Windows 7.  Or, if you are like me, you can pixie boot it.  Start the Windows 7 installation.  Once you've passed the language selection screen, hit SHIFT+F10.  This brings up a WinPE console.  Type "diskpart" in the console to bring up the disk partitioning tool.

If you need to, reformat your primary drive:

  • sel disk 0
  • cre part pri
  • format fs=ntfs quick
  • assign

Now, create a VHD for each OS you want to install (put in a value for maxfilesize, in megabytes):

  • create vdisk file=c:\vhdname.vhd maximum=maxfilesize type=expandable
  • select vdisk file=c:\vhdname.vhd
  • attach vdisk
  • cre part pri
  • format fs=ntfs quick

Exit diskpart and WinPE.  When you get to the destination media selection screen, where you decide which volume to install your OS to, select the first VHD in the list, not your base drive.  The size should be close to what you specified.  IMPORTANT:  Ignore the warning that Windows can't install to that disk - it will work just fine.

After you finish installing your first OS, you can insert the media for the next OS and restart.  Go into setup the same as before, and just like before, enter WinPE after the language selection by pressing SHIFT+F10.  This time you don't need to create the VHDs.  They are still there, you just need to attach them again:

  • select vdisk file=c:\vhdname.vhd
  • attach vdisk

I attached all of mine in the same order as before.  I don't know whether this is the right thing to do or not but it seems to have worked okay.

After exiting diskpart and WinPE, you should see the mounted VHDs listed in the media selection screen again.  This time install to the second VHD in the list.  You should be able to tell the difference because the first one shows less available space than the other VHD(s).  Again, run through the install as before.

Now once you finish you can boot to any of the options in your boot loader menu, if you can distinguish them.  You may wish to edit the boot loader config.  Do the following:

  • Launch an elevated command prompt
  • Run bcdedit.  Take note of the identifiers (GUIDs) for each of your machines - you will need this information momentarily.
  • To change the description of an entry, type bcdedit /set {identifier} Description "a meaningful description" where:
    • identifier is the GUID of the machine you want to change.  {current} and {default} work for the machines they pertain to in bcdedit
    • a meaningful description is the text you will use to identify this machine in the bootloader
  • To set a different entry as the default, type bcdedit /default {identifier}
  • To set the timeout value for the bootloader, type bcdedit /timeout <seconds> where <seconds> is the number of seconds you want to wait
  • For help with bcdedit, type bcdedit /?

Using this technique, I set my machine up for a triple-boot with Windows 7 32-bit, Windows 7 64-bit, and Windows Server 2008 R2.  And, within Windows Server 2008 R2, I should be able to run Hyper-V, where I can run additional Windows 7 test VMs with snapshotting.  The best of both worlds!