To perform hot resizing operations on a KVM virtual machine, the virtual machine must be initially configured with parameters that define its maximum capacity. This guide demonstrates how to dynamically adjust disk, network, memory, and CPU resources without shuting down the guest operating system.
Disk Hot Resizing
1.1 Hot-Adding a New Disk
First, create a new disk image file. Ensure the physical hardware supports hot-plugging if this is for a production environment.
[root@host ~]# qemu-img create -f qcow2 /data/images/ubuntu-vm_add_disk.qcow2 15G
Formatting '/data/images/ubuntu-vm_add_disk.qcow2', fmt=qcow2 size=16106127360 cluster_size=65536 lazy_refcounts=off refcount_bits=16
Next, attach this disk to the running virtual machine. The --subdriver option is crucial for specifying the correct disk format, and --config makes the change persistent across reboots.
[root@host ~]# virsh attach-disk --domain ubuntu-vm --source /data/images/ubuntu-vm_add_disk.qcow2 --target vdb --targetbus virtio --subdriver qcow2 --config
Disk attachment successful.
1.2 Hot-Removing a Disk
To remove a disk, use the detach-disk command. Without the --config flag, the removal is temporary and the disk will reappear on the next boot.
[root@host ~]# virsh detach-disk --domain ubuntu-vm --target vdb --config
Disk detachment successful.
1.3 Expanding an Existing Disk
The process for expanding a disk invovles detaching it, resizing the image file, reattaching it, and then resizing the filesystem within the guest.
-
Detach the disk from the virtual machine.
[root@host ~]# virsh detach-disk --domain ubuntu-vm --target vdb Disk detached successfully. -
Resize the disk image file on the host. You can specify an absolute size or a relative increase.
[root@host ~]# qemu-img resize /data/images/ubuntu-vm_disk.img +15G Image resized. -
Re-attach the resized disk.
[root@host ~]# virsh attach-disk --domain ubuntu-vm --source /data/images/ubuntu-vm_disk.img --target vdb --targetbus virtio --subdriver qcow2 Disk attachment successful. -
Resize the filesystem inside the guest OS. For XFS, use
xfs_growfs. For ext4, useresize2fs. If the partition table is not automatically updated, thegrowpartutility may be required first.# On the guest VM [root@guest ~]# xfs_growfs /data meta-data=/dev/vdb1 isize=512 agcount=4, agsize=1310656 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 data = bsize=4096 blocks=5242624, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0, ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 5242624 to 10485499
Network Interface Hot Resizing
2.1 Adding a Network Interface
You can add a new network interface to a running VM. This can be a bridge or a virtual network.
# Attach to a bridge interface
[root@host ~]# virsh attach-interface --domain ubuntu-vm --type bridge --source br1 --model virtio --config
Interface attachment successful.
# Attach to the default virtual network
[root@host ~]# virsh attach-interface --type network --domain ubuntu-vm --source default --config
Interface attachment successful.
2.2 Removing a Network Interface
To remove an interface, you must specify its MAC address. The --config flag determines if the change is permanent.
[root@host ~]# virsh detach-interface --domain ubuntu-vm --mac 52:54:00:11:22:33 --type bridge --config
Interface detached successfully.
Memory Hot Resizing
Memory can be increased or decreased while the VM is running. The VM must have been created with a currentMemory value lower than its memory value to allow for expansion.
# Increase memory to 2GB (live and persistent)
[root@host ~]# virsh setmem --domain ubuntu-vm --size 2048M --live --config
# Decrease memory to 512MB (live only)
[root@host ~]# virsh setmem --domain ubuntu-vm --size 512M --live
CPU Hot Resizing
CPU cores can be added to a running VM, provided it was configured with a maxvcpus value higher than its initial vcpus count. CPUs cannot be removed once added.
# Increase the number of vCPUs to 4 (live and persistent)
[root@host ~]# virsh setvcpus --domain ubuntu-vm 4 --live --config