The bulk of any virtual machine is made up of virtual hard disks (VHDs), which are large binary files that must...
be stored, maintained and backed up. Fortunately, most virtualization platforms provide you with several methods for creating and managing virtual storage based on the needs of a particular workload.
In Hyper-V, you can easily create new VHDs, manage their settings and attach them to VMs. The Hyper-V Management Console provides everything necessary to perform these basic tasks, but it's important to understand the options available and their potential ramifications. This tip describes the steps that are required to create and manage VHDs along with some performance-related recommendations.
Creating virtual hard disks
There are several ways to create new VHDs using the Hyper-V Management Console. Perhaps the most common method is to use the new virtual machine wizard to automatically create and attach a VHD when creating a new virtual machine (VM). You can also manually create new VHDs using the Hyper-V Management Console's new virtual hard disk wizard (see figure 1). Or, it's a straightforward process to use the Windows Management Instrumentation (WMI) API and Windows PowerShell to perform the necessary steps (I'll cover scripting and automation in a future tip).
Figure 1: Using the new virtual hard disk wizard
The most important selection is to determine which type of VHD you want to create. Here are some notes related to the options:
- Dynamically expanding disks: The default type of VHD starts with a very small physical on-disk size and expands as data is written to the file. The guest OS will see the total maximum size of the disk, as defined in a later step of the wizard. This option provides the most efficient use of disk space, but not always the best performance. Host file system fragmentation can lead to less-that-optimal read and write performance, and there's a risk that you'll run out of physical space on the host server if you don't monitor the physical size growth of the VHD files.
- Fixed size disks: Fixed size hard disks occupy physical disk space on the host equal to the maximum size of the disk. For this reason, they take longer to create initially. While fixed size VHDs consume disk space whether the guest OS requires it or not, they can provide improved performance because they are (hopefully) stored in a contiguous block on the host storage system.
- Differencing disks: These VHDs are used to create parent-child configurations. The most common use is to have a common base OS VHD that is shared among numerous VMs (and that must remain read-only), and to have differencing disks for storing any changes. The primary rules are that any parent hard disk must be considered read-only, otherwise it will invalidate and child VHDs that were generated from it. Hierarchies can become complicated and can lead to performance and management issues, but this is a powerful feature when used properly.
After you make these selections, you will be asked to provide the physical file system location for the VHD. This can generally be any storage that is accessible the host, including direct-attached and block-based network storage (iSCSI and Fibre Channel).
The default size for new VHDs is 127GB but you can choose from between 1GB and 2040GB (roughly 2 terabytes). Keep in mind that the guest OS must support the disk size you select in order to use all of the available space. That's not a problem for modern OSes but can be a problem if you're planning to fire up the original Doom in an MS-DOS VM. There is also an option on the configure disk step to copy the entire contents of an existing physical disk located on the host server, a feature that is useful for manual P2V conversions or when using a SAN or iSCSI volume attached to the host.
Maintaining and editing virtual hard disks
Like most aspects of an IT infrastructure, VHD configurations are subject to change. The Hyper-V Management Console provides a quick and easy way to manage existing virtual hard disks with the edit virtual hard disk wizard. The first step asks you to find the specific VHD file with which to work. The three available operations for a dynamically-expanding virtual hard disk are shown in figure 2.
Figure 2: Using the edit virtual hard disk wizard
The compact operation is used to reclaim any unused space that exists within a virtual hard disk. This option is available for dynamically-expanding virtual hard disks and is most useful when you have added a lot of data to the VHD. After the compact operation is complete, the physical size of the file on the host will be reduced. The guest OS will not see any differences, since all of this is performed without modifying the file allocation tables.
The convert operation allows you to change a dynamically-expanding VHD to a fixed-size one, or vice versa (depending on which type is selected in the initial step.) This can be done for performance reasons or to reclaim usable disk space on the host server. Keep in mind that some of these operations can generate a large amount of disk I/O, so don't schedule them to run during periods of high activity.
The edit virtual hard disk wizard allows you to expand an existing VHD, which is a very convenient feature if you underestimated the total amount of data you would need on a specific logical volume. When you expand the VHD, the guest OS will see the additional space as unallocated, unformatted disk space. Depending on the guest OS's capabilities, you can either expand the existing logical volume or you can create a new logical volume in the free space. In the Windows world, you can perform this using the disk management tool or by using the diskpart command-line utility. Of course, you can also create and attach a new VHD if it's OK to create a new volume within the guest OS.
Keep in mind that there are some potential gotchas to keep in mind when expanding VHDs. First of all, any VM to which the VHD is attached must be turned off (it cannot be in a saved state.) Also, if you have created snapshots for the VM, all previous snapshots will be invalidated after the VHD is resized. If the latter is a problem, consider attaching a new VHD to the VM to get some extra storage space. It's also possible to expand (but not compact) fixed-size VHDs.
Overall, Hyper-V provides a flexible virtual storage architecture that balances host disk space, performance and manageability. In my next tip, you'll learn how to make the most of VHDs, including how to attach them to VMs and how to maintain and improve overall performance.
About the author: Anil Desai is a Microsoft MVP and a Microsoft Certified Professional with numerous credentials including MCITP, MCSE, MCSD and MCDBA. He is the author or coauthor of nearly 20 technical books, including several study guides for Microsoft Certifications.