diff --git a/.gitignore b/.gitignore index b91c2f1..e6bc81a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ out/ *.rpm *.deb .vagrant*/ +*~bak diff --git a/src/etc/one-context.d/loc-10-network b/src/etc/one-context.d/loc-10-network new file mode 100755 index 0000000..d8fe937 --- /dev/null +++ b/src/etc/one-context.d/loc-10-network @@ -0,0 +1,96 @@ +#!/usr/bin/env bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2021, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +. /etc/one-context.d/loc-10-network.d/functions + +ACTION="$1" +OS_ID=$(detect_os) + +if [ -z "${ACTION}" ] ; then + ACTION="configure" +fi + +if [ -z "${CONTEXT_NETCFG_TYPE}" ] ; then + case "${OS_ID}" in + alpine) + CONTEXT_NETCFG_TYPE='interfaces' + ;; + altlinux) + CONTEXT_NETCFG_TYPE='networkd' + ;; + debian|ubuntu|devuan) + CONTEXT_NETCFG_TYPE='interfaces' + ;; + fedora|centos|rhel|almalinux|ol|rocky) + CONTEXT_NETCFG_TYPE='scripts' + ;; + opensuse*) + CONTEXT_NETCFG_TYPE='scripts' + ;; + freebsd) + CONTEXT_NETCFG_TYPE='bsd' + ;; + *) + CONTEXT_NETCFG_TYPE='none' + ;; + esac +fi + +if [ "${CONTEXT_NETCFG_TYPE}" != 'none' ] ; then + _found_valid_netcfg='no' + for _cfgtype in ${CONTEXT_NETCFG_TYPE} ; do + if [ -e "/etc/one-context.d/loc-10-network.d/netcfg-${_cfgtype}" ] ; then + . "/etc/one-context.d/loc-10-network.d/netcfg-${_cfgtype}" + else + echo "ERROR [!]: Requested network type is not implemented: ${_cfgtype}" >&2 + exit 1 + fi + + if is_network_supported ; then + _found_valid_netcfg='yes' + break + fi + done + + if [ "${_found_valid_netcfg}" = 'no' ] ; then + echo "ERROR [!]: None of the requested network types is supported on: ${OS_ID}" >&2 + exit 1 + fi +else + # any action is meaningless without functioning network type + ACTION='none' +fi + +case "$ACTION" in + none) + echo "INFO: Network will not be configured" >&2 + ;; + configure) + configure_network + ;; + reconfigure) + configure_network + reload_network + ;; + *) + echo "ERROR [!]: Unknown ACTION: ${ACTION}" >&2 + exit 1 + ;; +esac + +exit 0 diff --git a/src/etc/one-context.d/loc-10-network##apk.one b/src/etc/one-context.d/loc-10-network##apk.one deleted file mode 100755 index dbddc1b..0000000 --- a/src/etc/one-context.d/loc-10-network##apk.one +++ /dev/null @@ -1,357 +0,0 @@ -#!/usr/bin/env bash - -# -------------------------------------------------------------------------- # -# Copyright 2002-2021, OpenNebula Project, OpenNebula Systems # -# # -# Licensed under the Apache License, Version 2.0 (the "License"); you may # -# not use this file except in compliance with the License. You may obtain # -# a copy of the License at # -# # -# http://www.apache.org/licenses/LICENSE-2.0 # -# # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # -#--------------------------------------------------------------------------- # - -ACTION=$1 - -if [ -z "$ACTION" ]; then - ACTION="none" -fi - -# Gets IP address from a given MAC -mac2ip() { - mac=$1 - - let ip_a=0x`echo $mac | cut -d: -f 3` - let ip_b=0x`echo $mac | cut -d: -f 4` - let ip_c=0x`echo $mac | cut -d: -f 5` - let ip_d=0x`echo $mac | cut -d: -f 6` - - ip="$ip_a.$ip_b.$ip_c.$ip_d" - - echo $ip -} - -# Gets the network part of an IP -get_network() { - network=$(get_iface_var "NETWORK") - - if [ -z "$network" ]; then - IFS=. read -r i1 i2 i3 i4 <<< "$IP" - IFS=. read -r m1 m2 m3 m4 <<< "$(get_mask)" - network=$(printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))") - fi - - echo $network -} - -# Gets the network mask -get_mask() { - mask=$(get_iface_var "MASK") - - if [ -z "$mask" ]; then - mask="255.255.255.0" - fi - - echo $mask -} - -# Gets device MTU -get_mtu() { - mtu=$(get_iface_var "MTU") - - echo $mtu -} - -is_gateway() { - if [ -z "$GATEWAY_IFACE_NUM" ]; then - true - else - [ "$IFACE_NUM" = "$GATEWAY_IFACE_NUM" ] - fi -} - -# Gets the network gateway -get_gateway() { - if is_gateway; then - get_iface_var "GATEWAY" - fi -} - -# Gets the network gateway6 -get_gateway6() { - if is_gateway; then - get_iface_var "GATEWAY6" - fi -} - -get_ip() { - ip=$(get_iface_var "IP") - - echo $ip -} - -get_iface_var() { - var_name="${UPCASE_DEV}_$1" - var=$(eval "echo \"\${$var_name}\"") - - echo $var -} - -gen_iface_conf() { - cat < /proc/sys/net/ipv6/conf/${DEV}/autoconf - pre-up echo 0 > /proc/sys/net/ipv6/conf/${DEV}/accept_ra -EOT - - if [ -n "$IP6_ULA" ]; then - cat < /proc/sys/net/ipv6/conf/${DEV}/autoconf - pre-up echo 0 > /proc/sys/net/ipv6/conf/${DEV}/accept_ra -EOT - - if [ -n "$MTU" ]; then - echo " mtu $MTU" - fi - - if [ -n "$GATEWAY6" ]; then - echo " gateway $GATEWAY6" - fi - - if [ -n "$IP6_ULA" ]; then - cat < /etc/network/interfaces - #echo "source /etc/network/interfaces.d/*.cfg" >> /etc/network/interfaces -} - -deactivate_network() -{ - . /etc/os-release - - case "$ID" in - 'ubuntu') - IFACES=$(/sbin/ifquery --list -a) - - for i in $IFACES; do - if [ $i != 'lo' ]; then - /sbin/ifdown $i - /sbin/ip addr flush dev $i - fi - done - ;; - 'alpine') - service networking stop || true - - #IFACES=$(ip a | \ - # sed -n 's#^[0-9]\+:[[:space:]]\+\([^:]\+\):[[:space:]].*#\1#p') - - # took from find_ifaces in the networking service - IFACES=$(\ - awk '$1 == "auto" { - for (i = 2; i <= NF; i = i + 1) printf("%s ", $i) - }' /etc/network/interfaces) - - for i in $IFACES; do - if [ $i != 'lo' ]; then - /sbin/ip link set dev $i down || true - /sbin/ip addr flush dev $i || true - fi - done - ;; - *) - service networking stop - ;; - esac -} - -activate_network() -{ - . /etc/os-release - - case "$ID" in - 'ubuntu') - IFACES=$(/sbin/ifquery --list -a) - - for i in $IFACES; do - /sbin/ifup $i - done - ;; - 'alpine') - deactivate_network - service networking start - ;; - *) - service networking stop - sleep 1 - service networking start - ;; - esac -} - -[ $ACTION == "reconfigure" ] && deactivate_network -configure_network -[ $ACTION == "reconfigure" ] && activate_network - diff --git a/src/etc/one-context.d/loc-10-network##deb.one b/src/etc/one-context.d/loc-10-network##deb.one deleted file mode 100755 index 9e67be1..0000000 --- a/src/etc/one-context.d/loc-10-network##deb.one +++ /dev/null @@ -1,326 +0,0 @@ -#!/usr/bin/env bash - -# -------------------------------------------------------------------------- # -# Copyright 2002-2021, OpenNebula Project, OpenNebula Systems # -# # -# Licensed under the Apache License, Version 2.0 (the "License"); you may # -# not use this file except in compliance with the License. You may obtain # -# a copy of the License at # -# # -# http://www.apache.org/licenses/LICENSE-2.0 # -# # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # -#--------------------------------------------------------------------------- # - -COMMAND=${1} - -# Gets IP address from a given MAC -mac2ip() { - mac=$1 - - let ip_a=0x`echo $mac | cut -d: -f 3` - let ip_b=0x`echo $mac | cut -d: -f 4` - let ip_c=0x`echo $mac | cut -d: -f 5` - let ip_d=0x`echo $mac | cut -d: -f 6` - - ip="$ip_a.$ip_b.$ip_c.$ip_d" - - echo $ip -} - -# Gets the network part of an IP -get_network() { - network=$(get_iface_var "NETWORK") - - if [ -z "$network" ]; then - IFS=. read -r i1 i2 i3 i4 <<< "$IP" - IFS=. read -r m1 m2 m3 m4 <<< "$(get_mask)" - network=$(printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))") - fi - - echo $network -} - -# Gets the network mask -get_mask() { - mask=$(get_iface_var "MASK") - - if [ -z "$mask" ]; then - mask="255.255.255.0" - fi - - echo $mask -} - -# Gets device MTU -get_mtu() { - mtu=$(get_iface_var "MTU") - - echo $mtu -} - -is_gateway() { - if [ -z "$GATEWAY_IFACE_NUM" ]; then - true - else - [ "$IFACE_NUM" = "$GATEWAY_IFACE_NUM" ] - fi -} - -# Gets the network gateway -get_gateway() { - if is_gateway; then - gateway=$(get_iface_var "GATEWAY") - - echo $gateway - fi -} - -# Gets the network gateway6 -get_gateway6() { - if is_gateway; then - get_iface_var "GATEWAY6" - fi -} - -get_ip() { - ip=$(get_iface_var "IP") - - echo $ip -} - -get_iface_var() { - var_name="${UPCASE_DEV}_$1" - var=$(eval "echo \"\${$var_name}\"") - - echo $var -} - -gen_iface_conf() { - cat < /etc/network/interfaces - echo "source /etc/network/interfaces.d/*.cfg" >> /etc/network/interfaces -} - -deactivate_network() -{ - - if test -f "/usr/sbin/ifreload"; then - return - fi - - IFACES=`/sbin/ifquery --list -a` - - for i in $IFACES; do - if [ $i != 'lo' ]; then - /sbin/ifdown $i - /sbin/ip addr flush dev $i - fi - done -} - -activate_network() -{ - - if test -f "/usr/sbin/ifreload"; then - /usr/sbin/ifreload -a - return - fi - - IFACES=`/sbin/ifquery --list -a` - - for i in $IFACES; do - /sbin/ifup $i - done -} - -[ -z "$(env | cut -d= -f1 | grep -E '^ETH[0-9]+_IPV*6*')" ] && exit 0 - -deactivate_network -configure_network -activate_network diff --git a/src/etc/one-context.d/loc-10-network##networkd.one b/src/etc/one-context.d/loc-10-network##networkd.one deleted file mode 100755 index 51f6ea7..0000000 --- a/src/etc/one-context.d/loc-10-network##networkd.one +++ /dev/null @@ -1,353 +0,0 @@ -#!/usr/bin/env bash - -# -------------------------------------------------------------------------- # -# Copyright 2002-2021, OpenNebula Project, OpenNebula Systems # -# # -# Licensed under the Apache License, Version 2.0 (the "License"); you may # -# not use this file except in compliance with the License. You may obtain # -# a copy of the License at # -# # -# http://www.apache.org/licenses/LICENSE-2.0 # -# # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # -#--------------------------------------------------------------------------- # - -COMMAND=${1} - -# Gets IP address from a given MAC -mac2ip() { - mac=$1 - - let ip_a=0x`echo $mac | cut -d: -f 3` - let ip_b=0x`echo $mac | cut -d: -f 4` - let ip_c=0x`echo $mac | cut -d: -f 5` - let ip_d=0x`echo $mac | cut -d: -f 6` - - ip="$ip_a.$ip_b.$ip_c.$ip_d" - - echo $ip -} - -mask2cidr() { - mask=$1 - nbits=0 - IFS=. - for dec in $mask ; do - case $dec in - 255) let nbits+=8;; - 254) let nbits+=7 ; break ;; - 252) let nbits+=6 ; break ;; - 248) let nbits+=5 ; break ;; - 240) let nbits+=4 ; break ;; - 224) let nbits+=3 ; break ;; - 192) let nbits+=2 ; break ;; - 128) let nbits+=1 ; break ;; - 0);; - *) echo "Error: $dec is not recognised"; exit 1 - esac - done - echo "$nbits" -} - -# Gets the network part of an IP -get_network() { - network=$(get_iface_var "NETWORK") - - if [ -z "$network" ]; then - IFS=. read -r i1 i2 i3 i4 <<< "$IP" - IFS=. read -r m1 m2 m3 m4 <<< "$(get_mask)" - network=$(printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))") - fi - - echo $network -} - -# Gets the network mask -get_mask() { - mask=$(get_iface_var "MASK") - - if [ -z "$mask" ]; then - mask="255.255.255.0" - fi - - echo $mask -} - -# Gets device MTU -get_mtu() { - mtu=$(get_iface_var "MTU") - - echo $mtu -} - -is_gateway() { - if [ -z "$GATEWAY_IFACE_NUM" ]; then - true - else - [ "$IFACE_NUM" = "$GATEWAY_IFACE_NUM" ] - fi -} - -# Gets the network gateway -get_gateway() { - if is_gateway; then - gateway=$(get_iface_var "GATEWAY") - - if [ -z "$gateway" ]; then - if [ "$DEV" = "eth0" ]; then - net_prefix=$(echo $NETWORK | cut -d'.' -f1,2,3) - gateway="${net_prefix}.1" - fi - fi - - echo $gateway - fi -} - -# Gets the network gateway6 -get_gateway6() { - if is_gateway; then - get_iface_var "GATEWAY6" - fi -} - -get_dns() { - dns=$(get_iface_var "DNS") - - echo $dns -} - -get_search_domain() { - search_domain=$(get_iface_var "SEARCH_DOMAIN") - - echo $search_domain -} - -get_ip() { - ip=$(get_iface_var "IP") - - echo $ip -} - -get_iface_var() { - var_name="${UPCASE_DEV}_$1" - var=$(eval "echo \"\${$var_name}\"") - - echo $var -} - - -gen_iface_conf() { - - cat < /etc/systemd/network/"${DEV}".network - - done -} - -configure_network() -{ - gen_network_configuration - - if [ "${COMMAND}" = 'reconfigure' ]; then - systemctl restart systemd-networkd.service - fi - - sleep 2 -} - -[ -z "$(env | cut -d= -f1 | grep -E '^ETH[0-9]+_IPV*6*')" ] && exit 0 - -configure_network - diff --git a/src/etc/one-context.d/loc-10-network.d/functions b/src/etc/one-context.d/loc-10-network.d/functions new file mode 100644 index 0000000..53133e2 --- /dev/null +++ b/src/etc/one-context.d/loc-10-network.d/functions @@ -0,0 +1,206 @@ +#!/usr/bin/env bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2021, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +# +# network module interface +# + +is_network_supported() +{ + false +} + +configure_network() +{ + echo "ERROR [!]: No 'configure_network' implementation for the network type: ${CONTEXT_NETCFG_TYPE}" >&2 + exit 1 +} + +stop_network() +{ + echo "ERROR [!]: No 'stop_network' implementation for the network type: ${CONTEXT_NETCFG_TYPE}" >&2 + exit 1 +} + +start_network() +{ + echo "ERROR [!]: No 'start_network' implementation for the network type: ${CONTEXT_NETCFG_TYPE}" >&2 + exit 1 +} + +reload_network() +{ + echo "ERROR [!]: No 'reload_network' implementation for the network type: ${CONTEXT_NETCFG_TYPE}" >&2 + exit 1 +} + +# +# generic shared functions +# + +# return OS ID +detect_os() +( + if [ -f /etc/os-release ] ; then + ID= + # shellcheck disable=SC1091 + . /etc/os-release + echo "$ID" | tr '[:upper:]' '[:lower:]' + else + uname | tr '[:upper:]' '[:lower:]' + fi +) + +# args: +get_iface_var() +( + iface=$(echo "$1" | tr '[:lower:]' '[:upper:]') + var_name="${iface}_${2}" + eval "echo \"\${${var_name}}\"" +) + +# Gets IP address from a given MAC +mac2ip() +( + mac="$1" + + ip_a=$(echo "$mac" | cut -d: -f 3) + ip_b=$(echo "$mac" | cut -d: -f 4) + ip_c=$(echo "$mac" | cut -d: -f 5) + ip_d=$(echo "$mac" | cut -d: -f 6) + + echo "0x${ip_a}.0x${ip_b}.0x${ip_c}.0x${ip_d}" +) + +mask2cidr() +( + mask="$1" + nbits=0 + IFS=. + for dec in $mask ; do + case "$dec" in + 255) nbits=$((nbits + 8)) ;; + 254) nbits=$((nbits + 7)) ; break ;; + 252) nbits=$((nbits + 6)) ; break ;; + 248) nbits=$((nbits + 5)) ; break ;; + 240) nbits=$((nbits + 4)) ; break ;; + 224) nbits=$((nbits + 3)) ; break ;; + 192) nbits=$((nbits + 2)) ; break ;; + 128) nbits=$((nbits + 1)) ; break ;; + 0) break ;; + *) echo "Error: $dec is not recognised"; exit 1 ;; + esac + done + echo "$nbits" +) + +# Gets the network part of an IP +# arg: +get_network() +( + network=$(get_iface_var "$1" "NETWORK") + + if [ -z "$network" ]; then + ip=$(get_ip "$1") + mask=$(get_mask "$1") + network=$(awk -v ip="$ip" -v mask="$mask" 'END { + split(ip, ip_b, "."); split(mask, mask_b, "."); + for (i=1; i<=4; ++i) x = x "." and(ip_b[i], mask_b[i]); + sub(/^./, "", x); print x; }' +get_mask() +( + mask=$(get_iface_var "$1" "MASK") + echo "${mask:-255.255.255.0}" +) + +# Gets device MTU +# arg: +get_mtu() +( + mtu=$(get_iface_var "$1" "MTU") + echo "${mtu:-1500}" +) + +# Gets the network gateway +# arg: +get_gateway() +( + get_iface_var "$1" "GATEWAY" +) + +# Gets the network gateway6 +# arg: +get_gateway6() +( + get_iface_var "$1" "GATEWAY6" +) + +# arg: +get_ip() +( + get_iface_var "$1" "IP" +) + +# arg: +get_dns() +( + get_iface_var "$1" "DNS" +) + +# arg: +get_search_domain() +( + get_iface_var "$1" "SEARCH_DOMAIN" +) + +# arg: +get_interface_alias() +( + env | sed -n "s#^\(${1}_ALIAS[0-9]\+\)_MAC=.*#\1#p" | sort +) + +get_context_interfaces() +( + env | grep -E "^ETH[0-9]+_MAC=" | sed 's/_.*$//' | sort +) + +get_pci_interfaces() +( + env | grep -E "^PCI[0-9]+_MAC=" | sed 's/_.*$//' | sort +) + +get_interface_mac() +( + ip link show | awk '/^[0-9]+: [A-Za-z0-9@]+:/ { device=$2; gsub(/:/, "",device); split(device,dev,"@")} /link\/ether/ { print dev[1] " " $2 }' +) + +get_dev() +( + list="$1" + mac="$2" + + echo "$list" | grep "$mac" | cut -d' ' -f1 | tail -n1 +) + diff --git a/src/etc/one-context.d/loc-10-network##bsd.one b/src/etc/one-context.d/loc-10-network.d/netcfg-bsd old mode 100755 new mode 100644 similarity index 51% rename from src/etc/one-context.d/loc-10-network##bsd.one rename to src/etc/one-context.d/loc-10-network.d/netcfg-bsd index d28bc7c..805aef6 --- a/src/etc/one-context.d/loc-10-network##bsd.one +++ b/src/etc/one-context.d/loc-10-network.d/netcfg-bsd @@ -16,89 +16,57 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -COMMAND=${1} +# +# network module implementation +# -# Gets IP address from a given MAC -mac2ip() { - mac=$1 - - let ip_a=0x`echo $mac | cut -d: -f 3` - let ip_b=0x`echo $mac | cut -d: -f 4` - let ip_c=0x`echo $mac | cut -d: -f 5` - let ip_d=0x`echo $mac | cut -d: -f 6` - - ip="$ip_a.$ip_b.$ip_c.$ip_d" - - echo $ip -} - -# Gets the network part of an IP -get_network() { - network=$(get_iface_var "NETWORK") - - if [ -z "$network" ]; then - IFS=. read -r i1 i2 i3 i4 <<< "$IP" - IFS=. read -r m1 m2 m3 m4 <<< "$(get_mask)" - network=$(printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))") - fi - - echo $network -} - -# Gets the network mask -get_mask() { - mask=$(get_iface_var "MASK") - - if [ -z "$mask" ]; then - mask="255.255.255.0" - fi +is_network_supported() +{ + case "${OS_ID}" in + freebsd) + return 0 + ;; + esac - echo $mask + return 1 } -# Gets device MTU -get_mtu() { - mtu=$(get_iface_var "MTU") - - echo $mtu +configure_network() +{ + gen_network_configuration >/etc/rc.conf.d/network } -is_gateway() { - if [ -z "$GATEWAY_IFACE_NUM" ]; then - true - else - [ "$IFACE_NUM" = "$GATEWAY_IFACE_NUM" ] - fi +stop_network() +{ + service netif stop >/dev/null + service routing stop >/dev/null } -# Gets the network gateway -get_gateway() { - if is_gateway; then - gateway=$(get_iface_var "GATEWAY") - - echo $gateway - fi +start_network() +{ + service netif start >/dev/null + service routing start >/dev/null } -# Gets the network gateway6 -get_gateway6() { - if is_gateway; then - get_iface_var "GATEWAY6" - fi +reload_network() +{ + service netif restart >/dev/null + service routing restart >/dev/null } -get_ip() { - ip=$(get_iface_var "IP") +# +# helper functions +# - echo $ip -} - -get_iface_var() { - var_name="${UPCASE_DEV}_$1" - var=$(eval "echo \"\${$var_name}\"") +get_interface_mac() +( + macs=$(ifconfig | grep ether | awk '{print $2}') - echo $var -} + for mac in ${macs} ; do + iface=$(ifconfig | grep -B 2 "$mac" | head -n 1 | awk '{print $1}' | cut -d ':' -f 1) + echo "${iface} ${mac}" + done +) gen_iface_conf() { echo -n "ifconfig_${DEV}=\"inet ${IP} netmask ${MASK}" @@ -163,36 +131,6 @@ gen_iface6_conf() { fi } -get_interface_mac() -{ - macs_array=($(ifconfig | grep ether | awk '{print $2}')) - #iface_name=() - - for mac in "${macs_array[@]}" - do - echo "$(ifconfig | grep -B 2 $mac | head -n 1 | awk '{print $1}' | cut -d ':' -f 1) $mac" - #iface_mac+=$(ifconfig | grep -B 2 $mac | head -n 1 | awk '{print $1}' | cut -d ':' -f 1) - done -} - -get_context_interfaces() -{ - env | grep -E "^ETH[0-9]+_MAC=" | sed 's/_.*$//' | sort -} - -get_interface_alias() -{ - env | grep -E "^ETH${INDEX}_ALIAS[0-9]+_MAC=" | cut -d '_' -f 2 | sort -} - -get_dev() -{ - list="$1" - mac="$2" - - echo "$list" | grep "$mac" | cut -d' ' -f1 | tail -n1 -} - gen_network_configuration() { # clean routing information @@ -200,50 +138,46 @@ gen_network_configuration() INTERFACE_MAC=$(get_interface_mac) CONTEXT_INTERFACES=$(get_context_interfaces) - GATEWAY_IFACE_NUM=$(echo "$GATEWAY_IFACE" | sed 's/^ETH//') - for interface in $CONTEXT_INTERFACES; do - UPCASE_DEV=$interface - MAC=$(get_iface_var "MAC") + for iface in $CONTEXT_INTERFACES; do + MAC=$(get_iface_var "$iface" "MAC") DEV=$(get_dev "$INTERFACE_MAC" "$MAC") - IFACE_NUM=$(echo "$UPCASE_DEV" | sed 's/^ETH//') - IP=$(get_ip) - NETWORK=$(get_network) - MASK=$(get_mask) - MTU=$(get_mtu) - GATEWAY=$(get_gateway) + IP=$(get_ip "$iface") + MASK=$(get_mask "$iface") + MTU=$(get_mtu "$iface") + GATEWAY=$(get_gateway "$iface") + # TODO: not implemented + #METRIC=$(get_iface_var "$iface" "METRIC") - IP6=$(get_iface_var "IP6") - [[ -z $IP6 ]] && IP6=$(get_iface_var "IPV6") - IP6_PREFIX_LENGTH=$(get_iface_var "IP6_PREFIX_LENGTH") - IP6_ULA=$(get_iface_var "IP6_ULA") - GATEWAY6=$(get_gateway6) + IP6=$(get_iface_var "$iface" "IP6") + [ -z "$IP6" ] && IP6=$(get_iface_var "$iface" "IPV6") + IP6_PREFIX_LENGTH=$(get_iface_var "$iface" "IP6_PREFIX_LENGTH") + IP6_ULA=$(get_iface_var "$iface" "IP6_ULA") + GATEWAY6=$(get_gateway6 "$iface") [ -z "${IP}${IP6}" ] && continue [ -z "${DEV}" ] && continue - - INDEX=${interface: -1} - ALIAS=$(get_interface_alias) - ALIAS_NUM=0 - HAS_IP6=${IP6} + HAS_IP6="${IP6}" [ -n "${IP}" ] && gen_iface_conf [ -n "${IP6}" ] && gen_iface6_conf - for nic_alias in $ALIAS; do - UPCASE_DEV="ETH${INDEX}_${nic_alias}" - IP=$(get_ip) - MASK=$(get_mask) + ALIASES=$(get_interface_alias) + ALIAS_NUM=0 + + for nic_alias in $ALIASES; do + IP=$(get_ip "$nic_alias") + MASK=$(get_mask "$nic_alias") - IP6=$(get_iface_var "IP6") - [[ -z $IP6 ]] && IP6=$(get_iface_var "IPV6") - IP6_PREFIX_LENGTH=$(get_iface_var "IP6_PREFIX_LENGTH") - IP6_ULA=$(get_iface_var "IP6_ULA") + IP6=$(get_iface_var "$nic_alias" "IP6") + [ -z "$IP6" ] && IP6=$(get_iface_var "$nic_alias" "IPV6") + IP6_PREFIX_LENGTH=$(get_iface_var "$nic_alias" "IP6_PREFIX_LENGTH") + IP6_ULA=$(get_iface_var "$nic_alias" "IP6_ULA") - EXTERNAL=$(get_iface_var "EXTERNAL") + EXTERNAL=$(get_iface_var "$nic_alias" "EXTERNAL") EXTERNAL=${EXTERNAL^^} - DETACH=$(get_iface_var "DETACH") + DETACH=$(get_iface_var "$nic_alias" "DETACH") if [ -z "${DETACH}" ]; then if [ -z "${EXTERNAL}" ] || [ "$EXTERNAL" = "NO" ]; then @@ -251,7 +185,7 @@ gen_network_configuration() if [ -n "${IP6}" ]; then gen_alias6_conf - HAS_IP6=${IP6} + HAS_IP6="${IP6}" fi fi fi @@ -259,16 +193,3 @@ gen_network_configuration() done } -configure_network() -{ - gen_network_configuration >/etc/rc.conf.d/network - - if [ "${COMMAND}" = 'reconfigure' ]; then - service netif restart >/dev/null - service routing restart >/dev/null - fi -} - -[ -z "$(env | cut -d= -f1 | grep -E '^ETH[0-9]+_IPV*6*')" ] && exit 0 - -configure_network 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 new file mode 100644 index 0000000..bbecbb3 --- /dev/null +++ b/src/etc/one-context.d/loc-10-network.d/netcfg-interfaces @@ -0,0 +1,306 @@ +#!/usr/bin/env bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2021, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +# +# network module implementation +# + +is_network_supported() +{ + case "${OS_ID}" in + alpine) + return 0 + ;; + debian|ubuntu|devuan) + return 0 + ;; + esac + + return 1 +} + +configure_network() +{ + gen_network_configuration > /etc/network/interfaces + + case "${OS_ID}" in + debian|ubuntu|devuan) + echo "source /etc/network/interfaces.d/*.cfg" >> /etc/network/interfaces + ;; + esac +} + +stop_network() { + case "${OS_ID}" in + alpine) + service networking stop || true + + # took from find_ifaces in the networking service + IFACES=$(\ + awk '$1 == "auto" { + for (i = 2; i <= NF; i = i + 1) printf("%s ", $i) + }' /etc/network/interfaces) + + for i in $IFACES; do + if [ "${i}" != 'lo' ]; then + /sbin/ip link set dev "${i}" down || true + /sbin/ip addr flush dev "${i}" || true + fi + done + ;; + debian|ubuntu|devuan) + if [ -f "/usr/sbin/ifreload" ] ; then + return 0 + fi + + IFACES=$(/sbin/ifquery --list -a) + + for i in $IFACES; do + if [ "${i}" != 'lo' ] ; then + /sbin/ifdown "${i}" + /sbin/ip addr flush dev "${i}" + fi + done + ;; + *) + exit 1 + ;; + esac +} + +start_network() { + case "${OS_ID}" in + alpine) + service networking start + ;; + debian|ubuntu|devuan) + if [ -f "/usr/sbin/ifreload" ] ; then + /usr/sbin/ifreload -a + return 0 + fi + + IFACES=$(/sbin/ifquery --list -a) + + for i in $IFACES; do + /sbin/ifup "${i}" + done + ;; + *) + exit 1 + ;; + esac +} + +reload_network() { + stop_network + start_network +} + +# +# helper functions +# + +gen_iface_conf() +{ + cat < /proc/sys/net/ipv6/conf/${DEV}/autoconf + pre-up echo 0 > /proc/sys/net/ipv6/conf/${DEV}/accept_ra +EOT + ;; + debian|ubuntu|devuan) + cat < /proc/sys/net/ipv6/conf/${DEV}/autoconf + pre-up echo 0 > /proc/sys/net/ipv6/conf/${DEV}/accept_ra +EOT + ;; + debian|ubuntu|devuan) + cat < "/etc/systemd/network/${DEV}.network" + + done +} + diff --git a/src/etc/one-context.d/loc-10-network##network-scripts.one b/src/etc/one-context.d/loc-10-network.d/netcfg-scripts old mode 100755 new mode 100644 similarity index 54% rename from src/etc/one-context.d/loc-10-network##network-scripts.one rename to src/etc/one-context.d/loc-10-network.d/netcfg-scripts index 78f937a..8e7f5e3 --- a/src/etc/one-context.d/loc-10-network##network-scripts.one +++ b/src/etc/one-context.d/loc-10-network.d/netcfg-scripts @@ -16,94 +16,49 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -COMMAND=${1} +# +# network module implementation +# -# Gets IP address from a given MAC -mac2ip() { - mac=$1 - - let ip_a=0x`echo $mac | cut -d: -f 3` - let ip_b=0x`echo $mac | cut -d: -f 4` - let ip_c=0x`echo $mac | cut -d: -f 5` - let ip_d=0x`echo $mac | cut -d: -f 6` - - ip="$ip_a.$ip_b.$ip_c.$ip_d" - - echo $ip -} - -# Gets the network part of an IP -get_network() { - network=$(get_iface_var "NETWORK") - - if [ -z "$network" ]; then - IFS=. read -r i1 i2 i3 i4 <<< "$IP" - IFS=. read -r m1 m2 m3 m4 <<< "$(get_mask)" - network=$(printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))") - fi - - echo $network -} - -# Gets the network mask -get_mask() { - mask=$(get_iface_var "MASK") - - if [ -z "$mask" ]; then - mask="255.255.255.0" - fi - - echo $mask -} - -# Gets device MTU -get_mtu() { - mtu=$(get_iface_var "MTU") +is_network_supported() +{ + case "${OS_ID}" in + fedora|centos|rhel|almalinux|ol|rocky) + return 0 + ;; + opensuse*) + return 0 + ;; + esac - echo $mtu + return 1 } -is_gateway() { - if [ -z "$GATEWAY_IFACE_NUM" ]; then - true - else - [ "$IFACE_NUM" = "$GATEWAY_IFACE_NUM" ] - fi +configure_network() +{ + gen_network_configuration } -# Gets the network gateway -get_gateway() { - if is_gateway; then - gateway=$(get_iface_var "GATEWAY") - - echo $gateway - fi +stop_network() { + service network stop } -# Gets the network gateway6 -get_gateway6() { - if is_gateway; then - get_iface_var "GATEWAY6" - fi +start_network() { + service network start } -get_ip() { - ip=$(get_iface_var "IP") - - echo $ip +reload_network() { + service network restart } -get_iface_var() { - var_name="${UPCASE_DEV}_$1" - var=$(eval "echo \"\${$var_name}\"") - - echo $var -} +# +# helper functions +# gen_iface_conf() { cat <> /etc/sysconfig/network/ifsysctl-$DEV + cat <> "/etc/sysconfig/network/ifsysctl-${DEV}" net.ipv6.conf.\$SYSCTL_IF.autoconf = 0 net.ipv6.conf.\$SYSCTL_IF.accept_ra = 0 EOT @@ -170,7 +125,7 @@ EOT if [ -n "$GATEWAY6" ]; then if [ "$CONFIG_PATH" = "/etc/sysconfig/network" ]; then - echo "default $GATEWAY6 - $DEV" >> /etc/sysconfig/network/ifroute-$DEV + echo "default $GATEWAY6 - $DEV" >> "/etc/sysconfig/network/ifroute-${DEV}" else echo "IPV6_DEFAULTGW=$GATEWAY6" fi @@ -181,29 +136,6 @@ EOT fi } -get_interface_mac() -{ - ip link show | awk '/^[0-9]+: [A-Za-z0-9@]+:/ { device=$2; gsub(/:/, "",device); split(device,dev,"@")} /link\/ether/ { print dev[1] " " $2 }' -} - -get_context_interfaces() -{ - env | grep -E "^ETH[0-9]+_MAC=" | sed 's/_.*$//' | sort -} - -get_interface_alias() -{ - env | grep -E "^ETH${INDEX}_ALIAS[0-9]+_MAC=" | cut -d '_' -f 2 | sort -} - -get_dev() -{ - list="$1" - mac="$2" - - echo "$list" | grep "$mac" | cut -d' ' -f1 | tail -n1 -} - gen_network_configuration() { if [ -d /etc/sysconfig/network-scripts ]; then @@ -214,26 +146,22 @@ gen_network_configuration() INTERFACE_MAC=$(get_interface_mac) CONTEXT_INTERFACES=$(get_context_interfaces) - GATEWAY_IFACE_NUM=$(echo "$GATEWAY_IFACE" | sed 's/^ETH//') - for interface in $CONTEXT_INTERFACES; do - UPCASE_DEV=$interface - MAC=$(get_iface_var "MAC") + for iface in $CONTEXT_INTERFACES; do + MAC=$(get_iface_var "$iface" "MAC") DEV=$(get_dev "$INTERFACE_MAC" "$MAC") - IFACE_NUM=$(echo "$UPCASE_DEV" | sed 's/^ETH//') - IP=$(get_ip) - NETWORK=$(get_network) - MASK=$(get_mask) - MTU=$(get_mtu) - GATEWAY=$(get_gateway) - METRIC=$(get_iface_var "METRIC") + IP=$(get_ip "$iface") + MASK=$(get_mask "$iface") + MTU=$(get_mtu "$iface") + GATEWAY=$(get_gateway "$iface") + METRIC=$(get_iface_var "$iface" "METRIC") - IP6=$(get_iface_var "IP6") - [[ -z $IP6 ]] && IP6=$(get_iface_var "IPV6") - IP6_PREFIX_LENGTH=$(get_iface_var "IP6_PREFIX_LENGTH") - IP6_ULA=$(get_iface_var "IP6_ULA") - GATEWAY6=$(get_gateway6) + IP6=$(get_iface_var "$iface" "IP6") + [ -z "$IP6" ] && IP6=$(get_iface_var "$iface" "IPV6") + IP6_PREFIX_LENGTH=$(get_iface_var "$iface" "IP6_PREFIX_LENGTH") + IP6_ULA=$(get_iface_var "$iface" "IP6_ULA") + GATEWAY6=$(get_gateway6 "$iface") # cumulative variable IPV6ADDR_SECONDARIES='' @@ -241,10 +169,10 @@ gen_network_configuration() [ -z "${IP}${IP6}" ] && continue [ -z "${DEV}" ] && continue - ( - rm -f /etc/sysconfig/network-scripts/route-$DEV - rm -f /etc/sysconfig/network/ifroute-$DEV - rm -f /etc/sysconfig/network/ifsysctl-$DEV + { + rm -f "/etc/sysconfig/network-scripts/route-${DEV}" + rm -f "/etc/sysconfig/network/ifroute-${DEV}" + rm -f "/etc/sysconfig/network/ifsysctl-${DEV}" cat < ${CONFIG_PATH}/ifcfg-${DEV} + } > "${CONFIG_PATH}/ifcfg-${DEV}" - ifup ${DEV} + # TODO: do we want this here? + ifup "${DEV}" done } -configure_network() -{ - gen_network_configuration - - if [ "${COMMAND}" = 'reconfigure' ]; then - service network restart - fi - - sleep 2 -} - -[ -z "$(env | cut -d= -f1 | grep -E '^ETH[0-9]+_IPV*6*')" ] && exit 0 - -configure_network - diff --git a/src/usr/sbin/one-contextd b/src/usr/sbin/one-contextd index 0253afd..a64fead 100755 --- a/src/usr/sbin/one-contextd +++ b/src/usr/sbin/one-contextd @@ -98,10 +98,16 @@ function execute_scripts { # choose if [ "${_type}" = 'local' ]; then - local _scripts=$(ls ${SCRIPTS_DIR}/loc-* 2>/dev/null) + local _scripts=$(find "${SCRIPTS_DIR}" \ + -maxdepth 1 -mindepth 1 \ + -name 'loc-*' \ + 2>/dev/null) elif [ "${_type}" = 'network' ]; then - local _scripts=$(ls ${SCRIPTS_DIR}/!(net-*|loc-*) 2>/dev/null; \ - ls ${SCRIPTS_DIR}/net-* 2>/dev/null) + local _scripts=$(find "${SCRIPTS_DIR}" \ + -maxdepth 1 -mindepth 1 \ + \! \( -name 'net-*' -o -name 'loc-*' \) \ + -o -name 'net-*' \ + 2>/dev/null) fi export MOUNT_DIR @@ -109,6 +115,11 @@ function execute_scripts { log info "Processing ${_type} scripts" for _script in ${_scripts}; do + if [ -d "${_script}" ] ; then + # skip loc-10-network.d and similar + continue + fi + local _name=$(basename "${_script}") # run script and catch output and exit code