F #227: Refactor loc-10-network

- Use one script loc-10-network for all systems
- Deduplicate and cleanup the code
- Create a source file with functions
- Remove usage of global variables while using shared functions
- Add support for different network configuration via new context
  parameter: CONTEXT_NETCFG_TYPE

Signed-off-by: Petr Ospalý <pospaly@opennebula.io>
pull/244/head
Petr Ospalý 3 years ago committed by GitHub
parent 1e6741b345
commit 688c04e14c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

1
.gitignore vendored

@ -2,3 +2,4 @@ out/
*.rpm
*.deb
.vagrant*/
*~bak

@ -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

@ -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 <<EOT
iface $DEV inet static
address $IP
network $NETWORK
netmask $MASK
EOT
if [ -n "$MTU" ]; then
echo " mtu $MTU"
fi
if [ -n "$GATEWAY" ]; then
echo " gateway $GATEWAY"
if [ -n "$METRIC" ]; then
echo " metric $METRIC"
fi
fi
echo ""
}
gen_alias_conf() {
cat <<EOT
iface $DEV inet static
address $IP
network $NETWORK
netmask $MASK
EOT
echo ""
}
gen_alias6_conf() {
cat <<EOT
iface $DEV inet6 static
address $IP6
netmask ${IP6_PREFIX_LENGTH:-64}
pre-up echo 0 > /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 <<EOT
iface $DEV inet6 static
address $IP6_ULA
netmask 64
EOT
fi
echo ""
}
gen_iface6_conf() {
cat <<EOT
iface $DEV inet6 static
address $IP6
netmask ${IP6_PREFIX_LENGTH:-64}
pre-up echo 0 > /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 <<EOT
iface $DEV inet6 static
address $IP6_ULA
netmask 64
EOT
if [ -n "$MTU" ]; then
echo " mtu $MTU"
fi
fi
echo ""
}
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()
{
cat <<EOT
auto lo
iface lo inet loopback
EOT
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")
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")
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)
CONTEXT_FORCE_IPV4=$(get_iface_var "CONTEXT_FORCE_IPV4")
[ -z "${IP}${IP6}" ] && continue
[ -z "${DEV}" ] && continue
echo "auto $DEV"
[[ -n $IP ]] && gen_iface_conf
[[ -n $IP6 ]] && gen_iface6_conf
INDEX=${interface: -1}
ALIAS=$(get_interface_alias)
for nic_alias in $ALIAS; do
UPCASE_DEV="ETH${INDEX}_${nic_alias}"
IP=$(get_ip)
NETWORK=$(get_network)
MASK=$(get_mask)
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")
EXTERNAL=$(get_iface_var "EXTERNAL")
EXTERNAL=${EXTERNAL^^}
DETACH=$(get_iface_var "DETACH")
if [ -z "${DETACH}" ]; then
if [ -z "${EXTERNAL}" ] || [ $EXTERNAL = "NO" ]; then
[ -n "${IP}" ] && gen_alias_conf
[ -n "${IP6}" ] && gen_alias6_conf
fi
fi
done
done
}
configure_network()
{
gen_network_configuration > /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

@ -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 <<EOT
iface $DEV inet static
address $IP
network $NETWORK
netmask $MASK
EOT
if [ -n "$MTU" ]; then
echo " mtu $MTU"
fi
if [ -n "$GATEWAY" ]; then
echo " gateway $GATEWAY"
if [ -n "$METRIC" ]; then
echo " metric $METRIC"
fi
fi
echo ""
}
gen_alias_conf() {
cat <<EOT
iface $DEV inet static
address $IP
network $NETWORK
netmask $MASK
EOT
echo ""
}
gen_alias6_conf() {
cat <<EOT
iface $DEV inet6 static
address $IP6
netmask ${IP6_PREFIX_LENGTH:-64}
autoconf 0
accept_ra 0
EOT
if [ -n "$IP6_ULA" ]; then
cat <<EOT
iface $DEV inet6 static
address $IP6_ULA
netmask 64
EOT
fi
echo ""
}
gen_iface6_conf() {
cat <<EOT
iface $DEV inet6 static
address $IP6
netmask ${IP6_PREFIX_LENGTH:-64}
autoconf 0
accept_ra 0
EOT
if [ -n "$MTU" ]; then
echo " mtu $MTU"
fi
if [ -n "$GATEWAY6" ]; then
echo " gateway $GATEWAY6"
fi
if [ -n "$IP6_ULA" ]; then
cat <<EOT
iface $DEV inet6 static
address $IP6_ULA
netmask 64
autoconf 0
accept_ra 0
EOT
if [ -n "$MTU" ]; then
echo " mtu $MTU"
fi
fi
echo ""
}
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()
{
cat <<EOT
auto lo
iface lo inet loopback
EOT
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")
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")
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)
[ -z "${IP}${IP6}" ] && continue
[ -z "${DEV}" ] && continue
echo "auto $DEV"
[[ -n $IP ]] && gen_iface_conf
[[ -n $IP6 ]] && gen_iface6_conf
INDEX=${interface: -1}
ALIAS=$(get_interface_alias)
for nic_alias in $ALIAS; do
UPCASE_DEV="ETH${INDEX}_${nic_alias}"
IP=$(get_ip)
NETWORK=$(get_network)
MASK=$(get_mask)
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")
EXTERNAL=$(get_iface_var "EXTERNAL")
EXTERNAL=${EXTERNAL^^}
DETACH=$(get_iface_var "DETACH")
if [ -z "${DETACH}" ]; then
if [ -z "${EXTERNAL}" ] || [ $EXTERNAL = "NO" ]; then
[ -n "${IP}" ] && gen_alias_conf
[ -n "${IP6}" ] && gen_alias6_conf
fi
fi
done
done
}
configure_network()
{
gen_network_configuration > /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

@ -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 <<EOT
[Network]
Address=$IP/$CIDR
EOT
INDEX=${interface: -1}
ALIAS=$(get_interface_alias)
for nic_alias in $ALIAS; do
UPCASE_DEV="ETH${INDEX}_${nic_alias}"
IP=$(get_ip)
MASK=$(get_mask)
CIDR=$(mask2cidr "$MASK")
EXTERNAL=$(get_iface_var "EXTERNAL")
EXTERNAL=${EXTERNAL^^}
DETACH=$(get_iface_var "DETACH")
if [ -z "${DETACH}" ]; then
if [ -z "${EXTERNAL}" ] || [ $EXTERNAL = "NO" ]; then
if [ -n "${IP}" ]; then
echo "Address=$IP/$CIDR"
fi
fi
fi
done
if [ -n "$DNS" ]; then
for dns_server in $DNS; do
echo "DNS=$dns_server"
done
fi
if [ -n "$SEARCH_DOMAIN" ]; then
for domain in $SEARCH_DOMAIN; do
echo "Domains=$domain"
done
fi
cat <<EOT
[Route]
EOT
if [ -n "$GATEWAY" ]; then
echo "Gateway=$GATEWAY"
if [ -n "$METRIC" ]; then
echo "Metric=$METRIC"
fi
fi
echo ""
}
gen_iface6_conf() {
cat <<EOT
[Network]
Address=$IP6/${IP6_PREFIX_LENGTH:-64}
EOT
INDEX=${interface: -1}
ALIAS=$(get_interface_alias)
for nic_alias in $ALIAS; do
UPCASE_DEV="ETH${INDEX}_${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")
EXTERNAL=$(get_iface_var "EXTERNAL")
EXTERNAL=${EXTERNAL^^}
DETACH=$(get_iface_var "DETACH")
if [ -z "${DETACH}" ]; then
if [ -z "${EXTERNAL}" ] || [ $EXTERNAL = "NO" ]; then
if [ -n "${IP6}" ]; then
echo "Address=$IP6/${IP6_PREFIX_LENGTH:-64}"
fi
fi
fi
done
echo "IPv6AcceptRA=false"
if [ -n "$DNS" ]; then
for dns_server in $DNS; do
echo "DNS=$dns_server"
done
fi
if [ -n "$SEARCH_DOMAIN" ]; then
for domain in $SEARCH_DOMAIN; do
echo "Domains=$domain"
done
fi
cat <<EOT
[Route]
EOT
if [ -n "$GATEWAY6" ]; then
echo "Gateway=$GATEWAY6"
fi
if [ -n "$IP6_ULA" ]; then
cat <<EOT
[Network]
Address=$IP6_ULA/64
EOT
fi
echo ""
}
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()
{
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")
DEV=$(get_dev "$INTERFACE_MAC" "$MAC")
IFACE_NUM=$(echo "$UPCASE_DEV" | sed 's/^ETH//')
IP=$(get_ip)
NETWORK=$(get_network)
MASK=$(get_mask)
CIDR=$(mask2cidr "$MASK")
MTU=$(get_mtu)
GATEWAY=$(get_gateway)
METRIC=$(get_iface_var "METRIC")
DNS=$(get_dns)
SEARCH_DOMAIN=$(get_search_domain)
IP6=$(get_iface_var "IP6")
IP6_PREFIX_LENGTH=$(get_iface_var "IP6_PREFIX_LENGTH")
IP6_ULA=$(get_iface_var "IP6_ULA")
GATEWAY6=$(get_gateway6)
CONTEXT_FORCE_IPV4=$(get_iface_var "CONTEXT_FORCE_IPV4")
[ -z "${IP}${IP6}" ] && continue
[ -z "${DEV}" ] && continue
(
cat <<EOT
[Match]
Name=$DEV
EOT
if [ -n "$MTU" ]; then
cat <<EOT
[Link]
MTUBytes=$MTU
EOT
fi
if [ -n "$IP" ] || [ -n "$CONTEXT_FORCE_IPV4" ]; then
gen_iface_conf
fi
if [ -n "$IP6" ]; then
gen_iface6_conf
fi
) > /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

@ -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: <iface> <name>
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: <iface>
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; }' </dev/null)
fi
echo "$network"
)
# Gets the network mask
# arg: <iface>
get_mask()
(
mask=$(get_iface_var "$1" "MASK")
echo "${mask:-255.255.255.0}"
)
# Gets device MTU
# arg: <iface>
get_mtu()
(
mtu=$(get_iface_var "$1" "MTU")
echo "${mtu:-1500}"
)
# Gets the network gateway
# arg: <iface>
get_gateway()
(
get_iface_var "$1" "GATEWAY"
)
# Gets the network gateway6
# arg: <iface>
get_gateway6()
(
get_iface_var "$1" "GATEWAY6"
)
# arg: <iface>
get_ip()
(
get_iface_var "$1" "IP"
)
# arg: <iface>
get_dns()
(
get_iface_var "$1" "DNS"
)
# arg: <iface>
get_search_domain()
(
get_iface_var "$1" "SEARCH_DOMAIN"
)
# arg: <iface>
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
)

@ -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

@ -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 <<EOT
iface $DEV inet static
address $IP
network $NETWORK
netmask $MASK
EOT
if [ -n "$MTU" ]; then
echo " mtu $MTU"
fi
if [ -n "$GATEWAY" ]; then
echo " gateway $GATEWAY"
if [ -n "$METRIC" ]; then
echo " metric $METRIC"
fi
fi
echo ""
}
gen_alias_conf()
{
cat <<EOT
iface $DEV inet static
address $IP
network $NETWORK
netmask $MASK
EOT
echo ""
}
gen_alias6_conf()
{
case "${OS_ID}" in
alpine)
cat <<EOT
iface $DEV inet6 static
address $IP6
netmask ${IP6_PREFIX_LENGTH:-64}
pre-up echo 0 > /proc/sys/net/ipv6/conf/${DEV}/autoconf
pre-up echo 0 > /proc/sys/net/ipv6/conf/${DEV}/accept_ra
EOT
;;
debian|ubuntu|devuan)
cat <<EOT
iface $DEV inet6 static
address $IP6
netmask ${IP6_PREFIX_LENGTH:-64}
autoconf 0
accept_ra 0
EOT
;;
esac
if [ -n "$IP6_ULA" ]; then
cat <<EOT
iface $DEV inet6 static
address $IP6_ULA
netmask 64
EOT
fi
echo ""
}
gen_iface6_conf()
{
case "${OS_ID}" in
alpine)
cat <<EOT
iface $DEV inet6 static
address $IP6
netmask ${IP6_PREFIX_LENGTH:-64}
pre-up echo 0 > /proc/sys/net/ipv6/conf/${DEV}/autoconf
pre-up echo 0 > /proc/sys/net/ipv6/conf/${DEV}/accept_ra
EOT
;;
debian|ubuntu|devuan)
cat <<EOT
iface $DEV inet6 static
address $IP6
netmask ${IP6_PREFIX_LENGTH:-64}
autoconf 0
accept_ra 0
EOT
;;
esac
if [ -n "$MTU" ]; then
echo " mtu $MTU"
fi
if [ -n "$GATEWAY6" ]; then
echo " gateway $GATEWAY6"
fi
if [ -n "$IP6_ULA" ]; then
cat <<EOT
iface $DEV inet6 static
address $IP6_ULA
netmask 64
EOT
case "${OS_ID}" in
debian|ubuntu|devuan)
cat <<EOT
autoconf 0
accept_ra 0
EOT
;;
esac
if [ -n "$MTU" ]; then
echo " mtu $MTU"
fi
fi
echo ""
}
gen_network_configuration()
{
cat <<EOT
auto lo
iface lo inet loopback
EOT
INTERFACE_MAC=$(get_interface_mac)
CONTEXT_INTERFACES=$(get_context_interfaces)
for iface in $CONTEXT_INTERFACES; do
MAC=$(get_iface_var "$iface" "MAC")
DEV=$(get_dev "$INTERFACE_MAC" "$MAC")
IP=$(get_ip "$iface")
NETWORK=$(get_network "$iface")
MASK=$(get_mask "$iface")
MTU=$(get_mtu "$iface")
GATEWAY=$(get_gateway "$iface")
METRIC=$(get_iface_var "$iface" "METRIC")
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
echo "auto $DEV"
[ -n "${IP}" ] && gen_iface_conf
[ -n "${IP6}" ] && gen_iface6_conf
ALIASES=$(get_interface_alias "$iface")
for nic_alias in $ALIASES ; do
IP=$(get_ip "$nic_alias")
NETWORK=$(get_network "$nic_alias")
MASK=$(get_mask "$nic_alias")
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 "$nic_alias" "EXTERNAL")
EXTERNAL=${EXTERNAL^^}
DETACH=$(get_iface_var "$nic_alias" "DETACH")
if [ -z "${DETACH}" ]; then
if [ -z "${EXTERNAL}" ] || [ "${EXTERNAL}" = "NO" ]; then
[ -n "${IP}" ] && gen_alias_conf
[ -n "${IP6}" ] && gen_alias6_conf
fi
fi
done
done
}

@ -0,0 +1,219 @@
#!/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
altlinux)
return 0
;;
fedora|centos|rhel|almalinux|ol|rocky)
return 0
;;
opensuse*)
return 0
;;
esac
return 1
}
configure_network()
{
gen_network_configuration
}
stop_network() {
systemctl stop systemd-networkd.service
}
start_network() {
systemctl start systemd-networkd.service
}
reload_network() {
systemctl restart systemd-networkd.service
}
#
# helper functions
#
gen_iface_conf() {
cat <<EOT
[Network]
Address=${IP}/${CIDR}
EOT
ALIASES=$(get_interface_alias "$iface")
for nic_alias in $ALIASES; do
IP=$(get_ip "$nic_alias")
MASK=$(get_mask "$nic_alias")
CIDR=$(mask2cidr "$MASK")
EXTERNAL=$(get_iface_var "$nic_alias" "EXTERNAL")
EXTERNAL=${EXTERNAL^^}
DETACH=$(get_iface_var "$nic_alias" "DETACH")
if [ -z "${DETACH}" ]; then
if [ -z "${EXTERNAL}" ] || [ "${EXTERNAL}" = "NO" ]; then
if [ -n "${IP}" ]; then
echo "Address=$IP/$CIDR"
fi
fi
fi
done
if [ -n "$DNS" ]; then
for dns_server in $DNS; do
echo "DNS=$dns_server"
done
fi
if [ -n "$SEARCH_DOMAIN" ]; then
for domain in $SEARCH_DOMAIN; do
echo "Domains=$domain"
done
fi
cat <<EOT
[Route]
EOT
if [ -n "$GATEWAY" ]; then
echo "Gateway=$GATEWAY"
if [ -n "$METRIC" ]; then
echo "Metric=$METRIC"
fi
fi
echo ""
}
gen_iface6_conf() {
cat <<EOT
[Network]
Address=${IP6}/${IP6_PREFIX_LENGTH:-64}
EOT
ALIASES=$(get_interface_alias "$iface")
for nic_alias in $ALIASES; do
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 "$nic_alias" "EXTERNAL")
EXTERNAL=${EXTERNAL^^}
DETACH=$(get_iface_var "$nic_alias" "DETACH")
if [ -z "${DETACH}" ]; then
if [ -z "${EXTERNAL}" ] || [ "${EXTERNAL}" = "NO" ]; then
if [ -n "${IP6}" ]; then
echo "Address=$IP6/${IP6_PREFIX_LENGTH:-64}"
fi
fi
fi
done
echo "IPv6AcceptRA=false"
if [ -n "$DNS" ]; then
for dns_server in $DNS; do
echo "DNS=$dns_server"
done
fi
if [ -n "$SEARCH_DOMAIN" ]; then
for domain in $SEARCH_DOMAIN; do
echo "Domains=$domain"
done
fi
cat <<EOT
[Route]
EOT
if [ -n "$GATEWAY6" ]; then
echo "Gateway=$GATEWAY6"
fi
if [ -n "$IP6_ULA" ]; then
cat <<EOT
[Network]
Address=$IP6_ULA/64
EOT
fi
echo ""
}
gen_network_configuration()
{
INTERFACE_MAC=$(get_interface_mac)
CONTEXT_INTERFACES=$(get_context_interfaces)
for iface in $CONTEXT_INTERFACES; do
MAC=$(get_iface_var "$iface" "MAC")
DEV=$(get_dev "$INTERFACE_MAC" "$MAC")
IP=$(get_ip "$iface")
MASK=$(get_mask "$iface")
CIDR=$(mask2cidr "$MASK")
MTU=$(get_mtu "$iface")
GATEWAY=$(get_gateway "$iface")
METRIC=$(get_iface_var "$iface" "METRIC")
DNS=$(get_dns "$iface")
SEARCH_DOMAIN=$(get_search_domain "$iface")
IP6=$(get_iface_var "$iface" "IP6")
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
{
cat <<EOT
[Match]
Name=$DEV
EOT
if [ -n "$MTU" ]; then
cat <<EOT
[Link]
MTUBytes=$MTU
EOT
fi
[ -n "${IP}" ] && gen_iface_conf
[ -n "${IP6}" ] && gen_iface6_conf
} > "/etc/systemd/network/${DEV}.network"
done
}

@ -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 <<EOT
NETMASK=$MASK
IPADDR=$IP
NETMASK="$MASK"
IPADDR="$IP"
EOT
if [ -n "$GATEWAY" ]; then
@ -148,7 +103,7 @@ gen_iface6_conf() {
if [ "$CONFIG_PATH" = "/etc/sysconfig/network" ]; then
echo "IPADDR_6A=$IP6/${IP6_PREFIX_LENGTH:-64}"
cat <<EOT >> /etc/sysconfig/network/ifsysctl-$DEV
cat <<EOT >> "/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 <<EOT
DEVICE=$DEV
@ -258,29 +186,27 @@ EOT
echo "ONBOOT=yes"
fi
[[ -n $IP ]] && gen_iface_conf
[[ -n $IP6 ]] && gen_iface6_conf
[ -n "${IP}" ] && gen_iface_conf
[ -n "${IP6}" ] && gen_iface6_conf
INDEX=${interface: -1}
ALIAS=$(get_interface_alias)
ALIASES=$(get_interface_alias "$iface")
ALIAS_NUM=0
for nic_alias in $ALIAS; do
UPCASE_DEV="ETH${INDEX}_${nic_alias}"
IP=$(get_ip)
MASK=$(get_mask)
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
if [ -z "${EXTERNAL}" ] || [ "${EXTERNAL}" = "NO" ]; then
[ -n "${IP}" ] && gen_alias_conf
[ -n "${IP6}" ] && gen_alias6_conf
@ -296,25 +222,11 @@ EOT
if [ -n "${IPV6ADDR_SECONDARIES}" ]; then
echo "IPV6ADDR_SECONDARIES='${IPV6ADDR_SECONDARIES## }'"
fi
) > ${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

@ -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

Loading…
Cancel
Save