Ubuntu Pastebin

Paste from smoser at Mon, 21 Mar 2016 02:14:15 +0000

Download as text
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
=== modified file 'bin/cloud-init'
--- bin/cloud-init	2016-03-20 10:26:11 +0000
+++ bin/cloud-init	2016-03-21 00:53:33 +0000
@@ -251,6 +251,7 @@
     # Stage 5
     try:
         init.fetch()
+        init.apply_networking()
     except sources.DataSourceNotFoundException:
         # In the case of 'cloud-init init' without '--local' it is a bit
         # more likely that the user would consider it failure if nothing was
@@ -261,6 +262,7 @@
         else:
             util.logexc(LOG, ("No instance datasource found!"
                               " Likely bad things to come!"))
+        init.apply_networking()
         if not args.force:
             if args.local:
                 return (None, [])

=== modified file 'cloudinit/sources/__init__.py'
--- cloudinit/sources/__init__.py	2015-08-31 17:33:30 +0000
+++ cloudinit/sources/__init__.py	2016-03-20 10:24:18 +0000
@@ -217,6 +217,15 @@
     def get_package_mirror_info(self):
         return self.distro.get_package_mirror_info(data_source=self)
 
+    @property
+    def network_config(self):
+        return self.metadata.network_config
+
+    @property
+    def network_device_rules(self):
+        if not self.network_config:
+            return None
+
 
 def normalize_pubkey_data(pubkey_data):
     keys = []

=== modified file 'cloudinit/stages.py'
--- cloudinit/stages.py	2016-03-03 22:20:10 +0000
+++ cloudinit/stages.py	2016-03-21 02:09:37 +0000
@@ -52,6 +52,18 @@
 
 NULL_DATA_SOURCE = None
 
+DEF_ENI = """# The primary network interface
+auto {name}
+iface {name} inet dhcp
+"""
+DEF_LINK_FILE = """#cloud-init
+[Match]
+MACAddress={mac}
+
+[Link]
+Name={name}
+"""
+
 
 class Init(object):
     def __init__(self, ds_deps=None, reporter=None):
@@ -587,6 +599,69 @@
         # Run the handlers
         self._do_handlers(user_data_msg, c_handlers_list, frequency)
 
+    def apply_networking(self):
+        # this would apply networking the datasource provided
+        # and if no datasource provided networking, then apply fallback
+
+        # it also should manage this through the datasource.distro
+        # but just done here as hack first try
+
+        # first we just delete this file to avoid confusion
+        eni_d = "/etc/network/interfaces.d"
+        eth0_cfg = os.path.join(eni_d, "eth0.cfg")
+        if os.path.exists(eth0_cfg):
+            util.rename(eth0_cfg, eth0_cfg + ".obsolete")
+
+        ci_cfg = os.path.join(eni_d, "50-cloud-init.cfg")
+        # and then write our own.
+        name = None
+        if os.path.exists("/sys/class/net/eth0"):
+            name = "eth0"
+        else:
+            nics = os.listdir("/sys/class/net")
+            considered = []
+            for n in nics:
+                if n != "lo" and not n.startswith("lxc"):
+                    considered.append(n)
+            if len(considered):
+                name = considered[0]
+
+        if name is None:
+            util.write_file(ci_cfg, "# no nics found\n", "w")
+        else:
+            curname = name
+            mac = util.load_file("/sys/class/net/%s/address" % curname)
+
+            # i tested renaming, to see if it would work, but that causes
+            # issues with veth (see below)
+            # if name.startswith("eth"):
+            #    name = "en" + curname[3:]
+
+            # it seems that udev doesnt want to rename the veth devices (lxc)
+            # udevadm test-builtin net_setup_link /sys/class/net/eth0 shows:
+            # running Assertion 'udev_device' failed at
+            #   ../src/libudev/libudev-device.c:128, function
+            #   udev_device_get_driver(). Ignoring.
+            # this is probably because ID_NET_DRIVER=veth
+
+            # note also, can't find a way to actually run net_setup_link
+            # but only rather see what it *would* do.
+
+            # probably want to actually
+            # safeguard renaming by checking if virtual
+            # ls -l /sys/class/net/
+            #   eth0 -> ../../devices/virtual/net/eth0
+            # this is probably why the udev rules that we write have
+            #   DRIVERS=="?*"
+
+            # we could change it ourselves, or otherwise we have to know this
+            # ip link set dev eth0 name en0
+            syslink = "/etc/systemd/network/50-cloud-init-%s.link"
+            util.write_file(
+                syslink % name,
+                DEF_LINK_FILE.format(name=name, mac=mac))
+            util.write_file(ci_cfg, DEF_ENI.format(name=name), "w")
+
 
 class Modules(object):
     def __init__(self, init, cfg_files=None, reporter=None):
Download as text