We get lots of cool tricks with virtualization. Among them is the ability to change our minds about almost any provisioning decision. In this article, we’re going to examine Hyper-V’s ability to resize virtual hard disks. Both Hyper-V Server (2016+) and Client Hyper-V (Windows 10) have this capability.
An Overview of Hyper-V Disk Resizing
Hyper-V uses two different formats for virtual hard disk files: the original VHD and the newer VHDX. 2016 added a brokered form of VHDX called a “VHD Set”, which follows the same resize rules as VHDX. We can grow both the VHD and VHDX types easily. We can shrink VHDX files with only a bit of work. No supported way exists to shrink a VHD. Once upon a time, a tool was floating around the Internet that would do it. As far as I know, all links to it have gone stale.
You can resize any of Hyper-V’s three layout types (fixed, dynamically expanding, and differencing). However, you cannot resize an AVHDX file (a differencing disk automatically created by the checkpoint function).
Resizing a virtual disk file only changes the file. It does not impact its contents. The files, partitions, formatting — all of that remains the same. A VHD/X resize operation does not stand alone. You will need to perform additional steps for the contents.
Requirements for VHD/VHDX Disk Resizing
The shrink operation must occur on a system with Hyper-V installed. The tools rely on a service that only exists with Hyper-V.
If no virtual machine owns the virtual disk, then you can operate on it directly without any additional steps. Be aware that if a
If a virtual hard disk belongs to a virtual machine, the rules change a bit:
- If the virtual machine is Off, any of its disks can be resized as though no one owned them
- If the virtual machine is Saved or has checkpoints, none of its disks can be resized
- If the virtual machine is Running, then there are additional restrictions for resizing its virtual hard disks
Special Requirements for Shrinking VHDX
Growing a VHDX doesn’t require any changes inside the VHDX. Shrinking needs a bit more. Sometimes, quite a bit more. The resize directions that I show in this article will grow or shrink a virtual disk file, but you have to prepare the contents before a shrink operation. We have another article that goes into detail on this subject.
Can I Resize a Hyper-V Virtual Machine’s Virtual Hard Disks Online?
A very important question: do you need to turn off a Hyper-V virtual machine to resize its virtual hard disks? The answer: sometimes.
- If the virtual disk in question is the VHD type, then no, it cannot be resized online.
- If the VM attached the disk in question to its virtual IDE chain, then no, you cannot resize the virtual disk while the virtual machine is online.
- If the VM attached the disk in question to its virtual SCSI chain, then yes, you can resize the virtual disk while the virtual machine is online.
Does Online VHDX Resize Work with Generation 1 Hyper-V VMs?
The generation of the virtual machine does not matter for virtual hard disk resizing. If the virtual disk is on the virtual SCSI chain, then you can resize it online.
Does Hyper-V Virtual Disk Resize Work with Linux Virtual Machines?
The guest operating system and file system do not matter. Different guest operating systems might react differently to a resize event, and the steps that you take for the guest’s file system will vary. However, the act of resizing the virtual disk does not change.
Do I Need to Connect the Virtual Disk to a Virtual Machine to Resize It?
Most guides show you how to use a virtual machine’s property sheet to resize a virtual hard disk. That might lead to the impression that you can only resize a virtual hard disk while a virtual machine owns it. Fortunately, you can easily resize a disconnected virtual disk. Both PowerShell and the GUI provide suitable methods.
How to Resize a Virtual Hard Disk with PowerShell
PowerShell is the preferred method for all virtual hard disk resize operations. It’s universal, flexible, scriptable, and, once you get the hang of it, much faster than the GUI.
The cmdlet to use is Resize-VHD:
Resize-VHD -Path ‘\svstore01VMsVirtual Hard Diskstest.vhdx’ -SizeBytes 30gb
The VHDX that I used in the sample began life at 20GB. Therefore, the above cmdlet will work as long as I did at least one of the following:
- Left it unconnected
- Connected it to a VM’s virtual SCSI controller
- Turned the connected VM off
Notice the gb suffix on the SizeBytes parameter. PowerShell natively provides that feature; the cmdlet itself has nothing to do with it. PowerShell will automatically translate suffixes as necessary. Be aware that 1kb equals 1,024, not 1,000 (although both b and B both mean “byte”).
Had I used a number for SizeBytes smaller than the current size of the virtual hard disk file, I might have had some trouble. Each VHDX has a specific minimum size dictated by the contents of the file. See the discussion on shrinking at the end of this article for more information. Quickly speaking, the output of Get-VHD includes a MinimumSize field that shows how far you shrink the disk without taking additional actions.
This cmdlet only affects the virtual hard disk’s size. It does not affect the contained file system(s). We will cover that part in an upcoming section.
How to Resize a Disconnected Virtual Hard Disk with Hyper-V Manager
Hyper-V Manager allows you to resize a virtual hard disk whether or not a virtual machine owns it.
- From the main screen of Hyper-V Manager, first, select a host in the left pane. All VHD/X actions are carried out by the hypervisor’s subsystems, even if the target virtual hard disk does not belong to a specific virtual machine. Ensure that you pick a host that can reach the VHD/X. If the file resides on SMB storage, delegation may be necessary.
- In the far right Actions pane, click Edit Disk.
- The first page is information. Click Next.
- Browse to (or type) the location of the disk to edit.
- The directions from this point are the same as for a connected disk, so go to the next section and pick up at step 6.
Note: Even though these directions specify disconnected virtual hard disks, they can be used on connected virtual disks. All of the rules mentioned earlier apply.
Connect with fellow IT pros and master Hyper-V
Moderated by Microsoft MVPs
How to Resize a Virtual Machine’s Virtual Hard Disk with Hyper-V Manager
Hyper-V Manager can also resize virtual hard disks that are attached to virtual machines.
- If the virtual hard disk is attached to the VM’s virtual IDE controller, turn off the virtual machine. If the VM is saved, start it. If the VM has checkpoints, remove them.
- Open the virtual machine’s Settings dialog.
- In the left pane, choose the virtual disk to resize.
- In the right pane, click the Edit button in the Media block.
- The wizard will start by displaying the location of the virtual hard disk file, but the page will be grayed out. Otherwise, it will look just like the screenshot from step 4 of the preceding section. Click Next.
- Choose to Expand or Shrink the virtual hard disk. Shrink only appears for VHDXs or VHDSs, and only if they have unallocated space at the end of the file. If the VM is off, you will see additional options. Choose the desired operation and click Next.
- If you chose Expand, it will show you the current size and give you a New Size field to fill in. It will display the maximum possible size for this VHD/X’s file type. All values are in GB, so you can only change in GB increments (use PowerShell if that’s not acceptable).
If you chose Shrink (VHDX only), it will show you the current size and give you a New Size field to fill in. It will display the minimum possible size for this file, based on the contents. All values are in GB, so you can only change in GB increments (use PowerShell if that’s not acceptable).
- Enter the desired size and click Next.
- The wizard will show a summary screen. Review it to ensure accuracy. Click Finish when ready.
The wizard will show a progress bar. That might happen so briefly that you don’t see it, or it may take some time. The variance will depend on what you selected and the speed of your hardware. Growing fixed disks will take some time; shrinking disks usually happens almost instantaneously. Assuming that all is well, you’ll be quietly returned to the screen that you started on.
This change only affects the virtual hard disk’s size. It does not affect the contained file system(s). We will cover that in the next sections.
Following Up After a Virtual Hard Disk Resize Operation
When you grow a virtual hard disk, only the disk’s parameters change. Nothing happens to the file system(s) inside the VHD/X. For a growth operation, you’ll need to perform some additional action. For a Windows guest, that typically means using Disk Management to extend a partition:
Note: You might need to use the Rescan Disks operation on the Action menu to see the added space.
Of course, you could also create a new partition (or partitions) if you prefer.
Linux distributions have a wide variety of file systems with their own requirements for partitions and sizing. They also have a plenitude of tools to perform the necessary tasks. Perform an Internet search for your distribution and file system.
VHDX Shrink Operations
As previously mentioned, you can’t shrink a VHDX without making changes to the contained file system first. Review our separate article for steps.
What About VHD/VHDX Compact Operations?
I often see confusion between shrinking a VHD/X and compacting a VHD/X. These operations are unrelated. When we talk about resizing, then the proper term for reducing the size of a virtual hard disk is “shrink”. That changes the total allocated space of the contained partitions. “Compact” refers to removing the zeroed blocks of a dynamically expanding VHD/VHDX so that it consumes less space on physical storage. Compact makes no changes to the contained data or partitions. We have an article on compacting VHD/Xs that contain Microsoft file systems and another for compacting VHD/Xs with Linux file systems.
Note: this page was originally published in January 2018 and has been updated to be relevant as of December 2019.
Go to Original Article
Author: Eric Siron