From 0f96ceb58fd886c75f607cf6c540ae2ae361b70e Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Tue, 23 Sep 2025 15:34:55 +1000 Subject: [PATCH] gnu: Add --pid-file option to "guix system container" scripts. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * gnu/system/linux-container.scm (container-script): Accept pid-file command line option to write out the container's process ID. * doc/guix.texi (Invoking guix system): Document new option. Change-Id: I93e8a99b39c1dd831f116104bf92c723d96c9965 Signed-off-by: Ludovic Courtès --- doc/guix.texi | 7 ++++++- gnu/system/linux-container.scm | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index ffc797ccf3cc2fc906bb74782c30cc72c7cc9102..272a1579ec5fc4fd314754a8a0b3e913ad5a97ec 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -47633,8 +47633,13 @@ The @option{--share} and @option{--expose} options can also be passed to the generated script to bind-mount additional directories into the container. +The generated script also supports the @option{--pid-file} option to +write the container process ID to a file. This can be used, along with +@command{guix container exec} to execute scripts within the container +(@pxref{Invoking guix container}). + @quotation Note -This option requires Linux-libre 3.19 or newer. +The @code{container} action requires Linux-libre 3.19 or newer. @end quotation @end table diff --git a/gnu/system/linux-container.scm b/gnu/system/linux-container.scm index 9bcdf24a7e06137db8ed0463964bb2361fc9e0bc..87a2100fcc44021087972611cbce5f58f74442f9 100644 --- a/gnu/system/linux-container.scm +++ b/gnu/system/linux-container.scm @@ -8,6 +8,7 @@ ;;; Copyright © 2023 Pierre Langlois ;;; Copyright © 2024 Leo Nikkilä ;;; Copyright © 2024 Andreas Enge +;;; Copyright © 2025 Carlo Zancanaro ;;; ;;; This file is part of GNU Guix. ;;; @@ -247,6 +248,9 @@ Run the container with the given options.")) (display (G_ " --expose=SPEC expose host file system directory as read-only according to SPEC")) + (display (G_ " + --pid-file=FILE write the process ID of the container's PID 1 + process to FILE")) (newline) (display (G_ " -h, --help display this help and exit")) @@ -267,6 +271,11 @@ Run the container with the given options.")) (lambda (opt name arg result) (alist-cons 'file-system-mapping (specification->file-system-mapping arg #f) + result))) + (option '("pid-file") #t #f + (lambda (opt name arg result) + (alist-cons 'pid-file + arg result))))) (define (parse-options args options) @@ -290,6 +299,10 @@ Run the container with the given options.")) (newline (guix-warning-port))) (let* ((opts (parse-options (cdr (command-line)) %options)) + (pid-files (filter-map (match-lambda + (('pid-file . filename) filename) + (_ #f)) + opts)) (mappings (filter-map (match-lambda (('file-system-mapping . mapping) mapping) (_ #f)) @@ -318,7 +331,13 @@ Run the container with the given options.")) (delq 'net %namespaces) %namespaces) #:writable-root? #t - #:process-spawned-hook explain))))) + #:process-spawned-hook (lambda (pid) + ;; Write out the PID to the requested files + (for-each (lambda (filename) + (call-with-output-file filename + (lambda (port) (write pid port)))) + pid-files) + (explain pid))))))) (gexp->script "run-container" script)))