HowTo Add Memory in KVM Virtual Machine
In Linux, we have kernel virtual machine (KVM )tool which provide very nice features to create virtual machine for various test and production work, it also has many advanced features which make it quit reliable and durable in comparison of any other Virtual machine tool. In this post we will see how can we increase memory of any KVM virtual machine through its command-line tool virsh.
For this setup, we are using Ubuntu 18.04 latest version as of Feb, 2019.
~ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 18.04.1 LTS Release: 18.04 Codename: bionic ~ kvm --version QEMU emulator version 2.11.1(Debian 1:2.11+dfsg-1ubuntu7.9) Copyright (c) 2003-2017 Fabrice Bellard and the QEMU Project developers ~ virsh --version 4.0.0
Increase of Virtual Machine memory
So let’s start to see what Virtual Machine we have how much memory allocate to it so that we could increase it and analyze how to make it.
~ virsh list Id Name State ---------------------------------------------------- 7 CentOS6_1 running
Although it’s quit easy to identify memory information in any Machine, but let’s see in Linux Virtual machine.
[root@CentOS6_1 ~]# free -m total used free shared buffers cached Mem: 995 90 905 0 5 30
In Linux machine, we have free command to check memory, it’s easily available to any linux distribution and also help on internet, There is another way to find memory through KVM tools virsh.
~ virsh dominfo CentOS6_1| grep mem Max memory: 1048576 KiB Used memory: 1048576 KiB
So with both above commands we know how much memory assigned to virtual machine.Currently 1GB of memory assigned to Virtual Machine named CentOS6_1.
Now let’s see how we could increase virtual machine memory. For increasing memory in Virtual machine, we should shutdown Virtual machine first.in case we try to increase memory on running machine, it will through error like below.
~ virsh setmaxmem CentOS6_1 --size 5253125 --live error: Unable to change MaxMemorySize error: Requested operation is not valid: cannot resize the maximum memory on an active domain ~ virsh setmaxmem CentOS6_1 --size 5253125 --current error: Unable to change MaxMemorySize error: Requested operation is not valid: cannot resize the maximum memory on an active domain
These two options are available in virsh help setmaxmem, but anyhow its not working.
I suggest to shutdown virtual machine through its on console and wait for proper shutdown. Once it get shutdown, we have to increase both maxmemory and memory.
~ virsh setmaxmem CentOS6_1 --size 4194304 --config ~ virsh setmem CentOS6_1 --size 2097152 --config
In Above commands, we can see we changed memory assigned to Virtual machine.We can also manage these memory parameters through virsh edit domain-name command and edit configuration file directly, which could sometime risky process as manual error could cause issues while start domain again.
let’s how it effect on machine.
~ virsh dominfo CentOS6_1| grep mem Max memory: 4194304 KiB Used memory: 2097152 KiB
In Virtual machine, we can see 2GB of memory is allocated.
[root@CentOS6_1 ~]# free -m total used free shared buffers cached Mem: 1913 113 1799 0 5 30
But once we already set Max memory more than memory assigned of virtual Machine like max memory assigned is 4G and memory assigned 2G, we can anytime increase it to any limit within Maximum memory assigned.
~ virsh setmem CentOS6_1 --size 3145728 --live ~ virsh dominfo CentOS6_1| grep mem Max memory: 4194304 KiB Used memory: 3145728 KiB
Which also reflect on running Virtual Machine.
So i hope now we discuss enough to understand, how to increase memory in Kernel Virtual Machine (KVM) through virsh command-line tools.