Skip to content
Snippets Groups Projects
Commit 7c0137d6 authored by Cyril Brulebois's avatar Cyril Brulebois
Browse files

Merge branch 'merge-requests/9'

With thanks to Lukas Märdian for the nice work!

Conflicts:
	debian/changelog
parents 6a9f7d3c 6412a9f2
Branches
Tags
1 merge request!9Allow to generate Netplan configuration (using systemd-networkd or NetworkManager backend)
......@@ -13,9 +13,11 @@ NETCFG_STATIC_O = netcfg-static.o static.o ethtool-lite.o
WIRELESS = 1
NM = 1
NETPLAN = 1
ifneq ($(DEB_HOST_ARCH_OS),linux)
WIRELESS = 0
NM = 0
NETPLAN = 0
endif
ifeq ($(DEB_HOST_ARCH),s390)
WIRELESS = 0
......@@ -35,6 +37,10 @@ ifneq ($(NM),0)
CFLAGS += -DNM
NETCFG_O += nm-conf.o
endif
ifneq ($(NETPLAN),0)
CFLAGS += -DNETPLAN
NETCFG_O += netplan-conf.o
endif
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0 -g3
......
netcfg (1.190) UNRELEASED; urgency=medium
[ Lukas Märdian ]
* Initial Netplan enablement:
D-I will now detect if 'netplan-generator' is installed in the target
system and write corresponding configuration to /etc/netplan/ in that case.
If 'network-manager' is installed as well, it will set Netplan's default
renderer to be NetworkManager (it defaults to systemd-networkd otherwise).
In case Netplan is not installed D-I will continue to write NetworkManager
configuration to /etc/NetworkManager/system-connections and fall back to
ifupdown (/etc/network/interfaces) if none of the above is installed.
[ Updated translations ]
* Georgian (ka.po) by Temuri Doghonadze
......
......@@ -341,15 +341,15 @@ _Description: Configure the network
Template: netcfg/target_network_config
Type: select
Choices-C: nm_config, ifupdown, loopback
Choices: Network Manager, ifupdown (/etc/network/interfaces), No network configuration
Choices-C: netplan, nm_config, ifupdown, loopback
Choices: Netplan, NetworkManager, ifupdown (/etc/network/interfaces), No network configuration
Description: for internal use; can be preseeded
Specifies what kind of network connection management tool should be
configured post-installation if multiple are available. Automatic
selection is used in this order when not specified: network-manager if
available (on Linux only), ethernet configuration through ifupdown on wired
installation and loopback configuration through ifupdown on wireless
installations.
selection is used in this order when not specified: Netplan if
available (on Linux only), NetworkManager if available (on Linux only),
Ethernet or wireless configuration through ifupdown and loopback
configuration through ifupdown as a fallback.
Template: netcfg/link_wait_timeout
Type: string
......
......@@ -4,6 +4,8 @@ set -e
. /usr/share/debconf/confmodule
# File paths for various configuration files
NETPLAN_INSTALLER_FILE=00-installer-config.yaml
FILE_PATH_NETPLAN_CONFIG=etc/netplan
FILE_PATH_NM_CONFIG=etc/NetworkManager/system-connections
FILE_INTERFACES=/etc/network/interfaces
FILE_NETCFG_CONNECTION_TYPE=/tmp/connection_type
......@@ -17,6 +19,13 @@ if [ ! -e $FILE_NETCFG_CONNECTION_TYPE ]; then
exit 0
fi
# Flag to determine whether Netplan is installed.
if in-target sh -c "dpkg-query -s netplan-generator 2>/dev/null | grep -q '^Status: install ok installed'"; then
NETPLAN_IS_INSTALLED=true
else
NETPLAN_IS_INSTALLED=false
fi
# Flag to determine whether Network Manager is installed.
if in-target sh -c "dpkg-query -s network-manager 2>/dev/null | grep -q '^Status: install ok installed'"; then
NM_IS_INSTALLED=true
......@@ -31,6 +40,7 @@ NETCFG_CONNECTION_SECURITY=$(cat $FILE_NETCFG_CONNECTION_TYPE | \
grep "security" | cut -d ':' -f2 | sed 's/ //g')
# netcfg/target_network_config question values
CONFIG_NETPLAN="netplan"
CONFIG_NM="nm_config"
CONFIG_INTERFACES="ifupdown"
CONFIG_LOOPBACK="loopback"
......@@ -40,7 +50,11 @@ db_get netcfg/target_network_config
# Check for preseeding. If the value of the question is empty then set
# default options. Document automatic selection changes in the template.
if [ -z "$RET" ]; then
if $NM_IS_INSTALLED; then
# Fall through to classic NM/ifupdown config if no Netplan config was
# written (e.g. for WEP "secured" WiFi)
if $NETPLAN_IS_INSTALLED && [ -f "/$FILE_PATH_NETPLAN_CONFIG/$NETPLAN_INSTALLER_FILE" ]; then
db_set netcfg/target_network_config $CONFIG_NETPLAN
elif $NM_IS_INSTALLED; then
db_set netcfg/target_network_config $CONFIG_NM
else
db_set netcfg/target_network_config $CONFIG_INTERFACES
......@@ -50,6 +64,34 @@ fi
db_get netcfg/target_network_config
case $RET in
$CONFIG_NETPLAN)
# Copy Netplan configuration. First make sure the directory exists
mkdir -p /target/$FILE_PATH_NETPLAN_CONFIG
cp /$FILE_PATH_NETPLAN_CONFIG/* /target/$FILE_PATH_NETPLAN_CONFIG/
# Also write nm-all.yaml if NM_IS_INSTALLED.
# TODO: We might rather do that inside the network-manager package, see:
# https://bugs.launchpad.net/ubuntu/+source/ubuntu-settings/+bug/2020110
if $NM_IS_INSTALLED; then
cat > /target/$FILE_PATH_NETPLAN_CONFIG/01-network-manager-all.yaml <<EOF
# This file was written by debian-installer.
# Let NetworkManager manage all devices on this system.
# For more information, see netplan(5).
network:
version: 2
renderer: NetworkManager
EOF
chmod 600 /target/$FILE_PATH_NETPLAN_CONFIG/01-network-manager-all.yaml
fi
# Rewrite /etc/network/interfaces to leave breadcrumbs about Netplan, see:
# https://bugs.launchpad.net/netplan/+bug/1756742
cat > /target/$FILE_INTERFACES <<EOF
# Generated by debian-installer.
# This system is configured to use Netplan, see /etc/netplan/
EOF
;;
$CONFIG_NM)
# Copy NM config file. First make sure the directory exists
mkdir -p /target/$FILE_PATH_NM_CONFIG
......@@ -76,6 +118,8 @@ esac
case $RET in
$CONFIG_NM|$CONFIG_LOOPBACK)
# Copy /etc/network/interfaces to target.
# Netplan does not need /e/n/i for loopback as it depends on systemd,
# which configures 'lo' implicitly
mkdir -p /target$(dirname $FILE_INTERFACES)
cp $FILE_INTERFACES /target$FILE_INTERFACES
;;
......
......@@ -22,6 +22,7 @@
*/
#include "netcfg.h"
#include "netplan-conf.h"
#include "nm-conf.h"
#include <string.h>
#include <stdlib.h>
......@@ -343,6 +344,10 @@ int main(int argc, char *argv[])
break;
case QUIT:
#ifdef NETPLAN
if (num_interfaces > 0)
netplan_write_configuration(&interface);
#endif
#ifdef NM
if (num_interfaces > 0) {
nm_get_configuration(&interface, &nmconf);
......
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#ifdef WIRELESS
#include <iwlib.h>
#endif
#include <debian-installer.h>
#include "netplan-conf.h"
/* Functions for printing information in Netplan format. */
static void netplan_write_header(FILE *fd)
{
// Consider using libnetplan.so instead of writing plain YAML.
fprintf(fd, "# This is the network config written by debian-installer.\n");
fprintf(fd, "# For more information, see netplan(5).\n");
fprintf(fd, "network:\n");
fprintf(fd, " version: 2\n");
}
static void netplan_write_wireless(FILE *fd, const struct netcfg_interface *interface)
{
if (!(interface->essid && *interface->essid)) {
di_error("Unspecified SSID"); // "essid=any" not supported by Netplan
exit(1);
}
fprintf(fd, " wifis:\n");
fprintf(fd, " %s:\n", interface->name); // consider matching on mac address
fprintf(fd, " access-points:\n");
fprintf(fd, " \"%s\":\n", interface->essid);
if (interface->wifi_security == REPLY_WPA || interface->wpa_supplicant_status == WPA_QUEUED)
fprintf(fd, " password: %s\n", interface->passphrase);
else {
fprintf(fd, " auth:\n");
fprintf(fd, " key-management: none\n"); // open network
}
if (interface->mode == ADHOC)
fprintf(fd, " mode: adhoc\n");
}
static void netplan_write_wired(FILE *fd, const struct netcfg_interface *interface)
{
fprintf(fd, " ethernets:\n");
fprintf(fd, " %s:\n", interface->name); // consider matching on mac address
}
static void netplan_write_common(FILE *fd, const struct netcfg_interface *interface)
{
// DNS: nameservers and search domain
if (!empty_str(interface->nameservers[0]) || !empty_str(domain)) {
fprintf(fd, " nameservers:\n");
unsigned int i = 0;
if (!empty_str(interface->nameservers[0])) {
fprintf(fd, " addresses:\n");
for (i = 0; i < NETCFG_NAMESERVERS_MAX; i++) {
if (!empty_str(interface->nameservers[i]))
fprintf(fd, " - %s\n", interface->nameservers[i]);
}
}
if (!empty_str(domain)) {
fprintf(fd, " search:\n");
fprintf(fd, " - %s\n", domain);
}
}
// Hotplug
if (iface_is_hotpluggable(interface->name) || find_in_stab(interface->name)) {
fprintf(fd, " optional: true\n");
}
// DHCP
if (interface->dhcp == 1) {
di_debug("Writing DHCP stanza for %s", interface->name);
fprintf(fd, " ipv6-privacy: true\n");
fprintf(fd, " dhcp4: true\n");
fprintf(fd, " dhcp6: true\n");
if (!empty_str(interface->dhcp_hostname)) {
fprintf(fd, " dhcp4-overrides:\n");
fprintf(fd, " hostname: \"%s\"\n", interface->dhcp_hostname);
fprintf(fd, " dhcp6-overrides:\n");
fprintf(fd, " hostname: \"%s\"\n", interface->dhcp_hostname);
}
}
// SLAAC
if (interface->slaac == 1) {
di_debug("Writing SLAAC stanza for %s", interface->name);
// Implicitly enable Netplan's dhcp6 & ipv6-privacy, see:
// https://netplan.rtfd.io/en/stable/netplan-yaml/#properties-for-all-device-types
if (interface->dhcp == 0) {
fprintf(fd, " ipv6-privacy: true\n");
fprintf(fd, " dhcp6: true\n");
}
fprintf(fd, " accept-ra: true\n");
}
// Static IP
// IPv6 is unsupported on point-to-point links, according to
// "netcfg-static.templates", so we can hardcode a /32 netmask in that case.
if (interface->address_family == AF_INET || interface->address_family == AF_INET6) {
di_debug("Writing static IP stanza for %s", interface->name);
fprintf(fd, " addresses:\n");
fprintf(fd, " - \"%s/%i\"\n", interface->ipaddress,
empty_str(interface->pointopoint) ? interface->masklen : 32);
}
// Default gateway
// Installing an "on-link" route for point-to-point or manual IPv4 /32 or
// IPv6 /128 netmasks. See: https://salsa.debian.org/installer-team/netcfg/-/merge_requests/10
if (!empty_str(interface->gateway) || !empty_str(interface->pointopoint)) {
fprintf(fd, " routes:\n");
fprintf(fd, " - to: default\n");
fprintf(fd, " via: %s\n",
empty_str(interface->pointopoint) ? interface->gateway : interface->pointopoint);
if (!empty_str(interface->pointopoint) ||
(interface->address_family == AF_INET && interface->masklen == 32) ||
(interface->address_family == AF_INET6 && interface->masklen == 128))
fprintf(fd, " on-link: true\n");
}
}
/* Write Netplan config file. */
void netplan_write_configuration(const struct netcfg_interface *interface)
{
FILE *config_file = NULL;
char buffer[NETPLAN_MAX_LEN_BUF] = {0};
int is_loopback = (interface->loopback == 1);
int is_wireless = is_wireless_iface(interface->name);
// WEP is deprecated and not supported by Netplan. Do not write any configuration.
if (is_wireless && interface->wepkey != NULL) {
di_warning("Netplan does not support insecure WEP wireless networks.");
di_debug("Not writing any Netplan configuration.");
return;
}
/* Create the directory for the config file and clear any possible
* previous files found there. */
sprintf(buffer, "mkdir -p %s", NETPLAN_CONFIG_FILE_PATH);
di_exec_shell(buffer);
/* If the directory exist mkdir will do nothing, so just remove every file
* there. Rely on the fact that for now netcfg only does config for one
* interface. */
sprintf(buffer, "rm %s/*", NETPLAN_CONFIG_FILE_PATH);
di_exec_shell(buffer);
/* Open file using its full path. */
sprintf(buffer, "%s/%s", NETPLAN_CONFIG_FILE_PATH, NETPLAN_INSTALLER_FILE);
config_file = fopen(buffer, "w");
if (config_file == NULL) {
di_info("Unable to open file for writing Netplan configuration: "
"%s", strerror(errno));
return;
}
if (fchmod(fileno(config_file), 0600) != 0) {
di_error("Netplan configuration file cannot be protected "
"from reading: %s", strerror(errno));
exit(1);
}
netplan_write_header(config_file);
// Do not write anything, as loopback is set up by systemd implicitly.
// Being a dependency, systemd is always available when Netplan is in use.
if (is_loopback) {}
else if (is_wireless)
netplan_write_wireless(config_file, interface);
else
netplan_write_wired(config_file, interface);
netplan_write_common(config_file, interface);
fclose(config_file);
}
#ifndef _NETPLAN_CONF_H
#define _NETPLAN_CONF_H
#include "netcfg.h"
/* Constants for maximum size for Netplan config fields. */
#define NETPLAN_MAX_LEN_BUF 1024 /* Max len for most buffers */
/* Some Netplan default values. */
#define NETPLAN_CONFIG_FILE_PATH "/etc/netplan"
#define NETPLAN_INSTALLER_FILE "00-installer-config.yaml"
/* Public functions */
void netplan_write_configuration(const struct netcfg_interface *niface);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment