Merge branch 'upstream'
[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 # This condition is only needed because the nfsroot I use
73 # is based on Jessie, which has an old udev which can't
74 # figure out the persistent interface name used in stretch.
75 if ifclass VM; then NIC1=ens3; return; fi
76 if [[ ! $name ]]; then
77 echo "$0: error: could not find systemd predictable network name. Using $NIC1."
78 fi
79 }
80
81 if [ -z "$NIC1" ]; then
82 echo "WARNING: \$NIC1 is not defined. Cannot add ethernet to /etc/network/interfaces."
83 fi
84 CIDR=$(ip -o -f inet addr show $NIC1 | awk '{print $4}')
85 newnicnames
86
87 case "$FAI_ACTION" in
88 install|dirinstall)
89 ifclass DHCPC && METHOD=dhcp || METHOD=static
90 ifclass XORG && RENDERER=NetworkManager || RENDERER=networkd
91
92 if [ -d $target/etc/netplan ]; then
93 # Ubuntu >= 17.10 with netplan.io
94 if [ -n "$NIC1" ]; then
95 netplan_yaml $NIC1 $METHOD > $target/etc/netplan/01-${NIC1}.yaml
96 fi
97 elif [ -d $target/etc/network/interfaces.d ]; then
98 # ifupdown >= 0.7.41 (Debian >= 8, Ubuntu >= 14.04)
99
100 if ifclass VM; then
101 # note, this condition would apply to the elif below too,
102 # but I don't specify a static ip in fai, so not bothering
103 cat > $target/etc/network/interfaces <<-EOF
104 # generated by FAI
105 auto lo $NIC1
106 iface lo inet loopback
107 iface $NIC1 inet dhcp
108 EOF
109 else
110 cat > $target/etc/network/interfaces <<-EOF
111 # generated by FAI
112 auto lo br0
113 iface lo inet loopback
114 iface $NIC1 inet manual
115 # make a bridge by default so we can have bridged vms.
116 # Some example I read had stp on, but i don't need stp,
117 # and it causes a vm to fail pxe boot, presumably unless
118 # you add some delay.
119 # http://wiki.libvirt.org/page/PXE_boot_%28or_dhcp%29_on_guest_failed
120 iface br0 inet dhcp
121 bridge_ports $NIC1
122 bridge_stp off
123 bridge_maxwait 0
124 EOF
125 fi
126 else
127 (
128 iface_stanza lo loopback
129 iface_stanza $NIC1 $METHOD
130 ) > $target/etc/network/interfaces
131 fi
132
133 if ! ifclass DHCPC ; then
134 [ -n "$NETWORK" ] && echo "localnet $NETWORK" > $target/etc/networks
135 if [ ! -L $target/etc/resolv.conf -a -e /etc/resolv.conf ]; then
136 cp -p /etc/resolv.conf $target/etc
137 fi
138 fi
139 ;;
140 esac
141
142 # here fcopy is mostly used, when installing a client for running in a
143 # different subnet than during the installation
144 fcopy -iM /etc/resolv.conf
145 fcopy -iM /etc/network/interfaces /etc/networks
146
147 exit $error