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