From 1c60bf02927302a33263487046286d96be83b4bc Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Thu, 23 Feb 2023 17:33:04 -0600 Subject: [PATCH] F OpenNebula/one-context-linux#287: Static routing --- .../one-context.d/loc-10-network.d/netcfg-bsd | 39 ++++++++++++++++++- .../loc-10-network.d/netcfg-interfaces | 15 +++++++ .../loc-10-network.d/netcfg-netplan | 21 ++++++++++ .../loc-10-network.d/netcfg-networkd | 23 +++++++++++ .../one-context.d/loc-10-network.d/netcfg-nm | 17 ++++++++ .../loc-10-network.d/netcfg-scripts | 19 +++++++++ 6 files changed, 133 insertions(+), 1 deletion(-) diff --git a/src/etc/one-context.d/loc-10-network.d/netcfg-bsd b/src/etc/one-context.d/loc-10-network.d/netcfg-bsd index 01deafb..32ed772 100644 --- a/src/etc/one-context.d/loc-10-network.d/netcfg-bsd +++ b/src/etc/one-context.d/loc-10-network.d/netcfg-bsd @@ -117,8 +117,45 @@ gen_iface_conf() ### + routing_conf="/etc/rc.conf.d/routing" + if [ -n "${gateway}" ]; then - echo "defaultrouter=\"${gateway}\"" >> /etc/rc.conf.d/routing + echo "defaultrouter=\"${gateway}\"" >> $routing_conf + fi + + # Add static routes + if [ -n "${ROUTES}" ]; then + + # TODO: Validate possible comma with spaces + IFS=',' read -r -a routes <<< "$ROUTES" + + route_names="" + routes_info=() + + for index in "${!routes[@]}" + do + route_name="lan${index}" + route_names="${route_names}${route_name} " + + IFS=' via ' read -r -a route_args <<< "${routes[index]}" + + subnet=${route_args[0]} + static_gw=${route_args[1]} + out_iface=${route_args[2]} + + route_info="${route_name}=\"-net${subnet} ${static_gw}\"" + [ -n "${out_iface}" ] && route_info=" ${route_info} -iface ${dev}" + + routes_info+=(route_info) + done + + echo -e "${route_names}\n" >> $routing_conf + + for route_info in "${routes_info[@]}" + do + echo -e "${route_info}\n" >> $routing_conf + done + fi } diff --git a/src/etc/one-context.d/loc-10-network.d/netcfg-interfaces b/src/etc/one-context.d/loc-10-network.d/netcfg-interfaces index 90667f5..98027e4 100644 --- a/src/etc/one-context.d/loc-10-network.d/netcfg-interfaces +++ b/src/etc/one-context.d/loc-10-network.d/netcfg-interfaces @@ -158,6 +158,21 @@ EOT fi fi + # Add static routes + if [ -n "${ROUTES}" ]; then + + # TODO: Validate possible comma with spaces + IFS=',' read -r -a routes <<< "$ROUTES" + + for route in "${routes[@]}" + do + if [[ "$route" == *"${dev}"* ]] || [[ "$dev" == "eth0" && "$route" != *"dev"* ]]; then + echo " up ip route add $route" + fi + done + + fi + if [ -n "$mtu" ]; then echo " mtu ${mtu}" fi diff --git a/src/etc/one-context.d/loc-10-network.d/netcfg-netplan b/src/etc/one-context.d/loc-10-network.d/netcfg-netplan index 15b924a..bfc3ad1 100644 --- a/src/etc/one-context.d/loc-10-network.d/netcfg-netplan +++ b/src/etc/one-context.d/loc-10-network.d/netcfg-netplan @@ -135,6 +135,8 @@ gen_routes() via: ${gateway} EOT + + # Force default Linux IPv4 metric (man 8 route) to override # automatic metrics calculation done by NetworkManager and unify # behavior among different renderers. @@ -145,6 +147,25 @@ EOT fi fi + # Add static routes + if [ -n "${ROUTES}" ]; then + + # TODO: Validate possible comma with spaces + IFS=',' read -r -a routes <<< "$ROUTES" + + for route in "${routes[@]}" + do + if [[ "$route" == *"${dev}"* ]] || [[ "$dev" == "eth0" && "$route" != *"dev"* ]]; then + IFS=' via ' read -r -a route_args <<< "$route" + cat <> "${config_path}/route-${dev}" + # The route with no target iface will be set to eth0 + elif [[ "$dev" == "eth0" && "$route" != *"dev"* ]]; then + echo "$route" >> "${config_path}/route-eth0" + fi + done + + fi + if [ -n "${mtu}" ]; then echo "MTU=${mtu}" fi