summaryrefslogtreecommitdiff
path: root/vmdebootstrap
diff options
context:
space:
mode:
authorNeil Williams <codehelp@debian.org>2015-12-31 12:59:01 +0000
committerNeil Williams <codehelp@debian.org>2015-12-31 12:59:01 +0000
commit78f8e6657ba4fef04919dad889257d7f341c035b (patch)
tree9a3f4911d0dfb0f95e6843b9a50fb16b9bc368fd /vmdebootstrap
parentb7c3af89b3cd5c64717f008bb5e59d15752c5725 (diff)
downloadvmdebootstrap-78f8e6657ba4fef04919dad889257d7f341c035b.tar.gz
Add support for systemd-networkd
When masking udev/systemd predictable network interface names, the initramfs must be updated or the mask will not be effective. Add support for systemd-networkd using predictable network interface names - can be extended using customisation scripts.
Diffstat (limited to 'vmdebootstrap')
-rw-r--r--vmdebootstrap/base.py11
-rw-r--r--vmdebootstrap/filesystem.py8
-rw-r--r--vmdebootstrap/network.py105
3 files changed, 111 insertions, 13 deletions
diff --git a/vmdebootstrap/base.py b/vmdebootstrap/base.py
index 4ce5236..43d5a3e 100644
--- a/vmdebootstrap/base.py
+++ b/vmdebootstrap/base.py
@@ -204,14 +204,3 @@ class Base(object):
if self.settings['kernel-package']:
packages.append(self.settings['kernel-package'])
return packages
-
- def mask_udev_predictable_rules(self, rootdir):
- """
- This can be reset later but to get networking using immediately
- on boot, the interface we're going to use must be known
- http://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/
- """
- self.message('Disabling systemd predictable interface names')
- udev_path = os.path.join(
- 'etc', 'udev', 'rules.d', '80-net-setup-link.rules')
- runcmd(['chroot', rootdir, 'ln', '-s', '/dev/null', udev_path])
diff --git a/vmdebootstrap/filesystem.py b/vmdebootstrap/filesystem.py
index 106fa56..ad7575c 100644
--- a/vmdebootstrap/filesystem.py
+++ b/vmdebootstrap/filesystem.py
@@ -70,11 +70,15 @@ class Filesystem(Base):
runcmd(["chown", "-R", self.settings["owner"], filename])
def update_initramfs(self):
+ if not rootdir:
+ raise cliapp.AppException("rootdir not set")
+ if not os.path.exists(
+ os.path.join(rootdir, 'usr', 'sbin', 'update-initramfs')):
+ self.message("Error: Unable to run update-initramfs.")
+ return
if not self.settings['no-update-initramfs']:
return
rootdir = self.devices['rootdir']
- if not rootdir:
- raise cliapp.AppException("rootdir not set")
cmd = os.path.join('usr', 'sbin', 'update-initramfs')
if os.path.exists(os.path.join(str(rootdir), cmd)):
self.message("Updating the initramfs")
diff --git a/vmdebootstrap/network.py b/vmdebootstrap/network.py
new file mode 100644
index 0000000..740dede
--- /dev/null
+++ b/vmdebootstrap/network.py
@@ -0,0 +1,105 @@
+"""
+ Wrapper for network support
+"""
+# -*- coding: utf-8 -*-
+#
+# network.py
+#
+# Copyright 2015 Neil Williams <codehelp@debian.org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA.
+#
+#
+
+import os
+from vmdebootstrap.base import (
+ Base,
+ runcmd,
+)
+
+# pylint: disable=missing-docstring
+
+
+class Networking(Base):
+
+ name = 'networking'
+
+ def _write_network_interfaces(self, rootdir, line):
+ self.message('Setting up networking')
+ ifc_d = os.path.join(rootdir, 'etc', 'network', 'interfaces.d')
+ ifc_file = os.path.join(rootdir, 'etc', 'network', 'interfaces')
+ with open(ifc_file, 'w') as netfile:
+ netfile.write(line)
+ if not os.path.exists(ifc_d):
+ os.mkdir(ifc_d)
+ with open(ethpath, 'w') as eth:
+ eth.write('auto lo\n')
+ eth.write('iface lo inet loopback\n')
+ if self.settings['enable-dhcp']:
+ eth.write('\n')
+ eth.write('auto eth0\n')
+ eth.write('iface eth0 inet dhcp\n')
+
+ def setup_wheezy_networking(self, rootdir):
+ """
+ unconditionally write for wheezy
+ (which became oldstable on 2015.04.25)
+ """
+ self._write_network_interfaces(
+ rootdir, 'source /etc/network/interfaces.d/*\n')
+
+ def setup_networking(self, rootdir):
+ self._write_network_interfaces(
+ rootdir, 'source-directory /etc/network/interfaces.d\n')
+
+ def systemd_support(self, rootdir):
+ """
+ Handle the systemd-networkd setting
+ """
+ if self.settings['systemd-networkd']:
+ self.enable_systemd_networkd(rootdir)
+ else:
+ self.mask_udev_predictable_rules(rootdir)
+
+ def mask_udev_predictable_rules(self, rootdir):
+ """
+ This can be reset later but to get networking using eth0
+ immediately upon boot, the interface we're going to use must
+ be known and must update the initramfs after setting up the
+ mask.
+ """
+ self.message('Disabling systemd predictable interface names')
+ udev_path = os.path.join(
+ 'etc', 'udev', 'rules.d', '80-net-setup-link.rules')
+ runcmd(['chroot', rootdir, 'ln', '-s', '/dev/null', udev_path])
+
+ def enable_systemd_networkd(self, rootdir):
+ """
+ Get networking working immediately on boot, allow any en* interface
+ to be enabled by systemd-networkd using DHCP
+ https://coreos.com/os/docs/latest/network-config-with-networkd.html
+ http://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/
+ """
+ if not self.settings['enable-dhcp']:
+ return
+ self.message('Enabling systemd-networkd for DHCP')
+ ethpath = os.path.join(rootdir, 'etc', 'systemd', 'network', '99-dhcp.network')
+ with open(ethpath, 'w') as eth:
+ eth.write('[Match]\n')
+ eth.write('Name=en*\n')
+ eth.write('\n[Network]\n')
+ eth.write('DHCP=yes\n')
+ runcmd(['chroot', rootdir, 'systemctl', 'enable', 'systemd-networkd'])