Andrew Mercer
on this page

Change KVM VM resources

Part of the KVM overview. Adjust memory and vCPUs, and attach or detach disks.

Memory

Shut the guest down first for changes to the maximum.

virsh setmaxmem vm01 12G --config
virsh setmem vm01 8G --config
virsh start vm01
virsh dominfo vm01 | grep -i memory

With virsh edit, the equivalent is <memory> and <currentMemory> (values in KiB by default; 8 GiB is 8388608).

vCPUs

virsh setvcpus vm01 4 --maximum --config
virsh setvcpus vm01 4 --config

Add or remove a disk

qemu-img create -f qcow2 /var/lib/libvirt/images/vm01-data.qcow2 10G
virsh attach-disk vm01 --source /var/lib/libvirt/images/vm01-data.qcow2 \
  --target vdb --driver qemu --subdriver qcow2 --persistent
virsh detach-disk vm01 vdb --persistent

Always pass --subdriver qcow2 for qcow2 images. Without it libvirt attaches the file as raw and the guest sees the qcow2 headers as data.