From 603657a166bff4a16606a2575364446171466af6 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 23 Mar 2017 20:08:19 +0200 Subject: Initial implementation of network configuration --- init | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) (limited to 'init') diff --git a/init b/init index 0caf718..b733fde 100755 --- a/init +++ b/init @@ -57,7 +57,101 @@ udevadm settle || true cmdline="$(cat /proc/cmdline)" info "boot cmdline: $cmdline" -sleep 2 +# Figure out network configuration and generate the corresponding +# /etc/network/interfaces. +# +info "starting network..." + +# We are using udev's predictable interface names. The two character prefixes +# based on the type of interface: +# +# en -- ethernet +# sl -- serial line IP (slip) +# wl -- wlan +# ww -- wwan +# +eth_all="$(cd /sys/class/net && ls -d en?*)" + +if [ -z "$eth_all" ]; then + info "no ethernet interfaces found among:" + ip link show +fi + +eth= +eth_up= +for s in 1 2 4 8; do + + # Try to bring them all up and find the one that has carrier. + # + for i in $eth_all; do + ip link set "$i" up || true + done + + sleep "$s" + + for i in $eth_all; do + if [ "$(cat "/sys/class/net/$i/carrier")" -eq "1" ]; then + info "detected carrier on $i" + eth_up+=" $i" + fi + done + + # Bring them all down. + # + for i in $eth_all; do + ip link set "$i" down || true + done + + # If we didn't find anything, try to wait for carrier longer. + # + if [ -z "$eth_up" ]; then + continue + fi + + # If we end up with several interfaces we simply unleash dhcp on all of + # them and use the first that gets configured. + # + # Note also that it's possible the interface that we want is not yet ready + # in which case we will try to wait for carrier a bit longer. + # + for i in $eth_up; do + if dhclient -v "$i"; then + eth="$i" + break + fi + done + + if [ -n "$eth" ]; then + break + fi +done + +if [ -z "$eth_up" ]; then + error "no ethernet interfaces with carrier among:" + ip link show +fi + +if [ -z "$eth" ]; then + error "no ethernet interfaces with DHCP among:" + ip link show +fi + +mac="$(cat "/sys/class/net/$eth/address")" + +info "configured $eth ($mac)" + +# Set the hostname. +# +hname="$(hostname)" + +if [ "$hname" = "(none)" ]; then + hname="build-$(sed -e 's/://g' <<<"$mac")" + hostname "$hname" +fi + +echo "$hname" >/etc/hostname + +info "hostname $hname" # --machine-id # -- cgit v1.1