Table of Contents

KVM - Change Memory of a Guest

WARNING: Do not use virsh memtune.

The optional memtune element provides details regarding the memory tunable parameters for the domain. If this is omitted, it defaults to the OS provided defaults. For QEMU/KVM, the parameters are applied to the QEMU process as a whole. Thus, when counting them, one needs to add up guest RAM, guest video RAM, and some memory overhead of QEMU itself. The last piece is hard to determine so one needs guess and try. For each tunable, it is possible to designate which unit the number is in on input, using the same values as for <memory>. For backwards compatibility, output is always in KiB. units since 0.9.11. Possible values for all *_limit parameters are in range from 0 to VIR_DOMAIN_MEMORY_PARAM_UNLIMITED.

See here for more details.


Bring up the config for the guest:

virsh edit VM_ID

Change the memory and currentMemory fields to be the size you want in KiB:

  <memory unit='KiB'>4194304</memory>
  <currentMemory unit='KiB'>4194304</currentMemory>

Restart for the changes to take effect:

virsh shutdown VM_ID
virsh start VM_ID

Use a Script to Resize the Memory

#!/bin/bash
 
VM_ID="my_vm_id"
NEW_AMOUNT="4000"
 
EDITOR='sed -i "s;[0-9]*</currentMemory>;$NEW_AMOUNT</currentMemory>;"' virsh edit $VM_ID
EDITOR='sed -i "s;[0-9]*</memory>;$NEW_AMOUNT</memory>;"' virsh edit $VM_ID
 
virsh shutdown $VM_ID
virsh start $VM_ID

References

https://libvirt.org/formatdomain.html#elementsMemoryTuning