Add support systemd-networkd iface control functions

This commit is contained in:
Evgeny Sinelnikov 2021-08-31 01:08:04 +04:00
parent 71e46d2985
commit 1464a9ce32

View File

@ -4,6 +4,7 @@ PATH="/usr/lib/alterator-net-functions:$PATH"
etcnet_iface_dir=/etc/net/ifaces
etcnet_default_iface_dir="$etcnet_iface_dir/default"
systemd_networkd_dir="/etc/systemd/network"
resolvconf_rdelim='[[:space:]]\+'
resolvconf_wdelim=' '
__iface_removed_tag=".REMOVED"
@ -12,6 +13,7 @@ __iface_removed_tag=".REMOVED"
. shell-config
. shell-var
. shell-ip-address
. shell-ini-config
### IPv4 helpers
valid_ipv4prefix()
@ -1060,3 +1062,152 @@ ipv4addr_prefix_to_mask()
echo "Use ipv4_prefix2mask from libshell instead." 1>&2
ipv4_prefix2mask "$@"
}
### common systemd-networkd helpers
get_systemd_networkd_network_filename()
{
local name="$1"
local network_dir="${2:-$systemd_networkd_dir}"
echo "$network_dir/alterator-$name.network"
}
write_systemd_networkd_ini_option()
{
ini_config_set "$1" "$2" "$3" "$4"
}
add_systemd_networkd_ini_option()
{
local latest=
if ini_config_is_set "$1" "$2" "$3"; then
latest=$(grep "^\s*$3\s*=.*" $1 | sed -e 's/^\s*//g' | tail -1)
sed -i -e "1,// s/^\(\s*\)\($latest\)$/\1\2\n\1$3 = $4/" -- "$1"
else
ini_config_set "$1" "$2" "$3" "$4"
fi
}
unset_systemd_networkd_ini_option()
{
ini_config_del "$1" "$2" "$3"
}
overwrite_systemd_networkd_ini_option()
{
local value="$4"
if [ -n "$value" ]; then
write_systemd_networkd_ini_option "$1" "$2" "$3" "$value"
else
unset_systemd_networkd_ini_option "$1" "$2" "$3"
fi
}
write_systemd_networkd_bootproto()
{
local cachedir="$1"; shift
local network_filename="$1"; shift
local first_value_flag= value=
local ipv4_static= ipv6_static=
local ipv4_addresses= ipv6_addresses=
local ipv4_gateway= ipv6_gateway=
case "$1" in
dhcp) value=yes;;
dhcp6) value=ipv6; ipv4_static=1;;
dhcp4) value=ipv4; ipv6_static=1;;
static) ipv4_static=1; ipv6_static=1;;
esac
echo overwrite_systemd_networkd_ini_option "$network_filename" "Network" "DHCP" "$value"
overwrite_systemd_networkd_ini_option "$network_filename" "Network" "DHCP" "$value"
unset_systemd_networkd_ini_option "$network_filename" "Network" "Address"
unset_systemd_networkd_ini_option "$network_filename" "Network" "Gateway"
if [ -n "$ipv4_static" ]; then
ipv4_addresses="$(read_iface_addresses "$cachedir" 4)"
ipv4_gateway="$(read_iface_default_gw "$cachedir" 4)"
fi
if [ -n "$ipv6_static" ]; then
ipv6_addresses="$(read_iface_addresses "$cachedir" 6)"
ipv6_gateway="$(read_iface_default_gw "$cachedir" 6)"
fi
first_value_flag=
for addr in $ipv4_addresses $ipv6_addresses; do
if [ -n "$first_value_flag" ]; then
write_systemd_networkd_ini_option "$network_filename" "Network" "Address" "$addr"
first_value_flag=1
else
add_systemd_networkd_ini_option "$network_filename" "Network" "Address" "$addr"
fi
done
first_value_flag=
for gw in $ipv4_gateway $ipv6_gateway; do
if [ -n "$first_value_flag" ]; then
write_systemd_networkd_ini_option "$network_filename" "Network" "Gateway" "$gw"
first_value_flag=1
else
add_systemd_networkd_ini_option "$network_filename" "Network" "Gateway" "$gw"
fi
done
ini_config_del "$network_filename" "Network" "DNS"
for ns in $(read_iface_dns "$cachedir"); do
add_systemd_networkd_ini_option "$network_filename" "Network" "DNS" "$ns"
done
ini_config_is_set "$network_filename" "Network" "DNS" ||
write_systemd_networkd_ini_option "$network_filename" "Network" "UseDNS" "yes"
}
### system-networkd functions
init_systemd_networkd_cache()
{
local cachedir="$1"; shift
local name="$1"; shift
local network_filename="$(get_systemd_networkd_network_filename "$name")"
if [ -f "$network_filename" ]; then
cp -f "$network_filename" "$cachedir/"
fi
}
write_systemd_networkd()
{
local cachedir="$1"; shift
local name="$1"; shift
local bootproto="$1"; shift
local network_filename="$(get_systemd_networkd_network_filename "$name" "$cachedir")"
[ -f "$network_filename" ] || touch "$network_filename"
write_systemd_networkd_ini_option "$network_filename" "Match" "Name" "$name"
write_systemd_networkd_bootproto "$cachedir" "$network_filename" "$bootproto"
}
del_systemd_networkd()
{
local cachedir="$1"; shift
local name="$1"; shift
local network_filename="$(get_systemd_networkd_network_filename "$name" "$cachedir")"
rm -f "$network_filename"
}
commit_systemd_networkd()
{
local cachedir="$1"; shift
local name="$1"; shift
local new_network_filename="$(get_systemd_networkd_network_filename "$name" "$cachedir")"
local old_network_filename="$(get_systemd_networkd_network_filename "$name")"
if [ -f "$new_network_filename" ]; then
[ -d "$systemd_networkd_dir" ] ||
mkdir -p "$systemd_networkd_dir"
mv -f -- "$new_network_filename" "$old_network_filename"
else
rm -f "$old_network_filename"
fi
}