;;; genenetwork-machines --- Guix configuration for genenetwork machines ;;; Copyright © 2026 Pjotr Prins ;;; ;;; 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 ;;; . ;;; 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= 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 ;;; 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". 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)))))