upstream 00f2ea2b8 as of 2018-09-02
[automated-distro-installer] / fai / config / scripts / DEBIAN / 30-interface
1 #! /bin/bash
2
3 error=0; trap 'error=$(($?>$error?$?:$error))' ERR # save maximum error code
4
5
6 newnicnames() {
7
8 # determine predictable network names only for stretch and above
9
10 [ $do_init_tasks -eq 0 ] && return
11
12 ver=$($ROOTCMD dpkg-query --showformat='${Version}' --show udev)
13 if dpkg --compare-versions $ver lt 220-7; then
14 return
15 fi
16
17 [ -z "$NIC1" ] && return
18
19 fields="ID_NET_NAME_FROM_DATABASE ID_NET_NAME_ONBOARD ID_NET_NAME_SLOT ID_NET_NAME_PATH"
20 for field in $fields; do
21 name=$(udevadm info /sys/class/net/$NIC1 | sed -rn "s/^E: $field=(.+)/\1/p")
22 if [[ $name ]]; then
23 NIC1=$name
24 break
25 fi
26 done
27 if [[ ! $name ]]; then
28 echo "$0: error: could not find systemd predictable network name. Using $NIC1."
29 fi
30 }
31
32 if [ -z "$NIC1" ]; then
33 echo "ERROR: \$NIC1 is not defined. Cannot configure /etc/network/interfaces properly."
34 fi
35 newnicnames
36 CIDR=$(ip -o -f inet addr show $NIC1 | awk '{print $4}')
37 if ifclass DHCPC && [ $FAI_ACTION = "install" -o $FAI_ACTION = "dirinstall" ]; then
38
39 cat > $target/etc/network/interfaces <<-EOF
40 # generated by FAI
41 auto lo $NIC1
42 iface lo inet loopback
43 iface $NIC1 inet dhcp
44 EOF
45 elif [ $FAI_ACTION = "install" -o $FAI_ACTION = "dirinstall" ]; then
46 [ -n "$CIDR" ] && cat > $target/etc/network/interfaces <<-EOF
47 # generated by FAI
48 auto lo $NIC1
49 iface lo inet loopback
50 iface $NIC1 inet static
51 address $CIDR
52 gateway $GATEWAYS
53 EOF
54 [ -n "$NETWORK" ] && echo "localnet $NETWORK" > $target/etc/networks
55 if [ ! -L $target/etc/resolv.conf -a -e /etc/resolv.conf ]; then
56 cp -p /etc/resolv.conf $target/etc
57 fi
58 fi
59
60 # here fcopy is mostly used, when installing a client for running in a
61 # different subnet than during the installation
62 fcopy -iM /etc/resolv.conf
63 fcopy -iM /etc/network/interfaces /etc/networks
64
65 exit $error