networkd: work inside containers
authorTom Gundersen <teg@jklm.no>
Wed, 12 Feb 2014 15:40:24 +0000 (16:40 +0100)
committerTom Gundersen <teg@jklm.no>
Wed, 12 Feb 2014 16:04:35 +0000 (17:04 +0100)
Udev does not run in containers, so instead of relying on it to tell us when a
network device is ready to be used by networkd, we simply assume that any
device was fully initialized before being added to the container.

src/network/networkd-manager.c

index fb3360d..54e83e3 100644 (file)
@@ -27,6 +27,7 @@
 #include "udev-util.h"
 #include "rtnl-util.h"
 #include "mkdir.h"
+#include "virt.h"
 
 const char* const network_dirs[] = {
         "/etc/systemd/network",
@@ -63,9 +64,18 @@ int manager_new(Manager **ret) {
         if (!m->udev)
                 return -ENOMEM;
 
-        m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
-        if (!m->udev_monitor)
-                return -ENOMEM;
+        /* udev does not initialize devices inside containers,
+         * so we rely on them being already initialized before
+         * entering the container */
+        if (detect_container(NULL) > 0) {
+                m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "kernel");
+                if (!m->udev_monitor)
+                        return -ENOMEM;
+        } else {
+                m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
+                if (!m->udev_monitor)
+                        return -ENOMEM;
+        }
 
         m->links = hashmap_new(uint64_hash_func, uint64_compare_func);
         if (!m->links)
@@ -179,9 +189,14 @@ int manager_udev_enumerate_links(Manager *m) {
         if (r < 0)
                 return r;
 
-        r = udev_enumerate_add_match_is_initialized(e);
-        if (r < 0)
-                return r;
+        /* udev does not initialize devices inside containers,
+         * so we rely on them being already initialized before
+         * entering the container */
+        if (detect_container(NULL) <= 0) {
+                r = udev_enumerate_add_match_is_initialized(e);
+                if (r < 0)
+                        return r;
+        }
 
         r = udev_enumerate_scan_devices(e);
         if (r < 0)