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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
|
# Copyright 2022 Red Hat, Inc.
#
# Author: Lubomir Rintel <lkundrak@v3.sk>
# Fixes and suggestions contributed by James Falcon, Neal Gompa,
# Zbigniew Jędrzejewski-Szmek and Emanuele Giuseppe Esposito.
#
# This file is part of cloud-init. See LICENSE file for license information.
import configparser
import io
import itertools
import logging
import os
import uuid
from typing import List, Optional
from cloudinit import subp, util
from cloudinit.net import (
is_ipv6_address,
is_ipv6_network,
renderer,
subnet_is_ipv6,
)
from cloudinit.net.network_state import NetworkState
from cloudinit.net.sysconfig import available_nm_ifcfg_rh
NM_RUN_DIR = "/etc/NetworkManager"
NM_LIB_DIR = "/usr/lib/NetworkManager"
IFCFG_CFG_FILE = "/etc/sysconfig/network-scripts"
NM_IPV6_ADDR_GEN_CONF = """# This is generated by cloud-init. Do not edit.
#
[.config]
enable=nm-version-min:1.40
[connection.30-cloud-init-ip6-addr-gen-mode]
# Select EUI64 to be used if the profile does not specify it.
ipv6.addr-gen-mode=0
"""
LOG = logging.getLogger(__name__)
class NMConnection:
"""Represents a NetworkManager connection profile."""
def __init__(self, con_id):
"""
Initializes the connection with some very basic properties,
notably the UUID so that the connection can be referred to.
"""
# Chosen by fair dice roll
CI_NM_UUID = uuid.UUID("a3924cb8-09e0-43e9-890b-77972a800108")
self.config = configparser.ConfigParser()
# Identity option name mapping, to achieve case sensitivity
self.config.optionxform = str
self.config["connection"] = {
"id": f"cloud-init {con_id}",
"uuid": str(uuid.uuid5(CI_NM_UUID, con_id)),
"autoconnect-priority": "120",
}
# This is not actually used anywhere, but may be useful in future
self.config["user"] = {
"org.freedesktop.NetworkManager.origin": "cloud-init"
}
def _set_default(self, section, option, value):
"""
Sets a property unless it's already set, ensuring the section
exists.
"""
if not self.config.has_section(section):
self.config[section] = {}
if not self.config.has_option(section, option):
self.config[section][option] = value
def _config_option_is_set(self, section, option):
"""
Checks if a config option is set. Returns True if it is,
else returns False.
"""
return self.config.has_section(section) and self.config.has_option(
section, option
)
def _get_config_option(self, section, option):
"""
Returns the value of a config option if its set,
else returns None.
"""
if self._config_option_is_set(section, option):
return self.config[section][option]
else:
return None
def _change_set_config_option(self, section, option, value):
"""
Overrides the value of a config option if its already set.
Else, if the config option is not set, it does nothing.
"""
if self._config_option_is_set(section, option):
self.config[section][option] = value
def _set_mayfail_true_if_both_false_dhcp(self):
"""
If for both ipv4 and ipv6, 'may-fail' is set to be False,
set it to True for both of them.
"""
for family in ["ipv4", "ipv6"]:
if self._get_config_option(family, "may-fail") != "false":
# if either ipv4 or ipv6 sections are not set/configured,
# or if both are configured but for either ipv4 or ipv6,
# 'may-fail' is not 'false', do not do anything.
return
if self._get_config_option(family, "method") not in [
"dhcp",
"auto",
]:
# if both v4 and v6 are not dhcp, do not do anything.
return
# If we landed here, it means both ipv4 and ipv6 are configured
# with dhcp/auto and both have 'may-fail' set to 'false'. So set
# both to 'true'.
for family in ["ipv4", "ipv6"]:
self._change_set_config_option(family, "may-fail", "true")
def _set_ip_method(self, family, subnet_type):
"""
Ensures there's appropriate [ipv4]/[ipv6] for given family
appropriate for given configuration type
"""
method_map = {
"static": "manual",
"static6": "manual",
"dhcp6": "auto",
"ipv6_slaac": "auto",
"ipv6_dhcpv6-stateless": "auto",
"ipv6_dhcpv6-stateful": "dhcp",
"dhcp4": "auto",
"dhcp": "auto",
}
# Ensure we have an [ipvX] section, default to disabled
method = "disabled"
self._set_default(family, "method", method)
try:
if subnet_type:
method = method_map[subnet_type]
except KeyError:
# What else can we do
method = "auto"
self.config[family]["may-fail"] = "true"
# Make sure we don't "downgrade" the method in case
# we got conflicting subnets (e.g. static along with dhcp)
if self.config[family]["method"] == "dhcp":
return
if self.config[family]["method"] == "auto" and method == "manual":
return
if subnet_type in [
"ipv6_dhcpv6-stateful",
"ipv6_dhcpv6-stateless",
"ipv6_slaac",
]:
# set ipv4 method to 'disabled' to align with sysconfig renderer.
self._set_default("ipv4", "method", "disabled")
self.config[family]["method"] = method
# Network Manager sets the value of `may-fail` to `True` by default.
# Please see https://www.networkmanager.dev/docs/api/1.32.10/settings-ipv6.html.
# Therefore, when no configuration for ipv4 or ipv6 is specified,
# `may-fail = True` applies. When the user explicitly configures ipv4
# or ipv6, `may-fail` is set to `False`. This is so because it is
# assumed that a network failure with the user provided configuration
# is unexpected. In other words, we think that the user knows what
# works in their target environment and what does not and they have
# correctly configured cloud-init network configuration such that
# it works in that environment. When no such configuration is
# specified, we do not know what would work and what would not in
# user's environment. Therefore, we are more conservative in assuming
# that failure with ipv4 or ipv6 can be expected or tolerated.
self._set_default(family, "may-fail", "false")
def _get_next_numbered_section(self, section, key_prefix) -> str:
if not self.config.has_section(section):
self.config[section] = {}
for index in itertools.count(1):
key = f"{key_prefix}{index}"
if not self.config.has_option(section, key):
return key
return "not_possible" # for typing
def _add_numbered(self, section, key_prefix, value):
"""
Adds a numbered property, such as address<n> or route<n>, ensuring
the appropriate value gets used for <n>.
"""
key = self._get_next_numbered_section(section, key_prefix)
self.config[section][key] = value
def _add_route_options(self, section, route, key, value):
"""Add route options to a given route
Example:
Given:
section: ipv4
route: route0
key: mtu
value: 500
Create line under [ipv4] section:
route0_options=mtu=500
If the line already exists, then append the new key/value pair
"""
numbered_key = f"{route}_options"
route_options = self.config[section].get(numbered_key)
self.config[section][numbered_key] = (
f"{route_options},{key}={value}"
if route_options
else f"{key}={value}"
)
def _add_address(self, family, subnet):
"""
Adds an ipv[46]address<n> property.
"""
value = subnet["address"] + "/" + str(subnet["prefix"])
self._add_numbered(family, "address", value)
def _is_default_route(self, route):
default_nets = ("::", "0.0.0.0")
return route["prefix"] == 0 and route["network"] in default_nets
def _add_route(self, route):
"""Adds a ipv[46].route<n> property."""
# Because network v2 route definitions can have mixed v4 and v6
# routes, determine the family per route based on the network
family = "ipv6" if is_ipv6_network(route["network"]) else "ipv4"
value = f'{route["network"]}/{route["prefix"]}'
if "gateway" in route:
value += f',{route["gateway"]}'
if "metric" in route:
if self._is_default_route(route):
self.config[family]["route-metric"] = f'{route["metric"]}'
else:
value += f',{route["metric"]}'
route_key = self._get_next_numbered_section(family, "route")
self.config[family][route_key] = value
if "mtu" in route:
self._add_route_options(family, route_key, "mtu", route["mtu"])
def _add_nameserver(self, dns: str) -> None:
"""
Extends the ipv[46].dns property with a name server.
"""
family = "ipv6" if is_ipv6_address(dns) else "ipv4"
if (
self.config.has_section(family)
and self._get_config_option(family, "method") != "disabled"
):
self._set_default(family, "dns", "")
self.config[family]["dns"] = self.config[family]["dns"] + dns + ";"
def _add_dns_search(self, dns_search: List[str]) -> None:
"""
Extends the ipv[46].dns-search property with a name server.
"""
for family in ["ipv4", "ipv6"]:
if (
self.config.has_section(family)
and self._get_config_option(family, "method") != "disabled"
):
self._set_default(family, "dns-search", "")
self.config[family]["dns-search"] = (
self.config[family]["dns-search"]
+ ";".join(dns_search)
+ ";"
)
def con_uuid(self):
"""
Returns the connection UUID
"""
return self.config["connection"]["uuid"]
def valid(self):
"""
Can this be serialized into a meaningful connection profile?
"""
return self.config.has_option("connection", "type")
@staticmethod
def mac_addr(addr):
"""
Sanitize a MAC address.
"""
return addr.replace("-", ":").upper()
def render_interface(self, iface, network_state, renderer):
"""
Integrate information from network state interface information
into the connection. Most of the work is done here.
"""
# Initialize type & connectivity
_type_map = {
"physical": "ethernet",
"vlan": "vlan",
"bond": "bond",
"bridge": "bridge",
"infiniband": "infiniband",
"loopback": None,
}
if_type = _type_map[iface["type"]]
if if_type is None:
return
if "bond-master" in iface:
slave_type = "bond"
else:
slave_type = None
self.config["connection"]["type"] = if_type
if slave_type is not None:
self.config["connection"]["slave-type"] = slave_type
self.config["connection"]["master"] = renderer.con_ref(
iface[slave_type + "-master"]
)
# Add type specific-section
self.config[if_type] = {}
# These are the interface properties that map nicely
# to NetworkManager properties
# NOTE: Please ensure these items are formatted so as
# to match the schema in schema-network-config-v1.json
#
# Supported parameters
# https://networkmanager.dev/docs/libnm/latest/NMSettingBond.html#NMSettingBond.other
# https://www.kernel.org/doc/Documentation/networking/bonding.txt
_prop_map = {
"bond": {
"mode": "bond-mode",
"miimon": "bond-miimon",
# only in balance-xor(2), 802.3ad(4), balance-tlb(5)
"xmit_hash_policy": "bond-xmit_hash_policy",
# only in active-backup(1)
"num_grat_arp": "bond-num_grat_arp",
"downdelay": "bond-downdelay",
"updelay": "bond-updelay",
"fail_over_mac": "bond-fail_over_mac",
# only in active-backup(1), balance-tlb(5), balance-alb(6)
"primary_reselect": "bond-primary_reselect",
# only in active-backup(1), balance-tlb(5), balance-alb(6)
"primary": "bond-primary",
# only in active-backup(1), balance-tlb(5), balance-alb(6)
"active_slave": "bond-active_slave",
# only in 802.3ad(4)
"ad_actor_sys_prio": "bond-ad_actor_sys_prio",
# only in 802.3ad(4)
"ad_actor_system": "bond-ad_actor_system",
# only in 802.3ad(4)
"ad_select": "bond-ad_select",
# only in 802.3ad(4)
"ad_user_port_key": "bond-ad_user_port_key",
"all_slaves_active": "bond-all_slaves_active",
"arp_all_targets": "bond-arp_all_targets",
"arp_interval": "bond-arp_interval",
"arp_ip_target": "bond-arp_ip_target",
"arp_validate": "bond-arp_validate",
# only in 802.3ad(4)
"lacp_rate": "bond-lacp_rate",
# only in balance-tlb(5), balance-alb(6)
"lp_interval": "bond-lp_interval",
# only in 802.3ad(4)
"min_links": "bond-min_links",
# only in active-backup(1)
"num_unsol_na": "bond-num_unsol_na",
# only in balance-rr(0)
"packets_per_slave": "bond-packets_per_slave",
# only in active-backup(1)
"peer_notif_delay": "bond-peer_notif_delay",
# only in active-backup(1), balance rr(0), tlb(5), alb(6)
"resend_igmp": "bond-resend_igmp",
# only in balance-tlb(5)
"tlb_dynamic_lb": "bond-tlb_dynamic_lb",
"use_carrier": "bond-use_carrier",
},
"bridge": {
"stp": "bridge_stp",
"priority": "bridge_bridgeprio",
},
"vlan": {
"id": "vlan_id",
},
"ethernet": {},
"infiniband": {},
}
device_mtu = iface["mtu"]
ipv4_mtu = None
found_nameservers = []
found_dns_search = []
# Deal with Layer 3 configuration
if if_type == "bond" and not iface["subnets"]:
# If there is no L3 subnet config for a given connection,
# ensure it is disabled. Without this, the interface
# defaults to 'auto' which implies DHCP. This is problematic
# for certain configurations such as bonds where the root
# device itself may not have a subnet config and should be
# disabled while a separate VLAN interface on the bond holds
# the subnet information.
for family in ["ipv4", "ipv6"]:
self._set_ip_method(family, None)
for subnet in iface["subnets"]:
family = "ipv6" if subnet_is_ipv6(subnet) else "ipv4"
self._set_ip_method(family, subnet["type"])
if "address" in subnet:
self._add_address(family, subnet)
if "gateway" in subnet:
self.config[family]["gateway"] = subnet["gateway"]
for route in subnet["routes"]:
self._add_route(route)
# Add subnet-level DNS
if "dns_nameservers" in subnet:
found_nameservers.extend(subnet["dns_nameservers"])
if "dns_search" in subnet:
found_dns_search.extend(subnet["dns_search"])
if family == "ipv4" and "mtu" in subnet:
ipv4_mtu = subnet["mtu"]
# Add interface-level DNS
if "dns" in iface:
found_nameservers += [
dns
for dns in iface["dns"]["nameservers"]
if dns not in found_nameservers
]
found_dns_search += [
search
for search in iface["dns"]["search"]
if search not in found_dns_search
]
# We prefer any interface-specific DNS entries, but if we do not
# have any, add the global DNS to the connection
if not found_nameservers and network_state.dns_nameservers:
found_nameservers = network_state.dns_nameservers
if not found_dns_search and network_state.dns_searchdomains:
found_dns_search = network_state.dns_searchdomains
# Write out all DNS entries to the connection
for nameserver in found_nameservers:
self._add_nameserver(nameserver)
if found_dns_search:
self._add_dns_search(found_dns_search)
# we do not want to set may-fail to false for both ipv4 and ipv6 dhcp
# at the at the same time. This will make the network configuration
# work only when both ipv4 and ipv6 dhcp succeeds. This may not be
# what we want. If we have configured both ipv4 and ipv6 dhcp, any one
# succeeding should be enough. Therefore, if "may-fail" is set to
# False for both ipv4 and ipv6 dhcp, set them both to True.
self._set_mayfail_true_if_both_false_dhcp()
if ipv4_mtu is None:
ipv4_mtu = device_mtu
if not ipv4_mtu == device_mtu:
LOG.warning(
"Network config: ignoring %s device-level mtu:%s"
" because ipv4 subnet-level mtu:%s provided.",
iface["name"],
device_mtu,
ipv4_mtu,
)
# Parse type-specific properties
for nm_prop, key in _prop_map[if_type].items():
if key not in iface:
continue
if iface[key] is None:
continue
if isinstance(iface[key], bool):
self.config[if_type][nm_prop] = (
"true" if iface[key] else "false"
)
else:
self.config[if_type][nm_prop] = str(iface[key])
# These ones need special treatment
if if_type == "ethernet":
if iface["wakeonlan"] is True:
# NM_SETTING_WIRED_WAKE_ON_LAN_MAGIC
self.config["ethernet"]["wake-on-lan"] = str(0x40)
if ipv4_mtu is not None:
self.config["ethernet"]["mtu"] = str(ipv4_mtu)
if iface["mac_address"] is not None:
self.config["ethernet"]["mac-address"] = self.mac_addr(
iface["mac_address"]
)
if if_type == "vlan" and "vlan-raw-device" in iface:
self.config["vlan"]["parent"] = renderer.con_ref(
iface["vlan-raw-device"]
)
if if_type == "bond" and ipv4_mtu is not None:
if "ethernet" not in self.config:
self.config["ethernet"] = {}
self.config["ethernet"]["mtu"] = str(ipv4_mtu)
if if_type == "bridge":
# Bridge is ass-backwards compared to bond
for port in iface["bridge_ports"]:
port = renderer.get_conn(port)
port._set_default("connection", "slave-type", "bridge")
port._set_default("connection", "master", self.con_uuid())
if iface["mac_address"] is not None:
self.config["bridge"]["mac-address"] = self.mac_addr(
iface["mac_address"]
)
if if_type == "infiniband" and ipv4_mtu is not None:
self.config["infiniband"]["transport-mode"] = "datagram"
self.config["infiniband"]["mtu"] = str(ipv4_mtu)
if iface["mac_address"] is not None:
self.config["infiniband"]["mac-address"] = self.mac_addr(
iface["mac_address"]
)
# Finish up
if if_type == "bridge" or not self.config.has_option(
if_type, "mac-address"
):
self.config["connection"]["interface-name"] = iface["name"]
def dump(self):
"""
Stringify.
"""
buf = io.StringIO()
self.config.write(buf, space_around_delimiters=False)
header = "# Generated by cloud-init. Changes will be lost.\n\n"
return header + buf.getvalue()
class Renderer(renderer.Renderer):
"""Renders network information in a NetworkManager keyfile format.
See https://networkmanager.dev/docs/api/latest/nm-settings-keyfile.html
"""
def __init__(self, config=None):
self.connections = {}
self.config = config
def get_conn(self, con_id):
return self.connections[con_id]
def con_ref(self, con_id):
if con_id in self.connections:
return self.connections[con_id].con_uuid()
else:
# Well, what can we do...
return con_id
def render_network_state(
self,
network_state: NetworkState,
templates: Optional[dict] = None,
target=None,
) -> None:
# First pass makes sure there's NMConnections for all known
# interfaces that have UUIDs that can be linked to from related
# interfaces
for iface in network_state.iter_interfaces():
conn_key = iface.get("config_id") or iface["name"]
self.connections[conn_key] = NMConnection(iface["name"])
# Now render the actual interface configuration
for iface in network_state.iter_interfaces():
conn_key = iface.get("config_id") or iface["name"]
conn = self.connections[conn_key]
conn.render_interface(iface, network_state, self)
# And finally write the files
for con_id, conn in self.connections.items():
if not conn.valid():
continue
name = nm_conn_filename(con_id, target)
util.write_file(name, conn.dump(), 0o600)
# Select EUI64 to be used by default by NM for creating the address
# for use with RFC4862 IPv6 Stateless Address Autoconfiguration.
util.write_file(
cloud_init_nm_conf_filename(target), NM_IPV6_ADDR_GEN_CONF, 0o600
)
def nm_conn_filename(con_id, target=None):
target_con_dir = subp.target_path(target, NM_RUN_DIR)
con_file = f"cloud-init-{con_id}.nmconnection"
return f"{target_con_dir}/system-connections/{con_file}"
def sysconfig_conn_filename(devname, target=None):
target_con_dir = subp.target_path(target, IFCFG_CFG_FILE)
con_file = f"ifcfg-{devname}"
return f"{target_con_dir}/{con_file}"
def conn_filename(devname):
"""
This function returns the name of the interface config file.
It first checks for presence of network manager connection file.
If absent and ifcfg-rh plugin for network manager is available,
it returns the name of the ifcfg file if it is present. If the
plugin is not present or the plugin is present but ifcfg file is
not, it returns None.
This function is called from NetworkManagerActivator class in
activators.py.
"""
conn_file = nm_conn_filename(devname)
# If the network manager connection file is absent, also check for
# presence of ifcfg files for the same interface (if nm-ifcfg-rh plugin is
# present, network manager can handle ifcfg files). If both network manager
# connection file and ifcfg files are absent, return None.
if not os.path.isfile(conn_file) and available_nm_ifcfg_rh():
conn_file = sysconfig_conn_filename(devname)
return conn_file if os.path.isfile(conn_file) else None
def cloud_init_nm_conf_filename(target=None):
target_con_dir = subp.target_path(target, NM_RUN_DIR)
conf_file = "30-cloud-init-ip6-addr-gen-mode.conf"
return f"{target_con_dir}/conf.d/{conf_file}"
def available():
# TODO: Move `uses_systemd` to a more appropriate location
# It is imported here to avoid circular import
from cloudinit.distros import uses_systemd
nmcli_present = subp.which("nmcli")
service_active = True
if uses_systemd():
try:
subp.subp(["systemctl", "is-enabled", "NetworkManager.service"])
except subp.ProcessExecutionError:
service_active = False
return bool(nmcli_present) and service_active
|