KVM – Changing Memory of Guests Live

Programster asked:

I have a KVM guest that I would like to dynamically change the memory allocation of.

At the moment, to change the memory of the guest, I have to run:

sudo virsh edit $GUEST_DOMAIN

Then change the first line shown in the following section from the config

<memory unit='KiB'>512000</memory>
  <currentMemory unit='KiB'>512000</currentMemory>
    <hard_limit unit='KiB'>256000</hard_limit>

and then restart the guest with virsh shutdown and virsh start.

I have been trying to use virsh memtune --hard-limit 512000 --domain $GUEST_ID (reference) but it appears to have no effect.

I also tried “ballooning” from inside the guest using the docs here, but I keep getting the error
balloon: command not found


Is there a way to dynamically resize the KVM guest with a command from the host. If not, is there at least a way to resize the memory with a command, and not have to edit a config file (or pershaps the virsh file can be edited with sed)?

Extra Info

The guest is using a qcow2 sparse file.

Below is the full config from sudo virsh edit $GUEST

<domain type='kvm'>
  <memory unit='KiB'>512000</memory>
  <currentMemory unit='KiB'>512000</currentMemory>
    <hard_limit unit='KiB'>256000</hard_limit>
  <vcpu placement='static'>4</vcpu>
    <type arch='x86_64' machine='pc-i440fx-trusty'>hvm</type>
    <boot dev='hd'/>
  <clock offset='utc'/>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/media/storage/kvm/vms/svn.mydomain.com.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    <disk type='block' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hdc' bus='ide'/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    <controller type='usb' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    <interface type='bridge'>
      <mac address='52:54:00:a2:4a:f6'/>
      <source bridge='kvmbr0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    <serial type='pty'>
      <target port='0'/>
    <console type='pty'>
      <target type='serial' port='0'/>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>

My answer:

You don’t need to be messing with <memtune><hard_limit>. This is actually a rather dangerous setting when applied to KVM guests. I would remove that section immediately.

However, users of QEMU and KVM are strongly advised not to set this limit as domain may get killed by the kernel if the guess is too low. To determine the memory needed for a process to run is an undecidable problem.

To reduce the amount of memory the KVM guest has access to right now, change <currentMemory>. The maximum the guest can have is specified by <memory>, and changing it requires shutting down the guest.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.