Harald Hoyer 6844c2
From f333fbb1efc2f32527f78cbdb003d59bae01aa07 Mon Sep 17 00:00:00 2001
Harald Hoyer 6844c2
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Harald Hoyer 6844c2
Date: Wed, 17 Apr 2013 14:13:09 -0400
Harald Hoyer 6844c2
Subject: [PATCH] nspawn: create empty /etc/resolv.conf if necessary
Harald Hoyer 6844c2
Harald Hoyer 6844c2
nspawn will overmount resolv.conf if it exists. Since e.g.
Harald Hoyer 6844c2
default install with yum doesn't create /etc/resolv.conf,
Harald Hoyer 6844c2
a container created with yum will not have network. This
Harald Hoyer 6844c2
seems undesirable, and since we overmount the file anyway,
Harald Hoyer 6844c2
let's create it too.
Harald Hoyer 6844c2
Harald Hoyer 6844c2
Also, mounting a read-write /etc/resolv.conf in the container
Harald Hoyer 6844c2
is treated as a failure, since it makes it possible to
Harald Hoyer 6844c2
modify hosts /etc/resolv.conf from inside the container.
Harald Hoyer 6844c2
---
Harald Hoyer 6844c2
 src/nspawn/nspawn.c | 17 ++++++++++++-----
Harald Hoyer 6844c2
 1 file changed, 12 insertions(+), 5 deletions(-)
Harald Hoyer 6844c2
Harald Hoyer 6844c2
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
Harald Hoyer 6844c2
index f57c75f..5a43d5e 100644
Harald Hoyer 6844c2
--- a/src/nspawn/nspawn.c
Harald Hoyer 6844c2
+++ b/src/nspawn/nspawn.c
Harald Hoyer 6844c2
@@ -492,7 +492,8 @@ static int setup_timezone(const char *dest) {
Harald Hoyer 6844c2
 }
Harald Hoyer 6844c2
 
Harald Hoyer 6844c2
 static int setup_resolv_conf(const char *dest) {
Harald Hoyer 6844c2
-        char *where;
Harald Hoyer 6844c2
+        char _cleanup_free_ *where = NULL;
Harald Hoyer 6844c2
+        _cleanup_close_ int fd = -1;
Harald Hoyer 6844c2
 
Harald Hoyer 6844c2
         assert(dest);
Harald Hoyer 6844c2
 
Harald Hoyer 6844c2
@@ -504,12 +505,18 @@ static int setup_resolv_conf(const char *dest) {
Harald Hoyer 6844c2
         if (!where)
Harald Hoyer 6844c2
                 return log_oom();
Harald Hoyer 6844c2
 
Harald Hoyer 6844c2
+        fd = open(where, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
Harald Hoyer 6844c2
+
Harald Hoyer 6844c2
         /* We don't really care for the results of this really. If it
Harald Hoyer 6844c2
          * fails, it fails, but meh... */
Harald Hoyer 6844c2
-        if (mount("/etc/resolv.conf", where, "bind", MS_BIND, NULL) >= 0)
Harald Hoyer 6844c2
-                mount("/etc/resolv.conf", where, "bind", MS_BIND|MS_REMOUNT|MS_RDONLY, NULL);
Harald Hoyer 6844c2
-
Harald Hoyer 6844c2
-        free(where);
Harald Hoyer 6844c2
+        if (mount("/etc/resolv.conf", where, "bind", MS_BIND, NULL) < 0)
Harald Hoyer 6844c2
+                log_warning("Failed to bind mount /etc/resolv.conf: %m");
Harald Hoyer 6844c2
+        else
Harald Hoyer 6844c2
+                if (mount("/etc/resolv.conf", where, "bind",
Harald Hoyer 6844c2
+                          MS_BIND|MS_REMOUNT|MS_RDONLY, NULL) < 0) {
Harald Hoyer 6844c2
+                        log_error("Failed to remount /etc/resolv.conf readonly: %m");
Harald Hoyer 6844c2
+                        return -errno;
Harald Hoyer 6844c2
+                }
Harald Hoyer 6844c2
 
Harald Hoyer 6844c2
         return 0;
Harald Hoyer 6844c2
 }
Harald Hoyer 6844c2
-- 
Harald Hoyer 6844c2
1.8.2
Harald Hoyer 6844c2