- Published on
HW6. KVM Lab and Live VM Migration
- Authors

- Name
- seren-wib
Contents
- 0. Lab Environment
- 1. KVM Lab
- Installing KVM
- Installing a Rocky VM on KVM
- Common virsh Commands
- Cloning with KVM
- 2. Live VM Migration
- Installing Virtualization Packages and Cloning
- Configuring the VMs
- Creating a KVM VM and Live Migration
- 3. Exercises
- Ch11-5. hellocloud.c and the Virtual Memory Layout
- Ch11-9. Memory Ballooning
This homework follows the KVM labs in chapters 11 and 12 of the textbook. The first half is the basic lab: install KVM, create a Rocky Linux VM, manage it with virsh, and clone it. The second half is live migration: build two Rocky VMs that act as KVM hosts, then move a CirrOS VM running inside one of them to the other without stopping it.
0. Lab Environment
I used the same Ubuntu host as since HW2 (Ubuntu 24.04 aarch64 on UTM on an Apple M3 Mac). Because the host is aarch64, I used the aarch64 builds of the Rocky Linux ISO and the CirrOS image, and I noted under each figure where commands differ from the slides.
For live migration, instead of two VirtualBox VMs as in the slides, I created two Rocky VMs on the Ubuntu host's KVM. Each of them becomes a KVM host, and CirrOS is a VM nested one level further inside.
Ubuntu host (KVM)
├─ servera Rocky 10.2 192.168.56.101 ── CirrOS ──┐ virsh migrate --live
└─ serverb Rocky 10.2 192.168.56.102 ◀───────────┘
1. KVM Lab
Installing KVM
First I checked with kvm-ok from cpu-checker that /dev/kvm exists and KVM acceleration is available. Nested virtualization is enabled in UTM, so KVM works inside the Ubuntu host too.

kvm-ok after installing cpu-checker — /dev/kvm exists and KVM acceleration can be used
sudo apt install -y qemu-kvm qemu-utils libvirt-daemon-system libvirt-clients bridge-utils virt-manager virtinst
sudo systemctl enable --now libvirtd
systemctl status libvirtd --no-pager | head -5

Installing the KVM packages, enabling libvirtd and checking its status

Creating the image directory VM/instances, preparing the Rocky ISO, checking the virbr0 bridge with nmcli c
To let a regular user use the bridge network, I put allow all in /etc/qemu/bridge.conf and set the setuid bit on qemu-bridge-helper. The libvirt connection URI was set to the user session (qemu:///session).
sudo mkdir -p /etc/qemu; echo "allow all" | sudo tee /etc/qemu/bridge.conf
sudo chmod u+s /usr/lib/qemu/qemu-bridge-helper
export LIBVIRT_DEFAULT_URI=qemu:///session
sudo systemctl restart libvirtd

/etc/qemu/bridge.conf, setuid on qemu-bridge-helper, LIBVIRT_DEFAULT_URI=qemu:///session, and restarting libvirtd
Installing a Rocky VM on KVM
virt-install --name rocky10-1 --ram 3072 --disk path=VM/instances/rocky10-1.img,size=20 \
--vcpus 2 --cpu host-passthrough --network bridge=virbr0 --graphics none \
--console pty,target_type=serial --location Rocky-10.2-aarch64-minimal.iso \
--extra-args "inst.text console=ttyAMA0,115200 pci=nomsi" \
--boot loader=/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd,\
loader.readonly=yes,loader.type=pflash,loader.secure=no,\
nvram.template=/usr/share/AAVMF/AAVMF_VARS.fd \
--osinfo detect=on,require=off
Compared with the command on slide 8, four things changed because of aarch64.
- The ISO is the aarch64 build.
- The serial console is
ttyAMA0instead ofttyS0. - MSI interrupts are not delivered under nested virtualization, so I added
pci=nomsi. - The default UEFI firmware (Secure Boot) won't run the installed GRUB, so I specified firmware without Secure Boot with
--boot.
The last two are settings I found in HW2 while fixing the Rocky boot hang under nested virtualization. I split the --boot value over three lines with \ so the paths don't break in the middle. The continuation lines are not indented, because the shell would otherwise split the option value at the spaces.

Installer start — choosing 2) Use text mode

Installation summary after setting 5) Installation Destination — automatic partitioning (Automatic partitioning selected), Root password·User creation still [!]

After confirming the password (weak password → yes), 9) User creation — administrator account test, group wheel

Installation summary with every item at [x], starting the install with b

Installation complete

Rocky Linux 10.2 login (test) after reboot, leaving the console with Ctrl+]

ip -br addr on the host — the virtual bridge virbr0 is 192.168.122.1/24 (host IPv6 addresses masked)
Common virsh Commands
virsh list --all
virsh shutdown rocky10-1
virsh start rocky10-1
virsh reboot rocky10-1
virsh destroy rocky10-1
virsh console rocky10-1
shutdown asks the guest OS to shut down cleanly, while destroy cuts the power immediately. Despite its name, destroy does not delete the definition or the disk.

virsh list --all, virsh shutdown (shut off confirmed), virsh start

virsh reboot, virsh destroy (forced power off)

Connecting with virsh console and logging in
Cloning with KVM
Cloning is done while the original is shut off. virt-clone copies the disk image and the UEFI variable file (_VARS.fd) and creates a new definition.
virsh shutdown rocky10-1
virt-clone --original rocky10-1 --name rocky10-2 --file VM/instances/rocky10-2.img
virsh undefine rocky10-2 --nvram

Shutting down rocky10-1 and creating rocky10-2 with virt-clone

virsh undefine rocky10-2 — the definition is removed but the disk image rocky10-2.img remains
2. Live VM Migration
I followed "Creating VMs and setting up the lab environment" (pages 11–38) in the Ch12 slides.
Installing Virtualization Packages and Cloning
On the Rocky VM that serves as the original for servera and serverb (rocky-base), I installed the virtualization packages and turned on libvirtd. The two hosts must reach each other during migration, so in this lab environment I disabled firewalld and set SELinux to Permissive.
dnf install -y qemu-kvm libvirt python3-libvirt virt-install
systemctl enable libvirtd; systemctl start libvirtd
systemctl disable firewalld; systemctl stop firewalld
setenforce 0; getenforce

Confirming qemu-kvm, libvirt, python3-libvirt, virt-install are installed on the original Rocky

libvirtd enable·start·status

Disabling and stopping firewalld, setenforce 0
With the original shut off, I created two clones with virt-clone on the Ubuntu host. This corresponds to the VirtualBox clone in the slides.
virt-clone --original rocky-base --name servera --auto-clone
virt-clone --original rocky-base --name serverb --auto-clone

Cloning the original into servera and serverb with virt-clone (the VirtualBox clone in the slides)
Configuring the VMs
On each VM I checked its address on the second network (the Ubuntu host's virbr2, 192.168.56.0/24), 192.168.56.101 and 192.168.56.102, added both names to /etc/hosts, and changed the hostname. The name from before cloning was still set, so the prompt shows root@main and root@work1 until the rename.
printf "192.168.56.101 servera.cloud.org servera\n192.168.56.102 serverb.cloud.org serverb\n" >> /etc/hosts
hostnamectl set-hostname servera # serverb on serverb

servera — checking IP 192.168.56.101, /etc/hosts, hostname servera

serverb — IP 192.168.56.102, /etc/hosts, hostname serverb
virsh migrate connects from servera to serverb's libvirt over SSH (qemu+ssh://), so servera's root must be able to log in to serverb as root without a password. I generated a key on servera and copied it to serverb.
ssh-keygen -t ed25519 -f /root/.ssh/cloudkey -N "" -q
ssh-copy-id -i ~/.ssh/cloudkey yoosung@serverb

Generating the key cloudkey as root on servera and copying it to serverb with ssh-copy-id
ssh-copy-id puts the key into the regular user's account, so on serverb I moved that key into root's authorized_keys. The user's file also holds the key the Ubuntu host uses to log in, so instead of moving the whole file I appended only servera's key line.
grep root@servera /home/yoosung/.ssh/authorized_keys >> /root/.ssh/authorized_keys
chown root:root .ssh/authorized_keys; chmod 600 .ssh/authorized_keys

serverb — registering servera's key in root's authorized_keys, owner root, mode 600
Host serverb
HostName serverb.cloud.org
User root
Port 22
IdentityFile ~/.ssh/cloudkey

ssh serverb after writing ~/.ssh/config on servera — logged in to serverb as root without a password
Creating a KVM VM and Live Migration
This migration copies the disk as well, without shared storage, so the receiving side (serverb) also needs a storage pool at the same path.
virsh pool-define-as --name default --type dir --target /var/lib/libvirt/images/
virsh pool-autostart default; virsh pool-build default; virsh pool-start default

Creating the image storage pool default on serverb
wget -q --show-progress https://download.cirros-cloud.net/0.5.2/cirros-0.5.2-aarch64-disk.img
mv cirros-0.5.2-aarch64-disk.img /var/lib/libvirt/images/

Downloading the CirrOS 0.5.2 image (aarch64 build) on servera and moving it to /var/lib/libvirt/images/
There is no /dev/kvm inside the Rocky VMs, so as on slide 32 I used --virt-type qemu (software emulation). Unlike the slides, I added --osinfo linux2022 so the disk attaches as virtio, and --cpu cortex-a57 for emulation speed.
virt-install --name cirros --ram 512 \
--disk path=/var/lib/libvirt/images/cirros-0.5.2-aarch64-disk.img,format=qcow2 \
--osinfo linux2022 --virt-type qemu --cpu cortex-a57 --tpm none \
--nographics --import --noautoconsole

Creating the CirrOS VM — --virt-type qemu, --osinfo linux2022 (virtio disk), --cpu cortex-a57

virsh console cirros — logging in as cirros after boot
To see whether a process survives the migration, I wrote a script inside CirrOS that increments and prints a Count every second.
#!/bin/sh
count=0
while :
do
count=$((count+1))
echo "Testing Live Migration.. Press Ctrl+C to stop..."
echo "Count =" $count
date
sleep 1
done

Writing the job file test.sh inside CirrOS (Count goes up every second) and making it executable
With ./test.sh running, I left only the console with Ctrl+]. The VM and the script keep running.

Leaving only the console with Ctrl+] while ./test.sh runs (Count 1–6, 02:46:27–02:46:33 UTC) — cirros running on servera
virsh migrate --verbose --copy-storage-all --persistent --live cirros qemu+ssh://root@serverb/system
--live: moves the VM without shutting it down, copying memory while it runs.--copy-storage-all: there is no shared storage, so the disk image is copied too.--persistent: keeps the definition permanently on serverb.

virsh migrate — Migration 100%; the VM no longer runs on servera (only the definition remains, shut off)

cirros running on serverb; on the console the same test.sh keeps printing from Count 238 — the process was not terminated and moved to serverb
The test.sh started on servera before migration was not terminated, and its Count continued on serverb. Since the Count did not reset to 1, the VM was moved to another host while keeping its running state (live migration). With --copy-storage-all, the disk image was copied along without shared storage.
Finally I removed the leftover definition on servera and powered off cirros on serverb.

Cleanup — virsh undefine cirros on servera, virsh destroy cirros on serverb
3. Exercises
Ch11-5. hellocloud.c and the Virtual Memory Layout
Write and run hellocloud.c, and use pmap and similar tools to draw the layout of its virtual memory.
I made one variable for each memory region: an initialized global, an uninitialized global, a string constant, a local variable and a malloc block. The program prints their addresses and waits for Enter, and while it waits I checked the layout from another terminal with pmap and /proc/<pid>/maps.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int g_init = 2026; /* .data : initialized global */
int g_uninit; /* .bss : uninitialized global */
const char *msg = "Hello, Cloud!"; /* string literal goes in .rodata */
int main(void)
{
int local = 1; /* stack */
char *heap = malloc(4096); /* heap (4 KiB, brk area) */
printf("%s pid=%d\n", msg, getpid());
printf("text (main) : %p\n", (void *)main);
printf("rodata (msg) : %p\n", (void *)msg);
printf("data (g_init) : %p\n", (void *)&g_init);
printf("bss (g_uninit) : %p\n", (void *)&g_uninit);
printf("heap (malloc) : %p\n", (void *)heap);
printf("libc (printf) : %p\n", (void *)printf);
printf("stack (local) : %p\n", (void *)&local);
printf("press Enter to exit...\n");
getchar();
free(heap);
return 0;
}

Built with gcc -o hellocloud hellocloud.c and run — the address of each region (pid 13757)

pmap — start address, size, permissions and backing file of each mapping

/proc/13757/maps — names for the regions pmap shows as [ anon ]: [heap], [vvar], [vdso], [stack]

Virtual memory layout of hellocloud (higher addresses at the top)
main(0x…909d8) and the stringmsg(0x…90b38) sit in the executable's first mapping (r-x). Code and read-only data landed on the same page.g_init(.data) andg_uninit(.bss) sit next to each other on one page of the executable's writable mapping (rw-). The r-- page below it is the region made read-only after relocation (RELRO).malloc(4096)came from the [heap] (132K) right above the executable. The heap grows toward higher addresses with brk. A request of 128 KiB or more gets its own mapping in the mmap area instead of the heap.printfis in the libc.so.6 code in the mmap area, and the dynamic linker ld-linux-aarch64.so.1 and the kernel-provided [vvar]·[vdso] are in this area too.- The local variable
localis in the [stack] (132K) at the top of the user address space, and the stack grows toward lower addresses. - ASLR changes the addresses on every run, but the order and permissions of the regions stay the same.
Ch11-9. Memory Ballooning
Research the memory ballooning technique and explain it.
Memory ballooning is a technique that lets the hypervisor reclaim memory from a running virtual machine or give it back. A balloon driver lives inside the guest OS (virtio_balloon on KVM). The hypervisor tells the driver a target size, and the driver inflates or deflates a balloon inside the guest's memory.
- Why it is needed: the hypervisor cannot tell which of the guest's pages are actually in use (the semantic gap). If the hypervisor picks guest pages and swaps them out itself when host memory runs low, it may evict pages the guest kernel uses often, and performance can drop sharply. Ballooning lets the guest OS choose which pages to give up.
- Reclaim (inflate): when the hypervisor lowers the target size, the balloon driver allocates pages from the guest kernel, holds on to them and reports their physical page numbers to the hypervisor. From the guest's point of view the driver is using that memory, so no other process touches those pages. The hypervisor can then hand the pages to another virtual machine. If the guest has no free memory, the guest OS drops caches or pages out to its own swap by itself.
- Return (deflate): when the target size goes back up, the driver releases the pages it held back to the guest kernel, and the guest uses that memory again.
- Effect: memory size can change without stopping the virtual machine, so memory overcommit (the memory given to all virtual machines adds up to more than the physical memory) can be run safely. On KVM, changing the current memory (
currentMemory) withvirsh setmem <vm> <size>within the configured maximum (memory) drives the balloon driver. - Limits: the guest needs the balloon driver installed and the guest OS has to cooperate. Reclaim is tied to the guest's page reclaim speed, so it is not instant, and inflating too far can cause swapping or OOM inside the guest. Memory cannot grow past the maximum. That is why real systems use ballooning together with KSM (sharing pages with identical content) and hypervisor swapping.
References
- C. A. Waldspurger, "Memory Resource Management in VMware ESX Server," Proc. 5th Symposium on Operating Systems Design and Implementation (OSDI '02), USENIX, 2002.
- libvirt, "Domain XML format — Memory balloon device," https://libvirt.org/formatdomain.html#memory-balloon-device