Partilhar via


Boot from VHD experiences

Hello,

 

In my current project, I was having a Hyper-V R2 server box to play with and test various options and code with virtual machines. I recently learned the office I work in will be reassigned to someone else. That means loosing the Hyper-V box in a near future.

So, in addition to one of the project question (automate boot from VHD for SQL servers machines), I decided to give it a look and attention.

That’s pretty cool, once it works, but it’s less simple than it looks to set it up: the Windows 7 boot system is much more complicated and sensitive than the legacy one (Windows XP and previous). On the other hand, it is what enables the boot from VHD.

So here are my few learning:

Now that the references are set, my real life problem, and how I solved it:

  • My computer is a powerful laptop from one of the SKU available in MSFT, + 2 additions:
    • I bought 1 Ultrabay tray to replace the useless DVD drive with a high-end 500GB, 7200 rpm, 16 Mo cache HDD
    • I replaced the internal disk for a Patriot Torqx 128GB SSD
  • That leads to few points:
    1. The SSD has to be the system/OS disk (if not what is the interest?)
    2. This SSD has 2 partitions:
      • the 100 MB that Windows 7 uses to place Win_PE boot partition, and that you (almost) have no access to
      • the rest is 1 primary partition with Windows 7 OS partition (quite legacy setup)
    3. I wanted to have an additional new OS (Windows Server 2008R2 with Hyper-V) to be stored 1 dynamic VHD file on the Windows 7 partition so that:
      • It’s flexible and versatile
      • I don’t waste SSD (expensive) space with a second partition
      • I can add/remove/rollback the OS anytime I want (I just replace the VHD file by one of its previous copies)

How I had it to work?

Simple, when you know the right sequence and why …. which needed few hours of work by the way.

  1. Prepare a bootable device to setup the OS from:
    Like a lot of you, DVD are things from the past to setup software. But you need to boot without any OS loaded to have a dual boot (I know that seems obvious, but…). To get this done, I created a bootable USB key with windows server 2008R2 on it. This is explained here: https://technet.microsoft.com/en-us/magazine/dd535816.aspx and that works pretty well. Then modify your BIOS to have the USB boot option

  2. Now that you can boot on the USB drive, do it:

    • At the first screen, hold “SHIFT” and press “F10”. That launches the Windows PE command prompt ShiftF10[3]
    • Then type “diskpart” (be careful, the keyboard is QWERTY here if the ISO is en-us)
    • type “list vol”
    • and find the LETTER of your “normal” boot drive: the letter C (which is by default) is used by the 100 MB hidden partition! If you create something on C here, you’ll (almost) will never be able to access it!
    •  list_vol[1]
    • in my case, the letter is “E”, so type: “sel vol E”
    • create the VHD file you’ll set the OS in: whatever the type of VHD, there must be enough space available for the “potential maximal” size of the VHD file!
    • in my case, I created a dynamically expandable VHD file of a max size of 40GB (because my “normal C” partition has 60 GB free). To do so, type “create vdisk file=E:\2008R2-HVC.vhd maximum=40000 type=expandable”
    • you then need to select it to work with it: “select vdisk file=E:\2008R2-HVC.vhd”
    • attach it: “attach vdisk”
    • that’s it. Check the volume is visible with “list vol”: your VHD must be seen now with a * on the start of the line
    • type exit twice
    • setup the OS on the virtual disk (vhd) you must now see in the partition choices

     

  3. You now need to boot on this VHD and add an entry in your BCD store:

    • at the first OS reboot, you’ll must likely end with your previous OS started….. normal the setup don’t really get what you try to do here.

    • To solve this, logon to the “previous” OS. Don’t panic, everything is here. You just need to explain to Windows boot how to handle this.

    • Once logged in, launch a command prompt with administrator privilege

    • type: “bcdedit /copy {default} /d "Whatever new name you want for this new boot entry" “. The system will answer back with a {GUID}.

    • copy this GUID and type:

      • bcdedit /set {GUID} device vhd=[C:]\2008R2-HVC.vhd

      • bcdedit /set {GUID} osdevice vhd=[C:]\2008R2-HVC.vhd

      • NOTE: Here, the drive letter used MUST BE the one seen by the “previous OS”: in my case C:
        BootVHDBCDAfter[2]

  4. Reboot, select the new entry and finish your new OS booted from a VHD setup!

That sounds simple, but there are 2/3 tricks to have in mind to make it a full success.

Last 2 notes, then I 'am done with this topic:

  • The VHD file will be shown with 2 sizes depending on which OS you booted on:

    • The “former OS” will show its actual size:
      BootVHDRealSize[1]
    • The “booted from the VHD” OS will show the file with its maximum size. In fact the system checks during the boot on VHD process that the VHD would be able to expand till its limit, and reserve that space for it. That’s why the “maximum=” parameter is important. If there’s not enough space to host the “expanded to its maximum size” VHD on the disk, you’ll get a setup or boot error…. took me some time to figure this out.
      BootVHDFullSize[1]
  • I usually use a tool to edit the BCD store. I don’t like bcdedit.exe for that. Note that some graphical tools may have problems understanding VHD device and osdevice. The one I use call them “unknown”
    BootVHDBCDAfterEasyBCD[1]

Enjoy

< Emmanuel />