2013-06-25 20:04:59 +04:00
#!/bin/sh
# parse interface configuration tuples:
# iface:proto[:ipv4addr/netmask[:ipv4gw]]
# and write it down as intended
fatal() { echo "error: $*" >&2; exit 1; }
# simple etcnet configurations are also picked up by NM
2020-07-03 00:56:33 +03:00
if [ -n "$GLOBAL_NMCTL" ]; then
NMCTL="$GLOBAL_NMCTL"
2013-06-25 20:04:59 +04:00
else
2020-07-03 00:56:33 +03:00
if type -t NetworkManager >&/dev/null ||
type -t connmand >&/dev/null; then
NMCTL=yes
else
NMCTL=no
fi
2013-06-25 20:04:59 +04:00
fi
2015-12-07 09:48:08 +03:00
ETCNET_IFDIR="/etc/net/ifaces"
SYSTEMD_IFDIR="/etc/systemd/network"
2013-06-25 20:04:59 +04:00
write_iface() {
2015-12-07 09:48:08 +03:00
case "$proto" in
dhcp)
;;
static)
[ -n "$ipv4addr" ] || fatal "ipv4addr missing"
;;
*)
fatal "unknown proto value: $proto"
;;
esac
[ -d "$ETCNET_IFDIR" ] && write_etcnet_iface ||:
[ -d "$SYSTEMD_IFDIR" ] && write_systemd_iface ||:
}
# these use global variables
write_etcnet_iface() {
dir="$ETCNET_IFDIR/$iface"
2013-06-25 20:04:59 +04:00
mkdir -p "$dir"
2014-02-01 18:22:36 +04:00
append=
2013-06-25 20:04:59 +04:00
case "$proto" in
dhcp)
2021-02-05 18:48:52 +03:00
append="DHCP_TIMEOUT=$GLOBAL_NET_ETH_TIMEOUT"
2013-06-25 20:04:59 +04:00
;;
static)
echo "$ipv4addr" > "$dir/ipv4address"
[ -z "$ipv4gw" ] ||
echo "default via $ipv4gw" > "$dir/ipv4route"
;;
esac
{
echo "TYPE=eth"
echo "BOOTPROTO=$proto"
echo "NM_CONTROLLED=$NMCTL"
2013-07-05 22:27:14 +04:00
echo "DISABLED=$NMCTL"
2013-07-19 20:57:51 +04:00
echo "#USE_IFPLUGD=yes"
2014-04-21 17:49:08 +04:00
[ -z "$append" ] || echo "$append"
2013-06-25 20:04:59 +04:00
} > "$dir/options"
}
2015-12-07 09:48:08 +03:00
write_systemd_iface() {
case "$proto" in
dhcp)
echo "[Match]"
echo "Name=$iface"
echo
echo "[Network]"
echo "DHCP=yes"
2016-11-02 23:45:22 +03:00
echo
echo "[DHCP]"
echo "UseMTU=yes"
echo "UseDomains=yes"
echo "UseNTP=yes"
echo
2015-12-07 09:48:08 +03:00
;;
static)
echo "[Match]"
echo "Name=$iface"
echo
echo "[Network]"
echo "Address=$ipv4addr"
[ -z "$ipv4gw" ] ||
echo "Gateway=$ipv4gw"
echo "LinkLocalAddressing=no"
;;
esac > "$SYSTEMD_IFDIR/$iface.network"
}
2013-06-25 20:04:59 +04:00
[ -z "$GLOBAL_NET_ETH" ] ||
echo "$GLOBAL_NET_ETH" \
| tr ' ' '\n' \
| while IFS=':' read iface proto ipv4addr ipv4gw; do
[ -n "$iface" -a -n "$proto" ] || continue
write_iface
done