In "Diskpart Goes Virtual" (InstantDoc ID 103685), I showed you how to use Diskpart's Create Vdisk command to build a Virtual Hard Disk (VHD) file. A VHD is a file that lets you easily package and transport data that can be mounted to act as a hard disk or, of course, used as a virtual disk with Microsoft's virtualization tools. There's more to using a VHD than creating it, however, so this month I'll show you a few more VHD-related commands.
Last month, you created a 200MB VHD named e:\test.vhd. Now that you have this VHD, you'd like to interact with it (e.g., examine its contents, put files in it), so you'll need to use a couple more commands in Diskpart: Select Vdisk and Attach Vdisk. In our example, the Select Vdisk command and its results look like
DISKPART> select vdisk file=e:\test.vhd
DiskPart successfully selected the virtual disk file.
Select Vdisk doesn't offer any options except noerr, an option available on most Diskpart commands that simply instructs Diskpart to keep running a script despite errors.
You still can't look inside the VHD or create files in it, though. Once you've created and selected the VHD, you can use Attach Vdisk to instruct Windows to treat the VHD as if it were an actual physical disk. Its simplest syntax looks like
Note that you don't need file=
When you've finished creating files and installing software on your VHD, you can instruct Windows to stop treating the VHD as an actual hard disk by typing two commands:
select vdisk file=vhdfilename
You can then exit Diskpart, and your system will still be attached to the VHD and treating it like a physical hard disk.
We discussed the Create Vdisk command last month, but you probably won't use that command nearly as often as you'll use the Select Vdisk/Attach Vdisk and Select Vdisk/Detach Vdisk combinations. Not everyone seems to understand that you can select/attach as many VHDs as you want. For example, if you have three already-created VHDs on your hard drive, you could type three Select Vdisk/Attach Vdisk command pairs and end up simultaneously working with three VHDs attached as three "imaginary" hard disks.
Suppose you sit down at a system that you know nothing about, and you see a surprising number of lettered volumes. You want to know how to determine which, if any, of the system's hard drives are VHDs. If you're working from a system with a full GUI, the easiest way to find the VHDs is to look at the drive icons in Logical Disk Manager (diskmgmt.msc); LDM colors VHDs cyan instead of the normal gray. If you don't have a GUI to work with—such as on a Server Core system or on a system that you're remotely controlling via WinRS, PsExec or some other remote command window tool)—Diskpart can help. From Diskpart, type
Recall the List Disk command, which lists all the drives on a system. On a system with VHD support, List Disk lists only those drives that seem somewhat local. I say "somewhat local" because although mapped network drives aren't listed, iSCSI LUNs are listed—and so are attached VHDs. Unfortunately, List Disk doesn't offer much in the way of clues to a disk's true nature, so you can't quickly see which are iSCSI or VHD. You can always determine a disk's nature by first selecting it (Select Disk disknumber), then typing Detail Disk. It will then report a disk type (e.g., iSCSI, Virtual). But that's a pain, which is why List Vdisk is so likeable. The second column of List Vdisk's results—Disk ###—refers to the disk number shown in the List Disk output. Nice!
Oh, and one more thing about attached VHDs in Windows: The attachment doesn't survive a reboot, so if you're going to need a VHD attached all the time, consider cooking up a Diskpart script for your logon or startup script. Happy attaching!