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>
  <memtune>
    <hard_limit unit='KiB'>256000</hard_limit>
  </memtune>
...

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

Question

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'>
  <name>svn.mydomain.com</name>
  <uuid>8463ab11-628a-c07c-4366-4f768247934a</uuid>
  <memory unit='KiB'>512000</memory>
  <currentMemory unit='KiB'>512000</currentMemory>
  <memtune>
    <hard_limit unit='KiB'>256000</hard_limit>
  </memtune>
  <vcpu placement='static'>4</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-trusty'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/bin/kvm-spice</emulator>
    <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>
    <disk type='block' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <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'/>
    </interface>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </memballoon>
  </devices>
</domain>

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 answer on Server Fault.

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