about summary refs log tree commit diff
path: root/specials/slurm-container.scm
blob: 0aae29a6c433047954fb0df443655caea4acf9b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
;;; genenetwork-machines --- Guix configuration for genenetwork machines
;;; Copyright © 2026 Pjotr Prins <pjotr.public01@thebird.nl>
;;;
;;; This file is part of genenetwork-machines.
;;;
;;; genenetwork-machines is free software: you can redistribute it
;;; and/or modify it under the terms of the GNU General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; genenetwork-machines is distributed in the hope that it will be
;;; useful, but WITHOUT ANY WARRANTY; without even the implied
;;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;;; See the GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with genenetwork-machines.  If not, see
;;; <https://www.gnu.org/licenses/>.

;;; A throwaway Guix system container to test ravanan's slurm-api
;;; batch system against a real, rootful Slurm cluster (slurmctld,
;;; slurmd, slurmrestd with JWT auth) -- one node, one partition, no
;;; accounting database.
;;;
;;; It stays a *separate* box from the GN4AI webserver (gn4ai.scm in the repo
;;; root): genenetwork4 drives it over the slurm REST API
;;; (--slurm-api-endpoint), so the scheduler is not in the webserver's failure
;;; domain, the scheduler's accounts and munge key stay out of the container
;;; that faces the internet, and this file can be deployed on a second machine
;;; as it stands. Reasoned in gn4ai.scm's header; recorded in the ai-memory
;;; store of this repository.
;;;
;;; Unlike a rootless `guix shell -C` sandbox, this is a real `guix
;;; system container`, started with real root on the host via the
;;; run-guix-system-container daemon (see ../../run-guix-container),
;;; so slurmctld/slurmd can privilege-separate normally and actually
;;; run jobs as root/SlurmUser instead of being stuck at the sandbox's
;;; own single uid.
;;;
;;; Authentication is wired the way Slurm expects it, which is also what
;;; makes job *execution* work:
;;;
;;;   AuthType=auth/munge          -- the daemon-to-daemon plane
;;;                                   (slurmctld -> slurmd job launch).
;;;                                   auth/munge verifies by uid, so
;;;                                   slurmd can authenticate slurmctld.
;;;   AuthAltTypes=auth/jwt        -- accepted by slurmctld only (the
;;;                                   plugin loader reads AuthAltTypes
;;;                                   solely in slurmctld/slurmdbd), so
;;;                                   the REST plane can present a JWT.
;;;   SLURM_JWT=<token> in slurmrestd's environment -- auth_g_init()
;;;                                   rewrites authtype to auth/jwt for
;;;                                   any process with SLURM_JWT set,
;;;                                   which is what lets
;;;                                   rest_auth/jwt's per-request
;;;                                   identity switch (auth_g_thread_config,
;;;                                   always called on the *primary*
;;;                                   plugin) be accepted.  slurmrestd
;;;                                   then forwards the caller's token,
;;;                                   and slurmctld decodes it with the
;;;                                   key to learn the real uid.
;;;
;;; The earlier version set AuthType=auth/jwt for the whole cluster,
;;; which does make the REST identity switch work but breaks the
;;; slurmctld -> slurmd leg: auth_jwt's init() only loads the
;;; verification key in slurmctld/slurmdbd, so slurmd cannot decode the
;;; token and every launch RPC arrives as SLURM_AUTH_NOBODY (uid 99) and
;;; is rejected with "Security error".  auth/munge is used for
;;; daemon-to-daemon traffic, auth/jwt only for the REST plane.
;;;
;;; %jwt-fixed-key-bytes/%jwt-fixed-bootstrap-token below are a fixed key
;;; and a token pre-computed against it offline (plain HS256: see the
;;; commit introducing this file for the one-off Python snippet), written
;;; out at every activation so the REST plane is usable from the first
;;; boot without any restart.
;;;
;;; slurmrestd refuses to run as root or as SlurmUser, and its
;;; SLURMRESTD_SECURITY=become_user mode is incompatible with
;;; rest_auth/jwt (fatal: "rest_auth/jwt must not be loaded in
;;; become_user mode"), so it runs as its own dedicated, unprivileged
;;; "slurmrestd" account below.  No setuid() is needed: the caller's
;;; identity travels in the token.
;;;
;;; Build (--share is what makes ravanan testable against this container:
;;; its store and scratch area must be visible to the slurm jobs):
;;;   guix system container --network \
;;;       --share=$HOME/tmp/ai-root/slurm-share specials/slurm-container.scm
;;; Start (via the privileged daemon):
;;;   run-guix-container.rb --daemon 127.0.0.1:8989 --fingerprint <fp> <HASH>
;;; Then from the host, straight away (no manual setup step needed):
;;;   curl -H "X-SLURM-USER-TOKEN: <%jwt-fixed-bootstrap-token>" \
;;;        -H "X-SLURM-USER-NAME: testuser" \
;;;        http://127.0.0.1:6820/slurm/v0.0.41/ping
;;; (slurmrestd listens on all interfaces inside the container; the
;;; container's network namespace makes 127.0.0.1 there reachable
;;; from the host when built with --network.)

(use-modules (gnu)
             (gnu packages admin)
             (gnu packages base)
             (gnu packages bash)
             (gnu packages parallel)
             (gnu services base)
             (gnu services shepherd)
             (gnu services ssh)
             (gnu services sysctl)
             (guix gexp)
             (rnrs bytevectors)
             (srfi srfi-1))

;; The main channel's plain `slurm' is 23.11.10 and has no
;; CgroupPlugin=disabled escape hatch (added in a later release) --
;; this container has no /sys/fs/cgroup to delegate to it anyway, so
;; use the 25.05 series instead (already exported by (gnu packages
;; parallel), restd+jwt included, same as `slurm').
(define slurm slurm-25.05)

(define %slurm-etc "/etc/slurm")
(define %slurm-conf (string-append %slurm-etc "/slurm.conf"))
(define %jwt-key (string-append %slurm-etc "/jwt_hs256.key"))
(define %munge-key "/etc/munge/munge.key")
(define %cgroup-conf (string-append %slurm-etc "/cgroup.conf"))

;; This container has no cgroup delegation of its own (same
;; limitation as the rootless sandbox), so tell slurmd not to even
;; try -- see cgroup.conf(5), CgroupPlugin=disabled.
(define cgroup-conf-file
  (plain-file "cgroup.conf" "CgroupPlugin=disabled\n"))

;; shepherd's (requirement '(munged)) only waits for munged's fork to
;; succeed, not for it to finish initializing and create its socket
;; -- slurmctld/slurmd/slurmrestd starting that split second too early
;; hit a one-shot, fatal "Munge encode failed" at auth-plugin init and
;; never retry on their own even though shepherd still reports them
;; "running" (a stale PID). Wrap each in a real wait for the socket.
;; slurmrestd additionally gets /etc/slurm/jwt-bootstrap.token (written
;; at every activation, see %jwt-fixed-bootstrap-token below) exported as
;; SLURM_JWT, which is what makes auth_g_init() pick auth/jwt for it --
;; slurmctld and slurmd must NOT get it, or they would sign and verify
;; daemon RPCs with auth/jwt (see the header comment).  Read fresh on every
;; (re)start, not baked into the wrapper at build time.
(define* (wait-for-munge-then-exec name binary jwt? . args)
  "Return a program that waits until munged has created its socket and then
execs BINARY with ARGS.  With JWT? set, export the bootstrap token as
SLURM_JWT first -- auth_g_init() reads that variable and switches the
process to auth/jwt, which only slurmrestd should do: doing it for
slurmctld or slurmd makes them sign and verify daemon RPCs with auth/jwt,
which slurmd cannot decode (it never loads the JWT key)."
  (program-file
   (string-append "wait-for-munge-then-" name
                  (if jwt? "-jwt" ""))
   #~(begin
       (use-modules (ice-9 rdelim))
       (let loop ((n 0))
         (unless (or (file-exists? "/var/run/munge/munge.socket.2")
                     (> n 60))
           (sleep 1)
           (loop (+ n 1))))
       (when #$jwt?
         (let ((token-file "/etc/slurm/jwt-bootstrap.token"))
           (when (file-exists? token-file)
             (setenv "SLURM_JWT"
                     (string-trim-right
                      (call-with-input-file token-file read-line))))))
       (apply execl #$binary #$binary '#$args))))

(define slurm-conf-file
  (plain-file "slurm.conf"
              (string-append
               "ClusterName=slurm-container\n"
               "SlurmctldHost=localhost\n"
               "SlurmUser=slurm\n"
               "\n"
               ;; Munge authenticates the daemon plane (slurmctld ->
               ;; slurmd job launch); auth/jwt is accepted as an
               ;; *alternate* type by slurmctld for REST-submitted RPCs.
               ;; A cluster-wide AuthType=auth/jwt breaks job launch:
               ;; slurmd does not load the JWT key, cannot decode the
               ;; credential, and every launch RPC arrives as
               ;; SLURM_AUTH_NOBODY.
               "AuthType=auth/munge\n"
               "AuthAltTypes=auth/jwt\n"
               "AuthAltParameters=jwt_key=" %jwt-key "\n"
               "\n"
               "SlurmctldPort=6817\n"
               "SlurmdPort=6818\n"
               "SlurmdSpoolDir=/var/spool/slurmd\n"
               "StateSaveLocation=/var/spool/slurmctld\n"
               "SlurmctldLogFile=/var/log/slurm/slurmctld.log\n"
               "SlurmdLogFile=/var/log/slurm/slurmd.log\n"
               "\n"
               "ProctrackType=proctrack/linuxproc\n"
               "TaskPlugin=task/none\n"
               "SchedulerType=sched/backfill\n"
               ;; Default scheduler/backfill cycles are tens of seconds
               ;; (a job that is ready to run still waits for the next
               ;; cycle), which for a workflow that submits its steps a
               ;; few seconds apart becomes the dominant cost of a whole
               ;; run: 6 one-second jobs took ~3 minutes. With capacity
               ;; for 30 jobs the cycle length is what decides whether a
               ;; 40-job scatter starts in one go or trickles, so the
               ;; cycle is a second and one cycle may start them all.
               ;; These settings are for a throwaway test container, not a
               ;; deployment.
               "SchedulerParameters=sched_interval=1,sched_min_interval=1,bf_interval=1,bf_max_job_start=60\n"
               "SelectType=select/cons_tres\n"
               "SelectTypeParameters=CR_Core\n"
               ;; One job = one core = 1 GiB reserved. Without a
               ;; DefMemPerCPU a job that requests no memory is allocated
               ;; the node's whole memory, which queues the next job
               ;; behind it however many cores are free.
               "DefMemPerCPU=1024\n"
               ;; The conf, not slurmd's own probe, decides the node's
               ;; size: the container sees the host's cores and memory,
               ;; and slurmd refuses to start when the two disagree.
               "SlurmdParameters=config_overrides\n"
               "ReturnToService=2\n"
               "MpiDefault=none\n"
               "\n"
               ;; No slurmdbd/mariadb in this throwaway container --
               ;; just enough to prove ravanan's slurm-api submit/poll
               ;; cycle runs a real job to completion.
               "AccountingStorageType=accounting_storage/none\n"
               "JobAcctGatherType=jobacct_gather/none\n"
               ;; Without an accounting database slurmctld is the only
               ;; place a job's state ever lives, so keep completed jobs
               ;; there for the whole run instead of the 300s default.
               ;; Once a job is purged, a client that polls it again can
               ;; only fall back to slurmdbd (which does not exist
               ;; here) and gets a 404 -- ravanan does exactly that when
               ;; it re-reads a scattered step's state late in a
               ;; multi-job workflow.
               "MinJobAge=86400\n"
               "\n"
               ;; 30 CPUs because that is what the scatter needs: measured
               ;; with CPUs=2, a 30-job probe ran 2 at a time and ravanan's
               ;; 40-job LOCO workflow (20 chromosomes x kinship+gwa) took
               ;; 102 s cold against 14.3 s single-machine. RealMemory is
               ;; generous because the container uses the host's RAM; the
               ;; per-job share is DefMemPerCPU above, not this number.
               "NodeName=slurm-container NodeAddr=127.0.0.1 CPUs=30 RealMemory=65536 State=UNKNOWN\n"
               "PartitionName=debug Nodes=slurm-container Default=YES MaxTime=INFINITE State=UP\n")))

;; The JWT key is fixed, not random: `scontrol token` cannot mint one
;; here (it needs an existing JWT in the environment before the plugin
;; will even initialize), and shepherd's restart/stop handling hangs for
;; slurmctld/slurmrestd in this container runtime, so a "mint under
;; auth/munge, then switch to auth/jwt and restart" dance is impractical.
;; shipped in the image, so it is in place before the daemons first start.
;; Fine for a throwaway test container, not a real deployment: do not
;; reuse this key or token anywhere real.
(define %jwt-fixed-key-bytes
  (u8-list->bytevector (iota 32)))
;; Token for "testuser" (sun claim), 5-year expiry, computed offline
;; against %jwt-fixed-key-bytes with HMAC-SHA256 (HS256) -- see the
;; commit introducing this file for the one-off Python snippet used.
(define %jwt-fixed-bootstrap-token
  "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5NTAxOTY2MTYsImlhdCI6MTc5MjUxNjYxNiwic3VuIjoidGVzdHVzZXIifQ.zsTqK2yvFbIQP-2hJwia6JsqtBmtxvNZjNvqAGdJfJ4")

;; Generate the munge key once, at first activation (it is only used
;; for CredType job-step credentials here, not for cluster-wide RPC
;; auth, so it does not need to be fixed across boots), write the
;; fixed JWT key and its pre-computed bootstrap token, and lay out the
;; rest of /etc/slurm, /etc/munge, and the various spool/log dirs.
(define slurm-keys-activation
  #~(begin
      (use-modules (guix build utils))
      (use-modules (ice-9 binary-ports))
      (define (write-random-key path n-bytes)
        (unless (file-exists? path)
          (let ((bytes (call-with-input-file "/dev/urandom"
                         (lambda (urandom) (get-bytevector-n urandom n-bytes))
                         #:binary #t)))
            (call-with-output-file path
              (lambda (port) (put-bytevector port bytes))
              #:binary #t))
          (chmod path #o400)))
      (mkdir-p "/etc/munge")
      (mkdir-p "/var/run/munge")
      (mkdir-p "/var/log/munge")
      (mkdir-p "/var/lib/munge")
      (mkdir-p #$%slurm-etc)
      (mkdir-p "/var/spool/slurmd")
      (mkdir-p "/var/spool/slurmctld")
      (mkdir-p "/var/log/slurm")
      (write-random-key #$%munge-key 1024)
      (call-with-output-file #$%jwt-key
        (lambda (port) (put-bytevector port '#$%jwt-fixed-key-bytes))
        #:binary #t)
      (chmod #$%jwt-key #o400)
      (call-with-output-file "/etc/slurm/jwt-bootstrap.token"
        (lambda (port) (display #$%jwt-fixed-bootstrap-token port)))
      (copy-file #$cgroup-conf-file #$%cgroup-conf)
      (copy-file #$slurm-conf-file #$%slurm-conf)
      ;; slurmctld (running as root, dropping to SlurmUser) checks
      ;; StateSaveLocation/SlurmdSpoolDir ownership *before* dropping
      ;; privileges -- they must already belong to SlurmUser.
      ;; Likewise munge's own key/state must belong to the munge user.
      (let ((slurm-uid (passwd:uid (getpwnam "slurm")))
            (slurm-gid (passwd:gid (getpwnam "slurm")))
            (munge-uid (passwd:uid (getpwnam "munge")))
            (munge-gid (passwd:gid (getpwnam "munge"))))
        (chown "/var/spool/slurmctld" slurm-uid slurm-gid)
        (chown "/var/spool/slurmd" slurm-uid slurm-gid)
        (chown "/var/log/slurm" slurm-uid slurm-gid)
        (chown "/etc/munge" munge-uid munge-gid)
        (chown #$%munge-key munge-uid munge-gid)
        (chown "/var/run/munge" munge-uid munge-gid)
        (chown "/var/log/munge" munge-uid munge-gid)
        (chown "/var/lib/munge" munge-uid munge-gid))))

;; What this container is, said once.  A store path is a hash and a pid says
;; nothing; this string is the OS issue field, the ssh banner and the first line
;; of the boot log.
(define %slurm-banner
  "slurm-container -- Slurm (slurmctld, slurmd, slurmrestd :6820, ssh :2222)\nThis is a container, not the host.  Exit the shell and you are back on the\nhost; the definition is gn-machines/specials/slurm-container.scm.\n")

(operating-system
  (host-name "slurm-container")
  (issue %slurm-banner)
  (timezone "Europe/Amsterdam")
  (locale "en_US.utf8")
  (bootloader (bootloader-configuration
               (bootloader grub-bootloader)
               (targets '("/dev/sdX"))))
  (file-systems (cons (file-system
                        (device (file-system-label "does-not-matter"))
                        (mount-point "/")
                        (type "ext4"))
                      %base-file-systems))

  (users (cons* (user-account
                 (name "root")
                 (uid 0)
                 (group "root")
                 ;; Password "slurm" -- this is a throwaway test
                 ;; container, not a production deployment.
                 (password (crypt "slurm" "$6$abc$0123456789abcdef$")))
                (user-account
                 (name "munge")
                 (system? #t)
                 (group "munge")
                 (comment "munge daemon")
                 (home-directory "/var/lib/munge")
                 (shell (file-append shadow "/sbin/nologin")))
                (user-account
                 (name "slurm")
                 (system? #t)
                 (group "slurm")
                 (comment "SlurmUser -- slurmctld's own identity, distinct from root")
                 (home-directory "/var/lib/slurm")
                 (shell (file-append shadow "/sbin/nologin")))
                (user-account
                 (name "slurmrestd")
                 (system? #t)
                 (group "slurmrestd")
                 (comment "Runs slurmrestd -- must be neither root nor SlurmUser")
                 (home-directory "/var/lib/slurmrestd")
                 (shell (file-append shadow "/sbin/nologin")))
                ;; A real, ordinary user to submit test jobs as via
                ;; the REST API.
                (user-account
                 (name "testuser")
                 (comment "Submits test jobs via the REST API")
                 (password (crypt "slurm" "$6$abc$0123456789abcdef$"))
                 (group "users")
                 (supplementary-groups '("users")))
                %base-user-accounts))
  (groups (cons* (user-group (name "munge") (system? #t))
                 (user-group (name "slurm") (system? #t))
                 (user-group (name "slurmrestd") (system? #t))
                 %base-groups))

  (packages (cons* slurm munge %base-packages))

  (services
   (append
    (list
     (simple-service 'container-banner-files
                     etc-service-type
                     (list (list "issue.net" (plain-file "issue.net" %slurm-banner))
                           (list "motd" (plain-file "motd" %slurm-banner))))
     (simple-service 'container-banner
                     shepherd-root-service-type
                     (list (shepherd-service
                            (provision '(container-banner))
                            (one-shot? #t)
                            (start #~(lambda ()
                                       (display #$%slurm-banner)
                                       (force-output)
                                       (call-with-output-file
                                           "/var/log/container-banner.log"
                                         (lambda (port) (display #$%slurm-banner port)))
                                       #t)))))
     (service openssh-service-type
              (openssh-configuration
               (port-number 2222)
               (permit-root-login #t)
               ;; sshd shows this BEFORE authentication: you learn which container
               ;; you are entering before you are in it.  /etc/issue.net is written
               ;; below by the container-banner-files service.
               (extra-content "Banner /etc/issue.net\n")
               (password-authentication? #t)))

     ;; sshd needs the privilege separation directory /var/empty; on
     ;; a full system it is created as the home directory of some
     ;; system account, but a minimal container has none.
     (simple-service 'container-var-empty
                     activation-service-type
                     (with-imported-modules '((guix build utils))
                       #~(begin
                           (use-modules (guix build utils))
                           (mkdir-p "/var/empty"))))

     ;; A minimal Guix System has no /bin/bash, but batch scripts do:
     ;; slurm runs the job script through its shebang, and ravanan submits
     ;; every step as "#!/bin/bash\n<store script>".  Real cluster nodes
     ;; have /bin/bash; give the container the same courtesy.
     (simple-service 'container-bin-shell
                     activation-service-type
                     (with-imported-modules '((guix build utils))
                       #~(begin
                           (use-modules (guix build utils))
                           (mkdir-p "/bin")
                           (symlink #$(file-append bash "/bin/bash")
                                    "/bin/bash")
                           (symlink "bash" "/bin/sh"))))

     (simple-service 'slurm-keys-and-conf
                     activation-service-type
                     slurm-keys-activation)

     (simple-service 'munged
                     shepherd-root-service-type
                     (list (shepherd-service
                            (provision '(munged))
                            (requirement '(user-processes))
                            (start #~(make-forkexec-constructor
                                      (list #$(file-append munge "/sbin/munged")
                                            "--force"
                                            (string-append "--key-file=" #$%munge-key)
                                            "--socket=/var/run/munge/munge.socket.2"
                                            "--pid-file=/var/run/munge/munged.pid"
                                            "--seed-file=/var/lib/munge/munged.seed")
                                      #:log-file "/var/log/slurm/munged.log"))
                            (stop #~(make-kill-destructor)))))

     (simple-service 'slurmctld
                     shepherd-root-service-type
                     (list (shepherd-service
                            (provision '(slurmctld))
                            (requirement '(munged))
                            (start #~(make-forkexec-constructor
                                      (list #$(wait-for-munge-then-exec
                                               "slurmctld"
                                               (file-append slurm "/sbin/slurmctld")
                                               #f
                                               "-D" "-c" "-f" %slurm-conf))
                                      #:log-file "/var/log/slurm/slurmctld-stdout.log"))
                            (stop #~(make-kill-destructor)))))

     (simple-service 'slurmd
                     shepherd-root-service-type
                     (list (shepherd-service
                            (provision '(slurmd))
                            (requirement '(munged slurmctld))
                            (start #~(make-forkexec-constructor
                                      (list #$(wait-for-munge-then-exec
                                               "slurmd"
                                               (file-append slurm "/sbin/slurmd")
                                               #f
                                               "-D" "-c" "-f" %slurm-conf))
                                      #:log-file "/var/log/slurm/slurmd-stdout.log"))
                            (stop #~(make-kill-destructor)))))

     (simple-service 'slurmrestd
                     shepherd-root-service-type
                     (list (shepherd-service
                            (provision '(slurmrestd))
                            (requirement '(munged slurmctld slurmd))
                            (start #~(make-forkexec-constructor
                                      (list #$(wait-for-munge-then-exec
                                               "slurmrestd"
                                               (file-append slurm "/sbin/slurmrestd")
                                               #t
                                               "-f" %slurm-conf
                                               "-a" "rest_auth/jwt"
                                               "-s" "openapi/slurmctld"
                                               "0.0.0.0:6820"))
                                      ;; Run as a dedicated, unprivileged
                                      ;; "slurmrestd" account: slurmrestd
                                      ;; refuses to run as root or as
                                      ;; SlurmUser, and its become_user mode
                                      ;; is incompatible with
                                      ;; rest_auth/jwt.  No setuid() is
                                      ;; needed for identity anyway -- the
                                      ;; caller's token is forwarded to
                                      ;; slurmctld, which decodes it with
                                      ;; jwt_key and runs the job as that
                                      ;; user (see the header comment).
                                      #:user "slurmrestd"
                                      #:group "slurmrestd"
                                      #:log-file "/var/log/slurm/slurmrestd-stdout.log"))
                            (stop #~(make-kill-destructor)))))

     ;; In a container sysctl (writes /proc/sys) and udev (coldplug on
     ;; the static /dev the container runtime sets up) are useless and
     ;; their start-up hangs the whole boot: urandom-seed requires
     ;; udev, user-processes requires urandom-seed, and ssh-daemon
     ;; requires user-processes.  Provide no-ops instead.
     (simple-service 'container-sysctl
                     shepherd-root-service-type
                     (list (shepherd-service
                            (provision '(sysctl))
                            (one-shot? #t)
                            (start #~(const #t)))))
     (simple-service 'container-udev
                     shepherd-root-service-type
                     (list (shepherd-service
                            (provision '(udev))
                            (one-shot? #t)
                            (start #~(const #t))))))
    (modify-services %base-services
      ;; Drop the guix service: its activation runs 'guix archive
      ;; --generate-key' at every boot, which stalled the container
      ;; boot; a minimal container does not need guix-daemon anyway.
      (delete guix-service-type)
      (delete sysctl-service-type)
      (delete udev-service-type)))))