Skip to content

Instantly share code, notes, and snippets.

@zegelin
Last active January 19, 2023 16:45
Show Gist options
  • Save zegelin/e566e2f0893977920a784ed29803f528 to your computer and use it in GitHub Desktop.
Save zegelin/e566e2f0893977920a784ed29803f528 to your computer and use it in GitHub Desktop.
QEMU systemd/SSH
qemu-system-x86_64(16858) threads:
{qemu-system-x86}(16859): 0-3,24-27
{IO io1}(16860): 0-3,24-27
{IO mon_iothread}(16872): 0-3,24-27
{CPU 0/KVM}(16873): 4
{CPU 1/KVM}(16874): 28
{CPU 2/KVM}(16875): 5
{CPU 3/KVM}(16876): 29
{CPU 4/KVM}(16877): 6
{CPU 5/KVM}(16878): 30
{CPU 6/KVM}(16879): 7
{CPU 7/KVM}(16880): 31
{CPU 8/KVM}(16881): 8
{CPU 9/KVM}(16882): 32
{CPU 10/KVM}(16883): 9
{CPU 11/KVM}(16884): 33
{CPU 12/KVM}(16885): 10
{CPU 13/KVM}(16886): 34
{CPU 14/KVM}(16887): 11
{CPU 15/KVM}(16888): 35
{worker}(27836): 0-3,24-27
{worker}(27842): 0-3,24-27
{worker}(27878): 0-3,24-27
{worker}(27879): 0-3,24-27
{worker}(27880): 0-3,24-27
{worker}(27881): 0-3,24-27
{worker}(27882): 0-3,24-27
/usr/sbin/qemu-system-x86_64 \
-name seat1,debug-threads=on -daemonize -pidfile /run/qemu_seat1.pid -monitor unix:/tmp/seat1.sock,server,nowait -nodefconfig -debugcon file:/var/log/qemu@seat1-debugcon.log -global isa-debugcon.iobase=0x402 \
-nodefconfig -no-user-config -nodefaults -nographic \
-machine q35,accel=kvm \
-enable-kvm -cpu host,kvm=off,hv_spinlocks=0x1fff,hv_relaxed,hv_time,hv_vendor_id=Nvidia43FIX \
-rtc base=localtime,clock=host,driftfix=slew \
-mem-path /dev/hugepages \
-drive file=/tank/fw/active/OVMF-pure-efi.fd,if=pflash,format=raw,unit=0,readonly=on\
-object iothread,id=io1 \
-m 8192 \
-smp cores=8,threads=2,sockets=1 \
-drive file=/home/adam/win10-OVMF_VARS.fd,if=pflash,format=raw,unit=1 \
-device virtio-scsi-pci,id=scsi0,ioeventfd=on,iothread=io1,num_queues=4 \
-drive id=disk0,file=/tank/vm/adam-win10.qcow2,format=qcow2,cache=writeback,readonly=off,if=none \
-device scsi-hd,drive=disk0,bus=scsi0.0 \
-netdev bridge,id=netdev0,br=br0 -device virtio-net-pci,netdev=netdev0,mac=52:54:00:12:34:57 \
-device vfio-pci,host=08:00.0,addr=0x6,multifunction=on -device vfio-pci,host=08:00.1,addr=0x6.0x1 \
-device vfio-pci,host=03:00.0 -device vfio-pci,host=04:00.0 \
-drive id=disk2,file=/dev/disk/by-id/ata-Hitachi_HDS723020BLA642_MN1270FA12SE0D,format=raw,readonly=off,if=none,aio=native,cache=none,cache.direct=on \
-device scsi-hd,drive=disk2,bus=scsi0.0 \
-drive id=disk3,file="iscsi://192.168.3.1/iqn.2000-01.com.zegelin:adam-win10/0",format=raw,readonly=off,if=none \
-device scsi-hd,drive=disk3,bus=scsi0.0 \
-drive id=disk4,file="iscsi://192.168.3.1/iqn.2000-01.com.zegelin:adam-win10/1",format=raw,readonly=off,if=none \
-device scsi-hd,drive=disk4,bus=scsi0.0 \
-drive id=disk5,file="iscsi://192.168.3.1/iqn.2000-01.com.zegelin:adam-win10/2",format=raw,readonly=off,if=none \
-device scsi-hd,drive=disk5,bus=scsi0.0 \
[Unit]
Description=QEMU virtual machine (%i)
[Service]
Type=forking
PIDFile=/run/qemu_%i.pid
Environment=QEMU_AUDIO_DRV=pa
EnvironmentFile=/etc/conf.d/qemu.d/%i/*
ExecStart=/usr/bin/qemu-system-x86_64 -name %i,debug-threads=on -daemonize -pidfile /run/qemu_%i.pid -monitor unix:/tmp/%i.sock,server,nowait -nodefconfig -debugcon file:/var/log/qemu@%i-debugcon.log -global isa-debugcon.iobase=0x402 $common_args $args
ExecStop=/bin/sh -c 'while test -d /proc/$MAINPID; do /usr/bin/echo system_powerdown | /usr/bin/socat - UNIX-CONNECT:/tmp/%i.sock; sleep 60; done'
TimeoutStopSec=1m
[Install]
WantedBy=multi-user.target
[Service]
CPUSchedulingPolicy=rr
ExecStartPost=/bin/sh -c 'exec /usr/bin/qemu-affinity $MAINPID -p 0-3,24-27 -i *:0-3,24-27 -q 0-3,24-27 -w *:0-3,24-27 -k 4 28 5 29 6 30 7 31 8 32 9 33 10 34 11 35'
@xrysf03
Copy link

xrysf03 commented Jan 19, 2023

Regarding the qemu@.service, I'd like to suggest the following mod:

ExecStop=/bin/sh -c '/usr/bin/echo system_powerdown | /usr/bin/socat - UNIX-CONNECT:/tmp/%i.sock; while test -d /proc/$MAINPID; do sleep 2; done'

I,e,: send the system_powerdown command just once, then keep watching the PID in a "not very tight" loop - just wait for the PID to die, and only then return from the ExecStop action.
In the original version, the loop sends the system_powerdown command and then sleeps no less than a minute, before trying again.

Also, you might wish to increase the TimeoutStopExec to something more than just a minute - some guests may take longer to shut down. But this is probably something that anyone could figure out, eventually.

Thanks for this cool piece. Has saved me quite some time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment