Today, I’ll talk about a thing that any sysadmin running Hyper-V VMs does (or still dreams about) while managing infrastructure resources: hot modifying assigned to VM memory amount. I’ll discuss not only the feature itself but also how it works on different OS and its impact on the environment stability.
All of us keep an eye on resource consumption within our environments. If a VM needs extra RAM to have the job done, we provide it with some, right? And, we usually run many VMs on our servers each with own purpose and configuration. That’s, actually, why changing the amount of assigned to a VM memory without rebooting it may come in handy. Also, many guys run some parts of their environments on Windows while having other parts run on something from Linux family. Looks pretty hectic in terms of management, doesn’t it?
To make resource management not that annoying, Microsoft came up with the ability to assign more/less RAM to Hyper-V VMs on the run. This option has been introduced together with other cool Hyper-V features in September 2017. To find out more, look through the following Microsoft’s doc: https://docs.microsoft.com/en-us/windows-server/virtualization/hyper-v/what-s-new-in-hyper-v-on-windows.
Regarding how handy that option is for me, I’ll discuss it today. In particular, I’ll talk about how to hot change VM memory on Windows and Linux family OS and how doing this impacts infrastructure stability. For that purpose, I’m gonna run a series of tests and experiments to:
- Check the feature’s availability and how it affects the VM OS operation on various operating systems.
- Check the feature’s availability on various VM generations for different OS
- Check the GUI and PowerShell settings availability for various guest OS
First, let’s check whether this feature is available at all and if it affects VM OS stability
Initially, I want to say a couple of words about stuff I do here.
I carried out tests on generation 1 and generation 2 VMs. In total, I created 8 machines of each generation with exactly same initial configurations:
- 2 x Intel(R) Xeon(R) CPU E5-2609 @ 2.40GHz;
- 1 x 4 GB RAM;
- 1 x 30 GB HDD;
- 1 x IDE controller;
- 1 x 1 Gb/s LAN.
Next, I installed 8 OS listed below on each VM generation.
- CentOS 7.4.1708 (Core);
- Debian 9.4.0 (stretch);
- FreeBSD 11.1 Release;
- Suse Linux Enterprise Server 12 SP3;
- Ubuntu Server 16.04.4 LTS;
- Windows 10 (Version 10.0.14393);
- Windows Server 2012 R2 (Version 6.3.9600);
- Windows Server 2016 (Version 10.0.14393).
During this research, I’m gonna run four tests on each VM with changing memory amount. Therefore, I’ll increase RAM from 4 GB to 6 GB first and reduce it to 2 GB afterward. The amount of RAM assigned to each VM will be changed using both GUI and PowerShell. To check out whether the changes were applied I’ll use Task Manager for Windows-powered machines, and the top utility, for the Linux-powered ones. All the data I collected through the study were put in the pivot table at the end of the article.
Note that while configuring VM RAM, the Dynamic Memory option should be disabled. If it is enabled for some reason, get rid of the appropriate flag in settings. Also, while reducing VM RAM, ensure that the new amount of given memory corresponds the minimum OS requirements. For exact numbers, check out official info on that matter. Here, I assigned 4 GB for each VM, which is more than enough to install all OS on both VM generations.
Now, let’s have the job done!
Looking at Linux family
Note that Linux-powered generation 2 Hyper-V VMs do not support the Secure Boot feature so you have to disable it or execute the following PowerShell command on the hypervisor to enable Microsoft UEFI Certificate Authority :
Set-VMFirmware <VM NAME> -SecureBootTemplate MicrosoftUEFICertificateAuthority
Further, select Microsoft UEFI Certificate Authority as a Template option in the VM settings.
Just out of my curiosity I’ve checked how Hyper-V behaves while hot adding/removing VM memory on the operating systems that are claimed not to support this feature. As one could expect, doing this in FreeBSD 11.1 and Windows Server 2012 R2 environments ends up with errors. Regarding this fact I did not perform any testing on these OS here at all.
The output in the screenshot below shows how the error looks like in when using PowerShell:
In GUI, it looks like this:
While testing, I found out that Linux family OS partially support the discussed feature. You can hot add more memory, but you cannot reduce it that easily. The subtracted portion of RAM isn’t freed until rebooting even though it is not used anymore. In other words, you should reboot the system after assigning less RAM to the VM, and then the amount of given memory is displayed right. This can be explained by how Linux family treats RAM. Furthermore, if you are out of luck, and an OS has already utilized some of the given memory with apps or system files, reducing it may cause an error.
In some cases, if you are lucky enough, there will be no error while assigning less RAM. Furthermore, Hyper-V Manager may even display everything right. Yet, top utility breaks that illusion for Linux family showing that the amount of memory remains the same until rebooting. However, this situation did not affect the OS operation inside the VMs at all.
In the screenshots below, I provide the top outputs. The first one shows how the initial amount of memory looked like:
Now, that’s what top showed after increasing the overall amount of memory up to 6 GB.
And, eventually, that’s how things looked like after reducing memory amount to 2 GB and rebooting the VM.
What about Windows?
Things look better on Windows. At least, you do not need to reboot everything to have the changes applied! After playing around the RAM amount on the run, changes are displayed properly without rebooting. The plot below traces all memory changes I made. I took this screenshot after reducing the memory to 2 GB.
Checking out how does the feature work on both VM generations
According to Microsoft, hot changing the given memory amount works on both VM generations without any restrictions. Note that Hyper-V VMs may also have different configuration versions. Be honest, I did not study here whether configuration versions impact system stability or the ability to hot change RAM since I run VMs only with the default configuration version 8.0.
Can I hot change the given memory amount both with GUI and PowerShell on all OS?
Initially, let’s do all that magic with Hyper-V Manager GUI. After modifying the amount of memory given to a VM, the recently assigned value is displayed in the console. Looks good!
To have things done with PowerShell, execute the following commands:
- Set-VMMemory –VMName <VM Name> -StartupBytes 6gb
- Get-VMMemory -VMName.
The former enables to change the amount of RAM, while the later allows verifying whether the change has been applied:
All VMs were reconfigured smoothly. Both ways that I’ve provided above are similar, but I prefer PowerShell since it provides a room for automatization.
Now, here’s the pivot table I mentioned above. As one may assume, the + sign refers to the successful attempts, and the – sign addresses unsuccessful ones. Also, I provide some notes in the self-titled column to understand better how the studied operation systems work.
After carrying out all those measurements, here’re the things that I wanna to share with you:
- Hot RAM modifying that was first announced in Hyper-V 2016 does work on the operating systems that officially support it. But, there’s a thing about Linux: you need to reboot a VM after reducing RAM. Adding RAM – no problem.
- Hot memory changing is available for both Hyper-V VMs generations (configuration version 8.0).
- You can hot add/reduce VM memory amount using both PowerShell and GUI. Everything works nice. Note that PowerShell provides you a room for automation.
- After hot modifying the memory amount, the system remains stable.
Taking all these things together, changing RAM on the run is a handy feature for administrating VMs. It won’t impede infrastructure stability. Thus, use it whenever needed!