Debian Codegraph @

finish-install.d/55netcfg-copy-config

raw
1 #! /bin/sh
2 set -e
3
4 . /usr/share/debconf/confmodule
5
6 # File paths for various configuration files
7 NETPLAN_INSTALLER_FILE=00-installer-config.yaml
8 FILE_PATH_NETPLAN_CONFIG=etc/netplan
9 FILE_PATH_NM_CONFIG=etc/NetworkManager/system-connections
10 FILE_INTERFACES=/etc/network/interfaces
11 FILE_NETCFG_CONNECTION_TYPE=/tmp/connection_type
12
13 # The connection type file is written by the nm-conf code.
14 # The base-installer snippet will already take care of copying
15 # the generated interfaces file, so if we are not dealing
16 # with nm-conf anyway: just exit.
17 if [ ! -e $FILE_NETCFG_CONNECTION_TYPE ]; then
18 logger -t netcfg "DEBUG: copy-config: $FILE_NETCFG_CONNECTION_TYPE not found: netcfg did not complete or was compiled without network-manager support; exiting."
19 exit 0
20 fi
21
22 # Flag to determine whether Netplan is installed.
23 if in-target sh -c "dpkg-query -s netplan-generator 2>/dev/null | grep -q '^Status: install ok installed'"; then
24 NETPLAN_IS_INSTALLED=true
25 else
26 NETPLAN_IS_INSTALLED=false
27 fi
28
29 # Flag to determine whether Network Manager is installed.
30 if in-target sh -c "dpkg-query -s network-manager 2>/dev/null | grep -q '^Status: install ok installed'"; then
31 NM_IS_INSTALLED=true
32 else
33 NM_IS_INSTALLED=false
34 fi
35
36 # The type of the connection used during installation
37 NETCFG_CONNECTION_TYPE=$(cat $FILE_NETCFG_CONNECTION_TYPE | \
38 grep "connection type" | cut -d ':' -f2 | sed 's/ //g')
39 NETCFG_CONNECTION_SECURITY=$(cat $FILE_NETCFG_CONNECTION_TYPE | \
40 grep "security" | cut -d ':' -f2 | sed 's/ //g')
41
42 # netcfg/target_network_config question values
43 CONFIG_NETPLAN="netplan"
44 CONFIG_NM="nm_config"
45 CONFIG_INTERFACES="ifupdown"
46 CONFIG_LOOPBACK="loopback"
47
48 db_get netcfg/target_network_config
49
50 # Check for preseeding. If the value of the question is empty then set
51 # default options. Document automatic selection changes in the template.
52 if [ -z "$RET" ]; then
53 # Fall through to classic NM/ifupdown config if no Netplan config was
54 # written (e.g. for WEP "secured" WiFi)
55 if $NETPLAN_IS_INSTALLED && [ -f "/$FILE_PATH_NETPLAN_CONFIG/$NETPLAN_INSTALLER_FILE" ]; then
56 db_set netcfg/target_network_config $CONFIG_NETPLAN
57 elif $NM_IS_INSTALLED; then
58 db_set netcfg/target_network_config $CONFIG_NM
59 else
60 db_set netcfg/target_network_config $CONFIG_INTERFACES
61 fi
62 fi
63
64 db_get netcfg/target_network_config
65
66 case $RET in
67 $CONFIG_NETPLAN)
68 # Copy Netplan configuration. First make sure the directory exists
69 mkdir -p /target/$FILE_PATH_NETPLAN_CONFIG
70 cp /$FILE_PATH_NETPLAN_CONFIG/* /target/$FILE_PATH_NETPLAN_CONFIG/
71
72 # Also write nm-all.yaml if NM_IS_INSTALLED.
73 # TODO: We might rather do that inside the network-manager package, see:
74 # https://bugs.launchpad.net/ubuntu/+source/ubuntu-settings/+bug/2020110
75 if $NM_IS_INSTALLED; then
76 cat > /target/$FILE_PATH_NETPLAN_CONFIG/01-network-manager-all.yaml <<EOF
77 # This file was written by debian-installer.
78 # Let NetworkManager manage all devices on this system.
79 # For more information, see netplan(5).
80 network:
81 version: 2
82 renderer: NetworkManager
83 EOF
84 chmod 600 /target/$FILE_PATH_NETPLAN_CONFIG/01-network-manager-all.yaml
85 fi
86
87 # Rewrite /etc/network/interfaces to leave breadcrumbs about Netplan, see:
88 # https://bugs.launchpad.net/netplan/+bug/1756742
89 cat > /target/$FILE_INTERFACES <<EOF
90 # Generated by debian-installer.
91 # This system is configured to use Netplan, see /etc/netplan/
92 EOF
93 ;;
94
95 $CONFIG_NM)
96 # Copy NM config file. First make sure the directory exists
97 mkdir -p /target/$FILE_PATH_NM_CONFIG
98 cp /$FILE_PATH_NM_CONFIG/* /target/$FILE_PATH_NM_CONFIG/
99
100 # Rewrite /etc/network/interfaces to contain only loopback
101 netcfg write_loopback
102 ;;
103
104 $CONFIG_INTERFACES)
105 # Files are copied by the base-installer hook already, beware of secrets:
106 if [ "$NETCFG_CONNECTION_TYPE" = wireless ] && [ "$NETCFG_CONNECTION_SECURITY" = secured ]; then
107 logger -t netcfg "setting $FILE_INTERFACES mode to 600 since it contains secrets"
108 chmod 600 /target/$FILE_INTERFACES
109 fi
110 ;;
111
112 $CONFIG_LOOPBACK)
113 # Rewrite /etc/network/interfaces to contain only loopback
114 netcfg write_loopback
115 ;;
116 esac
117
118 case $RET in
119 $CONFIG_NM|$CONFIG_LOOPBACK)
120 # Copy /etc/network/interfaces to target.
121 # Netplan does not need /e/n/i for loopback as it depends on systemd,
122 # which configures 'lo' implicitly
123 mkdir -p /target$(dirname $FILE_INTERFACES)
124 cp $FILE_INTERFACES /target$FILE_INTERFACES
125 ;;
126 esac
127
128 exit 0