From 693844189aabab2a77d85e70bded7063c7fa211c Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Fri, 17 Mar 2023 10:24:30 -0600 Subject: [PATCH] F OpenNebula/one-context-linux#287: Route parsing refactor --- .../one-context.d/loc-10-network.d/functions | 27 ++++++++++++++++ .../one-context.d/loc-10-network.d/netcfg-bsd | 31 ++++++++++--------- .../loc-10-network.d/netcfg-interfaces | 2 +- .../loc-10-network.d/netcfg-netplan | 13 +++++--- .../loc-10-network.d/netcfg-networkd | 13 +++++--- .../one-context.d/loc-10-network.d/netcfg-nm | 9 ++++-- .../loc-10-network.d/netcfg-scripts | 1 - 7 files changed, 68 insertions(+), 28 deletions(-) diff --git a/src/etc/one-context.d/loc-10-network.d/functions b/src/etc/one-context.d/loc-10-network.d/functions index 02c5f40..014caec 100644 --- a/src/etc/one-context.d/loc-10-network.d/functions +++ b/src/etc/one-context.d/loc-10-network.d/functions @@ -571,3 +571,30 @@ gen_resolvconf() sed -i "/^NETCONFIG_DNS_STATIC_SEARCHLIST=/ s/=.*$/=\"${all_search_domains}\"/" /etc/sysconfig/network/config fi } + +# Space separate source, destination and output interface of a route +get_route_info() { + route=$1 + + # Remove whitespaces beginning and end + route="${route# }" + route=$(echo "$route" | sed 's/[[:space:]]*$//') + + # Parse traffic source, destination and outgoing interface + dst=$(echo "$route" | cut -d ' ' -f 1) + + if [[ "$route" == *"via"* ]]; then + gw=${route##*" via "} + gw=${gw%" dev"*} + else + gw='' + fi + + if [[ "$route" == *"dev"* ]]; then + iface=${route##*" dev "} + else + iface='' + fi + + echo "$dst $gw $iface" +} \ No newline at end of file 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 32ed772..b720f0a 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,43 +117,46 @@ gen_iface_conf() ### - routing_conf="/etc/rc.conf.d/routing" + routes_conf_path="/etc/rc.conf.d/routing" if [ -n "${gateway}" ]; then - echo "defaultrouter=\"${gateway}\"" >> $routing_conf + echo "defaultrouter=\"${gateway}\"" >> $routes_conf_path fi # Add static routes if [ -n "${ROUTES}" ]; then + # static_routes="lan mumoffice foo" + # route_lan="-net 192.168.1.0/24 192.168.1.254" + # route_mumoffice="-net 10.0.0.0/8 10.30.110.5" + # route_foo="-host 169.254.1.1 -iface lo0" - # TODO: Validate possible comma with spaces IFS=',' read -r -a routes <<< "$ROUTES" route_names="" - routes_info=() + routes_conf=() for index in "${!routes[@]}" do route_name="lan${index}" route_names="${route_names}${route_name} " - IFS=' via ' read -r -a route_args <<< "${routes[index]}" + IFS=' ' read -r -a route_info <<< "$(get_route_info "${routes[index]}")" - subnet=${route_args[0]} - static_gw=${route_args[1]} - out_iface=${route_args[2]} + dst=${route_info[0]} + gw=${route_info[1]} + iface=${route_info[2]} - route_info="${route_name}=\"-net${subnet} ${static_gw}\"" - [ -n "${out_iface}" ] && route_info=" ${route_info} -iface ${dev}" + route_conf="${route_name}=\"-net${dst} ${gw}\"" + [ -n "${iface}" ] && route_conf=" ${route_conf} -iface ${dev}" - routes_info+=(route_info) + routes_conf+=(route_conf) done - echo -e "${route_names}\n" >> $routing_conf + echo -e "static_routes=\"${route_names}\"\n" >> $routes_conf_path - for route_info in "${routes_info[@]}" + for route_conf in "${routes_conf[@]}" do - echo -e "${route_info}\n" >> $routing_conf + echo -e "$route_conf\n" >> $routes_conf_path 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 98027e4..833f858 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 @@ -161,11 +161,11 @@ EOT # Add static routes if [ -n "${ROUTES}" ]; then - # TODO: Validate possible comma with spaces IFS=',' read -r -a routes <<< "$ROUTES" for route in "${routes[@]}" do + # Apply every route except the ones specified for other devices if [[ "$route" == *"${dev}"* ]] || [[ "$dev" == "eth0" && "$route" != *"dev"* ]]; then echo " up ip route add $route" 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 bfc3ad1..398c310 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 @@ -150,16 +150,21 @@ EOT # Add static routes if [ -n "${ROUTES}" ]; then - # TODO: Validate possible comma with spaces IFS=',' read -r -a routes <<< "$ROUTES" for route in "${routes[@]}" do + # Apply every route except the ones specified for other devices if [[ "$route" == *"${dev}"* ]] || [[ "$dev" == "eth0" && "$route" != *"dev"* ]]; then - IFS=' via ' read -r -a route_args <<< "$route" + + IFS=' ' read -r -a route_info <<< "$(get_route_info "$route")" + + dst=${route_info[0]} + gw=${route_info[1]} + cat <