3. Optimizing virtual disks
Although both fixed-size and dynamically expanding virtual hard
disks are supported in production environments, Microsoft still
recommends in Windows Server 2012 Hyper-V that you continue to use
fixed-size virtual hard disks to ensure that virtualized workloads on
your production hosts do not run out of storage space.
Because dynamically expanding disks can automatically grow over
time as data is written to them but do not automatically shrink when
data is deleted from them, you might want to shrink such disks to
reclaim unused storage space on the host so that you can allocate the
reclaimed space to other virtual machines. And because fixed-size
disks can fill up over time, you might need to expand such disks to
make more room so that the guest operating system and applications can
continue to perform optimally. Hyper-V allows you to perform different
kinds of actions on virtual hard disks to help maintain or optimize
their performance:
-
Compacting virtual disks This
action reclaims empty disk space and reduces the size of the
virtual hard disk. Such empty disk space is left behind when files
are deleted in the disk. You should defragment the guest operating
system before you compact the disk. Both dynamically expanding and
differencing disks can be compacted; fixed-size disks cannot be
compacted.
-
Converting virtual disks This
action can be used to convert between different virtual hard disk
formats (VHD and VHDX), between different virtual hard disk types
(dynamically expanding and fixed-size), or both. During the
conversion process, all data is retained and the guest operating
system is not changed. You cannot convert dynamically expanding or
fixed-size disks to or from differencing disks.
-
Expanding virtual disks This
action can be used to expand the size of an existing virtual hard
disk instead of creating a new disk and manually migrating the
data. You can expand all types of virtual hard disks provided you
have free storage space on the host.
-
Shrinking virtual disks This
action is new in Windows Server 2012 Hyper-V and can be used to
reduce the storage capacity of a virtual hard disk. You can shrink
all types of virtual hard disks provided you have first freed up
space in them.
Important
Shrinking a virtual hard disk
Before you use either Windows PowerShell or Hyper-V Manager to
shrink a VHD or VHDX file, you should log on to the guest operating
system and use the Disk Management snap-in to shrink the volume on
the virtual disk you want to shrink.
There are also two actions you can perform only on
differencing disks:
-
Merging differencing disks
This action can be used to combine the changes stored in a
differencing disk with the contents of its parent disk. You can
do this either by copying the contents of the parent disk and
differencing disk into a new virtual hard disk, leaving the two
source disks intact, or by applying the changes contained in the
differencing disk to the parent disk.
-
Reconnecting differencing
disks This action is available only when you select a
differencing disk and the parent disk cannot be located. If the
disks involved belong to a chain of differencing disks, this
action is available only if none of the disks in the chain can
be found.
Important
Avoiding data loss
Do not compact, convert, expand, shrink, or merge a virtual
hard disk when any of the following conditions apply:
-
The disk is associated with a virtual machine that has
snapshots.
-
The disk is associated with a virtual machine that has
replication enabled.
-
The disk is associated with a chain of differencing
disks.
If you perform any of these actions under such conditions,
data loss or corruption might occur.
You can use Hyper-V Manager to compact, convert, expand,
shrink, merge, and perform other actions on virtual hard disks. The
actions that are available to be performed will depend on the type
of virtual disk you are selecting. To perform any of these actions,
click Edit Disk in the Actions pane to launch the Edit Virtual Hard
Disk Wizard. Then after browsing to select the disk you want to
edit, you can choose the action you want to perform on the disk as
shown in Figure 3.
You can also use Windows PowerShell to compact, convert,
expand, shrink, merge, and perform other actions on virtual hard
disks. For example, you can use the Optimize-VHD cmdlet to compact a
virtual disk, the Resize-VHD cmdlet to expand or shrink a virtual
disk, or the Convert-VHD cmdlet to change the format version or type
of a virtual disk. For help with any of these cmdlets, use the
Get-Help cmdlet.