summaryrefslogtreecommitdiff
path: root/cloudinit/config/cc_refresh_rmc_and_interface.py
blob: 87923f0b07d17132c173edca274762a73924a2ff (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
# (c) Copyright IBM Corp. 2020 All Rights Reserved
#
# Author: Aman Kumar Sinha <amansi26@in.ibm.com>
#
# This file is part of cloud-init. See LICENSE file for license information.

"""Refresh IPv6 interface and RMC:
Ensure Network Manager is not managing IPv6 interface"""

import errno
from logging import Logger

from cloudinit import log as logging
from cloudinit import netinfo, subp, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema
from cloudinit.distros import ALL_DISTROS
from cloudinit.settings import PER_ALWAYS

MODULE_DESCRIPTION = """\
This module is IBM PowerVM Hypervisor specific

Reliable Scalable Cluster Technology (RSCT) is a set of software components
that together provide a comprehensive clustering environment(RAS features)
for IBM PowerVM based virtual machines. RSCT includes the Resource
Monitoring and Control (RMC) subsystem. RMC is a generalized framework used
for managing, monitoring, and manipulating resources. RMC runs as a daemon
process on individual machines and needs creation of unique node id and
restarts during VM boot.
More details refer
https://www.ibm.com/support/knowledgecenter/en/SGVKBA_3.2/admin/bl503_ovrv.htm

This module handles
- Refreshing RMC
- Disabling NetworkManager from handling IPv6 interface, as IPv6 interface
  is used for communication between RMC daemon and PowerVM hypervisor.
"""

meta: MetaSchema = {
    "id": "cc_refresh_rmc_and_interface",
    "name": "Refresh IPv6 Interface and RMC",
    "title": "Ensure Network Manager is not managing IPv6 interface",
    "description": MODULE_DESCRIPTION,
    "distros": [ALL_DISTROS],
    "frequency": PER_ALWAYS,
    "examples": [],
    "activate_by_schema_keys": [],
}

# This module is undocumented in our schema docs
__doc__ = ""

LOG = logging.getLogger(__name__)
# Ensure that /opt/rsct/bin has been added to standard PATH of the
# distro. The symlink to rmcctrl is /usr/sbin/rsct/bin/rmcctrl .
RMCCTRL = "rmcctrl"


def handle(
    name: str, cfg: Config, cloud: Cloud, log: Logger, args: list
) -> None:
    if not subp.which(RMCCTRL):
        LOG.debug("No '%s' in path, disabled", RMCCTRL)
        return

    LOG.debug(
        "Making the IPv6 up explicitly. "
        "Ensuring IPv6 interface is not being handled by NetworkManager "
        "and it is  restarted to re-establish the communication with "
        "the hypervisor"
    )

    ifaces = find_ipv6_ifaces()

    # Setting NM_CONTROLLED=no for IPv6 interface
    # making it down and up

    if len(ifaces) == 0:
        LOG.debug("Did not find any interfaces with ipv6 addresses.")
    else:
        for iface in ifaces:
            refresh_ipv6(iface)
            disable_ipv6(sysconfig_path(iface))
        restart_network_manager()


def find_ipv6_ifaces():
    info = netinfo.netdev_info()
    ifaces = []
    for iface, data in info.items():
        if iface == "lo":
            LOG.debug("Skipping localhost interface")
        if len(data.get("ipv4", [])) != 0:
            # skip this interface, as it has ipv4 addrs
            continue
        ifaces.append(iface)
    return ifaces


def refresh_ipv6(interface):
    # IPv6 interface is explicitly brought up, subsequent to which the
    # RMC services are restarted to re-establish the communication with
    # the hypervisor.
    subp.subp(["ip", "link", "set", interface, "down"])
    subp.subp(["ip", "link", "set", interface, "up"])


def sysconfig_path(iface):
    return "/etc/sysconfig/network-scripts/ifcfg-" + iface


def restart_network_manager():
    subp.subp(["systemctl", "restart", "NetworkManager"])


def disable_ipv6(iface_file):
    # Ensuring that the communication b/w the hypervisor and VM is not
    # interrupted due to NetworkManager. For this purpose, as part of
    # this function, the NM_CONTROLLED is explicitly set to No for IPV6
    # interface and NetworkManager is restarted.
    try:
        contents = util.load_file(iface_file)
    except IOError as e:
        if e.errno == errno.ENOENT:
            LOG.debug("IPv6 interface file %s does not exist\n", iface_file)
        else:
            raise e

    if "IPV6INIT" not in contents:
        LOG.debug("Interface file %s did not have IPV6INIT", iface_file)
        return

    LOG.debug("Editing interface file %s ", iface_file)

    # Dropping any NM_CONTROLLED or IPV6 lines from IPv6 interface file.
    lines = contents.splitlines()
    lines = [line for line in lines if not search(line)]
    lines.append("NM_CONTROLLED=no")

    with open(iface_file, "w") as fp:
        fp.write("\n".join(lines) + "\n")


def search(contents):
    # Search for any NM_CONTROLLED or IPV6 lines in IPv6 interface file.
    return (
        contents.startswith("IPV6ADDR")
        or contents.startswith("IPADDR6")
        or contents.startswith("IPV6INIT")
        or contents.startswith("NM_CONTROLLED")
    )