Sunday 4 April 2010

Windows 7's Boot from VHD - Transferring the VHD to a New Hard Drive

You might be aware that Windows 7 and Windows Server 2008 R2 are capable of booting from a VHD.  The advantages of this set up include ease of backing up the whole O/S partition and the ability to dual-boot (or multi-boot) without the need to create multiple partitions.

If you search for terms such as "Windows 7 boot VHD", you will get lots of hits on how to install a new instance of the O/S using this feature.  But what if you have already set up such a booting arrangement, and now wants to move the VHD from one drive to another?  For example if you have bought a new faster hard drive and want to transfer over the VHD.  I haven't found any post describing how to do this.

One way to do this would be to run through the O/S install process again on the new hard drive, creating a brand new VHD boot.  Then just copy your existing VHD onto the new hard drive, overwriting the new VHD you just created.  However, this meant going through the whole install process again, and I wanted to find if there is a faster way to do this.  After some experiments and some searches on the web, the following steps worked for me (assuming you have connected up the new hard drive, created a primary partition and formatted it):
    1. Boot up your machine in Windows 7 install DVD.
    2. Press shift-F10 to bring up the command prompt.
    3. Copy the VHD to the new hard drive.  (You could do this beforehand if you have another O/S you could boot up in, so that the VHD could be copied.)
    4. Power down and disconnect your old hard drive.  This step is optional, but it avoid any confusion between new and old drives.  Then repeat steps 1 and 2 to get back to the command prompt.
    5. Run diskpart, then enter these commands (where C:\Win7.vhd is the location of your VHD on the new hard drive):
      • select vdisk file=C:\Win7.vhd
        • attach vdisk
        • exit
      1. From the command prompt, enter:
        • bootsect /nt60 C: /mbr
      2. From the command prompt, enter (where E: is the drive letter of the attached VHD):
        • bcdboot E:\Windows /s C:
      3. Exist the command prompt
      4. Go back and follow the main Windows 7 install screens.  On the 2nd screen, click the "Repair your computer" link.
      5. On System Recovery Options screen, choose "Use recovery tools" then click Next.
      6. Choose the "Startup Repair" option.
      7. After it has done its work, click the Finish button.  It will reboot.
      Now the machine should boot up in your original VHD, running on your new hard drive.

      Some additional notes:
      • Instead of pressing shift-F10 (or if you don't remember which keys to press), you can also go through the "Repair your computer" option (steps 9-10) to get to the option to bring up the command prompt.
      • These steps assumes you have formatted your new hard drive while running in Windows.  That would be easier and safer.  If you consider yourself hardcore, you can do the formatting from within diskpart in step 5.  But be very careful, because if you select the wrong disk, you would wipe out your existing hard drive.
      • If you are running on a physical partition, you can convert it to booting from VHD.  Firstly, use Disk2vhd to copy your existing partition to a VHD.  Then follow the steps similar to above to set up booting from the VHD.