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.
