Appendix B: Final Exam
Goal: Four exercises without instructions that tie together everything from the book. · Duration: approx. 60 min · Prerequisite: Day 0–15
Rules of Play
- You work on kvm1 in
~/kvm-lab/pruefung. Thedefaultandinternnetworks from Day 6 are running. - Each exercise has check commands. Only when they show what is stated there is the exercise solved.
- The solutions are in
~/kvm-lab/code/loesung/pruefung/.
mkdir -p ~/kvm-lab/pruefung
cd ~/kvm-lab/pruefungExercise 1: A VM for intern only
Build a VM pruef from the base image with 768 MiB and
one vCPU. It is attached only to the
intern network and always has the address
10.10.10.40 there. You log in with your SSH key as
ubuntu.
Check:
ssh [email protected] "hostname; ip route"Expected:
pruef
10.10.10.0/24 dev enp1s0 proto kernel scope link src 10.10.10.40 metric 100
10.10.10.1 dev enp1s0 proto dhcp scope link src 10.10.10.40 metric 100
Solution
cat > user-data <<EOF
#cloud-config
users:
- name: ubuntu
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
ssh_authorized_keys:
- $(cat ~/.ssh/id_ed25519.pub)
EOF
cat > meta-data <<'EOF'
instance-id: pruef-001
local-hostname: pruef
EOF
virsh net-update intern add ip-dhcp-host \
"<host mac='52:54:00:10:10:40' name='pruef' ip='10.10.10.40'/>" --live --config
virt-install --name pruef --osinfo ubuntu24.04 --memory 768 --vcpus 1 --import \
--disk size=10,backing_store=/var/lib/libvirt/images/basis/noble.qcow2 \
--network network=intern,mac=52:54:00:10:10:40 \
--cloud-init \
user-data=user-data,meta-data=meta-data,network-config=../tag07/netz-config \
--graphics none --noautoconsoleWithout internet access, cloud-init installs no packages. That is why the user-data here needs none. After about a minute the VM answers.
Save your progress? Sign up for free
Exercise 2: A standalone backup
Back up the disk of pruef while it is running to
/var/lib/libvirt/lab/pruef.qcow2. The file must work
without the base image.
Check:
sudo qemu-img info /var/lib/libvirt/lab/pruef.qcow2 | grep -E "^backing|virtual size"
sudo virt-cat -a /var/lib/libvirt/lab/pruef.qcow2 /etc/hostnameExpected:
virtual size: 10 GiB (10737418240 bytes)
pruef
Solution
cat > backup.xml <<'EOF'
<domainbackup>
<disks>
<disk name="vda" type="file">
<target file="/var/lib/libvirt/lab/pruef.qcow2"/>
<driver type="qcow2"/>
</disk>
</disks>
</domainbackup>
EOF
virsh backup-begin pruef backup.xml
sleep 25
virsh domjobinfo pruef --completed | grep -E "Job type|Operation"Expected:
Backup started
Job type: Completed
Operation: Backup
Save your progress? Sign up for free
Exercise 3: Permanent and bigger
pruef should start with the host and have two vCPUs from
the next start on.
Check after a power-off and power-on:
virsh dominfo pruef | grep -E "CPU\(s\)|Autostart"
ssh [email protected] nprocExpected:
CPU(s): 2
Autostart: enable
2
Solution
virsh autostart pruef
virsh setvcpus pruef 2 --config --maximum
virsh setvcpus pruef 2 --config
virsh shutdown pruefWhen pruef is off:
virsh start pruefThe maximum (--maximum) has to go up first, then the
count. With --config, both only take effect at the next
start.
Save your progress? Sign up for free
Exercise 4: The view from outside
From kvm2, show all VMs on kvm1 that start with the host.
Check on kvm2:
virsh -c qemu+ssh://kvm1/system list --all --autostartExpected (the IDs will differ):
Id Name State
-----------------------
1 notes running
3 db running
5 pruef running
Solution
The command is the solution, provided SSH from kvm2 to kvm1 is set up
(Day 11). If pruef is not in the list, Exercise 3 is
missing.
Save your progress? Sign up for free
Cleaning up
On kvm1:
virsh destroy pruef
virsh undefine pruef --remove-all-storage
virsh net-update intern delete ip-dhcp-host \
"<host mac='52:54:00:10:10:40' name='pruef' ip='10.10.10.40'/>" --live --config
sudo rm /var/lib/libvirt/lab/pruef.qcow2Expected:
Domain 'pruef' destroyed
Domain 'pruef' has been undefined
Volume 'vda'(/var/lib/libvirt/images/pruef.qcow2) removed.
Updated network intern persistent config and live state
You passed if all four check commands showed what was expected.
Congratulations: you have run KVM VMs yourself, from
qemu-system-x86_64 all the way to live migration.