From unknown Fri Sep 19 16:51:38 2025 X-Loop: help-debbugs@gnu.org Subject: [bug#57076] [PATCH] linux-container: container-script: Parse command line options. Resent-From: Ricardo Wurmus Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 09 Aug 2022 12:58:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 57076 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 57076@debbugs.gnu.org Cc: Ricardo Wurmus X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.166004983824002 (code B ref -1); Tue, 09 Aug 2022 12:58:01 +0000 Received: (at submit) by debbugs.gnu.org; 9 Aug 2022 12:57:18 +0000 Received: from localhost ([127.0.0.1]:42836 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oLOnO-0006F3-Ax for submit@debbugs.gnu.org; Tue, 09 Aug 2022 08:57:18 -0400 Received: from lists.gnu.org ([209.51.188.17]:55786) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oLOnK-0006En-2z for submit@debbugs.gnu.org; Tue, 09 Aug 2022 08:57:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52864) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oLOnJ-0000kL-St for guix-patches@gnu.org; Tue, 09 Aug 2022 08:57:13 -0400 Received: from sender4-of-o51.zoho.com ([136.143.188.51]:21177) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oLOnG-0003JA-Cr for guix-patches@gnu.org; Tue, 09 Aug 2022 08:57:13 -0400 ARC-Seal: i=1; a=rsa-sha256; t=1660049826; cv=none; d=zohomail.com; s=zohoarc; b=H3kBsCpGOvXSnq1mW/HdY/pPvX21/5WLQtI6O3Kk1rmRtlzG4yvEpexI1VPMm9ot6oOKwdCPwds1yF3liEc+DLgmdIuiOOlNXCN/MlvhabI6RRnzbgqkgauJAAjXMFTDwV5r5xRq/Od1sT8YdfrCKO5gjggjf2wbeclEWZ3d1+w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1660049826; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:MIME-Version:Message-ID:Subject:To; bh=A6uDV6VuamTXvt46yMdepMbNuaubH19bioVl8ezkjNQ=; b=QubI71bGudQaa9VPCji8MyyLgi+RVpu3obfCpdS2KqAXl1/ptMvw5xfKXeXOIKeGcsbv+/jU9mq20WdZ9IALo9iJgkUrsyIEawrHGpdI5W8TcEkgTNjNuposW+A0XqHE0S28vDy2k9HRKphaM3+098bGVbdgFzYbdc04FAA7i0k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=elephly.net; spf=pass smtp.mailfrom=rekado@elephly.net; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1660049826; s=zoho; d=elephly.net; i=rekado@elephly.net; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-Id:Message-Id:MIME-Version:Content-Type:Content-Transfer-Encoding:Reply-To; bh=A6uDV6VuamTXvt46yMdepMbNuaubH19bioVl8ezkjNQ=; b=YsGWpDXtkzL1Du7iNJ3JnmqMddPgGmo4qAVf7Xy61kuLQwF0ZhF1q3v9yjs58snT Mdf3kE4HRs5Cleh4ZVTlNIb8yvXKib3yd/HeP53P1ESn4Qb+ci+xikkFxIGLy0nURTb EnOwZUjtidqRuVq0gTaUPx5MRwOWHU15pXmHYPI0= Received: from localhost (i5E862C49.versanet.de [94.134.44.73]) by mx.zohomail.com with SMTPS id 1660049824136937.9373657577713; Tue, 9 Aug 2022 05:57:04 -0700 (PDT) From: Ricardo Wurmus Date: Tue, 9 Aug 2022 14:56:59 +0200 Message-Id: <20220809125659.12302-1-rekado@elephly.net> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-ZohoMailClient: External Received-SPF: pass client-ip=136.143.188.51; envelope-from=rekado@elephly.net; helo=sender4-of-o51.zoho.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.3 (-) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -2.3 (--) * gnu/system/linux-container.scm (container-script): Accept command line options to bind mount host directories into the container. --- gnu/system/linux-container.scm | 97 +++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 25 deletions(-) diff --git a/gnu/system/linux-container.scm b/gnu/system/linux-container.scm index 24077e347a..69080bcacb 100644 --- a/gnu/system/linux-container.scm +++ b/gnu/system/linux-container.scm @@ -4,6 +4,7 @@ ;;; Copyright © 2019 Arun Isaac ;;; Copyright © 2020 Efraim Flashner ;;; Copyright © 2020 Google LLC +;;; Copyright © 2022 Ricardo Wurmus ;;; ;;; This file is part of GNU Guix. ;;; @@ -202,16 +203,49 @@ (define script (guix build utils) (guix i18n) (guix diagnostics) - (srfi srfi-1)) + (srfi srfi-1) + (srfi srfi-37) + (ice-9 match)) - (define file-systems - (filter-map (lambda (spec) - (let* ((fs (spec->file-system spec)) - (flags (file-system-flags fs))) - (and (or (not (memq 'bind-mount flags)) - (file-exists? (file-system-device fs))) - fs))) - '#$specs)) + (define (show-help) + (display (G_ "Usage: run-container [OPTION ...] +Run the container with the given options.")) + (newline) + (display (G_ " + --share=SPEC share host file system with read/write access + according to SPEC")) + (display (G_ " + --expose=SPEC expose host file system directory as read-only + according to SPEC")) + (newline) + (display (G_ " + -h, --help display this help and exit")) + (newline)) + + (define %options + ;; Specifications of the command-line options. + (list (option '(#\h "help") #f #f + (lambda args + (show-help) + (exit 0))) + (option '("share") #t #f + (lambda (opt name arg result) + (alist-cons 'file-system-mapping + (specification->file-system-mapping arg #t) + result))) + (option '("expose") #t #f + (lambda (opt name arg result) + (alist-cons 'file-system-mapping + (specification->file-system-mapping arg #f) + result))))) + + (define (parse-options args options) + (args-fold args options + (lambda (opt name arg . rest) + (report-error (G_ "~A: unrecognized option~%") name) + (exit 1)) + (lambda (op res) (cons op res)) + '())) (define (explain pid) ;; XXX: We can't quite call 'bindtextdomain' so there's actually @@ -225,22 +259,35 @@ (define (explain pid) (info (G_ "or run 'sudo nsenter -a -t ~a' to get a shell into it.~%") pid) (newline (guix-warning-port))) - (call-with-container file-systems - (lambda () - (setenv "HOME" "/root") - (setenv "TMPDIR" "/tmp") - (setenv "GUIX_NEW_SYSTEM" #$os) - (for-each mkdir-p '("/run" "/bin" "/etc" "/home" "/var")) - (primitive-load (string-append #$os "/boot"))) - ;; A range of 65536 uid/gids is used to cover 16 bits worth of - ;; users and groups, which is sufficient for most cases. - ;; - ;; See: http://www.freedesktop.org/software/systemd/man/systemd-nspawn.html#--private-users= - #:host-uids 65536 - #:namespaces (if #$shared-network? - (delq 'net %namespaces) - %namespaces) - #:process-spawned-hook explain)))) + (let* ((opts (parse-options (cdr (command-line)) %options)) + (mappings (filter-map (match-lambda + (('file-system-mapping . mapping) mapping) + (_ #f)) + opts)) + (file-systems + (filter-map (lambda (fs) + (let ((flags (file-system-flags fs))) + (and (or (not (memq 'bind-mount flags)) + (file-exists? (file-system-device fs))) + fs))) + (append (map file-system-mapping->bind-mount mappings) + (map spec->file-system '#$specs))))) + (call-with-container file-systems + (lambda () + (setenv "HOME" "/root") + (setenv "TMPDIR" "/tmp") + (setenv "GUIX_NEW_SYSTEM" #$os) + (for-each mkdir-p '("/run" "/bin" "/etc" "/home" "/var")) + (primitive-load (string-append #$os "/boot"))) + ;; A range of 65536 uid/gids is used to cover 16 bits worth of + ;; users and groups, which is sufficient for most cases. + ;; + ;; See: http://www.freedesktop.org/software/systemd/man/systemd-nspawn.html#--private-users= + #:host-uids 65536 + #:namespaces (if #$shared-network? + (delq 'net %namespaces) + %namespaces) + #:process-spawned-hook explain))))) (gexp->script "run-container" script))) -- 2.36.1 From unknown Fri Sep 19 16:51:38 2025 X-Loop: help-debbugs@gnu.org Subject: [bug#57076] [PATCH] linux-container: container-script: Parse command line options. Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 09 Aug 2022 14:55:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 57076 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Ricardo Wurmus Cc: 57076@debbugs.gnu.org Received: via spool by 57076-submit@debbugs.gnu.org id=B57076.166005684712297 (code B ref 57076); Tue, 09 Aug 2022 14:55:02 +0000 Received: (at 57076) by debbugs.gnu.org; 9 Aug 2022 14:54:07 +0000 Received: from localhost ([127.0.0.1]:44455 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oLQcQ-0003CH-Q4 for submit@debbugs.gnu.org; Tue, 09 Aug 2022 10:54:07 -0400 Received: from eggs.gnu.org ([209.51.188.92]:35712) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oLQcO-0003Bf-1l for 57076@debbugs.gnu.org; Tue, 09 Aug 2022 10:54:06 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:39808) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oLQcI-0005iA-N9; Tue, 09 Aug 2022 10:53:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:In-Reply-To:Date:References:Subject:To: From; bh=KnwSaM4jEv8eKRlgN//pS0oUpeW3u68asVAdmpENrbQ=; b=Pu6pV6IUTV6hPTFg9oIw rNDEqrhzbjisZQS7f+qB+m+Nrqbg7cKbH3XMqqGI1JqgXzdZt/EPFRr0oRc8DDPu9hFk8ozRwPAlb BcH6ZRerbpwF3b1oDSXTK7XqRa874d3O/YYMcXWEsMXith+dPMxNRKJFEtkUaHF6FbsFdCJjF+00h n6InhcOGv4+ahh7uw5LX2bqbnPgDN+FUyObyqCNYJ/4Y59ke0WOH3ACRD1W00Mdke+xDA653r0qEh cmysp65zYOQFlawInjjk/uRznqSinUwn4QrTuQXdbYhW1g7UVcN5sInCIcJfyAPAUozdlzLryvVsg zZ9C5CacZiDTEA==; Received: from [2001:660:6102:320:e120:2c8f:8909:cdfe] (port=55326 helo=ribbon) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oLQcI-0000GO-9B; Tue, 09 Aug 2022 10:53:58 -0400 From: Ludovic =?UTF-8?Q?Court=C3=A8s?= References: <20220809125659.12302-1-rekado@elephly.net> Date: Tue, 09 Aug 2022 16:53:56 +0200 In-Reply-To: <20220809125659.12302-1-rekado@elephly.net> (Ricardo Wurmus's message of "Tue, 9 Aug 2022 14:56:59 +0200") Message-ID: <875yj179iz.fsf@gnu.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: -2.3 (--) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) Hi, Ricardo Wurmus skribis: > * gnu/system/linux-container.scm (container-script): Accept command line > options to bind mount host directories into the container. I like that, go for it! Perhaps you can add a line in doc/guix.texi, under =E2=80=98container=E2=80=99 in =E2=80=9CInvoking guix system=E2=80=9D= , like: The @option{--share} and @option{--expose} can also be passed to the generated script. Thanks, Ludo=E2=80=99. From unknown Fri Sep 19 16:51:38 2025 MIME-Version: 1.0 X-Mailer: MIME-tools 5.505 (Entity 5.505) X-Loop: help-debbugs@gnu.org From: help-debbugs@gnu.org (GNU bug Tracking System) To: Ricardo Wurmus Subject: bug#57076: closed (Re: bug#57076: [PATCH] linux-container: container-script: Parse command line options.) Message-ID: References: <87pmh9uuqe.fsf@elephly.net> <20220809125659.12302-1-rekado@elephly.net> X-Gnu-PR-Message: they-closed 57076 X-Gnu-PR-Package: guix-patches X-Gnu-PR-Keywords: patch Reply-To: 57076@debbugs.gnu.org Date: Tue, 09 Aug 2022 18:40:02 +0000 Content-Type: multipart/mixed; boundary="----------=_1660070402-3705-1" This is a multi-part message in MIME format... ------------=_1660070402-3705-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Your bug report #57076: [PATCH] linux-container: container-script: Parse command line optio= ns. which was filed against the guix-patches package, has been closed. The explanation is attached below, along with your original report. If you require more details, please reply to 57076@debbugs.gnu.org. --=20 57076: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=3D57076 GNU Bug Tracking System Contact help-debbugs@gnu.org with problems ------------=_1660070402-3705-1 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit Received: (at 57076-done) by debbugs.gnu.org; 9 Aug 2022 18:39:52 +0000 Received: from localhost ([127.0.0.1]:44948 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oLU8t-0000xG-O7 for submit@debbugs.gnu.org; Tue, 09 Aug 2022 14:39:51 -0400 Received: from sender4-of-o50.zoho.com ([136.143.188.50]:21036) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oLU8s-0000x8-E9 for 57076-done@debbugs.gnu.org; Tue, 09 Aug 2022 14:39:50 -0400 ARC-Seal: i=1; a=rsa-sha256; t=1660070381; cv=none; d=zohomail.com; s=zohoarc; b=AD9YDVXAtA7cPjIc9jjV+21ilopktXZUjuiSLjYMznjOoSqtZfiPlq7OJ8lByFt1KlzrA7EcJPj3UKvyGZ4BwxbezaypI+MbCmyu2kffJPSosrOVqkea3H6vs6xDnvljAE1389ed/Rzb7Q3dOsd6GPQjGPjJczoia/j9OdjYwuY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1660070381; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=uGQNfhZ3zcbqGJGlix8ld/5jknt2p7D++jLtbBleix8=; b=oLAqeYV4hHwAfz9dlWuAO6sqZ2W+WOoZee/lgJN8V76i/rMndgOiDRgRabcggBAnXFwvF79S7ArnePbVAKIfTnJ6ORCBH5dtA66bTg6DJMNcrTYimdD5OsePIN9eOEd5Hum9TD0h2D7riLTTPpfdz2EVVnL7rwimBNOI8IQbMBQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=elephly.net; spf=pass smtp.mailfrom=rekado@elephly.net; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1660070381; s=zoho; d=elephly.net; i=rekado@elephly.net; h=References:From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:In-reply-to:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Reply-To; bh=uGQNfhZ3zcbqGJGlix8ld/5jknt2p7D++jLtbBleix8=; b=ZqM0Sg5Q9LwJXuuOmSUOHVX+uh5yiShEEFrr5XxFAbJwYi93FjFl29KTXwUx0mCZ ySrMXZ90h2IKfZgyA5DefINfFKweI0PGitePQKuY+2iLUjjav08dSTHsJra1t0skyT3 ye90SYmGAbPivr7z/zGdWgZHWbuIEJKeNfuFnb18= Received: from localhost (i5E862C49.versanet.de [94.134.44.73]) by mx.zohomail.com with SMTPS id 1660070380144392.57688798420634; Tue, 9 Aug 2022 11:39:40 -0700 (PDT) References: <20220809125659.12302-1-rekado@elephly.net> <875yj179iz.fsf@gnu.org> User-agent: mu4e 1.8.7; emacs 28.1 From: Ricardo Wurmus To: Ludovic =?utf-8?Q?Court=C3=A8s?= Subject: Re: bug#57076: [PATCH] linux-container: container-script: Parse command line options. Date: Tue, 09 Aug 2022 20:39:20 +0200 In-reply-to: <875yj179iz.fsf@gnu.org> X-URL: https://elephly.net X-PGP-Key: https://elephly.net/rekado.pubkey X-PGP-Fingerprint: BCA6 89B6 3655 3801 C3C6 2150 197A 5888 235F ACAC Message-ID: <87pmh9uuqe.fsf@elephly.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-ZohoMailClient: External X-Spam-Score: -0.0 (/) X-Debbugs-Envelope-To: 57076-done Cc: 57076-done@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) Ludovic Court=C3=A8s writes: > Ricardo Wurmus skribis: > >> * gnu/system/linux-container.scm (container-script): Accept command line >> options to bind mount host directories into the container. > > I like that, go for it! Perhaps you can add a line in doc/guix.texi, > under =E2=80=98container=E2=80=99 in =E2=80=9CInvoking guix system=E2=80= =9D, like: > > The @option{--share} and @option{--expose} can also be passed to the > generated script. Done. Thanks for the quick review! --=20 Ricardo ------------=_1660070402-3705-1 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit Received: (at submit) by debbugs.gnu.org; 9 Aug 2022 12:57:18 +0000 Received: from localhost ([127.0.0.1]:42836 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oLOnO-0006F3-Ax for submit@debbugs.gnu.org; Tue, 09 Aug 2022 08:57:18 -0400 Received: from lists.gnu.org ([209.51.188.17]:55786) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oLOnK-0006En-2z for submit@debbugs.gnu.org; Tue, 09 Aug 2022 08:57:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52864) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oLOnJ-0000kL-St for guix-patches@gnu.org; Tue, 09 Aug 2022 08:57:13 -0400 Received: from sender4-of-o51.zoho.com ([136.143.188.51]:21177) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oLOnG-0003JA-Cr for guix-patches@gnu.org; Tue, 09 Aug 2022 08:57:13 -0400 ARC-Seal: i=1; a=rsa-sha256; t=1660049826; cv=none; d=zohomail.com; s=zohoarc; b=H3kBsCpGOvXSnq1mW/HdY/pPvX21/5WLQtI6O3Kk1rmRtlzG4yvEpexI1VPMm9ot6oOKwdCPwds1yF3liEc+DLgmdIuiOOlNXCN/MlvhabI6RRnzbgqkgauJAAjXMFTDwV5r5xRq/Od1sT8YdfrCKO5gjggjf2wbeclEWZ3d1+w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1660049826; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:MIME-Version:Message-ID:Subject:To; bh=A6uDV6VuamTXvt46yMdepMbNuaubH19bioVl8ezkjNQ=; b=QubI71bGudQaa9VPCji8MyyLgi+RVpu3obfCpdS2KqAXl1/ptMvw5xfKXeXOIKeGcsbv+/jU9mq20WdZ9IALo9iJgkUrsyIEawrHGpdI5W8TcEkgTNjNuposW+A0XqHE0S28vDy2k9HRKphaM3+098bGVbdgFzYbdc04FAA7i0k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=elephly.net; spf=pass smtp.mailfrom=rekado@elephly.net; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1660049826; s=zoho; d=elephly.net; i=rekado@elephly.net; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-Id:Message-Id:MIME-Version:Content-Type:Content-Transfer-Encoding:Reply-To; bh=A6uDV6VuamTXvt46yMdepMbNuaubH19bioVl8ezkjNQ=; b=YsGWpDXtkzL1Du7iNJ3JnmqMddPgGmo4qAVf7Xy61kuLQwF0ZhF1q3v9yjs58snT Mdf3kE4HRs5Cleh4ZVTlNIb8yvXKib3yd/HeP53P1ESn4Qb+ci+xikkFxIGLy0nURTb EnOwZUjtidqRuVq0gTaUPx5MRwOWHU15pXmHYPI0= Received: from localhost (i5E862C49.versanet.de [94.134.44.73]) by mx.zohomail.com with SMTPS id 1660049824136937.9373657577713; Tue, 9 Aug 2022 05:57:04 -0700 (PDT) From: Ricardo Wurmus To: guix-patches@gnu.org Subject: [PATCH] linux-container: container-script: Parse command line options. Date: Tue, 9 Aug 2022 14:56:59 +0200 Message-Id: <20220809125659.12302-1-rekado@elephly.net> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-ZohoMailClient: External Received-SPF: pass client-ip=136.143.188.51; envelope-from=rekado@elephly.net; helo=sender4-of-o51.zoho.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.3 (-) X-Debbugs-Envelope-To: submit Cc: Ricardo Wurmus X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -2.3 (--) * gnu/system/linux-container.scm (container-script): Accept command line options to bind mount host directories into the container. --- gnu/system/linux-container.scm | 97 +++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 25 deletions(-) diff --git a/gnu/system/linux-container.scm b/gnu/system/linux-container.scm index 24077e347a..69080bcacb 100644 --- a/gnu/system/linux-container.scm +++ b/gnu/system/linux-container.scm @@ -4,6 +4,7 @@ ;;; Copyright © 2019 Arun Isaac ;;; Copyright © 2020 Efraim Flashner ;;; Copyright © 2020 Google LLC +;;; Copyright © 2022 Ricardo Wurmus ;;; ;;; This file is part of GNU Guix. ;;; @@ -202,16 +203,49 @@ (define script (guix build utils) (guix i18n) (guix diagnostics) - (srfi srfi-1)) + (srfi srfi-1) + (srfi srfi-37) + (ice-9 match)) - (define file-systems - (filter-map (lambda (spec) - (let* ((fs (spec->file-system spec)) - (flags (file-system-flags fs))) - (and (or (not (memq 'bind-mount flags)) - (file-exists? (file-system-device fs))) - fs))) - '#$specs)) + (define (show-help) + (display (G_ "Usage: run-container [OPTION ...] +Run the container with the given options.")) + (newline) + (display (G_ " + --share=SPEC share host file system with read/write access + according to SPEC")) + (display (G_ " + --expose=SPEC expose host file system directory as read-only + according to SPEC")) + (newline) + (display (G_ " + -h, --help display this help and exit")) + (newline)) + + (define %options + ;; Specifications of the command-line options. + (list (option '(#\h "help") #f #f + (lambda args + (show-help) + (exit 0))) + (option '("share") #t #f + (lambda (opt name arg result) + (alist-cons 'file-system-mapping + (specification->file-system-mapping arg #t) + result))) + (option '("expose") #t #f + (lambda (opt name arg result) + (alist-cons 'file-system-mapping + (specification->file-system-mapping arg #f) + result))))) + + (define (parse-options args options) + (args-fold args options + (lambda (opt name arg . rest) + (report-error (G_ "~A: unrecognized option~%") name) + (exit 1)) + (lambda (op res) (cons op res)) + '())) (define (explain pid) ;; XXX: We can't quite call 'bindtextdomain' so there's actually @@ -225,22 +259,35 @@ (define (explain pid) (info (G_ "or run 'sudo nsenter -a -t ~a' to get a shell into it.~%") pid) (newline (guix-warning-port))) - (call-with-container file-systems - (lambda () - (setenv "HOME" "/root") - (setenv "TMPDIR" "/tmp") - (setenv "GUIX_NEW_SYSTEM" #$os) - (for-each mkdir-p '("/run" "/bin" "/etc" "/home" "/var")) - (primitive-load (string-append #$os "/boot"))) - ;; A range of 65536 uid/gids is used to cover 16 bits worth of - ;; users and groups, which is sufficient for most cases. - ;; - ;; See: http://www.freedesktop.org/software/systemd/man/systemd-nspawn.html#--private-users= - #:host-uids 65536 - #:namespaces (if #$shared-network? - (delq 'net %namespaces) - %namespaces) - #:process-spawned-hook explain)))) + (let* ((opts (parse-options (cdr (command-line)) %options)) + (mappings (filter-map (match-lambda + (('file-system-mapping . mapping) mapping) + (_ #f)) + opts)) + (file-systems + (filter-map (lambda (fs) + (let ((flags (file-system-flags fs))) + (and (or (not (memq 'bind-mount flags)) + (file-exists? (file-system-device fs))) + fs))) + (append (map file-system-mapping->bind-mount mappings) + (map spec->file-system '#$specs))))) + (call-with-container file-systems + (lambda () + (setenv "HOME" "/root") + (setenv "TMPDIR" "/tmp") + (setenv "GUIX_NEW_SYSTEM" #$os) + (for-each mkdir-p '("/run" "/bin" "/etc" "/home" "/var")) + (primitive-load (string-append #$os "/boot"))) + ;; A range of 65536 uid/gids is used to cover 16 bits worth of + ;; users and groups, which is sufficient for most cases. + ;; + ;; See: http://www.freedesktop.org/software/systemd/man/systemd-nspawn.html#--private-users= + #:host-uids 65536 + #:namespaces (if #$shared-network? + (delq 'net %namespaces) + %namespaces) + #:process-spawned-hook explain))))) (gexp->script "run-container" script))) -- 2.36.1 ------------=_1660070402-3705-1--