nixos/vms: Use async QEMU client
This commit is contained in:
parent
baf605fbe2
commit
28fe4192f0
@ -18,29 +18,34 @@ let
|
|||||||
|
|
||||||
doCleanShutdown =
|
doCleanShutdown =
|
||||||
let
|
let
|
||||||
pyEnv = pkgs.python310.withPackages (ps: with ps; [ qemu ]);
|
pyEnv = pkgs.python3.withPackages (ps: with ps; [ qemu ]);
|
||||||
in
|
in
|
||||||
pkgs.writeScript "qemu-clean-shutdown" ''
|
pkgs.writeScript "qemu-clean-shutdown" ''
|
||||||
#!${pyEnv}/bin/python
|
#!${pyEnv}/bin/python
|
||||||
|
import asyncio
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import qemu.qmp.legacy
|
import qemu.qmp
|
||||||
|
|
||||||
if len(sys.argv) != 2:
|
async def main():
|
||||||
print(f'usage: {sys.argv[0]} <qmp unix socket>', file=sys.stderr)
|
if len(sys.argv) != 2:
|
||||||
sys.exit(1)
|
print(f'usage: {sys.argv[0]} <qmp unix socket>', file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
if 'MAINPID' not in os.environ:
|
if 'MAINPID' not in os.environ:
|
||||||
# Special case: systemd is calling us after QEMU exited on its own
|
# Special case: systemd is calling us after QEMU exited on its own
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# TODO: Upgrade to async QMPClient
|
client = qemu.qmp.QMPClient('clean-shutdown')
|
||||||
with qemu.qmp.legacy.QEMUMonitorProtocol(sys.argv[1]) as mon:
|
await client.connect(sys.argv[1])
|
||||||
mon.connect()
|
await client.execute('system_powerdown')
|
||||||
mon.command('system_powerdown')
|
async for event in client.events:
|
||||||
while mon.pull_event(wait=True)['event'] != 'SHUTDOWN':
|
if event['event'] == 'SHUTDOWN':
|
||||||
pass
|
break
|
||||||
|
await client.disconnect()
|
||||||
|
|
||||||
|
asyncio.run(main())
|
||||||
'';
|
'';
|
||||||
|
|
||||||
# TODO: Upstream or something...
|
# TODO: Upstream or something...
|
||||||
|
Loading…
x
Reference in New Issue
Block a user