We’ve started to look into memory ballooning on RHEL and KVM. I thought I’d document some of my findings here.
The concept is simple. You have your RHEL host, running KVM, that host runs a number of virtual machine guests. The guests can be anything that runs on the chip archetecture that you’re emulating. In my case, x86_64 and i386. We run some Windows guests, and mostly RHEL. In my testing, i’m using a RHEL guest. I’ll test this on Windows at some point, and see what happens.
Guest Configuration
You’ll need to specify the following in your quests configuration. In my case, I was working with an already created guest, not a new install. So i already had the disk image, and the config xml in /etc/livbirt/qemu/.
There are two key fields to making this work. First is memory, and second is currentMemory. Memory should be set to the maximum amount of memory that you’d like this VM to utilize. currentMemory should be set to the amount that you’d like the VM to use at boot. In my test VM, i’m using the following:
<memory>1048576</memory>
<currentMemory>524288</currentMemory>
This gives the VM roughtly 1gb of memory to balloon to, and starts it out with half of that.
If you’re modifying a that already exists, you’ll need to shut it down, re-register its config, and then start it back up.
[root@steel ~]# vim /etc/libvirt/qemu/rhel6-demo.xml
[root@steel ~]# virsh shutdown rhel6-demo
Domain rhel6-demo is being shutdown[root@steel ~]# virsh define /etc/libvirt/qemu/rhel6-demo.xml
Domain rhel6-demo defined from /etc/libvirt/qemu/rhel6-demo.xml[root@steel ~]# virsh start rhel6-demo
How to Balloon
The act of ballooning is easy once you have that config in place.
Notice that free show’s roughly 500MB of total memory.
[root@rhel6-demo ~]# free
total used free shared buffers cached
Mem: 495280 137820 357460 0 11820 44412
-/+ buffers/cache: 81588 413692
Swap: 2097144 0 2097144
Now, we use virsh on the host to up that.
[root@steel ~]# virsh setmem rhel6-demo 1024000
Now, we check free again on the guest.
root@rhel6-demo ~]# free
total used free shared buffers cached
Mem: 994992 138060 856932 0 11844 44412
-/+ buffers/cache: 81804 913188
Swap: 2097144 0 2097144
It goes both ways, we can lower it again later.
[root@steel ~]# virsh setmem rhel6-demo 768000
And here it is on the guest.
[root@rhel6-demo ~]# free
total used free shared buffers cached
Mem: 738992 137608 601384 0 11848 44412
-/+ buffers/cache: 81348 657644
Swap: 2097144 0 2097144
How useful is this?
This information, alone, is only so useful. It shows how to balloon, but not necessarily how to apply it. You can manually balloon with the information here, but you’ll need to make it automated, somehow, in order to make this happen on an as needed basis. I will one day do this, and probably blog about it.