Site icon UnixArena

Linux KVM – How to add /Remove Memory to Guest on Fly ? Part 8

Memory Management on KVM Guest

Memory Management on KVM Guest

If the application/DB is demanding more  Memory, you need to adjust the VM’s memory limit accordingly. KVM supports dynamic memory addition when you have configured the VM’s with Maximum memory limits. There are two parts to the VM configuration. 1. Maximum Limits 2. current allocation. At any point in time, you can’t exceed the maximum memory limit using “virsh setmem” command on the fly. You need to shut down the guest to perform VM Maximum Memory limit adjustment.

Identify the VM’s Memory Limit and current Memory:

[root@UA-HA ~]# virsh dumpxml  UAKVM2|grep -i memo
VM Configured Memory settings – KVM
[root@UA-HA ~]#

As per the above command out,

Allocated memory to VM: 1GB  (Current Memory Unit)
Maximum Memory Limit: 2.5GB  (Memory Unit)
(Using “virsh setmem” command, we can increase the allocated memory to 3GB on the fly but not more than that.)

Note: VM can only see the “currentMemory Unit” value.

Resize RAM/Memory on Running VM: 

1. Login to the KVM guest and see the currently allocated & used memory.

[root@UA-KVM1 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1001          78         780           6         141         787
Swap:           411           0         411
[root@UA-KVM1 ~]#

2.Login to the KVM host as root.

3.List the KVM guest and see the domain configuration.

[root@UA-HA ~]# virsh dominfo UAKVM2
Id:             34
Name:           UAKVM2
UUID:           6013be3b-08f9-4827-83fb-390bd5a86de6
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       318.4s
Max memory:     2621440 KiB
Used memory:    1048576 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c361,c741 (permissive)

[root@UA-HA ~]#

4.Reduce the memory to 512MB.

[root@UA-HA ~]# virsh setmem UAKVM2 512M

[root@UA-HA ~]# virsh dominfo UAKVM2
Id:             34
Name:           UAKVM2
UUID:           6013be3b-08f9-4827-83fb-390bd5a86de6
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       320.2s
Max memory:     2621440 KiB
Used memory:    524288 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c361,c741 (permissive)

[root@UA-HA ~]#

Check in the KVM guest,

[root@UA-KVM1 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            479          78         259           6         141         266
Swap:           411           0         411
[root@UA-KVM1 ~]#

Same way you can increase the virtual machine memory.

[root@UA-HA ~]# virsh setmem UAKVM2 2048M

[root@UA-HA ~]# virsh dominfo UAKVM2
Id:             36
Name:           UAKVM2
UUID:           6013be3b-08f9-4827-83fb-390bd5a86de6
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       56.0s
Max memory:     2621440 KiB
Used memory:    2097152 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c305,c827 (permissive)

[root@UA-HA ~]#

5. You must edit the VM configuration to preserve the memory settings across the VM power OFF/ ON .

Method:1  – Use config option

[root@UA-HA ~]# virsh setmem UAKVM2 2048M --config

Method:2 – Use virsh edit to update XML file.

[root@UA-HA ~]# virsh edit UAKVM2
Edit VM Memory – KVM
Save & Exit. (:wq)
Domain UAKVM2 XML configuration edited.
[root@UA-HA ~]#

I have modified the “CurrentMemory” from “1048576KB” to “2621440KB” .

How to Increase the Memory Limit for VM ?

When you tried to cross the configured VM memory limit, you will get an error like below. (error: invalid argument: cannot set memory higher than max memory)

Note: This is not bug.

[root@UA-HA ~]# virsh setmem UAKVM2 4G
error: invalid argument: cannot set memory higher than max memory
[root@UA-HA ~]#

“virsh setmem” command can work within the configured memory limit and it can’t cross the border.

1. To increase the memory limit, you must stop the VM.

[root@UA-HA ~]# virsh shutdown UAKVM2
[root@UA-HA ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     UAKVM2                         shut off

[root@UA-HA ~]#

2. Increase the VM memory limit to the desired size. (Note: We are just setting the maximum limit to the VM.)

[root@UA-HA ~]# virsh setmaxmem UAKVM2 4G

3.Start the VM .

[root@UA-HA ~]# virsh start UAKVM2
[root@UA-HA ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 37    UAKVM2                         running

[root@UA-HA ~]#

4. Login to the KVM guest “UAKVM2” and check current memory size.

[root@UA-KVM1 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            719          90         502           8         126         473
Swap:           411           0         411
[root@UA-KVM1 ~]#

5. Let’s increase the allocated memory to 3GB.

[root@UA-HA ~]# virsh setmem UAKVM2 3G

[root@UA-HA ~]#

6. Verify the allocated memory from guest.

[root@UA-KVM1 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           2767          90        2544           8         132        2518
Swap:           411           0         411
[root@UA-KVM1 ~]#

7. Update the VM config.

Method:1  – Use config option

[root@UA-HA ~]# virsh setmem UAKVM2 3G --config

Method:2 – Edit the VM XML file to update the new currentMemmory value to 3GB .

[root@UA-HA ~]# virsh edit UAKVM2
Edit VM config – KVM

We have successfully increased the allocated memory and Maximum memory limit for KVM guests.

Hope this article is informative to you. Share it ! Comment it !! Be Sociable !!!

Exit mobile version