new stack trace, linode fixes, minor improvements
[automated-distro-installer] / fai / config / scripts / DEBIAN / 30-interface
1 #! /bin/bash
2
3 # modified from upstream fai example
4 error=0; trap 'error=$(($?>$error?$?:$error))' ERR # save maximum error code
5
6 netplan_yaml() {
7 # network configuration using ubuntu's netplan.io
8 local IFNAME="$1"
9 local METHOD="$2"
10 echo "Generating netplan configuration for $IFNAME ($METHOD)" >&2
11 echo "# generated by FAI"
12 echo "network:"
13 echo " version: 2"
14 echo " renderer: $RENDERER"
15 case "$RENDERER" in
16 networkd)
17 echo " ethernets:"
18 echo " $IFNAME:"
19 case "$METHOD" in
20 dhcp)
21 echo " dhcp4: true"
22 ;;
23 static)
24 echo " addresses: [$CIDR]"
25 echo " gateway4: $GATEWAYS_1"
26 echo " nameservers:"
27 echo " search: [$DOMAIN]"
28 echo " addresses: [${DNSSRVS// /, }]"
29 ;;
30 esac
31 esac
32 }
33
34 iface_stanza() {
35 # classic network configuration using /etc/network/interfaces
36 local IFNAME="$1"
37 local METHOD="$2"
38 echo "Generating interface configuration for $IFNAME ($METHOD)" >&2
39 echo "# generated by FAI"
40 echo "auto $IFNAME"
41 echo "iface $IFNAME inet $METHOD"
42 case "$METHOD" in
43 static)
44 echo " address $IPADDR"
45 echo " netmask $NETMASK"
46 echo " broadcast $BROADCAST"
47 echo " gateway $GATEWAYS"
48 ;;
49 esac
50 }
51
52 newnicnames() {
53
54 # determine predictable network names only for stretch and above
55
56 [ $do_init_tasks -eq 0 ] && return
57 [ -z "$NIC1" ] && return
58 ver=$($ROOTCMD dpkg-query --showformat='${Version}' --show udev)
59 if dpkg --compare-versions $ver lt 220-7; then
60 return
61 fi
62
63
64 fields="ID_NET_NAME_FROM_DATABASE ID_NET_NAME_ONBOARD ID_NET_NAME_SLOT ID_NET_NAME_PATH"
65 for field in $fields; do
66 name=$(udevadm info /sys/class/net/$NIC1 | sed -rn "s/^E: $field=(.+)/\1/p")
67 if [[ $name ]]; then
68 NIC1=$name
69 break
70 fi
71 done
72 if [[ ! $name ]]; then
73 echo "$0: error: could not find systemd predictable network name. Using $NIC1."
74 fi
75 }
76
77 if [ -z "$NIC1" ]; then
78 echo "WARNING: \$NIC1 is not defined. Cannot add ethernet to /etc/network/interfaces."
79 fi
80 CIDR=$(ip -o -f inet addr show $NIC1 | awk '{print $4}')
81 newnicnames
82
83 case "$FAI_ACTION" in
84 install|dirinstall)
85 ifclass DHCPC && METHOD=dhcp || METHOD=static
86 ifclass XORG && RENDERER=NetworkManager || RENDERER=networkd
87
88 if [ -d $target/etc/netplan ]; then
89 # Ubuntu >= 17.10 with netplan.io
90 if [ -n "$NIC1" ]; then
91 netplan_yaml $NIC1 $METHOD > $target/etc/netplan/01-${NIC1}.yaml
92 fi
93 elif [ -d $target/etc/network/interfaces.d ]; then
94 # ifupdown >= 0.7.41 (Debian >= 8, Ubuntu >= 14.04)
95
96 if ifclass VM || ifclass LINODE; then
97 # note, this condition would apply to the elif below too,
98 # but I don't specify a static ip in fai, so not bothering
99 cat > $target/etc/network/interfaces <<-EOF
100 # generated by FAI
101 auto lo $NIC1
102 iface lo inet loopback
103 iface $NIC1 inet dhcp
104 iface $NIC1 inet6 auto
105 EOF
106
107 elif ifclass LINODE
108 # NIC1 is wrong, probably because the installer distro
109 # is stretch and the installed one is buster.
110 # todo: after upgrading installer to buster,
111 # try removing this condition
112 cat > $target/etc/network/interfaces <<EOF
113 # generated by FAI
114 auto lo $linode_if
115 iface lo inet loopback
116 iface $linode_if inet dhcp
117 iface $linode_if inet6 auto
118 EOF
119
120 else
121 cat > $target/etc/network/interfaces <<EOF
122 # generated by FAI
123 auto lo br0
124 iface lo inet loopback
125 iface $NIC1 inet manual
126 # make a bridge by default so we can have bridged vms.
127 # Some example I read had stp on, but i don't need stp,
128 # and it causes a vm to fail pxe boot, presumably unless
129 # you add some delay.
130 # http://wiki.libvirt.org/page/PXE_boot_%28or_dhcp%29_on_guest_failed
131 iface br0 inet dhcp
132 bridge_ports $NIC1
133 bridge_stp off
134 bridge_maxwait 0
135 EOF
136 fi
137
138 if ifclass LINODE; then
139 mkdir -p $target/etc/initramfs-tools/conf.d
140 cat >$target/etc/initramfs-tools/conf.d/mine <<EOF
141 # dhcp in initramfs doesn't work on linode. i dunno why, whatever.
142 # man 5 initramfs.conf
143 # /usr/share/doc/klibc-utils/README.ipconfig.gz
144 # /usr/share/initramfs-tools/scripts/functions
145 IP=$linode_ip::$linode_gw:255.255.255.0::$linode_if:off
146 EOF
147 fi
148
149 else
150 (
151 iface_stanza lo loopback
152 iface_stanza $NIC1 $METHOD
153 ) > $target/etc/network/interfaces
154 fi
155
156 if ! ifclass DHCPC ; then
157 [ -n "$NETWORK" ] && echo "localnet $NETWORK" > $target/etc/networks
158 if [ ! -L $target/etc/resolv.conf -a -e /etc/resolv.conf ]; then
159 cp -p /etc/resolv.conf $target/etc
160 fi
161 fi
162 ;;
163
164 esac
165
166 # here fcopy is mostly used, when installing a client for running in a
167 # different subnet than during the installation
168 fcopy -iM /etc/resolv.conf
169 fcopy -iM /etc/network/interfaces /etc/networks
170
171 exit $error