From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id MOduKfVZ8mIFLAAAbAwnHQ (envelope-from ) for ; Tue, 09 Aug 2022 14:58:29 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id yNGrKPVZ8mJZIQAAG6o9tA (envelope-from ) for ; Tue, 09 Aug 2022 14:58:29 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id B43F39446 for ; Tue, 9 Aug 2022 14:58:28 +0200 (CEST) Received: from localhost ([::1]:41790 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oLOoV-0001ts-NJ for larch@yhetil.org; Tue, 09 Aug 2022 08:58:27 -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 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-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+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1660049909; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:list-id:list-help: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=A6uDV6VuamTXvt46yMdepMbNuaubH19bioVl8ezkjNQ=; b=TDdtXP7+Z8G8xiHy5lf1ymxbidGCcRD7OoT6fpNl/Ih9MW7FBg3R17uiIYGeK59qH23DzQ 8ZFSQ9nFngvGFnQANLr4zW2R8+fnbhMb+zNPnOyEFDIiZfXXs36DUSyr5vU05/wxn1BCio 62oH2LlCPUJuoePI58mp6/18ArC1C2BFT0tv4tNOEopL0NlTkvsL8YDARkat0o1qoXIyU2 GQuct/tomq6TuCORMjWgASEs49yUhEr23z+Nk0g6drZgFQfiEFO3/5Ra88zuy5O8BYloN2 7HeSsC1j4qQbyjNdM9NvGLRRXTgIF47TcWqhql3YaIsIWyaOuywzeCE4/K0d4g== ARC-Seal: i=2; s=key1; d=yhetil.org; t=1660049909; a=rsa-sha256; cv=fail; b=s+Ry8Qv9mjEpw+qDsBTtibewuBkbiEpGAmhBEy3C4wxTxiHuuFdvC9kDCSWoVlGNRWT2bF 6pkWyRqOqeVnlULkKoEE5WcANHnwVC9FoM3ximaC55ZoSc6L7+roIjd98eHTIAG2ry6tS8 zVFXMiprQdkMLdWaoiWqUmTxUZ6uqfiqcQQim9J4L3qWzuxhrp/ESu3YEcXpir9jfc47Yg L4g/ROnf32kmz8MlnrOTWEKT1AChog+6f3qFhbhXR5NjxnMprlm3KcMxtBHmQXH5O/cSvw Rt8NKRgMixO09Cwkmqo6cQmpIMwRNbs1E3xe0nqHDJemocPNG1uCxHw8jRvhDA== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=elephly.net header.s=zoho header.b=YsGWpDXt; arc=reject ("signature check failed: fail, {[1] = sig:zohomail.com:reject}"); dmarc=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: 1.61 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=elephly.net header.s=zoho header.b=YsGWpDXt; arc=reject ("signature check failed: fail, {[1] = sig:zohomail.com:reject}"); dmarc=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: B43F39446 X-Spam-Score: 1.61 X-Migadu-Scanner: scn0.migadu.com X-TUID: fNobCWW200QK * 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