From patchwork Tue Aug 9 12:56:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ricardo Wurmus X-Patchwork-Id: 41429 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 8400D27BBE9; Tue, 9 Aug 2022 13:58:35 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 1E78827BBEA for ; Tue, 9 Aug 2022 13:58:34 +0100 (BST) Received: from localhost ([::1]:41790 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oLOob-0001ts-8f for patchwork@mira.cbaines.net; Tue, 09 Aug 2022 08:58:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53010) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oLOo6-0001rP-Cz for guix-patches@gnu.org; Tue, 09 Aug 2022 08:58:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:53092) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oLOo6-0003Og-47 for guix-patches@gnu.org; Tue, 09 Aug 2022 08:58:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oLOo6-0006GM-05 for guix-patches@gnu.org; Tue, 09 Aug 2022 08:58:02 -0400 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 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-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * 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)))