build: linux-container: Fix run-container.
[jackhill/guix/guix.git] / gnu / build / linux-container.scm
index 95bfd92..2d4de78 100644 (file)
@@ -1,6 +1,6 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
-;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -22,7 +22,6 @@
   #:use-module (ice-9 match)
   #:use-module (ice-9 rdelim)
   #:use-module (srfi srfi-98)
-  #:use-module (guix utils)
   #:use-module (guix build utils)
   #:use-module (guix build syscalls)
   #:use-module (gnu system file-systems)          ;<file-system>
@@ -61,9 +60,14 @@ exists."
     (const #t)
     (lambda ()
       (thunk)
-      (primitive-exit 0))
+
+      ;; XXX: Somehow we sometimes get EBADF from write(2) or close(2) upon
+      ;; exit (coming from fd finalizers) when used by the Shepherd.  To work
+      ;; around that, exit forcefully so fd finalizers don't have a chance to
+      ;; run and fail.
+      (primitive-_exit 0))
     (lambda ()
-      (primitive-exit 1))))
+      (primitive-_exit 1))))
 
 (define (purify-environment)
   "Unset all environment variables."
@@ -126,9 +130,14 @@ for the process."
               "/dev/random"
               "/dev/urandom"
               "/dev/tty"
-              "/dev/ptmx"
               "/dev/fuse"))
 
+  ;; Mount a new devpts instance on /dev/pts.
+  (when (file-exists? "/dev/ptmx")
+    (mount* "none" (scope "/dev/pts") "devpts" 0
+            "newinstance,mode=0620")
+    (symlink "/dev/pts/ptmx" (scope "/dev/ptmx")))
+
   ;; Setup the container's /dev/console by bind mounting the pseudo-terminal
   ;; associated with standard input when there is one.
   (let* ((in      (current-input-port))
@@ -152,8 +161,7 @@ for the process."
 
   ;; Mount user-specified file systems.
   (for-each (lambda (file-system)
-              (mount-file-system (file-system->spec file-system)
-                                 #:root root))
+              (mount-file-system file-system #:root root))
             mounts)
 
   ;; Jail the process inside the container's root file system.
@@ -164,9 +172,12 @@ for the process."
     (umount "real-root" MNT_DETACH)
     (rmdir "real-root")))
 
-(define (initialize-user-namespace pid host-uids)
+(define* (initialize-user-namespace pid host-uids
+                                    #:key (guest-uid 0) (guest-gid 0))
   "Configure the user namespace for PID.  HOST-UIDS specifies the number of
-host user identifiers to map into the user namespace."
+host user identifiers to map into the user namespace.  GUEST-UID and GUEST-GID
+specify the first UID (respectively GID) that host UIDs (respectively GIDs)
+map to in the namespace."
   (define proc-dir
     (string-append "/proc/" (number->string pid)))
 
@@ -187,10 +198,10 @@ host user identifiers to map into the user namespace."
     ;; within the container.
     (call-with-output-file (scope "/uid_map")
       (lambda (port)
-        (format port "0 ~d ~d" uid host-uids)))
+        (format port "~d ~d ~d" guest-uid uid host-uids)))
     (call-with-output-file (scope "/gid_map")
       (lambda (port)
-        (format port "0 ~d ~d" gid host-uids)))))
+        (format port "~d ~d ~d" guest-gid gid host-uids)))))
 
 (define (namespaces->bit-mask namespaces)
   "Return the number suitable for the 'flags' argument of 'clone' that
@@ -206,13 +217,17 @@ corresponds to the symbols in NAMESPACES."
                ('net  CLONE_NEWNET))
               namespaces)))
 
-(define (run-container root mounts namespaces host-uids thunk)
+(define* (run-container root mounts namespaces host-uids thunk
+                        #:key (guest-uid 0) (guest-gid 0))
   "Run THUNK in a new container process and return its PID.  ROOT specifies
 the root directory for the container.  MOUNTS is a list of <file-system>
 objects that specify file systems to mount inside the container.  NAMESPACES
 is a list of symbols that correspond to the possible Linux namespaces: mnt,
-ipc, uts, user, and net.  HOST-UIDS specifies the number of
-host user identifiers to map into the user namespace."
+ipc, uts, user, and net.
+
+HOST-UIDS specifies the number of host user identifiers to map into the user
+namespace.  GUEST-UID and GUEST-GID specify the first UID (respectively GID)
+that host UIDs (respectively GIDs) map to in the namespace."
   ;; The parent process must initialize the user namespace for the child
   ;; before it can boot.  To negotiate this, a pipe is used such that the
   ;; child process blocks until the parent writes to it.
@@ -228,7 +243,8 @@ host user identifiers to map into the user namespace."
              (match (read child)
                ('ready
                 (purify-environment)
-                (when (memq 'mnt namespaces)
+                (when (and (memq 'mnt namespaces)
+                           (not (string=? root "/")))
                   (catch #t
                     (lambda ()
                       (mount-file-systems root mounts
@@ -250,7 +266,9 @@ host user identifiers to map into the user namespace."
          (pid
           (close-port child)
           (when (memq 'user namespaces)
-            (initialize-user-namespace pid host-uids))
+            (initialize-user-namespace pid host-uids
+                                       #:guest-uid guest-uid
+                                       #:guest-gid guest-gid))
           ;; TODO: Initialize cgroups.
           (write 'ready parent)
           (newline parent)
@@ -266,30 +284,55 @@ host user identifiers to map into the user namespace."
               (_                                  ;unexpected termination
                #f)))))))))
 
+;; FIXME: This is copied from (guix utils), which we cannot use because it
+;; would pull (guix config) and all.
+(define (call-with-temporary-directory proc)
+  "Call PROC with a name of a temporary directory; close the directory and
+delete it when leaving the dynamic extent of this call."
+  (let* ((directory (or (getenv "TMPDIR") "/tmp"))
+         (template  (string-append directory "/guix-directory.XXXXXX"))
+         (tmp-dir   (mkdtemp! template)))
+    (dynamic-wind
+      (const #t)
+      (lambda ()
+        (proc tmp-dir))
+      (lambda ()
+        (false-if-exception (delete-file-recursively tmp-dir))))))
+
 (define* (call-with-container mounts thunk #:key (namespaces %namespaces)
-                              (host-uids 1))
-  "Run THUNK in a new container process and return its exit status.
+                              (host-uids 1) (guest-uid 0) (guest-gid 0)
+                              (process-spawned-hook (const #t)))
+  "Run THUNK in a new container process and return its exit status; call
+PROCESS-SPAWNED-HOOK with the PID of the new process that has been spawned.
 MOUNTS is a list of <file-system> objects that specify file systems to mount
 inside the container.  NAMESPACES is a list of symbols corresponding to
 the identifiers for Linux namespaces: mnt, ipc, uts, pid, user, and net.  By
-default, all namespaces are used.  HOST-UIDS is the number of host user
-identifiers to map into the container's user namespace, if there is one.  By
-default, only a single uid/gid, that of the current user, is mapped into the
-container.  The host user that creates the container is the root user (uid/gid
-0) within the container.  Only root can map more than a single uid/gid.
+default, all namespaces are used.
+
+HOST-UIDS is the number of host user identifiers to map into the container's
+user namespace, if there is one.  By default, only a single uid/gid, that of
+the current user, is mapped into the container.  The host user that creates
+the container is the root user (uid/gid 0) within the container.  Only root
+can map more than a single uid/gid.
+
+GUEST-UID and GUEST-GID specify the first UID (respectively GID) that host
+UIDs (respectively GIDs) map to in the namespace.
 
 Note that if THUNK needs to load any additional Guile modules, the relevant
 module files must be present in one of the mappings in MOUNTS and the Guile
 load path must be adjusted as needed."
   (call-with-temporary-directory
    (lambda (root)
-     (let ((pid (run-container root mounts namespaces host-uids thunk)))
+     (let ((pid (run-container root mounts namespaces host-uids thunk
+                               #:guest-uid guest-uid
+                               #:guest-gid guest-gid)))
        ;; Catch SIGINT and kill the container process.
        (sigaction SIGINT
          (lambda (signum)
            (false-if-exception
             (kill pid SIGKILL))))
 
+       (process-spawned-hook pid)
        (match (waitpid pid)
          ((_ . status) status))))))
 
@@ -336,7 +379,8 @@ return the exit status."
      (match (container-excursion pid
               (lambda ()
                 (close-port in)
-                (write (thunk) out)))
+                (write (thunk) out)
+                (close-port out)))
        (0
         (close-port out)
         (let ((result (read in)))