2014-02-28 18:40:43

Qemu systemd service

Stopping a Qemu vm on shutdown is a tricky thing with systemd. The solution mentioned in the arch wiki isnt working for me. It always kills the vm instantly.

After some trying I found a solution that works for me. Socat is required for this and the vm should listen to acpi events like power button pressed.

The systemd service file (/etc/systemd/system/qemu@.service) is shown below:

Description=QEMU virtual machine (%i)
After=network.target netctl@br0.service



ExecStart=/usr/bin/qemu-system-x86_64 -name %i -daemonize -pidfile /run/qemu_%i.pid -monitor unix:/tmp/%i.sock,server,nowait $args

ExecStop=/bin/sh -c '/usr/bin/echo system_powerdown | /usr/bin/socat - UNIX-CONNECT:/tmp/%i.sock; while ps ax | grep "/usr/bin/qemu-system-x86_64 -name %i" | grep -vq grep; do sleep 1; done'


As you see, the configuration for each vm is located in /etc/conf.d/qemu/$name. A sample config could look like this:

args=-cpu kvm64 -display none -m 7G -smp 2 -enable-kvm -net nic,macaddr=01:23:45:67:89:AB -net bridge,br=br0 /path/to/vm.img

A comment to the kvm64 processor type: I used to use the host cpu type but with the Intel(R) Core(TM) i7-4770 CPU of the server the vms were throwing NMI errors and one oopsed because of this some days ago.

Posted by toerb | Permanent link | File under: arch