Over the past two months—in "Initializing Windows Disks with Diskpart" (InstantDoc ID 103422) and "Formatting and Resizing Partitions with Diskpart" (InstantDoc ID 103539)—I've showed you how to use Diskpart to wipe a hard disk clean, partition it into volumes, give those volumes drive letters, and format those drives. That's all useful stuff to know, but IT pros increasingly find themselves concerned not only with real hard disks but also with virtual hard disks. Microsoft's virtualization tools (e.g., Hyper-V, Windows 7's XP Mode) need some standard way to package the "imaginary" hard drives that their virtual machines (VMs) rely on, and that standard is the Virtual Hard Disk (VHD) file format.
Pick apart a VM built with one of Microsoft's VM-management offerings (e.g., System Center Virtual Machine Manager—SCVMM), and there's at least one VHD hiding in there. And it's not only VMs that use VHDs: Microsoft's Complete PC Backup tool stores its backup files as VHDs. Further, VHDs are a great way to package a bunch of files and folders into just one file; it's sort of like a .zip file, but unlike .zip files, VHDs can store NTFS attributes and security permissions. In fact, one of Windows 7's and Windows Server 2008 R2's more interesting new features is the ability to boot from VHD files whose virtual hard disks contain bootable OSs!
So, VHDs are here to stay, and I don't know about you, but whenever a new file format becomes prevalent, I start looking around for diagnostic and configuration tools that understand that file format. In Windows 7 Enterprise, Windows 7 Ultimate, and Server 2008 R2, that tool is Diskpart. With Diskpart, you can create VHDs from scratch or examine existing ones and give them drive letters so that you can use Windows Explorer to work with them as if they're physical hard disks.
Let's start examining Diskpart and VHDs by seeing how to create a VHD. From inside Diskpart, the command to create a new VHD looks like (in its most minimal form):
create vdisk file=<filename> maximum=<size in megabytes>
For example, to create a 200MB VHD named e:\test.vhd, you'd type
create vdisk file="e:\test.vhd" maximum=200
That command would immediately create a 200MB file structured as a VHD. Notice that the parameter that sets a VHD's size is maximum= rather than size=. This syntax drives me crazy. I can't seem to break myself of the habit of typing size=. But there's actually a reason for using the maximum= parameter: expandable VHDs.
Immediately allocating that 200MB for test.vhd probably makes perfect sense because 200MB isn't very big. For larger VHDs, however, you might not want to have Diskpart immediately allocate all the VHD's space (for reasons I'll explain in a moment). Instead of immediately allocating all the hard disk space that the VHD might ever need, you can allocate only a small amount of space on the actual hard disk, growing the VHD's space only when necessary in the future. You do that by adding type=expandable to the Create Vdisk command:
create vdisk file=e:\test2.vhd maximum=200
Running that command instead of the first causes Diskpart to complete its task considerably faster, and it creates a VHD that initially takes up only about 2500 bytes on the hard disk. (The maximum=200 command makes a bit more sense now.) By the way, when you're creating a fixed-size VHD, you can add the type=fixed option to the Create Vdisk command, although I'm not sure why you'd want to do the extra typing.
Fixed or expandable, which way to go? Fixed VHDs can be faster because all the time necessary to allocate the VHD's disk space happens when you create it. Expandable VHDs haven't allocated their space up front, so when you do need that extra space, the application writing to the VHD often must wait for the OS to obtain more space for an expandable VHD, thus slowing down the system—perhaps unacceptably. That being said, transporting a 20GB VHD over the web or on a USB stick is much easier to do if that 20GB VHD is expandable and hasn't yet used more than a gigabyte; a fixed-size VHD is always 20GB, no matter how much of its space has actually been used. Expandable VHDs have another advantage in that they can be compacted—shrunk in size to the smallest size possible. Diskpart can't reduce a fixed-size VHD.
Now you know how to use Diskpart to create a virtual hard disk, but that VHD is no good unless you take a few more important steps. I'll guide you through those steps next month.