[systemd-devel] [PATCH] nspawn: add /dev FD symlinks in container setup

Dave Reisner dreisner at archlinux.org
Mon Aug 13 21:02:42 PDT 2012


This creates /dev/fd, /dev/stdin, /dev/stdout, and /std/stderr as
symlinks to the /proc/self/fd tree on container creation. These are
needed for shells like bash to be fully functional.
---
 src/nspawn/nspawn.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 7d188f0..2a7f843 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -373,6 +373,35 @@ static int setup_resolv_conf(const char *dest) {
         return 0;
 }
 
+static int create_dev_fd_symlinks(const char *dest) {
+
+        static const char symlinks[] =
+                "/proc/self/fd\0"    "fd\0"
+                "/proc/self/fd/0\0"  "stdin\0"
+                "/proc/self/fd/1\0"  "stdout\0"
+                "/proc/self/fd/2\0"  "stderr\0";
+
+        const char *i, *to;
+        int r = 0;
+
+        NULSTR_FOREACH_PAIR(to, i, symlinks) {
+                char *from = NULL;
+
+                if (asprintf(&from, "%s/dev/%s", dest, i) < 0)
+                        return log_oom();
+
+                r = symlink(to, from);
+                free(from);
+
+                if (r < 0) {
+                        log_error("failed to symlink %s to %s: %m", from, to);
+                        break;
+                }
+        }
+
+        return r;
+}
+
 static int copy_devnodes(const char *dest) {
 
         static const char devnodes[] =
@@ -1204,6 +1233,9 @@ int main(int argc, char *argv[]) {
                 if (copy_devnodes(arg_directory) < 0)
                         goto child_fail;
 
+                if (create_dev_fd_symlinks(arg_directory) < 0)
+                        goto child_fail;
+
                 if (setup_dev_console(arg_directory, console) < 0)
                         goto child_fail;
 
-- 
1.7.11.4



More information about the systemd-devel mailing list