How to Set Hyper-V Storage Quality of Service (QoS) on All Virtual Hard Disks via PowerShell? #HyperV #PowerShell

5 min read

Hello folks,

As we are approaching the holidays, and being busy preparing for the new year! I would like to share a script with you.

Hyper-V Storage QoS!

Killer feature… Microsoft introduced in Windows Server 2012 R2, Hyper-V storage Quality of Service (QoS). Storage QoS enables you to manage storage throughput for virtual hard disks that are accessed by your virtual machines.

Storage QoS enables you to specify the maximum and minimum I/O loads in terms of I/O operations per second (IOPS) for each virtual disk in your virtual machines. Minimum and maximum IOPS are measured in 8 KB increments

Now If you are concerned about a specific virtual hard disk for a particular VM is not receiving sufficient disk I/O, then you can set a minimum IOPS level. Remember that minimum is a soft reserve (best effort) which means it might fail but an event will be logged that things are going wrong. On the other hand, if the virtual hard disk is accommodating a very high I/O then you may want to populate the Maximum IOPS field as a way of limiting the total number of IOPS that the virtual hard disk can consume, and ensure that the storage throughput of one virtual hard disk does not impact the performance of another virtual hard disk on the same Hyper-V host.

For more information on what’s new in Storage QoS in Windows Server 2012 R2, click here.

Best Practices: Hyper-V Storage QoS!

If you identify a VM that is using more resources than you want it to use, then you can go ahead and apply a limit to it right away, this what we call a reactive approach.

To measure a VM, you can use Measure-VM cmdlet but first you must enable VMResourceMetering. The virtual machine metrics infrastructure have been extended with storage related attributes in Windows Server 2012 R2, so you can monitor the performance. To do this Microsoft use what they call “normalized IOPs” where every 8K of data is counted as an I/O.

HV12R2StorageQoS-01

The reactive approach is not really ideal, because you are reacting since you have a problem.

What I recommend doing is to take a proactive approach earlier.

So as you are deploying your virtual machines, you can start applying a default limit to all individual VM’s VHDs and then you can raise based on workload demand.

As best practices, consider tiers based on planned usage as Virtual Machines are deployed as the following:

  • VDI VMs: Max 100 IOPs limit
  • General VMs: Max 500 IOPs limit
  • High Performance VMs: > 500 IOPs limit

How can you limit individual VM’s VHD?

The answer is straight forward, the new Storage QoS feature is available under Advanced Features for each individual virtual hard drive as shown in below figure:

HV12R2StorageQoS-02

Automation: Hyper-V Storage QoS!

Now the challenging question is what if you have hundreds of VMs already deployed and you need to start using the proactive approach?

The answer is simple, open each VM settings and limit the maximum IOPs to 500.

And what if each VM has 4 virtual hard disks attached to it? so 400 VHDs Crying face

The answer is PowerShell of course Winking smile

I created the following tool script that will help you to limit all individual VM’s VHDs to a default IO number you specify.

HV12R2StorageQoS-04

<#
.SYNOPSIS
Set Storage Quality of Service for all Virtual Machines.

.DESCRIPTION
Set Minimum and Maximum Storage Quality of Service for all Virtual Machines.
.NOTES
File Name : Set-VMStorageIO.ps1
Author    : Charbel Nemnom
Version   : 1.1
Requires  : PowerShell Version 4.0 or above
OS        : Windows Server 2012 R2 Hyper-V or Free Hyper-V Server 2012 R2

.LINK
To provide feedback or for further assistance please visit:
https://charbelnemnom.com

.PARAMETER HVServerName
The name of a single Hyper-V Server
.PARAMETER Disable
Switch to disable Storage QoS
.PARAMETER MinMax
The storage QoS to be set - Maximum, Minimum or Both
.PARAMETER StorageMinIO
The Storage minimum limit IO number to be set
.PARAMETER StorageMaxIO
The Storage maximum limit IO number to be set

.EXAMPLE
.\Set-VMStorageIO -HVServerName <HyperVServerName> -MinMax <Maximum> -StorageMaxIO <500>
This example will set the maximum Storage QoS for all virtual machines to 500.
.EXAMPLE
.\Set-VMStorageIO -HVServerName <HyperVServerName> -MinMax <Minimum> -StorageMinIO <250>
This example will set the minimum Storage QoS for all virtual machines to 250.
.EXAMPLE
.\Set-VMStorageIO -HVServerName <HyperVServerName> -MinMax <Both> -StorageMinIO <250> -StorageMaxIO <1000>
This example will set the Storage QoS for minimum 250 IOPS and maximum 1000 IOPS for all virtual machines.
.EXAMPLE
.\Set-VMStorageIO -HVServerName <HyperVServerName> -Disable
This example will disable the Storage QoS for all virtual machines.
#>


[CmdletBinding()]
param (
	[Parameter(Mandatory = $true, Position = 0, HelpMessage = 'Hyper-V Server Name')]
	[Alias('Hyper-V Server')]
	[String]$HVServerName,
	
	[Parameter(HelpMessage = 'Disable Storage QoS', ParameterSetName = "Disable")]
	[Switch]$Disable,
	
	[Parameter(Mandatory = $true, HelpMessage = 'Maximun, Minimum or Both', ParameterSetName = "Enable")]
	[ValidateSet("Maximum", "Minimum", "Both")]
	[String]$MinMax = "Maximum",
	
	[Parameter(HelpMessage = 'Storage Minimum IO', ParameterSetName = "Enable")]
	[Int]$StorageMinIO,
	
	[Parameter(HelpMessage = 'Storage Maximum IO', ParameterSetName = "Enable")]
	[Int]$StorageMaxIO
)

# If Disable switch equal to true, disable Storage QoS on all VMs
If ($Disable) {
	$VMIOPs = Get-VM -ComputerName $HVServerName | Get-VMHardDiskDrive
	foreach ($VM in $VMIOPs) {
		Set-VMHardDiskDrive -ComputerName $HVServerName -VMName $VM.VMName -ControllerType $VM.ControllerType -ControllerNumber $VM.ControllerNumber -ControllerLocation $VM.ControllerLocation -MinimumIOPS $false -MaximumIOPS $false
	}	
} else {
	
	If ($StorageMinIO -eq '0' -and $StorageMaxIO -eq '0') {
		Write-Error "Minimum and Maximum IOPs cannot both be set to Zero!"
		Break
	}
	
	If ($StorageMinIO -gt $StorageMaxIO) {
		Write-Error "The maximum IOPS must be equal to or greater than the minimum IOPS or set to zero to accept the system default value!"
		break
	}
	
	
	$VMIOPs = Get-VM -ComputerName $HVServerName | Get-VMHardDiskDrive
	
	If ($MinMax -eq "Maximum") {
		foreach ($VM in $VMIOPs) {
			Set-VMHardDiskDrive -ComputerName $HVServerName -VMName $VM.VMName -ControllerType $VM.ControllerType -ControllerNumber $VM.ControllerNumber -ControllerLocation $VM.ControllerLocation -MaximumIOPS $StorageMaxIO
		}
	} elseif ($MinMax -eq "Minimum") {
		foreach ($VM in $VMIOPs) {
			Set-VMHardDiskDrive -ComputerName $HVServerName -VMName $VM.VMName -ControllerType $VM.ControllerType -ControllerNumber $VM.ControllerNumber -ControllerLocation $VM.ControllerLocation -MinimumIOPS $StorageMinIO
		}
	} else {
		foreach ($VM in $VMIOPs) {
			Set-VMHardDiskDrive -ComputerName $HVServerName -VMName $VM.VMName -ControllerType $VM.ControllerType -ControllerNumber $VM.ControllerNumber -ControllerLocation $VM.ControllerLocation -MinimumIOPS $StorageMinIO -MaximumIOPS $StorageMaxIO
		}	
	}
}
# End

Here you so go Hyper-V storage QoS Maximum IOPs is set for all virtual machines Smile, I feel it’s a much easier than having to set the limit individually for each VM’s VHD.

HV12R2StorageQoS-05

If you have more scenarios, please feel free to leave a comment below:

Demo: Hyper-V Storage QoS!

Would you like to see Hyper-V Storage QoS in action? Then make sure to watch this short demo:

Hyper-V Storage QoS In Action!

And now It’s time to sign-off and prepare for the holidays… Airplane 

HV12R2StorageQoS-03

Until then… See you in 2015!

Enjoy the holidays… Party smile

Cheers,
/Charbel

About Charbel Nemnom 525 Articles
Charbel Nemnom is a Cloud Architect, ICT Security Expert and Microsoft Most Valuable Professional (MVP), totally fan of the latest's IT platform solutions, accomplished hands-on technical professional with over 17 years of broad IT Infrastructure experience serving on and guiding technical teams to optimize the performance of mission-critical enterprise systems. Excellent communicator is adept at identifying business needs and bridging the gap between functional groups and technology to foster targeted and innovative IT project development. Well respected by peers through demonstrating passion for technology and performance improvement. Extensive practical knowledge of complex systems builds, network design, business continuity, and cloud security.

Be the first to comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.