summaryrefslogtreecommitdiff
path: root/cloudinit/net/cmdline.py
blob: c5024654d4d3032e426e7c52220a28a383c2c365 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Copyright (C) 2013-2014 Canonical Ltd.
#
# Author: Scott Moser <scott.moser@canonical.com>
# Author: Blake Rouse <blake.rouse@canonical.com>
#
# This file is part of cloud-init. See LICENSE file for license information.

import abc
import base64
import glob
import gzip
import io
import logging
import os
import shlex

from cloudinit import util
from cloudinit.net import get_devicelist, read_sys_net_safe

_OPEN_ISCSI_INTERFACE_FILE = "/run/initramfs/open-iscsi.interface"

KERNEL_CMDLINE_NETWORK_CONFIG_DISABLED = "disabled"

LOG = logging.getLogger(__name__)


class InitramfsNetworkConfigSource(metaclass=abc.ABCMeta):
    """ABC for net config sources that read config written by initramfses"""

    @abc.abstractmethod
    def is_applicable(self) -> bool:
        """Is this initramfs config source applicable to the current system?"""

    @abc.abstractmethod
    def render_config(self) -> dict:
        """Render a v1 network config from the initramfs configuration"""


class KlibcNetworkConfigSource(InitramfsNetworkConfigSource):
    """InitramfsNetworkConfigSource for klibc initramfs (i.e. Debian/Ubuntu)

    Has three parameters, but they are intended to make testing simpler, _not_
    for use in production code.  (This is indicated by the prepended
    underscores.)
    """

    def __init__(self, _files=None, _mac_addrs=None, _cmdline=None):
        self._files = _files
        self._mac_addrs = _mac_addrs
        self._cmdline = _cmdline

        # Set defaults here, as they require computation that we don't want to
        # do at method definition time
        if self._files is None:
            self._files = _get_klibc_net_cfg_files()
        if self._cmdline is None:
            self._cmdline = util.get_cmdline()
        if self._mac_addrs is None:
            self._mac_addrs = {}
            for k in get_devicelist():
                mac_addr = read_sys_net_safe(k, "address")
                if mac_addr:
                    self._mac_addrs[k] = mac_addr

    def is_applicable(self) -> bool:
        """
        Return whether this system has klibc initramfs network config or not

        Will return True if:
            (a) klibc files exist in /run, AND
            (b) either:
                (i) ip= or ip6= are on the kernel cmdline, OR
                (ii) an open-iscsi interface file is present in the system
        """
        if self._files:
            for item in shlex.split(self._cmdline):
                if item.startswith("ip=") or item.startswith("ip6="):
                    return True
            if os.path.exists(_OPEN_ISCSI_INTERFACE_FILE):
                # iBft can configure networking without ip=
                return True
        return False

    def render_config(self) -> dict:
        return config_from_klibc_net_cfg(
            files=self._files,
            mac_addrs=self._mac_addrs,
        )


_INITRAMFS_CONFIG_SOURCES = [KlibcNetworkConfigSource]


def _klibc_to_config_entry(content, mac_addrs=None):
    """Convert a klibc written shell content file to a 'config' entry
    When ip= is seen on the kernel command line in debian initramfs
    and networking is brought up, ipconfig will populate
    /run/net-<name>.cfg.

    The files are shell style syntax, and examples are in the tests
    provided here.  There is no good documentation on this unfortunately.

    DEVICE=<name> is expected/required and PROTO should indicate if
    this is 'none' (static) or 'dhcp' or 'dhcp6' (LP: #1621507).
    note that IPV6PROTO is also written by newer code to address the
    possibility of both ipv4 and ipv6 getting addresses.

    Full syntax is documented at:
    https://git.kernel.org/pub/scm/libs/klibc/klibc.git/plain/usr/kinit/ipconfig/README.ipconfig
    """

    if mac_addrs is None:
        mac_addrs = {}

    data = util.load_shell_content(content)
    try:
        name = data["DEVICE"] if "DEVICE" in data else data["DEVICE6"]
    except KeyError as e:
        raise ValueError("no 'DEVICE' or 'DEVICE6' entry in data") from e

    # ipconfig on precise does not write PROTO
    # IPv6 config gives us IPV6PROTO, not PROTO.
    proto = data.get("PROTO", data.get("IPV6PROTO"))
    if not proto:
        if data.get("filename"):
            proto = "dhcp"
        else:
            proto = "none"

    if proto not in ("none", "dhcp", "dhcp6"):
        raise ValueError("Unexpected value for PROTO: %s" % proto)

    iface = {
        "type": "physical",
        "name": name,
        "subnets": [],
    }

    if name in mac_addrs:
        iface["mac_address"] = mac_addrs[name]

    # Handle both IPv4 and IPv6 values
    for pre in ("IPV4", "IPV6"):
        # if no IPV4ADDR or IPV6ADDR, then go on.
        if pre + "ADDR" not in data:
            continue

        # PROTO for ipv4, IPV6PROTO for ipv6
        cur_proto = data.get(pre + "PROTO", proto)
        # ipconfig's 'none' is called 'static'
        if cur_proto == "none":
            cur_proto = "static"
        subnet = {"type": cur_proto, "control": "manual"}

        # only populate address for static types. While the rendered config
        # may have an address for dhcp, that is not really expected.
        if cur_proto == "static":
            subnet["address"] = data[pre + "ADDR"]

        # these fields go right on the subnet
        for key in ("NETMASK", "BROADCAST", "GATEWAY"):
            if pre + key in data:
                subnet[key.lower()] = data[pre + key]

        dns = []
        # handle IPV4DNS0 or IPV6DNS0
        for nskey in ("DNS0", "DNS1"):
            ns = data.get(pre + nskey)
            # verify it has something other than 0.0.0.0 (or ipv6)
            if ns and len(ns.strip(":.0")):
                dns.append(data[pre + nskey])
        if dns:
            subnet["dns_nameservers"] = dns
            # add search to both ipv4 and ipv6, as it has no namespace
            search = data.get("DOMAINSEARCH")
            if search:
                if "," in search:
                    subnet["dns_search"] = search.split(",")
                else:
                    subnet["dns_search"] = search.split()

        iface["subnets"].append(subnet)

    return name, iface


def _get_klibc_net_cfg_files():
    return glob.glob("/run/net-*.conf") + glob.glob("/run/net6-*.conf")


def config_from_klibc_net_cfg(files=None, mac_addrs=None):
    if files is None:
        files = _get_klibc_net_cfg_files()

    entries = []
    names = {}
    for cfg_file in files:
        name, entry = _klibc_to_config_entry(
            util.load_file(cfg_file), mac_addrs=mac_addrs
        )
        if name in names:
            prev = names[name]["entry"]
            if prev.get("mac_address") != entry.get("mac_address"):
                raise ValueError(
                    "device '{name}' was defined multiple times ({files})"
                    " but had differing mac addresses: {old} -> {new}.".format(
                        name=name,
                        files=" ".join(names[name]["files"]),
                        old=prev.get("mac_address"),
                        new=entry.get("mac_address"),
                    )
                )
            prev["subnets"].extend(entry["subnets"])
            names[name]["files"].append(cfg_file)
        else:
            names[name] = {"files": [cfg_file], "entry": entry}
            entries.append(entry)

    return {"config": entries, "version": 1}


def read_initramfs_config():
    """
    Return v1 network config for initramfs-configured networking (or None)

    This will consider each _INITRAMFS_CONFIG_SOURCES entry in turn, and return
    v1 network configuration for the first one that is applicable.  If none are
    applicable, return None.
    """
    for src_cls in _INITRAMFS_CONFIG_SOURCES:
        cfg_source = src_cls()

        if not cfg_source.is_applicable():
            continue

        return cfg_source.render_config()
    return None


def _decomp_gzip(blob):
    # decompress blob or return original blob
    with io.BytesIO(blob) as iobuf:
        gzfp = None
        try:
            gzfp = gzip.GzipFile(mode="rb", fileobj=iobuf)
            return gzfp.read()
        except IOError:
            return blob
        finally:
            if gzfp:
                gzfp.close()


def _b64dgz(data):
    """Decode a string base64 encoding, if gzipped, uncompress as well

    :return: decompressed unencoded string of the data or empty string on
       unencoded data.
    """
    try:
        blob = base64.b64decode(data)
    except (TypeError, ValueError):
        LOG.error(
            "Expected base64 encoded kernel commandline parameter"
            " network-config. Ignoring network-config=%s.",
            data,
        )
        return ""

    return _decomp_gzip(blob)


def read_kernel_cmdline_config(cmdline=None):
    if cmdline is None:
        cmdline = util.get_cmdline()

    if "network-config=" in cmdline:
        data64 = None
        for tok in cmdline.split():
            if tok.startswith("network-config="):
                data64 = tok.split("=", 1)[1]
        if data64:
            if data64 == KERNEL_CMDLINE_NETWORK_CONFIG_DISABLED:
                return {"config": "disabled"}
            return util.load_yaml(_b64dgz(data64))

    return None


# vi: ts=4 expandtab