Skip to content

Commit a50d1bf

Browse files
SuperSandro2000astro
authored andcommitted
virtiofsd: use type notify
1 parent 36e261a commit a50d1bf

File tree

2 files changed

+78
-16
lines changed

2 files changed

+78
-16
lines changed

nixos-modules/host/default.nix

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -261,41 +261,55 @@ in
261261
restartIfChanged = false;
262262
serviceConfig = {
263263
ExecReload = "${lib.getExe' supervisor "supervisorctl"} reload";
264-
KillMode = "process";
264+
ExecStop = "${lib.getExe' supervisor "supervisorctl"} shutdown";
265265
LimitNOFILE = 1048576;
266+
NotifyAccess = "all";
266267
PrivateTmp = "yes";
267268
Restart = "always";
268269
RestartSec = "5s";
269270
SyslogIdentifier = "microvm-virtiofsd@%i";
271+
Type = "notify";
270272
WorkingDirectory = "${stateDir}/%i";
271273
};
272-
path = with pkgs; [ virtiofsd ];
273274
script = ''
274275
echo "[supervisord]
276+
nodaemon=true
277+
user=root
278+
279+
[eventlistener:notify]
280+
command=${pkgs.writers.writePython3 "supervisord-event-handler" { } (lib.readFile ./supervisord-event-handler.py)}
281+
events=PROCESS_STATE
275282
" > /tmp/supervisord.conf
276283
284+
virtiofsd_count=0
285+
277286
for d in $PWD/current/share/microvm/virtiofs/*; do
278287
SOCKET="$(realpath "$(cat $d/socket)")"
279288
SOURCE="$(cat $d/source)"
280289
mkdir -p "$SOURCE"
281290
291+
group_programs+="virtiofsd-$(basename "$d"),"
292+
virtiofsd_count=$((virtiofsd_count+1))
293+
282294
echo "[program:virtiofsd-$(basename "$d")]
283-
command=virtiofsd \
284-
--socket-path=$SOCKET \
285-
--socket-group=${config.users.users.microvm.group} \
286-
--shared-dir "$SOURCE" \
287-
--rlimit-nofile ${toString serviceConfig.LimitNOFILE} \
288-
--thread-pool-size ${toString config.microvm.virtiofsd.threadPoolSize} \
289-
--posix-acl --xattr \
290-
${lib.optionalString (config.microvm.virtiofsd.inodeFileHandles != null)
291-
"--inode-file-handles=${config.microvm.virtiofsd.inodeFileHandles}"
292-
} \
293-
${lib.concatStringsSep " " config.microvm.virtiofsd.extraArgs}
294-
" >> /tmp/supervisord.conf
295+
stderr_syslog=true
296+
stdout_syslog=true
297+
command=${lib.getExe pkgs.virtiofsd} \
298+
--socket-path=$SOCKET \
299+
--socket-group=${config.users.users.microvm.group} \
300+
--shared-dir "$SOURCE" \
301+
--rlimit-nofile ${toString serviceConfig.LimitNOFILE} \
302+
--thread-pool-size ${toString config.microvm.virtiofsd.threadPoolSize} \
303+
--posix-acl --xattr \
304+
${lib.optionalString (config.microvm.virtiofsd.inodeFileHandles != null)
305+
"--inode-file-handles=${config.microvm.virtiofsd.inodeFileHandles}"
306+
} \
307+
${lib.concatStringsSep " " config.microvm.virtiofsd.extraArgs}
308+
" >> /tmp/supervisord.conf
295309
done
296310
297-
exec ${lib.getExe' supervisor "supervisord"} --nodaemon --user root \
298-
--configuration /tmp/supervisord.conf
311+
echo -n $virtiofsd_count > /tmp/virtiofsd_count
312+
exec ${lib.getExe' supervisor "supervisord"} --configuration /tmp/supervisord.conf
299313
'';
300314
};
301315

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import subprocess
2+
import sys
3+
4+
5+
def write_stdout(s):
6+
# only eventlistener protocol messages may be sent to stdout
7+
sys.stdout.write(s)
8+
sys.stdout.flush()
9+
10+
11+
def write_stderr(s):
12+
sys.stderr.write(s)
13+
sys.stderr.flush()
14+
15+
16+
def main():
17+
count = 0
18+
with open('/tmp/virtiofsd_count') as f:
19+
expected_count = int(f.read())
20+
21+
while True:
22+
write_stdout('READY\n')
23+
line = sys.stdin.readline()
24+
25+
# read event payload and print it to stderr
26+
headers = dict([x.split(':') for x in line.split()])
27+
sys.stdin.read(int(headers['len']))
28+
# body = dict([x.split(':') for x in data.split()])
29+
30+
if headers["eventname"] == "PROCESS_STATE_RUNNING":
31+
count += 1
32+
write_stderr("Process state running...\n")
33+
34+
if headers["eventname"] == "PROCESS_STATE_STOPPING":
35+
count -= 1
36+
write_stderr("Process state stopping...\n")
37+
38+
if count >= expected_count:
39+
subprocess.run(["systemd-notify", "--ready"])
40+
41+
if count <= 0:
42+
subprocess.run(["systemd-notify", "--stopping"])
43+
44+
write_stdout('RESULT 2\nOK')
45+
46+
47+
if __name__ == '__main__':
48+
main()

0 commit comments

Comments
 (0)