When working with KVM (Kernel-based Virtual Machine), administrators often need to adjust resource allocation for running VMs. Unlike some other hypervisors, KVM allows dynamic modification of both memory and CPU resources without requiring a full VM restart in many cases.
Before proceeding, ensure:
1. Your KVM/libvirt environment is properly configured
2. The VM uses QEMU/KVM virtualization
3. You have appropriate permissions (typically root or via sudo)
4. The host has sufficient available resources
For active VMs using libvirt, the virsh command-line tool provides direct control. To increase vCPUs:
virsh setvcpus --domain vm_name --count 4 --live
To make the change persistent across reboots:
virsh setvcpus --domain vm_name --count 4 --config
Memory adjustment requires slightly different handling. For a running VM:
virsh setmem vm_name 8192 --live
To configure the permanent memory setting:
virsh setmaxmem vm_name 16384 --config virsh setmem vm_name 8192 --config
Note: The "setmaxmem" command defines the upper limit before setting the current allocation.
For more complex changes, directly edit the VM's XML configuration:
virsh edit vm_name
Locate and modify these sections:
<memory unit='KiB'>8388608</memory> <currentMemory unit='KiB'>4194304</currentMemory> <vcpu placement='static'>4</vcpu>
- Some guest OSes require proper acpi configuration for hot-plugging CPUs
- Memory balloon driver must be installed in the guest for dynamic memory
- NUMA configurations may require additional parameters
- Always monitor qemu process memory usage after changes
For batch operations, combine virsh commands with shell scripts:
#!/bin/bash VM_LIST="web1 db2 app3" for vm in $VM_LIST; do virsh setvcpus $vm 2 --live virsh setmem $vm 4096 --live done
If changes don't take effect:
- Verify guest agent is running (for memory ballooning)
- Check dmesg and /var/log/libvirt/qemu logs
- Confirm the hypervisor supports the requested configuration
- Some older VM definitions may need conversion to newer XML formats
Before modifying resources, ensure:
- The VM is shut down (for certain changes)
- Libvirt tools are installed (
sudo apt-get install libvirt-clients
) - You have sudo privileges
- Host has sufficient available resources
# View current configuration
virsh dumpxml vm_name | grep -E "memory|vcpu"
# Edit VM configuration
virsh edit vm_name
Locate these sections in the XML:
<memory unit='KiB'>2097152</memory> <!-- 2GB RAM -->
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'>2</vcpu> <!-- 2 CPU cores -->
# Hot-add CPUs (requires guest OS support)
virsh setvcpu vm_name 4 --live --config
# Verify changes
virsh vcpucount vm_name
# Set memory to 4GB (4096MB)
virsh setmem vm_name 4096 --live --config
# Check new allocation
virsh dommemstat vm_name
- For Windows VMs, install virtio drivers before hot-plug
- Some guest OSes require manual CPU/RAM recognition
- Maximum memory cannot exceed original allocation without shutdown
- Use
virsh dominfo vm_name
to verify changes
#!/bin/bash
VM_NAME="ubuntu-server"
NEW_CPUS=4
NEW_MEM_GB=8
virsh setvcpu "$VM_NAME" "$NEW_CPUS" --config
virsh setmem "$VM_NAME" $((NEW_MEM_GB*1024)) --config
echo "Resources updated: $NEW_CPUS vCPUs, ${NEW_MEM_GB}GB RAM"