QEMU virtual machine management
README.md

ocaml-qemu #

Spawn and control QEMU virtual machines from OCaml over QMP.

Testing a kernel, a unikernel or an embedded Linux image usually means booting it under QEMU from a shell script and scraping its console. QEMU also exposes a control socket that speaks the QEMU Machine Protocol (QMP), a JSON protocol for querying and driving a running machine. qemu builds the QEMU command line from a typed configuration, spawns the process under an Eio switch, and talks QMP to it: status queries, pause and resume, ACPI shutdown and quit, or any other QMP command built by hand.

The configuration covers the architecture and accelerator (KVM on Linux, HVF on macOS, TCG elsewhere, chosen from the host by default: KVM and HVF, Apple's Hypervisor.framework, run the guest's instructions directly on the host CPU with hardware virtualisation, while TCG is QEMU's own translator, which rewrites guest instructions into host code, runs anywhere, including on a different architecture, and is many times slower), CPUs and memory, disks, 9p filesystem exports, user, tap, bridge or inherited-socket networking, the display, the serial console and direct kernel boot. QEMU's seccomp sandbox is on by default. Two executables come with the library: qemu-ctl, a small QMP control tool, and qemu-mrg, which boots a MirageOS unikernel with an OCaml slirp gateway as its network backend.

Installation #

Install with opam:

$ opam install qemu

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install qemu

Usage #

open Eio.Std

let run env =
  let net = Eio.Stdenv.net env in
  let proc_mgr = Eio.Stdenv.process_mgr env in
  Switch.run @@ fun sw ->
  let config =
    Qemu.Vm.Config.default ~name:"test-vm"
    |> Qemu.Vm.Config.with_memory_mb 1024
    |> Qemu.Vm.Config.with_disk "/path/to/disk.img"
    |> Qemu.Vm.Config.with_kernel "/path/to/vmlinuz"
    |> Qemu.Vm.Config.with_cmdline "console=ttyS0"
  in

  let vm = Qemu.Vm.spawn ~sw ~proc_mgr config in
  match Qemu.Vm.connect ~sw ~net vm with
  | Error e -> failwith e
  | Ok () ->
      (match Qemu.Vm.query_status vm with
      | Ok status -> Fmt.pr "Status: %a@." Qemu.Qmp.Status.pp status
      | Error e -> Fmt.pr "Error: %s@." e);
      ignore (Qemu.Vm.execute vm Qemu.Qmp.Command.system_powerdown)

Configuration #

A configuration starts from Config.default and each with_* function changes one setting:

let config =
  Qemu.Vm.Config.default ~name:"my-vm"
  |> Qemu.Vm.Config.with_cpus 4
  |> Qemu.Vm.Config.with_memory_mb 4096
  |> Qemu.Vm.Config.with_disk ~format:`Qcow2 "/path/to/disk.qcow2"
  |> Qemu.Vm.Config.with_network Qemu.Vm.Config.User
  |> Qemu.Vm.Config.with_display (Qemu.Vm.Config.Vnc { port = 0 })
  |> Qemu.Vm.Config.with_accel Qemu.Vm.Config.KVM

Direct Kernel Boot #

A unikernel or an embedded Linux image boots directly from its kernel, with no bootloader:

let config =
  Qemu.Vm.Config.default ~name:"unikernel"
  |> Qemu.Vm.Config.with_kernel "/path/to/vmlinuz"
  |> Qemu.Vm.Config.with_initrd "/path/to/initrd.cpio"
  |> Qemu.Vm.Config.with_cmdline "console=ttyS0 root=/dev/vda"
  |> Qemu.Vm.Config.with_disk "/path/to/rootfs.img"

QMP Commands #

The common QMP commands are values of Qemu.Qmp.Command.t:

let lifecycle_commands =
  [ Qemu.Qmp.Command.query_status
  ; Qemu.Qmp.Command.stop
  ; Qemu.Qmp.Command.cont
  ; Qemu.Qmp.Command.system_powerdown
  ; Qemu.Qmp.Command.quit
  ]

Other commands are built with Command.v:

let cmd =
  let arguments =
    let open Json.Value in
    object' [ (name "command-line", string "info status") ]
  in
  Qemu.Qmp.Command.v "human-monitor-command" ~arguments

References #

Licence #

ISC. See LICENSE.md.