<div dir="ltr">Hello!<br><br>I'am running Archlinux (recent version) including Systemd and Qemu.<br><br>Since killing qemu directly is highly destructive for guest os I want to shut it gracefully. I think it is obvious nessesity To do so I decided to use 'net rpc' or qemu monitor command. Both requires networking to be up and running.<br>
<br>Since My distro does not supply service file at all I wrote several service files on my own. Without success. I have encouraged following problems:<br><br>1. Service file follows:<br><br>cut------<br><br>[Unit]<br>Description=QEMU virtual machine %i<br>
After=network.target<br>Before=shutdown.target reboot.target poweroff.target halt.target<br><br>[Service]<br>Type=forking<br>KillMode=none<br>SendSIGKILL=yes<br>#Environment="type=system-x86-64" "haltcmd=kill -INT $MAINPID"<br>
<br>PIDFile=/run/qemu/%i.pid<br>EnvironmentFile=/etc/conf.d/qemu.d/%i<br>ExecStart=/usr/bin/qemu-system-x86_64 -name %i -daemonize -pidfile /run/qemu/%i.pid -nographic $options<br>ExecStop=/bin/sh -c ${haltcmd}<br><br><br>
TimeoutStartSec=10<br>TimeoutStopSec=120<br><br><br><br>[Install]<br>WantedBy=multi-user.target<br><br>cut------<br><br>haltcmd="/usr/bin/echo 'system_powerdown' | /usr/bin/nc localhost $port"<br>Using this setup I am able to start vm at startup or manually from console. Stopping from console also works. I can see guest stopping as expected. But after invoking poweroff/reboot systemd does not wait for qemu process to close.....<br>
<br><br>2. To workaround  problems described in 1 I wrote bash script.  See service file below:<br><br>cut---<br>[Unit]<br>Description=QEMU virtual machine %i<br>After=network.target<br>Before=shutdown.target reboot.target poweroff.target halt.target<br>
<br>[Service]<br>#Type=oneshot<br>ExecStart=/etc/rc.d/qemu start %i<br>ExecStop=/etc/rc.d/qemu stop %i<br>RemainAfterExit=true<br>KillMode=none<br><br>#TimeoutStartSec=10sec<br>TimeoutSec="80sec"<br><br><br><br>
[Install]<br>WantedBy=multi-user.target<br>cut--<br><br>Script qemu when invoked with 'start' runs qemu in daeon mode and exits with 0 status. This work fine during system startup and manual invoking.<br><br>When run with 'stop' 'qemu' script send signal to guest os via net rpc or qemu monitor. In this case script 'qemu' stay active until timeout occurs or qemu vm proces stops.<br>
<br>This way when invoked from commandline (systemctl stop qemu@vm) systemctl exit after actual guest powerdown.<br><br>Seems ok but when I issue poweroff/reboot guest is killed in a brutal way. It is because (afaik) systemd turns network off before closing qemu. It is clear that this problem is caused by startup/poweroff sequence. During startup it is required to setup networking before starting any listening daeon. But when system is going to poweroff/reboot seqence should be: first close daemons and then turn off network.<br>
<br>Reasuming:<br><br>1. I am looking for an advice how to setup different startup and powerdown sequences. Is it possible at all ??<br>2. Is it possible to wait for process to exit ? I think this behaviour is essential when shutting down  guest os in a proper way.<br>
<br>3. I know that this is not a proper mailing list but if another way of making poweroff on quest exists please - let me know.<br><br><br>Thanks<br>Bartek<br></div>