|
|
@@ -2,10 +2,22 @@
|
|
|
# All Rights Reserved.
|
|
|
|
|
|
import os
|
|
|
+import yaml
|
|
|
+
|
|
|
+from io import StringIO
|
|
|
|
|
|
from coriolis import utils
|
|
|
from coriolis.osmorphing import base
|
|
|
|
|
|
+LO_NIC_TPL = """
|
|
|
+auto lo
|
|
|
+iface lo inet loopback
|
|
|
+"""
|
|
|
+
|
|
|
+INTERFACES_NIC_TPL = """
|
|
|
+auto %(device_name)s
|
|
|
+iface %(device_name)s inet dhcp
|
|
|
+"""
|
|
|
|
|
|
class BaseDebianMorphingTools(base.BaseLinuxOSMorphingTools):
|
|
|
def _check_os(self):
|
|
|
@@ -45,13 +57,67 @@ class BaseDebianMorphingTools(base.BaseLinuxOSMorphingTools):
|
|
|
self._write_file_sudo(grub_cfg, cfg.dump())
|
|
|
self._exec_cmd_chroot("/usr/sbin/update-grub")
|
|
|
|
|
|
+ def _compose_interfaces_config(self, nics_info):
|
|
|
+ fp = StringIO()
|
|
|
+ fp.write(LO_NIC_TPL)
|
|
|
+ fp.write("\n\n")
|
|
|
+ for idx,_ in enumerate(nics_info):
|
|
|
+ dev_name = "eth%d" % idx
|
|
|
+ cfg = INTERFACES_NIC_TPL % {
|
|
|
+ "device_name": dev_name,
|
|
|
+ }
|
|
|
+ fp.write(cfg)
|
|
|
+ fp.write("\n\n")
|
|
|
+ fp.seek(0)
|
|
|
+ return fp.read()
|
|
|
+
|
|
|
+ def _compose_netplan_cfg(self, nics_info):
|
|
|
+ cfg = {
|
|
|
+ "network":{
|
|
|
+ "version": 2,
|
|
|
+ "ethernets": {
|
|
|
+ "lo": {
|
|
|
+ "match": {
|
|
|
+ "name": "lo"
|
|
|
+ },
|
|
|
+ "addresses": ["127.0.0.1/8"]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for idx,_ in enumerate(nics_info):
|
|
|
+ cfg["network"]["ethernets"]["eth%d" % idx] = {
|
|
|
+ "dhcp4": True,
|
|
|
+ "dhcp6": True,
|
|
|
+ }
|
|
|
+ return yaml.dump(cfg, default_flow_style=False)
|
|
|
+
|
|
|
def set_net_config(self, nics_info, dhcp):
|
|
|
- if dhcp:
|
|
|
- # NOTE: doesn't work with chroot
|
|
|
- interfaces_path = os.path.join(
|
|
|
- self._os_root_dir, "etc/network/interfaces")
|
|
|
- self._exec_cmd('sudo sed -i.bak "s/static/dhcp/g" %s' %
|
|
|
- interfaces_path)
|
|
|
+ if not dhcp:
|
|
|
+ return
|
|
|
+
|
|
|
+ self.disable_predictable_nic_names()
|
|
|
+ # NOTE: doesn't work with chroot
|
|
|
+ if self._test_path("etc/network"):
|
|
|
+ ifaces_file = "etc/network/interfaces"
|
|
|
+ contents = self._compose_interfaces_config(nics_info)
|
|
|
+ if self._test_path(ifaces_file):
|
|
|
+ self._exec_cmd_chroot(
|
|
|
+ "cp %s %s.bak" % (ifaces_file, ifaces_file))
|
|
|
+ self._write_file_sudo(ifaces_file, contents)
|
|
|
+
|
|
|
+ netplan_base = "etc/netplan"
|
|
|
+ if self._test_path(netplan_base):
|
|
|
+ curr_files = self._list_dir(netplan_base)
|
|
|
+ for cnf in curr_files:
|
|
|
+ if cnf.endswith(".yaml") or cnf.endswith(".yml"):
|
|
|
+ pth = "%s/%s" % (netplan_base, cnf)
|
|
|
+ self._exec_cmd_chroot(
|
|
|
+ "cp %s %s.bak" % (pth, pth)
|
|
|
+ )
|
|
|
+ new_cfg = self._compose_netplan_cfg(nics_info)
|
|
|
+ cfg_name = "%s/coriolis_netplan.yaml" % netplan_base
|
|
|
+ self._write_file_sudo(cfg_name, new_cfg)
|
|
|
|
|
|
def pre_packages_install(self, package_names):
|
|
|
super(BaseDebianMorphingTools, self).pre_packages_install(
|