TT Lab
Get started
Learn Learning paths Courses

Virtualisation with QEMU/KVM

Booting a VM With QEMU

Continue in TT Lab

Goal

Actually bring up a QEMU VM with the prepared disk image, and prove that it is alive with three things: the serial log, the monitor socket, and the hostfwd listening socket.

Why it matters

In practice you use libvirt or a cloud console, but when a problem arises, you end up having to read the QEMU command line underneath. Why -netdev (the host-side backend) and -device (the NIC the guest sees) are separate, why you must specify format=, and why the hostfwd listening socket opens immediately regardless of the guest boot — these things stick only when you write the command line yourself.

This lab runs inside a virtual machine, and you bring up another virtual machine inside it. Nested virtualization is on, so /dev/kvm exists, and QEMU uses hardware acceleration as is.\n\nIt used to run in a Pod, which had no /dev/kvm, and QEMU ran only with TCG software emulation, translating instructions one by one. It took several minutes for the guest to reach a login prompt, so the lab could only look as far as "did the firmware come up."\n\nPick the accelerator that is available. -accel kvm and -accel tcg differ by tens of times. The order is the same in practice — first check whether /dev/kvm exists, and if it does, use it. The grader also looks at what is in this box and judges accordingly.

Steps

  1. Create the /root/vmboot directory, and save the qemu-img info output of the disk image provided under /opt/vm/ to /root/vmboot/base-info.txt.
  2. Create an overlay /root/vmboot/vm.qcow2 that uses that image as its backing. The original must not change.
  3. Write /root/vmboot/run.sh. It must contain all of the following elements.
    • qemu-system-x86_64
    • -accel tcg
    • -m 512
    • -smp 1
    • -drive file=/root/vmboot/vm.qcow2,if=virtio,format=qcow2
    • -netdev user,id=n0,hostfwd=tcp::12222-:22
    • -device virtio-net-pci,netdev=n0
    • -nographic
    • -serial file:/root/vmboot/console.log
    • -monitor unix:/root/vmboot/mon.sock,server,nowait
  4. Write the list of available accelerators and whether /dev/kvm exists to /root/vmboot/accel.txt as the following two lines. ACCELS=<쉼표로 구분한 가속기 목록> / DEVKVM=<yes|no> (the first placeholder is a comma-separated list of accelerators)
  5. Run run.sh in the background, wait at least 20 seconds, and then check whether firmware messages remain in /root/vmboot/console.log. The log must contain the string SeaBIOS or Booting.
  6. Send info status to the monitor socket and save the response to /root/vmboot/monitor.txt. The response must show running or paused.
  7. Check whether port 12222 is listening and save the result to /root/vmboot/hostfwd.txt. The process must show as qemu.
  8. Create /root/vmboot/report.txt with the following 5 lines. ACCEL=tcg / MEM=512 / VCPU=1 / FWD_PORT=12222 / DISK_IF=virtio

Notes

Check the provided image

Create the /root/vmboot directory, and save the qemu-img info output of the disk image provided under /opt/vm/ to /root/vmboot/base-info.txt.

There is a prepared image under /opt/vm. Check its format and virtual size first.

Protect the original with an overlay

Create an overlay /root/vmboot/vm.qcow2 that uses that image as its backing. The original must not change.

If you create an overlay that uses the original as its backing, the original does not change. Do not forget to specify the backing format.

Write the launch script

Write /root/vmboot/run.sh. It must contain all of the following elements.

The required flags are fixed. If even one is missing, grading catches it. Do the backgrounding outside the script.

Check the accelerators

Write the list of available accelerators and whether /dev/kvm exists to /root/vmboot/accel.txt as the following two lines. ACCELS=<쉼표로 구분한 가속기 목록> / DEVKVM=<yes|no> (the first placeholder is a comma-separated list of accelerators)

qemu-system-x86_64 has a help option that lists the accelerators you can use. Record whether /dev/kvm exists as well.

Actually bring it up and keep a console log

Run run.sh in the background, wait at least 20 seconds, and then check whether firmware messages remain in /root/vmboot/console.log. The log must contain the string SeaBIOS or Booting.

There is an option that sends the serial output to a file. Firmware messages come out right after the start, so you do not need to wait long.

Query the monitor socket

Send info status to the monitor socket and save the response to /root/vmboot/monitor.txt. The response must show running or paused.

Open the monitor as a Unix socket and attach with the -U of nc. Just pipe the command into standard input.

Check port forwarding

Check whether port 12222 is listening and save the result to /root/vmboot/hostfwd.txt. The process must show as qemu.

The listening socket of hostfwd opens the moment QEMU starts. It has nothing to do with the guest boot.

VM run report

Create /root/vmboot/report.txt with the following 5 lines. ACCEL=tcg / MEM=512 / VCPU=1 / FWD_PORT=12222 / DISK_IF=virtio

Gather the values you confirmed in the earlier steps. The accelerator and the forwarding port must be the actual values.