F #227: Refactor loc-10-network

Deprecate EC2
Introduce one-context-online service
Support NetPlan, NetworkManager
Various fixes
DHCP WIP
pull/244/head
Petr Ospalý 3 years ago committed by GitHub
parent 688c04e14c
commit 914ceae299
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,6 +16,8 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# shellcheck disable=SC1091
if [ -z "${TARGET}" ]; then
echo 'Error: env. variable TARGET not set' >&2
exit 1
@ -28,7 +30,7 @@ set +e
###
if [ -z "${RELEASE}" ]; then
if git describe --contains $(git rev-parse HEAD) &>/dev/null; then
if git describe --contains "$(git rev-parse HEAD)" &>/dev/null; then
RELEASE=1
else
DATE=${DATE:-$(date +%Y%m%d)}
@ -90,12 +92,14 @@ _PREUN=$(mktemp)
_POSTUN=$(mktemp)
_POSTUP=$(mktemp)
# shellcheck disable=SC2064
trap "rm -rf ${UNAME_PATH} ${BUILD_DIR} ${_POSTIN} ${_PREUN} ${_POSTUN} ${_POSTUP}" EXIT
while IFS= read -r -d $'\0' SRC; do
F_TAGS=${SRC##*##}
if [ "x${SRC}" != "x${F_TAGS}" ]; then
for F_TAG in $(echo ${F_TAGS} | sed -e 's/\./ /g'); do
# shellcheck disable=SC2001
for F_TAG in $(echo "${F_TAGS}" | sed -e 's/\./ /g'); do
for TAG in ${TAGS}; do
if [ "${F_TAG}" = "${TAG}" ]; then
continue 2 # tag matches, continue with next tag
@ -111,7 +115,7 @@ while IFS= read -r -d $'\0' SRC; do
cp "src/${SRC}" "${BUILD_DIR}/${DST}"
done < <(cd src/ && find . -type f -print0)
for F in $@; do
for F in "$@"; do
cp -r "$F" "${BUILD_DIR}/"
done
@ -123,7 +127,8 @@ umask 0022
# cleanup
if [ -z "${OUT}" ]; then
OUT="out/${FILENAME}"
mkdir -p $(dirname "${OUT}")
_out_dir=$(dirname "${OUT}")
mkdir -p "${_out_dir}"
rm -rf "${OUT}"
fi
@ -144,11 +149,12 @@ if [ "${TYPE}" = 'dir' ]; then
cp -rT "${BUILD_DIR}" "${OUT}"
elif [ "${TYPE}" = 'iso' ]; then
_out_dir=$(dirname "${OUT}")
mkisofs -J -R -input-charset utf8 \
-m '*.iso' \
-V "${LABEL}" \
-o "${OUT}" \
$(dirname "${OUT}")
"${_out_dir}"
else
CONFIG_FILES=$(cd "${BUILD_DIR}" && \
@ -159,25 +165,26 @@ else
# concatenate pre/postinstall scripts
if [ -n "${POSTIN}" ]; then
cat ${POSTIN} >"${_POSTIN}"
cat "${POSTIN}" >"${_POSTIN}"
fi
if [ -n "${PREUN}" ]; then
cat ${PREUN} >"${_PREUN}"
cat "${PREUN}" >"${_PREUN}"
fi
if [ -n "${POSTUN}" ]; then
cat ${POSTUN} >"${_POSTUN}"
cat "${POSTUN}" >"${_POSTUN}"
fi
if [ -n "${POSTUP}" ]; then
cat ${POSTUP} >"${_POSTUP}"
cat "${POSTUP}" >"${_POSTUP}"
fi
# set the package version of onesysprep
sed -i "s/\<_PACKAGE_VERSION_\>/${VERSION}/" \
"${BUILD_DIR}/usr/sbin/onesysprep"
# shellcheck disable=SC2086
fpm --name "${NAME}" --version "${VERSION}" --iteration "${RELEASE_FULL}" \
--architecture all --license "${LICENSE}" \
--vendor "${VENDOR}" --maintainer "${MAINTAINER}" \
@ -201,4 +208,4 @@ else
--package "${OUT}"
fi
echo $(basename ${OUT})
basename "${OUT}"

@ -1,8 +1,7 @@
#!/usr/bin/env bash
NETWORK=${NETWORK:-yes}
NETWORK_EC2=${NETWORK_EC2:-no}
SERVICES=${SERVICES:-one-context-local one-context}
SERVICES=${SERVICES:-one-context-local one-context-online one-context}
rm -f /etc/udev/rules.d/70-persistent-cd.rules
rm -f /etc/udev/rules.d/70-persistent-net.rules
@ -65,26 +64,15 @@ fi
# Debian based distros
if [ -d /etc/network ]; then
# Prepare network files
cp /etc/network/interfaces /etc/network/interfaces.$(date "+%s")
cp /etc/network/interfaces "/etc/network/interfaces.$(date '+%s')"
rm -rf /etc/network/interfaces.d
rm -rf /etc/netplan/*
if [ "${NETWORK_EC2}" = 'yes' ]; then
cat > /etc/network/interfaces <<EOT
# The loopback network interface
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOT
else
cat > /etc/network/interfaces <<EOT
cat > /etc/network/interfaces <<EOT
# The loopback network interface
auto lo
iface lo inet loopback
EOT
fi
# Do not reconfigure network interfaces on boot
# if [ -f /etc/default/networking ]; then
@ -101,20 +89,6 @@ if [ -d /etc/sysconfig/network-scripts ]; then
\( -name 'ifcfg-*' -o -name 'route-*' \) \
! -name 'ifcfg-lo' ! -name 'route-lo' \
-exec rm -f {} \;
if [ "${NETWORK_EC2}" = 'yes' ]; then
cat >/etc/sysconfig/network-scripts/ifcfg-eth0 <<EOT
DEVICE="eth0"
BOOTPROTO="dhcp"
ONBOOT="yes"
TYPE="Ethernet"
USERCTL="yes"
PEERDNS="yes"
IPV6INIT="no"
NM_CONTROLLED="no"
PERSISTENT_DHCLIENT="1"
EOT
fi
fi
# openSUSE based distros
@ -129,15 +103,6 @@ if [ -d /etc/sysconfig/network ]; then
sed -i '/^NETCONFIG_DNS_STATIC_SERVERS=/ s/=.*$/="/' /etc/sysconfig/network/config
sed -i '/^NETCONFIG_DNS_STATIC_SEARCHLIST=/ s/=.*$/=""/' /etc/sysconfig/network/config
if [ "${NETWORK_EC2}" = 'yes' ]; then
cat >/etc/sysconfig/network/ifcfg-eth0 <<EOT
DEVICE=eth0
BOOTPROTO=dhcp4
STARTMODE=auto
USERCONTROL=yes
EOT
fi
fi
# FreeBSD based distros

@ -1,4 +0,0 @@
#!/usr/bin/env bash
NETWORK_EC2=yes
SERVICES='one-context'

@ -1,6 +1,6 @@
#!/usr/bin/env bash
SERVICES=${SERVICES:-one-context one-context-local}
SERVICES=${SERVICES:-one-context one-context-online one-context-local}
# Disable services
if which systemctl >/dev/null 2>&1 && [ -d /etc/systemd ]; then

@ -1,3 +0,0 @@
#!/usr/bin/env bash
SERVICES='one-context'

@ -0,0 +1,3 @@
[main]
no-auto-default=*
dns=none

@ -18,6 +18,11 @@
set -e
# THIS IS A CONTEXTUALIZATION GUARD
if [ "$1" != 'local' ] ; then
exit 0
fi
# Umount the directory and remove it
cleanup()
{

@ -20,6 +20,11 @@
set -e
# THIS IS A CONTEXTUALIZATION GUARD
if [ "$1" != 'local' ] ; then
exit 0
fi
GROW_ROOTFS=${GROW_ROOTFS:-YES}
GROW_ROOTFS=${GROW_ROOTFS^^}
#GROW_FS # list of mountpoints to resize

@ -16,6 +16,11 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# THIS IS A CONTEXTUALIZATION GUARD
if [ "$1" != 'local' ] ; then
exit 0
fi
if [ -z "${TIMEZONE}" ]; then
exit 0
fi

@ -16,44 +16,47 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# shellcheck disable=SC1091,SC1090
. /etc/one-context.d/loc-10-network.d/functions
ACTION="$1"
OS_ID=$(detect_os)
context_type="$1"
action="$2"
os_id=$(detect_os)
if [ -z "${ACTION}" ] ; then
ACTION="configure"
if [ -z "${action}" ] ; then
action="configure"
fi
if [ -z "${CONTEXT_NETCFG_TYPE}" ] ; then
case "${OS_ID}" in
if [ -z "${NETCFG_TYPE}" ] ; then
case "${os_id}" in
alpine)
CONTEXT_NETCFG_TYPE='interfaces'
NETCFG_TYPE='interfaces'
;;
altlinux)
CONTEXT_NETCFG_TYPE='networkd'
NETCFG_TYPE='networkd nm'
;;
debian|ubuntu|devuan)
CONTEXT_NETCFG_TYPE='interfaces'
debian|devuan|ubuntu)
NETCFG_TYPE='interfaces netplan nm networkd'
;;
fedora|centos|rhel|almalinux|ol|rocky)
CONTEXT_NETCFG_TYPE='scripts'
NETCFG_TYPE='scripts nm networkd'
;;
opensuse*)
CONTEXT_NETCFG_TYPE='scripts'
NETCFG_TYPE='scripts nm networkd'
;;
freebsd)
CONTEXT_NETCFG_TYPE='bsd'
NETCFG_TYPE='bsd'
;;
*)
CONTEXT_NETCFG_TYPE='none'
NETCFG_TYPE='none'
;;
esac
fi
if [ "${CONTEXT_NETCFG_TYPE}" != 'none' ] ; then
if [ "${NETCFG_TYPE}" != 'none' ] ; then
_found_valid_netcfg='no'
for _cfgtype in ${CONTEXT_NETCFG_TYPE} ; do
for _cfgtype in ${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
@ -67,16 +70,30 @@ if [ "${CONTEXT_NETCFG_TYPE}" != 'none' ] ; then
fi
done
# this is a contextualization guard
case "${required_context_type}" in
''|local|online)
if [ "${required_context_type:-local}" != "${context_type}" ] ; then
# skip this netcfg at this stage
exit 0
fi
;;
*)
echo "ERROR [!]: Unknown required context type: ${required_context_type}" >&2
exit 1
;;
esac
if [ "${_found_valid_netcfg}" = 'no' ] ; then
echo "ERROR [!]: None of the requested network types is supported on: ${OS_ID}" >&2
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'
action='none'
fi
case "$ACTION" in
case "$action" in
none)
echo "INFO: Network will not be configured" >&2
;;
@ -88,7 +105,7 @@ case "$ACTION" in
reload_network
;;
*)
echo "ERROR [!]: Unknown ACTION: ${ACTION}" >&2
echo "ERROR [!]: Unknown ACTION: ${action}" >&2
exit 1
;;
esac

@ -16,6 +16,11 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# THIS IS A CONTEXTUALIZATION GUARD
if [ "$1" != 'local' ] ; then
exit 0
fi
get_iface_var()
{
var_name="${UPCASE_DEV}_$1"

@ -16,10 +16,14 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# shellcheck disable=SC2155
#
# network module interface
#
export required_context_type='local' # this is the default, the next option is 'online'
is_network_supported()
{
false
@ -27,25 +31,25 @@ is_network_supported()
configure_network()
{
echo "ERROR [!]: No 'configure_network' implementation for the network type: ${CONTEXT_NETCFG_TYPE}" >&2
echo "ERROR [!]: No 'configure_network' implementation for the network type: ${NETCFG_TYPE}" >&2
exit 1
}
stop_network()
{
echo "ERROR [!]: No 'stop_network' implementation for the network type: ${CONTEXT_NETCFG_TYPE}" >&2
echo "ERROR [!]: No 'stop_network' implementation for the network type: ${NETCFG_TYPE}" >&2
exit 1
}
start_network()
{
echo "ERROR [!]: No 'start_network' implementation for the network type: ${CONTEXT_NETCFG_TYPE}" >&2
echo "ERROR [!]: No 'start_network' implementation for the network type: ${NETCFG_TYPE}" >&2
exit 1
}
reload_network()
{
echo "ERROR [!]: No 'reload_network' implementation for the network type: ${CONTEXT_NETCFG_TYPE}" >&2
echo "ERROR [!]: No 'reload_network' implementation for the network type: ${NETCFG_TYPE}" >&2
exit 1
}
@ -53,6 +57,20 @@ reload_network()
# generic shared functions
#
is_true()
(
_value=$(eval echo "\$${1}" | \
sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | \
tr '[:upper:]' '[:lower:]')
case "$_value" in
1|true|yes|y)
return 0
;;
esac
return 1
)
# return OS ID
detect_os()
(
@ -66,81 +84,93 @@ detect_os()
fi
)
skip_interface()
{
[ -z "${dev}" ] && return 0
is_true "${dhcp}" && return 1
is_true "${dhcp6}" && return 1
[ -z "${ip}${ip6}" ] && return 0
return 1
}
# args: <iface> <name>
get_iface_var()
(
iface=$(echo "$1" | tr '[:lower:]' '[:upper:]')
var_name="${iface}_${2}"
eval "echo \"\${${var_name}}\""
_iface=$(echo "$1" | tr '[:lower:]' '[:upper:]')
_var_name="${_iface}_${2}"
eval "echo \"\${${_var_name}}\""
)
# Gets IP address from a given MAC
mac2ip()
(
mac="$1"
_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)
_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}"
echo "0x${_ip_a}.0x${_ip_b}.0x${_ip_c}.0x${_ip_d}"
)
mask2cidr()
(
mask="$1"
nbits=0
_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 ;;
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 ;;
*) echo "Error: $_dec is not recognised"; exit 1 ;;
esac
done
echo "$nbits"
echo "$_nbits"
)
# Gets the network part of an IP
# arg: <iface>
get_network()
(
network=$(get_iface_var "$1" "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 {
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"
echo "$_network"
)
# Gets the network mask
# arg: <iface>
get_mask()
(
mask=$(get_iface_var "$1" "MASK")
echo "${mask:-255.255.255.0}"
_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}"
_mtu=$(get_iface_var "$1" "MTU")
echo "${_mtu:-1500}"
)
# Gets the network gateway
@ -178,17 +208,20 @@ get_search_domain()
# arg: <iface>
get_interface_alias()
(
env | sed -n "s#^\(${1}_ALIAS[0-9]\+\)_MAC=.*#\1#p" | sort
# sed on freebsd does not recognize '+' - replacing with asterisk
env | sed -n "s#^\(${1}_ALIAS[0-9][0-9]*\)_MAC=.*#\1#p" | sort
)
get_context_interfaces()
(
env | grep -E "^ETH[0-9]+_MAC=" | sed 's/_.*$//' | sort
# sed on freebsd does not recognize '+' - replacing with asterisk
env | sed -n 's/^\(ETH[0-9][0-9]*\)_MAC=.*/\1/p' | sort
)
get_pci_interfaces()
(
env | grep -E "^PCI[0-9]+_MAC=" | sed 's/_.*$//' | sort
# sed on freebsd does not recognize '+' - replacing with asterisk
env | sed -n 's/^\(PCI[0-9][0-9]*\)_MAC=.*/\1/p' | sort
)
get_interface_mac()
@ -198,9 +231,111 @@ get_interface_mac()
get_dev()
(
list="$1"
mac="$2"
_list="$1"
_mac="$2"
echo "$list" | grep "$mac" | cut -d' ' -f1 | tail -n1
echo "$_list" | grep "$_mac" | cut -d' ' -f1 | tail -n1
)
# arg: <interface/alias>
setup_ipadr_vars()
{
export ip=$(get_ip "$1")
export network=$(get_network "$1")
export mask=$(get_mask "$1")
export cidr=$(mask2cidr "$mask")
export dhcp=$(get_iface_var "$1" "DHCP")
}
# arg: <interface/alias>
setup_ip6adr_vars()
{
export ip6=$(get_iface_var "$1" "IP6")
export ip6_prefix_length=$(get_iface_var "$1" "IP6_PREFIX_LENGTH")
export ip6_ula=$(get_iface_var "$1" "IP6_ULA")
export dhcp6=$(get_iface_var "$1" "DHCP6")
[ -z "$ip6" ] && ip6=$(get_iface_var "$1" "IPV6")
[ -z "$ip6_prefix_length" ] && ip6_prefix_length=64
}
# arg: <interface>
setup_iface_vars()
{
_iface_mac=$(get_interface_mac)
export mac=$(get_iface_var "$1" "MAC")
export dev=$(get_dev "$_iface_mac" "$mac")
export mtu=$(get_mtu "$1")
export gateway=$(get_gateway "$1")
export metric=$(get_iface_var "$1" "METRIC")
export dns=$(get_dns "$1")
export search_domains=$(get_search_domain "$1")
export gateway6=$(get_gateway6 "$1")
setup_ipadr_vars "$1"
setup_ip6adr_vars "$1"
}
# arg: <alias>
setup_alias_vars()
{
export external=$(get_iface_var "$1" "EXTERNAL")
export detach=$(get_iface_var "$1" "DETACH")
}
get_nameservers()
(
# sed on freebsd does not recognize '+' - replacing with asterisk
_dns_variables=$(env | sed -n 's/^\(ETH[0-9][0-9]*_DNS\)=.*/\1/p' | sort)
for _dns in DNS ${_dns_variables} ; do
_value=$(eval "echo \"\${$_dns}\"")
if [ -n "$_value" ] ; then
echo "$_value"
fi
done
)
get_searchdomains()
(
# sed on freebsd does not recognize '+' - replacing with asterisk
_search_domains=$(env | sed -n 's/^\(ETH[0-9][0-9]*_SEARCH_DOMAIN\)=.*/\1/p' | sort)
for _search in SEARCH_DOMAIN ${_search_domains} ; do
_value=$(eval "echo \"\${$_search}\"")
if [ -n "$_value" ] ; then
echo "$_value"
fi
done
)
gen_resolvconf()
{
export all_nameservers=$(get_nameservers)
export all_search_domains=$(get_searchdomains)
[ -z "$all_nameservers" ] && return 0
if [ -L /etc/resolv.conf ]; then
unlink /etc/resolv.conf
else
cat /dev/null > /etc/resolv.conf
fi
for _nameserver in $all_nameservers ; do
echo "nameserver ${_nameserver}" >> /etc/resolv.conf
done
if [ -f /etc/sysconfig/network/config ]; then
sed -i "/^NETCONFIG_DNS_STATIC_SERVERS=/ s/=.*$/=\"${all_nameservers}\"/" /etc/sysconfig/network/config
fi
[ -z "$all_search_domains" ] && return 0
echo "search ${all_search_domains}" >> /etc/resolv.conf
if [ -f /etc/sysconfig/network/config ]; then
sed -i "/^NETCONFIG_DNS_STATIC_SEARCHLIST=/ s/=.*$/=\"${all_search_domains}\"/" /etc/sysconfig/network/config
fi
}

@ -22,7 +22,7 @@
is_network_supported()
{
case "${OS_ID}" in
case "${os_id}" in
freebsd)
return 0
;;
@ -33,7 +33,8 @@ is_network_supported()
configure_network()
{
gen_network_configuration >/etc/rc.conf.d/network
gen_resolvconf
gen_network_configuration > /etc/rc.conf.d/network
}
stop_network()
@ -58,138 +59,166 @@ reload_network()
# helper functions
#
# TODO: remove global variables and get rid off exports
#
# to satisfy shellcheck SC2154:
export os_id
export ip
export network
export mask
export cidr
export dhcp
export ip6
export ip6_prefix_length
export ip6_ula
export dhcp6
export mac
export dev
export mtu
export gateway
export metric
export dns
export search_domains
export gateway6
export external
export detach
export all_nameservers
export all_search_domains
get_interface_mac()
(
macs=$(ifconfig | grep ether | awk '{print $2}')
_macs=$(ifconfig | grep ether | awk '{print $2}')
for mac in ${macs} ; do
iface=$(ifconfig | grep -B 2 "$mac" | head -n 1 | awk '{print $1}' | cut -d ':' -f 1)
echo "${iface} ${mac}"
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}"
gen_iface_conf()
{
echo -n "ifconfig_${dev}=\"inet ${ip} netmask ${mask}"
if [ -n "${MTU}" ]; then
echo -n " mtu ${MTU}"
if [ -n "${mtu}" ]; then
echo -n " mtu ${mtu}"
fi
echo "\""
###
if [ -n "${GATEWAY}" ]; then
echo "defaultrouter=\"${GATEWAY}\"" >> /etc/rc.conf.d/routing
if [ -n "${gateway}" ]; then
echo "defaultrouter=\"${gateway}\"" >> /etc/rc.conf.d/routing
fi
}
gen_alias_conf() {
echo "ifconfig_${DEV}_alias${ALIAS_NUM}=\"inet ${IP} netmask ${MASK}\""
ALIAS_NUM=$((ALIAS_NUM + 1))
gen_dhcp_conf()
{
echo "ifconfig_${dev}=\"DHCP\""
}
gen_alias6_conf() {
# very first IPv6 can't be alias
if [ -n "${HAS_IP6}" ]; then
echo "ifconfig_${DEV}_alias${ALIAS_NUM}=\"inet6 ${IP6} prefixlen ${IP6_PREFIX_LENGTH:-64}\""
ALIAS_NUM=$((ALIAS_NUM + 1))
else
echo -n "ifconfig_${DEV}_ipv6=\"inet6 ${IP6} prefixlen ${IP6_PREFIX_LENGTH:-64}"
if [ -n "${MTU}" ]; then
echo -n " mtu ${MTU}"
fi
echo " -accept_rtadv\""
fi
if [ -n "${IP6_ULA}" ]; then
echo "ifconfig_${DEV}_alias${ALIAS_NUM}=\"inet6 ${IP6_ULA} prefixlen 64\""
ALIAS_NUM=$((ALIAS_NUM + 1))
fi
gen_alias_conf()
{
echo "ifconfig_${dev}_alias${alias_num}=\"inet ${ip} netmask ${mask}\""
alias_num=$((alias_num + 1))
}
gen_iface6_conf() {
echo -n "ifconfig_${DEV}_ipv6=\"inet6 ${IP6} prefixlen ${IP6_PREFIX_LENGTH:-64}"
gen_iface6_conf()
{
echo -n "ifconfig_${dev}_ipv6=\"inet6 ${ip6} prefixlen ${ip6_prefix_length:-64}"
if [ -n "${MTU}" ]; then
echo -n " mtu ${MTU}"
if [ -n "${mtu}" ]; then
echo -n " mtu ${mtu}"
fi
echo " -accept_rtadv\""
if [ -n "${IP6_ULA}" ]; then
echo "ifconfig_${DEV}_alias${ALIAS_NUM}=\"inet6 ${IP6_ULA} prefixlen 64\""
ALIAS_NUM=$((ALIAS_NUM + 1))
if [ -n "${ip6_ula}" ]; then
echo "ifconfig_${dev}_alias${alias_num}=\"inet6 ${ip6_ula} prefixlen 64\""
alias_num=$((alias_num + 1))
fi
###
if [ -n "${GATEWAY6}" ]; then
echo "ipv6_defaultrouter=\"${GATEWAY6}\"" >> /etc/rc.conf.d/routing
if [ -n "${gateway6}" ]; then
echo "ipv6_defaultrouter=\"${gateway6}\"" >> /etc/rc.conf.d/routing
fi
}
gen_network_configuration()
gen_dhcp6_conf()
{
# clean routing information
echo -n > /etc/rc.conf.d/routing
# TODO: FreeBSD support for DHCP6 does not seem to be great:
# https://forums.freebsd.org/threads/ipv6-dhcpv6-client-and-accept_rtadv-vs-rtsold.77421/
# https://forums.freebsd.org/threads/is-there-a-working-dhcpv6-client-for-freebsd.60168/
echo "ERROR [!]: DHCP6 on '${os_id}' is not supported" >&2
return 1
}
INTERFACE_MAC=$(get_interface_mac)
CONTEXT_INTERFACES=$(get_context_interfaces)
gen_alias6_conf()
{
# very first IPv6 can't be alias
if [ -n "${has_ip6}" ]; then
echo "ifconfig_${dev}_alias${alias_num}=\"inet6 ${ip6} prefixlen ${ip6_prefix_length:-64}\""
alias_num=$((alias_num + 1))
else
echo -n "ifconfig_${dev}_ipv6=\"inet6 ${ip6} prefixlen ${ip6_prefix_length:-64}"
for iface in $CONTEXT_INTERFACES; do
MAC=$(get_iface_var "$iface" "MAC")
DEV=$(get_dev "$INTERFACE_MAC" "$MAC")
if [ -n "${mtu}" ]; then
echo -n " mtu ${mtu}"
fi
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")
echo " -accept_rtadv\""
fi
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")
if [ -n "${ip6_ula}" ]; then
echo "ifconfig_${dev}_alias${alias_num}=\"inet6 ${ip6_ula} prefixlen 64\""
alias_num=$((alias_num + 1))
fi
}
[ -z "${IP}${IP6}" ] && continue
[ -z "${DEV}" ] && continue
HAS_IP6="${IP6}"
gen_network_configuration()
{
# clean routing information
cat /dev/null > /etc/rc.conf.d/routing
_context_interfaces=$(get_context_interfaces)
[ -n "${IP}" ] && gen_iface_conf
[ -n "${IP6}" ] && gen_iface6_conf
for _iface in $_context_interfaces; do
setup_iface_vars "$_iface"
ALIASES=$(get_interface_alias)
ALIAS_NUM=0
skip_interface && continue
for nic_alias in $ALIASES; do
IP=$(get_ip "$nic_alias")
MASK=$(get_mask "$nic_alias")
if is_true "${dhcp}" ; then
gen_dhcp_conf
elif [ -n "${ip}" ] ; then
gen_iface_conf
fi
if is_true "${dhcp6}" ; then
gen_dhcp6_conf
elif [ -n "${ip6}" ] ; then
gen_iface6_conf
fi
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")
has_ip6="${ip6}"
_aliases=$(get_interface_alias)
alias_num=0
EXTERNAL=$(get_iface_var "$nic_alias" "EXTERNAL")
EXTERNAL=${EXTERNAL^^}
DETACH=$(get_iface_var "$nic_alias" "DETACH")
for _nic_alias in $_aliases; do
setup_ipadr_vars "$_nic_alias"
setup_ip6adr_vars "$_nic_alias"
setup_alias_vars "$_nic_alias"
if [ -z "${DETACH}" ]; then
if [ -z "${EXTERNAL}" ] || [ "$EXTERNAL" = "NO" ]; then
[ -n "${IP}" ] && gen_alias_conf
if [ -z "${detach}" ]; then
if ! is_true "${external}" ; then
[ -n "${ip}" ] && gen_alias_conf
if [ -n "${IP6}" ]; then
if [ -n "${ip6}" ]; then
gen_alias6_conf
HAS_IP6="${IP6}"
has_ip6="${ip6}"
fi
fi
fi
done
done
}

@ -22,7 +22,7 @@
is_network_supported()
{
case "${OS_ID}" in
case "${os_id}" in
alpine)
return 0
;;
@ -36,30 +36,32 @@ is_network_supported()
configure_network()
{
gen_resolvconf
gen_network_configuration > /etc/network/interfaces
case "${OS_ID}" in
case "${os_id}" in
debian|ubuntu|devuan)
echo "source /etc/network/interfaces.d/*.cfg" >> /etc/network/interfaces
;;
esac
}
stop_network() {
case "${OS_ID}" in
stop_network()
{
case "${os_id}" in
alpine)
service networking stop || true
# took from find_ifaces in the networking service
IFACES=$(\
_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
for _iface in $_ifaces; do
if [ "${_iface}" != 'lo' ]; then
/sbin/ip link set dev "${_iface}" down || true
/sbin/ip addr flush dev "${_iface}" || true
fi
done
;;
@ -68,12 +70,12 @@ stop_network() {
return 0
fi
IFACES=$(/sbin/ifquery --list -a)
_ifaces=$(/sbin/ifquery --list -a)
for i in $IFACES; do
if [ "${i}" != 'lo' ] ; then
/sbin/ifdown "${i}"
/sbin/ip addr flush dev "${i}"
for _iface in $_ifaces; do
if [ "${_iface}" != 'lo' ] ; then
/sbin/ifdown "${_iface}"
/sbin/ip addr flush dev "${_iface}"
fi
done
;;
@ -83,8 +85,9 @@ stop_network() {
esac
}
start_network() {
case "${OS_ID}" in
start_network()
{
case "${os_id}" in
alpine)
service networking start
;;
@ -94,10 +97,10 @@ start_network() {
return 0
fi
IFACES=$(/sbin/ifquery --list -a)
_ifaces=$(/sbin/ifquery --list -a)
for i in $IFACES; do
/sbin/ifup "${i}"
for _iface in $_ifaces; do
/sbin/ifup "${_iface}"
done
;;
*)
@ -106,7 +109,8 @@ start_network() {
esac
}
reload_network() {
reload_network()
{
stop_network
start_network
}
@ -115,128 +119,164 @@ reload_network() {
# helper functions
#
# TODO: remove global variables and get rid off exports
#
# to satisfy shellcheck SC2154:
export os_id
export ip
export network
export mask
export cidr
export dhcp
export ip6
export ip6_prefix_length
export ip6_ula
export dhcp6
export mac
export dev
export mtu
export gateway
export metric
export dns
export search_domains
export gateway6
export external
export detach
export all_nameservers
export all_search_domains
gen_iface_conf()
{
cat <<EOT
iface $DEV inet static
address $IP
network $NETWORK
netmask $MASK
iface ${dev} inet static
address ${ip}
network ${network}
netmask ${mask}
EOT
if [ -n "$MTU" ]; then
echo " mtu $MTU"
if [ -n "$mtu" ]; then
echo " mtu ${mtu}"
fi
if [ -n "$GATEWAY" ]; then
echo " gateway $GATEWAY"
if [ -n "$gateway" ]; then
echo " gateway ${gateway}"
if [ -n "$METRIC" ]; then
echo " metric $METRIC"
if [ -n "$metric" ]; then
echo " metric ${metric}"
fi
fi
echo ""
}
gen_dhcp_conf()
{
echo "iface ${dev} inet dhcp"
}
gen_alias_conf()
{
cat <<EOT
iface $DEV inet static
address $IP
network $NETWORK
netmask $MASK
iface ${dev} inet static
address ${ip}
network ${network}
netmask ${mask}
EOT
echo ""
}
gen_alias6_conf()
gen_iface6_conf()
{
case "${OS_ID}" in
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
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}
iface ${dev} inet6 static
address ${ip6}
netmask ${ip6_prefix_length:-64}
autoconf 0
accept_ra 0
EOT
;;
esac
if [ -n "$IP6_ULA" ]; then
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
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_iface6_conf()
gen_dhcp6_conf()
{
case "${OS_ID}" in
echo "iface ${dev} inet6 dhcp"
}
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
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}
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
if [ -n "${ip6_ula}" ]; then
cat <<EOT
iface $DEV inet6 static
address $IP6_ULA
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 ""
@ -250,57 +290,40 @@ iface lo inet loopback
EOT
INTERFACE_MAC=$(get_interface_mac)
CONTEXT_INTERFACES=$(get_context_interfaces)
_context_interfaces=$(get_context_interfaces)
for iface in $CONTEXT_INTERFACES; do
MAC=$(get_iface_var "$iface" "MAC")
DEV=$(get_dev "$INTERFACE_MAC" "$MAC")
for _iface in $_context_interfaces; do
setup_iface_vars "$_iface"
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")
skip_interface && continue
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")
echo "auto ${dev}"
[ -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")
if is_true "${dhcp}" ; then
gen_dhcp_conf
elif [ -n "${ip}" ] ; then
gen_iface_conf
fi
for nic_alias in $ALIASES ; do
IP=$(get_ip "$nic_alias")
NETWORK=$(get_network "$nic_alias")
MASK=$(get_mask "$nic_alias")
if is_true "${dhcp6}" ; then
gen_dhcp6_conf
elif [ -n "${ip6}" ] ; then
gen_iface6_conf
fi
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")
_aliases=$(get_interface_alias "$_iface")
EXTERNAL=$(get_iface_var "$nic_alias" "EXTERNAL")
EXTERNAL=${EXTERNAL^^}
DETACH=$(get_iface_var "$nic_alias" "DETACH")
for _nic_alias in $_aliases ; do
setup_ipadr_vars "$_nic_alias"
setup_ip6adr_vars "$_nic_alias"
setup_alias_vars "$_nic_alias"
if [ -z "${DETACH}" ]; then
if [ -z "${EXTERNAL}" ] || [ "${EXTERNAL}" = "NO" ]; then
[ -n "${IP}" ] && gen_alias_conf
[ -n "${IP6}" ] && gen_alias6_conf
if [ -z "${detach}" ]; then
if ! is_true "${external}" ; then
[ -n "${ip}" ] && gen_alias_conf
[ -n "${ip6}" ] && gen_alias6_conf
fi
fi
done
done
}

@ -0,0 +1,220 @@
#!/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()
{
command -v netplan >/dev/null
return $?
}
configure_network()
{
gen_resolvconf
gen_network_configuration > /etc/netplan/00-opennebula-generated-network.yaml
netplan generate
}
stop_network()
{
service networking stop
}
start_network()
{
service networking start
netplan generate
netplan apply
}
reload_network()
{
netplan generate
netplan apply
}
#
# helper functions
#
# TODO: remove global variables and get rid off exports
#
# to satisfy shellcheck SC2154:
export os_id
export ip
export network
export mask
export cidr
export dhcp
export ip6
export ip6_prefix_length
export ip6_ula
export dhcp6
export mac
export dev
export mtu
export gateway
export metric
export dns
export search_domains
export gateway6
export external
export detach
export all_nameservers
export all_search_domains
gen_addresses()
{
echo " addresses:"
if [ -n "${ip}" ] ; then
gen_addr_conf
fi
if [ -n "${ip6}" ] ; then
gen_addr6_conf
fi
_aliases=$(get_interface_alias "$_iface")
for _nic_alias in $_aliases; do
setup_ipadr_vars "$_nic_alias"
setup_ip6adr_vars "$_nic_alias"
setup_alias_vars "$_nic_alias"
if [ -z "${detach}" ]; then
if ! is_true "${external}" ; then
[ -n "${ip}" ] && gen_addr_conf
[ -n "${ip6}" ] && gen_addr6_conf
fi
fi
done
}
gen_routes()
{
echo " routes:"
if [ -n "${gateway}" ] ; then
cat <<EOT
- to: "0.0.0.0/0"
via: ${gateway}
EOT
if [ -n "${metric}" ] ; then
echo " metric: ${metric}"
fi
fi
if [ -n "${gateway6}" ] ; then
cat <<EOT
- to: "::/0"
via: ${gateway6}
EOT
if [ -n "${metric}" ] ; then
echo " metric: ${metric}"
fi
fi
}
gen_dhcp_conf()
{
cat <<EOT
dhcp4: true
EOT
}
gen_addr_conf()
{
echo " - ${ip}/${cidr}"
}
gen_addr6_conf()
{
echo " - ${ip6}/${ip6_prefix_length:-64}"
if [ -n "$ip6_ula" ]; then
echo " - ${ip6_ula}/64"
fi
}
gen_dhcp6_conf()
{
cat <<EOT
dhcp6: true
EOT
}
init_netplan_renderer()
{
if [ -z "${NETCFG_NETPLAN_RENDERER}" ] ; then
if command -v networkctl >/dev/null ; then
NETCFG_NETPLAN_RENDERER='networkd'
elif command -v nmcli >/dev/null ; then
NETCFG_NETPLAN_RENDERER='NetworkManager'
fi
fi
}
gen_network_configuration()
{
init_netplan_renderer
cat <<EOT
network:
version: 2
renderer: ${NETCFG_NETPLAN_RENDERER:-networkd}
EOT
# ethernets key must have at least one interface
_ethernets_written=
_context_interfaces=$(get_context_interfaces)
for _iface in $_context_interfaces; do
setup_iface_vars "$_iface"
skip_interface && continue
if [ -z "${_ethernets_written}" ] ; then
echo " ethernets:"
_ethernets_written=yes
fi
echo " ${dev}:"
if [ -n "$mtu" ]; then
echo " mtu: ${mtu}"
fi
if is_true "${dhcp}" ; then
gen_dhcp_conf
fi
if is_true "${dhcp6}" ; then
gen_dhcp6_conf
fi
gen_addresses
gen_routes
done
}

@ -22,35 +22,29 @@
is_network_supported()
{
case "${OS_ID}" in
altlinux)
return 0
;;
fedora|centos|rhel|almalinux|ol|rocky)
return 0
;;
opensuse*)
return 0
;;
esac
return 1
command -v networkctl >/dev/null
return $?
}
configure_network()
{
gen_resolvconf
gen_network_configuration
}
stop_network() {
stop_network()
{
systemctl stop systemd-networkd.service
}
start_network() {
start_network()
{
systemctl start systemd-networkd.service
}
reload_network() {
reload_network()
{
systemctl restart systemd-networkd.service
}
@ -58,41 +52,48 @@ reload_network() {
# helper functions
#
gen_iface_conf() {
# TODO: remove global variables and get rid off exports
#
# to satisfy shellcheck SC2154:
export os_id
export ip
export network
export mask
export cidr
export dhcp
export ip6
export ip6_prefix_length
export ip6_ula
export dhcp6
export mac
export dev
export mtu
export gateway
export metric
export dns
export search_domains
export gateway6
export external
export detach
export all_nameservers
export all_search_domains
gen_iface_conf()
{
cat <<EOT
[Network]
Address=${IP}/${CIDR}
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"
if [ -n "$dns" ]; then
for _domain in $dns; do
echo "DNS=${_domain}"
done
fi
if [ -n "$SEARCH_DOMAIN" ]; then
for domain in $SEARCH_DOMAIN; do
echo "Domains=$domain"
if [ -n "$search_domains" ]; then
for _search_domain in $search_domains; do
echo "Domains=${_search_domain}"
done
fi
@ -100,55 +101,51 @@ EOT
[Route]
EOT
if [ -n "$GATEWAY" ]; then
echo "Gateway=$GATEWAY"
if [ -n "$gateway" ]; then
echo "Gateway=${gateway}"
if [ -n "$METRIC" ]; then
echo "Metric=$METRIC"
if [ -n "$metric" ]; then
echo "Metric=${metric}"
fi
fi
echo ""
}
gen_iface6_conf() {
gen_dhcp_conf()
{
cat <<EOT
[Network]
Address=${IP6}/${IP6_PREFIX_LENGTH:-64}
DHCP=ipv4
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")
gen_alias_conf()
{
cat <<EOT
[Address]
Address=${ip}/${cidr}
EOT
}
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
gen_iface6_conf()
{
cat <<EOT
[Network]
Address=${ip6}/${ip6_prefix_length:-64}
EOT
echo "IPv6AcceptRA=false"
if [ -n "$DNS" ]; then
for dns_server in $DNS; do
echo "DNS=$dns_server"
if [ -n "$dns" ]; then
for _domain in $dns; do
echo "DNS=${_domain}"
done
fi
if [ -n "$SEARCH_DOMAIN" ]; then
for domain in $SEARCH_DOMAIN; do
echo "Domains=$domain"
if [ -n "$search_domains" ]; then
for _search_domain in $search_domains; do
echo "Domains=${_search_domain}"
done
fi
@ -156,64 +153,87 @@ EOT
[Route]
EOT
if [ -n "$GATEWAY6" ]; then
echo "Gateway=$GATEWAY6"
if [ -n "$gateway6" ]; then
echo "Gateway=${gateway6}"
fi
if [ -n "$IP6_ULA" ]; then
if [ -n "$ip6_ula" ]; then
cat <<EOT
[Network]
Address=$IP6_ULA/64
Address=${ip6_ula}/64
EOT
fi
echo ""
}
gen_dhcp6_conf()
{
cat <<EOT
[Network]
DHCP=ipv6
IPv6AcceptRA=true
EOT
}
gen_alias6_conf()
{
cat <<EOT
[Address]
Address=${ip6}/${ip6_prefix_length:-64}
EOT
}
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
_context_interfaces=$(get_context_interfaces)
for _iface in $_context_interfaces; do
setup_iface_vars "$_iface"
skip_interface && continue
{
cat <<EOT
[Match]
Name=$DEV
Name=${dev}
EOT
if [ -n "$MTU" ]; then
cat <<EOT
if [ -n "$mtu" ]; then
cat <<EOT
[Link]
MTUBytes=$MTU
MTUBytes=${mtu}
EOT
fi
fi
[ -n "${IP}" ] && gen_iface_conf
[ -n "${IP6}" ] && gen_iface6_conf
if is_true "${dhcp}" ; then
gen_dhcp_conf
elif [ -n "${ip}" ] ; then
gen_iface_conf
fi
} > "/etc/systemd/network/${DEV}.network"
if is_true "${dhcp6}" ; then
gen_dhcp6_conf
elif [ -n "${ip6}" ] ; then
gen_iface6_conf
fi
_aliases=$(get_interface_alias "$_iface")
for _nic_alias in $_aliases ; do
setup_ipadr_vars "$_nic_alias"
setup_ip6adr_vars "$_nic_alias"
setup_alias_vars "$_nic_alias"
if [ -z "${detach}" ]; then
if ! is_true "${external}" ; then
[ -n "${ip}" ] && gen_alias_conf
[ -n "${ip6}" ] && gen_alias6_conf
fi
fi
done
} > "/etc/systemd/network/${dev}.network"
done
}

@ -0,0 +1,223 @@
#!/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
#
export required_context_type=online
is_network_supported()
{
command -v nmcli >/dev/null
return $?
}
configure_network()
{
wait_for_nm
gen_resolvconf
gen_network_configuration
# this enables sensible default for 'unmanaged-devices'
mkdir -p /etc/NetworkManager/conf.d
touch /etc/NetworkManager/conf.d/10-globally-managed-devices.conf
}
stop_network()
{
service NetworkManager stop
}
start_network()
{
service NetworkManager start
}
reload_network()
{
nmcli connection reload
}
#
# helper functions
#
# TODO: remove global variables and get rid off exports
#
# to satisfy shellcheck SC2154:
export os_id
export ip
export network
export mask
export cidr
export dhcp
export ip6
export ip6_prefix_length
export ip6_ula
export dhcp6
export mac
export dev
export mtu
export gateway
export metric
export dns
export search_domains
export gateway6
export external
export detach
export all_nameservers
export all_search_domains
wait_for_nm()
{
_timeout=30
while [ "$_timeout" -gt 0 ] ; do
if _nm_networking=$(nmcli networking 2>/dev/null) ; then
break
fi
_timeout=$(( _timeout - 1 ))
sleep 1
done
if [ "${_timeout}" -eq 0 ] ; then
echo "ERROR [!]: NetworkManager is not running" >&2
exit 1
elif [ "${_nm_networking}" = 'enabled' ] ; then
return 0
else
echo "ERROR [!]: NetworkManager is disabled" >&2
exit 1
fi
}
gen_iface_conf()
{
nmcli con mod "${dev}" ipv4.method manual ipv4.addr "${ip}/${cidr}"
if [ -n "$gateway" ]; then
nmcli con mod "${dev}" ipv4.gateway "${gateway}"
fi
if [ -n "$metric" ]; then
nmcli con mod "${dev}" ipv4.route-metric "${metric}"
fi
}
gen_dhcp_conf()
{
nmcli con mod "${dev}" ipv4.method auto
}
gen_alias_conf()
{
nmcli con mod "${dev}" +ipv4.addr "${ip}/${cidr}"
}
gen_iface6_conf()
{
nmcli con mod "${dev}" ipv6.method manual \
ipv6.addr "${ip6}/${ip6_prefix_length:-64}"
if [ -n "$ip6_ula" ]; then
nmcli con mod "${dev}" +ipv6.addr "${ip6_ula}/64"
fi
if [ -n "$gateway6" ]; then
nmcli con mod "${dev}" ipv6.gateway "${gateway6}"
fi
if [ -n "$metric" ]; then
nmcli con mod "${dev}" ipv6.route-metric "${metric}"
fi
}
gen_alias6_conf()
{
nmcli con mod "${dev}" +ipv6.addr "${ip6}/${ip6_prefix_length:-64}"
if [ -n "$ip6_ula" ]; then
nmcli con mod "${dev}" +ipv6.addr "${ip6_ula}/64"
fi
}
gen_dhcp6_conf()
{
nmcli con mod "${dev}" ipv6.method dhcp
}
# arg: <interface-connection>
nm_connection_exist()
(
_iface=$(nmcli --field connection.interface-name con show "$1" | awk '{print $2}')
if [ "${_iface}" = "$1" ] ; then
return 0
fi
return 1
)
gen_network_configuration()
{
_context_interfaces=$(get_context_interfaces)
for _iface in $_context_interfaces; do
setup_iface_vars "$_iface"
skip_interface && continue
if ! nm_connection_exist "${dev}" ; then
nmcli con add type ethernet con-name "${dev}" ifname "${dev}"
fi
nmcli con mod "${dev}" connection.autoconnect yes
if [ -n "$mtu" ]; then
nmcli con mod "${dev}" ethernet.mtu "${mtu}"
fi
if is_true "${dhcp}" ; then
gen_dhcp_conf
elif [ -n "${ip}" ] ; then
gen_iface_conf
fi
if is_true "${dhcp6}" ; then
gen_dhcp6_conf
elif [ -n "${ip6}" ] ; then
gen_iface6_conf
fi
_aliases=$(get_interface_alias "$_iface")
for _nic_alias in $_aliases; do
setup_ipadr_vars "$_nic_alias"
setup_ip6adr_vars "$_nic_alias"
setup_alias_vars "$_nic_alias"
if [ -z "${detach}" ]; then
if ! is_true "${external}" ; then
[ -n "${ip}" ] && gen_alias_conf
[ -n "${ip6}" ] && gen_alias6_conf
fi
fi
done
done
}

@ -22,32 +22,33 @@
is_network_supported()
{
case "${OS_ID}" in
fedora|centos|rhel|almalinux|ol|rocky)
return 0
;;
opensuse*)
return 0
;;
esac
if [ -e /etc/sysconfig/network-scripts/ifup ] || \
[ -e /etc/sysconfig/network/ifup ] ;
then
return 0
fi
return 1
}
configure_network()
{
gen_resolvconf
gen_network_configuration
}
stop_network() {
stop_network()
{
service network stop
}
start_network() {
start_network()
{
service network start
}
reload_network() {
reload_network()
{
service network restart
}
@ -55,163 +56,192 @@ reload_network() {
# helper functions
#
gen_iface_conf() {
# TODO: remove global variables and get rid off exports
#
# to satisfy shellcheck SC2154:
export os_id
export ip
export network
export mask
export cidr
export dhcp
export ip6
export ip6_prefix_length
export ip6_ula
export dhcp6
export mac
export dev
export mtu
export gateway
export metric
export dns
export search_domains
export gateway6
export external
export detach
export all_nameservers
export all_search_domains
gen_iface_conf()
{
cat <<EOT
NETMASK="$MASK"
IPADDR="$IP"
BOOTPROTO=none
NETMASK="${mask}"
IPADDR="${ip}"
EOT
if [ -n "$GATEWAY" ]; then
if [ "$CONFIG_PATH" = "/etc/sysconfig/network" ]; then
echo "default $GATEWAY - $DEV ${METRIC:+metric ${METRIC}}" \
>> "${CONFIG_PATH}/ifroute-${DEV}"
if [ -n "$gateway" ]; then
if [ "$config_path" = "/etc/sysconfig/network" ]; then
echo "default ${gateway} - ${dev} ${metric:+metric ${metric}}" \
>> "${config_path}/ifroute-${dev}"
else
echo "default via $GATEWAY dev $DEV ${METRIC:+metric ${METRIC}}" \
>> "${CONFIG_PATH}/route-${DEV}"
echo "default via ${gateway} dev ${dev} ${metric:+metric ${metric}}" \
>> "${config_path}/route-${dev}"
fi
fi
if [ -n "$MTU" ]; then
echo "MTU=$MTU"
if [ -n "$mtu" ]; then
echo "MTU=${mtu}"
fi
}
gen_dhcp_conf()
{
echo "BOOTPROTO=dhcp"
}
gen_alias_conf() {
cat <<EOT
IPADDR${ALIAS_NUM}="${IP}"
NETMASK${ALIAS_NUM}="${MASK}"
IPADDR${alias_num}="${ip}"
NETMASK${alias_num}="${mask}"
EOT
}
gen_alias6_conf() {
if [ "$CONFIG_PATH" = "/etc/sysconfig/network" ]; then
echo "IPADDR_A6A${ALIAS_NUM}=$IP6/${IP6_PREFIX_LENGTH:-64}"
else
IPV6ADDR_SECONDARIES="${IPV6ADDR_SECONDARIES} ${IP6}/${IP6_PREFIX_LENGTH:-64}"
fi
if [ -n "$IP6_ULA" ]; then
if [ "$CONFIG_PATH" = "/etc/sysconfig/network" ]; then
echo "IPADDR_A6B${ALIAS_NUM}=$IP6_ULA/64"
else
IPV6ADDR_SECONDARIES="${IPV6ADDR_SECONDARIES} ${IP6_ULA}/64"
fi
fi
}
gen_iface6_conf() {
if [ "$CONFIG_PATH" = "/etc/sysconfig/network" ]; then
echo "IPADDR_6A=$IP6/${IP6_PREFIX_LENGTH:-64}"
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
else
cat <<EOT
IPV6INIT=yes
IPV6ADDR=$IP6/${IP6_PREFIX_LENGTH:-64}
IPV6ADDR=${ip6}/${ip6_prefix_length:-64}
IPV6_AUTOCONF=no
EOT
fi
if [ -n "$IP6_ULA" ]; then
if [ "$CONFIG_PATH" = "/etc/sysconfig/network" ]; then
echo "IPADDR_6B=$IP6_ULA/64"
if [ -n "$ip6_ula" ]; then
if [ "$config_path" = "/etc/sysconfig/network" ]; then
echo "IPADDR_6B=${ip6_ula}/64"
else
IPV6ADDR_SECONDARIES="${IPV6ADDR_SECONDARIES} ${IP6_ULA}/64"
ipv6addr_secondaries="${ipv6addr_secondaries} ${ip6_ula}/64"
fi
fi
if [ -n "$GATEWAY6" ]; then
if [ "$CONFIG_PATH" = "/etc/sysconfig/network" ]; then
echo "default $GATEWAY6 - $DEV" >> "/etc/sysconfig/network/ifroute-${DEV}"
if [ -n "$gateway6" ]; then
if [ "$config_path" = "/etc/sysconfig/network" ]; then
echo "default ${gateway6} - ${dev}" >> "/etc/sysconfig/network/ifroute-${dev}"
else
echo "IPV6_DEFAULTGW=$GATEWAY6"
echo "IPV6_DEFAULTGW=${gateway6}"
fi
fi
if [ -n "$MTU" ]; then
echo "IPV6_MTU=$MTU"
if [ -n "${mtu}" ]; then
echo "IPV6_MTU=${mtu}"
fi
}
gen_dhcp6_conf()
{
cat <<EOT
IPV6INIT=yes
IPV6_AUTOCONF=no
DHCPV6C=yes
EOT
}
gen_alias6_conf()
{
if [ "$config_path" = "/etc/sysconfig/network" ]; then
echo "IPADDR_A6A${alias_num}=${ip6}/${ip6_prefix_length:-64}"
else
ipv6addr_secondaries="${ipv6addr_secondaries} ${ip6}/${ip6_prefix_length:-64}"
fi
if [ -n "$ip6_ula" ]; then
if [ "$config_path" = "/etc/sysconfig/network" ]; then
echo "IPADDR_A6B${alias_num}=${ip6_ula}/64"
else
ipv6addr_secondaries="${ipv6addr_secondaries} ${ip6_ula}/64"
fi
fi
}
gen_network_configuration()
{
if [ -d /etc/sysconfig/network-scripts ]; then
CONFIG_PATH=/etc/sysconfig/network-scripts
config_path=/etc/sysconfig/network-scripts
elif [ -d /etc/sysconfig/network ]; then
CONFIG_PATH=/etc/sysconfig/network
config_path=/etc/sysconfig/network
fi
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")
_context_interfaces=$(get_context_interfaces)
IP=$(get_ip "$iface")
MASK=$(get_mask "$iface")
MTU=$(get_mtu "$iface")
GATEWAY=$(get_gateway "$iface")
METRIC=$(get_iface_var "$iface" "METRIC")
for _iface in $_context_interfaces; do
setup_iface_vars "$_iface"
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")
skip_interface && continue
# cumulative variable
IPV6ADDR_SECONDARIES=''
[ -z "${IP}${IP6}" ] && continue
[ -z "${DEV}" ] && continue
ipv6addr_secondaries=''
{
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
BOOTPROTO=static
DEVICE=${dev}
NM_CONTROLLED=no
TYPE=Ethernet
EOT
if [ "$CONFIG_PATH" = "/etc/sysconfig/network" ]; then
if [ "$config_path" = "/etc/sysconfig/network" ]; then
echo "STARTMODE=auto"
else
echo "ONBOOT=yes"
fi
[ -n "${IP}" ] && gen_iface_conf
[ -n "${IP6}" ] && gen_iface6_conf
ALIASES=$(get_interface_alias "$iface")
ALIAS_NUM=0
if is_true "${dhcp}" ; then
gen_dhcp_conf
elif [ -n "${ip}" ] ; then
gen_iface_conf
fi
for nic_alias in $ALIASES; do
IP=$(get_ip "$nic_alias")
MASK=$(get_mask "$nic_alias")
if is_true "${dhcp6}" ; then
gen_dhcp6_conf
elif [ -n "${ip6}" ] ; then
gen_iface6_conf
fi
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")
_aliases=$(get_interface_alias "$_iface")
alias_num=0
EXTERNAL=$(get_iface_var "$nic_alias" "EXTERNAL")
EXTERNAL=${EXTERNAL^^}
DETACH=$(get_iface_var "$nic_alias" "DETACH")
for _nic_alias in $_aliases; do
setup_ipadr_vars "$_nic_alias"
setup_ip6adr_vars "$_nic_alias"
setup_alias_vars "$_nic_alias"
if [ -z "${DETACH}" ]; then
if [ -z "${EXTERNAL}" ] || [ "${EXTERNAL}" = "NO" ]; then
[ -n "${IP}" ] && gen_alias_conf
[ -n "${IP6}" ] && gen_alias6_conf
if [ -z "${detach}" ]; then
if ! is_true "${external}" ; then
[ -n "${ip}" ] && gen_alias_conf
[ -n "${ip6}" ] && gen_alias6_conf
if [ -n "${IP}${IP6}" ]; then
ALIAS_NUM=$((ALIAS_NUM + 1))
if [ -n "${ip}${ip6}" ]; then
alias_num=$((alias_num + 1))
fi
fi
fi
@ -219,14 +249,13 @@ EOT
# on Red Hats, we need just a single configuration
# entry with all additional IPv6 addresses
if [ -n "${IPV6ADDR_SECONDARIES}" ]; then
echo "IPV6ADDR_SECONDARIES='${IPV6ADDR_SECONDARIES## }'"
if [ -n "${ipv6addr_secondaries}" ]; then
echo "IPV6ADDR_SECONDARIES='${ipv6addr_secondaries## }'"
fi
} > "${CONFIG_PATH}/ifcfg-${DEV}"
} > "${config_path}/ifcfg-${dev}"
# TODO: do we want this here?
ifup "${DEV}"
ifup "${dev}"
done
}

@ -1,63 +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. #
#--------------------------------------------------------------------------- #
export DNS_VARIABLES="DNS $(env | sed 's/=.*$//' | grep -E '^ETH[0-9]+_DNS$' | sort)"
export SEARCH_VARIABLES="SEARCH_DOMAIN $(env | sed 's/=.*$//' | grep -E '^ETH[0-9]+_SEARCH_DOMAIN$' | sort)"
nameservers=$(
for var in ${DNS_VARIABLES}; do
value=$(eval "echo \"\${$var}\"")
if [ -n "$value" ]; then
echo "$value"
fi
done
)
searchdomains=$(
for var in ${SEARCH_VARIABLES}; do
value=$(eval "echo \"\${$var}\"")
if [ -n "$value" ]; then
echo "$value"
fi
done
)
[ -z "$nameservers" ] && exit 0
if [ -L /etc/resolv.conf ]; then
unlink /etc/resolv.conf
else
echo -n '' > /etc/resolv.conf
fi
for nameserver in $nameservers; do
echo nameserver $nameserver >> /etc/resolv.conf
done
if [ -f /etc/sysconfig/network/config ]; then
sed -i "/^NETCONFIG_DNS_STATIC_SERVERS=/ s/=.*$/=\"$nameservers\"/" /etc/sysconfig/network/config
fi
[ -z "$searchdomains" ] && exit 0
echo search $searchdomains >> /etc/resolv.conf
if [ -f /etc/sysconfig/network/config ]; then
sed -i "/^NETCONFIG_DNS_STATIC_SEARCHLIST=/ s/=.*$/=\"$searchdomains\"/" /etc/sysconfig/network/config
fi

@ -16,6 +16,11 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# THIS IS A CONTEXTUALIZATION GUARD
if [ "$1" != 'local' ] ; then
exit 0
fi
get_management_interfaces() {
env | grep -E "^ETH[0-9]+_VROUTER_MANAGEMENT=YES" | sed 's/_.*$//' | tr 'ETH' 'eth' | sort
}
@ -81,7 +86,7 @@ gen_footer
) > /etc/iptables/rules-save
if [ "$1" == "reconfigure" ]; then
if [ "$2" == "reconfigure" ]; then
reload_service
else
rc-update add iptables boot

@ -16,6 +16,11 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# THIS IS A CONTEXTUALIZATION GUARD
if [ "$1" != 'local' ] ; then
exit 0
fi
activate_swaps_linux() {
SWAP_DRIVES=$(blkid -t TYPE="swap" -o device)
for SWAP in $SWAP_DRIVES ; do

@ -16,6 +16,11 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# THIS IS A CONTEXTUALIZATION GUARD
if [ "$1" != 'local' ] ; then
exit 0
fi
if [ -z "${VROUTER_ID}${VROUTER_KEEPALIVED_ID}" ]; then
if [ -f /etc/sysctl.d/01-one.conf ]; then
unlink /etc/sysctl.d/01-one.conf

@ -16,6 +16,11 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# THIS IS A CONTEXTUALIZATION GUARD
if [ "$1" != 'local' ] ; then
exit 0
fi
get_context_interfaces() {
env | grep -E "^ETH[0-9]+_VROUTER_IP=" | sed 's/_.*$//' | sort
}
@ -181,7 +186,7 @@ gen_instances
) > /etc/keepalived/keepalived.conf
if [ "$1" == "reconfigure" ]; then
if [ "$2" == "reconfigure" ]; then
reload_service
else
rc-update add keepalived boot

@ -16,6 +16,11 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# THIS IS A CONTEXTUALIZATION GUARD
if [ "$1" != 'local' ] ; then
exit 0
fi
ENV_FILE=/var/run/one-context/one_env
MOUNT_DIR="${MOUNT_DIR:-/mnt}"
TOKEN_FILE="${MOUNT_DIR}/token.txt"

@ -16,6 +16,11 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# THIS IS A CONTEXTUALIZATION GUARD
if [ "$1" != 'local' ] ; then
exit 0
fi
# defaults
USERNAME=${USERNAME:-root}
USERNAME_SUDO=${USERNAME_SUDO:-${GRANT_SUDO:-YES}}

@ -16,6 +16,11 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# THIS IS A CONTEXTUALIZATION GUARD
if [ "$1" != 'local' ] ; then
exit 0
fi
[ -z "${SSH_PUBLIC_KEY}${EC2_PUBLIC_KEY}" ] && exit 0
if [ -z "${USERNAME}" ]

@ -16,6 +16,11 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# THIS IS A CONTEXTUALIZATION GUARD
if [ "$1" != 'local' ] ; then
exit 0
fi
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
if ssh-keygen -A; then
if type restorecon >/dev/null 2>&1; then

@ -16,6 +16,11 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# THIS IS A CONTEXTUALIZATION GUARD
if [ "$1" != 'local' ] ; then
exit 0
fi
# Linux
for _dev_tty in $(find /dev -type c -name 'tty[0-9]*'); do
TERM=linux setterm -blank 0 -powerdown 0 >>"${_dev_tty}"

@ -16,6 +16,11 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# THIS IS A CONTEXTUALIZATION GUARD
if [ "$1" != 'local' ] ; then
exit 0
fi
_pam_file="/etc/pam.d/login"
if [ ! -f "${_pam_file}" ]; then
exit 0

@ -1,8 +1,8 @@
[Unit]
Description=OpenNebula pre-networking contextualization script
DefaultDependencies=no
Before=network-pre.target
Wants=network-pre.target local-fs.target systemd-udev-settle.service syslog.target
Before=network-pre.target
After=local-fs.target systemd-udev-settle.service syslog.target
ConditionPathExists=!/var/run/one-context/context.sh.local

@ -1,7 +1,7 @@
[Unit]
Description=OpenNebula pre-networking contextualization script
Before=network-pre.target
Wants=network-pre.target local-fs.target systemd-udev-settle.service syslog.target
Before=network-pre.target
After=local-fs.target systemd-udev-settle.service syslog.target
ConditionPathExists=!/var/run/one-context/context.sh.local

@ -0,0 +1,17 @@
[Unit]
Description=OpenNebula early-networking contextualization script
Wants=one-context-local.service
Wants=NetworkManager.service
After=one-context-local.service
After=NetworkManager.service
Before=network-online.target
Before=NetworkManager-wait-online.service
ConditionPathExists=!/var/run/one-context/context.sh.online
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/sbin/one-contextd online
[Install]
WantedBy=network-online.target

@ -18,8 +18,8 @@
set -e
TYPE="${1:-all}" # local, network, all
COMMAND="${2}" # reconfigure?
TYPE="${1:-all}" # local, online, network, all
COMMAND="${2}" # force, reconfigure?
SCRIPTS_DIR="${SCRIPTS_DIR:-/etc/one-context.d}"
RUNTIME_DIR="${RUNTIME_DIR:-/var/run/one-context}"
@ -93,38 +93,36 @@ function export_rc_vars
}
function execute_scripts {
local _type=$1
local _command=$2
local _type="$1"
local _command="$2"
# choose
if [ "${_type}" = 'local' ]; then
local _scripts=$(find "${SCRIPTS_DIR}" \
-maxdepth 1 -mindepth 1 \
-name 'loc-*' \
2>/dev/null)
elif [ "${_type}" = 'network' ]; then
local _scripts=$(find "${SCRIPTS_DIR}" \
-maxdepth 1 -mindepth 1 \
\! \( -name 'net-*' -o -name 'loc-*' \) \
-o -name 'net-*' \
2>/dev/null)
fi
case "${_type}" in
local|online)
local _scripts=$(find "${SCRIPTS_DIR}" \
-maxdepth 1 -mindepth 1 -type f \
-name 'loc-*' \
2>/dev/null | sort)
;;
network)
local _scripts=$(find "${SCRIPTS_DIR}" \
-maxdepth 1 -mindepth 1 -type f \
\! \( -name 'net-*' -o -name 'loc-*' \) \
-o -name 'net-*' \
2>/dev/null | sort)
;;
esac
export MOUNT_DIR
export RUNTIME_DIR
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
log debug "Script ${_name}: Starting ..."
_out=$("${_script}" "${_command}" 2>&1)
_out=$("${_script}" "${_type}" "${_command}" 2>&1)
local _rtn=$?
# log on any output
@ -346,7 +344,7 @@ function cleanup {
#####
if ! [[ ${TYPE} =~ ^(local|network|all)$ ]]; then
if ! [[ ${TYPE} =~ ^(local|online|network|all)$ ]]; then
log err "Error: Invalid or missing execution type ${TYPE}" 2
exit 1
fi
@ -367,6 +365,7 @@ get_new_context
if [ "${TYPE}" = 'all' ]; then
run_context 'local' "${COMMAND}"
run_context 'online' "${COMMAND}"
run_context 'network' "${COMMAND}"
else
run_context "${TYPE}" "${COMMAND}"

@ -37,7 +37,7 @@ case "${TARGET}" in
NAME=${NAME:-one-context}
RELSUFFIX=${RELSUFFIX:-.el6}
TYPE=${TYPE:-rpm}
TAGS=${TAGS:-rpm sysv one network-scripts}
TAGS=${TAGS:-linux rpm sysv one network-scripts}
DEPENDS=${DEPENDS:-util-linux-ng bash curl bind-utils cloud-utils-growpart dracut-modules-growroot parted ruby rubygem-json sudo shadow-utils openssh-server open-vm-tools qemu-guest-agent gawk}
PROVIDES=${PROVIDES:-}
REPLACES=${REPLACES:-cloud-init}
@ -48,27 +48,11 @@ case "${TARGET}" in
POSTUP=${POSTUP:-pkg/postupgrade}
;;
'el6_ec2')
NAME=${NAME:-one-context-ec2}
RELSUFFIX=${RELSUFFIX:-.el6}
TYPE=${TYPE:-rpm}
TAGS=${TAGS:-rpm sysv ec2 network-scripts}
DEPENDS=${DEPENDS:-util-linux-ng bash curl bind-utils cloud-utils-growpart dracut-modules-growroot parted ruby rubygem-json sudo shadow-utils openssh-server gawk}
PROVIDES=${PROVIDES:-}
REPLACES=${REPLACES:-cloud-init}
CONFLICTS=${CONFLICTS:-${REPLACES} one-context}
POSTIN=${POSTINST:-pkg/postinstall.ec2 pkg/postinstall}
PREUN=${PREUN:-pkg/preuninstall.ec2 pkg/preuninstall}
POSTUN=${POSTUN:-pkg/postuninstall}
POSTUP=${POSTUP:-pkg/postupgrade}
;;
'el7')
NAME=${NAME:-one-context}
RELSUFFIX=${RELSUFFIX:-.el7}
TYPE=${TYPE:-rpm}
TAGS=${TAGS:-rpm systemd one network-scripts}
TAGS=${TAGS:-linux rpm systemd one network-scripts}
DEPENDS=${DEPENDS:-util-linux bash curl bind-utils cloud-utils-growpart parted ruby rubygem-json sudo shadow-utils openssh-server open-vm-tools qemu-guest-agent gawk}
PROVIDES=${PROVIDES:-}
REPLACES=${REPLACES:-cloud-init}
@ -79,26 +63,11 @@ case "${TARGET}" in
POSTUP=${POSTUP:-pkg/postupgrade}
;;
'el7_ec2')
NAME=${NAME:-one-context-ec2}
RELSUFFIX=${RELSUFFIX:-.el7}
TYPE=${TYPE:-rpm}
TAGS=${TAGS:-rpm systemd ec2 network-scripts}
DEPENDS=${DEPENDS:-util-linux bash curl bind-utils cloud-utils-growpart parted ruby rubygem-json sudo shadow-utils openssh-server gawk}
PROVIDES=${PROVIDES:-}
REPLACES=${REPLACES:-cloud-init}
CONFLICTS=${CONFLICTS:-${REPLACES} one-context}
POSTIN=${POSTINST:-pkg/postinstall.ec2 pkg/postinstall}
PREUN=${PREUN:-pkg/preuninstall.ec2 pkg/preuninstall}
POSTUN=${POSTUN:-pkg/postuninstall}
POSTUP=${POSTUP:-pkg/postupgrade}
;;
'el8')
NAME=${NAME:-one-context}
RELSUFFIX=${RELSUFFIX:-.el8}
TYPE=${TYPE:-rpm}
TAGS=${TAGS:-rpm systemd one network-scripts}
TAGS=${TAGS:-linux rpm systemd one network-scripts}
DEPENDS=${DEPENDS:-util-linux bash curl bind-utils cloud-utils-growpart parted ruby rubygem-json sudo shadow-utils openssh-server open-vm-tools qemu-guest-agent network-scripts gawk}
PROVIDES=${PROVIDES:-}
REPLACES=${REPLACES:-cloud-init}
@ -109,26 +78,11 @@ case "${TARGET}" in
POSTUP=${POSTUP:-pkg/postupgrade}
;;
'el8_ec2')
NAME=${NAME:-one-context-ec2}
RELSUFFIX=${RELSUFFIX:-.el8}
TYPE=${TYPE:-rpm}
TAGS=${TAGS:-rpm systemd ec2 network-scripts}
DEPENDS=${DEPENDS:-util-linux bash curl bind-utils cloud-utils-growpart parted ruby rubygem-json sudo shadow-utils openssh-server network-scripts gawk}
PROVIDES=${PROVIDES:-}
REPLACES=${REPLACES:-cloud-init}
CONFLICTS=${CONFLICTS:-${REPLACES} one-context}
POSTIN=${POSTINST:-pkg/postinstall.ec2 pkg/postinstall}
PREUN=${PREUN:-pkg/preuninstall.ec2 pkg/preuninstall}
POSTUN=${POSTUN:-pkg/postuninstall}
POSTUP=${POSTUP:-pkg/postupgrade}
;;
'alt')
NAME=${NAME:-one-context}
RELSUFFIX=${RELSUFFIX:-alt}
TYPE=${TYPE:-rpm}
TAGS=${TAGS:-rpm systemd one networkd}
TAGS=${TAGS:-linux rpm systemd one networkd}
DEPENDS=${DEPENDS:-bind-utils btrfs-progs cloud-utils-growpart curl e2fsprogs iproute2 openssl parted passwd qemu-guest-agent open-vm-tools ruby-json-pure sudo systemd-services wget which xfsprogs gawk}
PROVIDES=${PROVIDES:-}
REPLACES=${REPLACES:-cloud-init}
@ -144,7 +98,7 @@ case "${TARGET}" in
NAME=${NAME:-one-context}
RELSUFFIX=${RELSUFFIX:-.suse}
TYPE=${TYPE:-rpm}
TAGS=${TAGS:-rpm systemd one network-scripts}
TAGS=${TAGS:-linux rpm systemd one network-scripts}
DEPENDS=${DEPENDS:-util-linux bash curl bind-utils growpart parted parted ruby sudo shadow openssh open-vm-tools qemu-guest-agent gawk} # rubygem-json}
PROVIDES=${PROVIDES:-}
REPLACES=${REPLACES:-cloud-init cloud-init-config-suse}
@ -155,27 +109,11 @@ case "${TARGET}" in
POSTUP=${POSTUP:-pkg/postupgrade}
;;
'suse_ec2')
NAME=${NAME:-one-context-ec2}
RELSUFFIX=${RELSUFFIX:-.suse}
TYPE=${TYPE:-rpm}
TAGS=${TAGS:-rpm systemd ec2 network-scripts}
DEPENDS=${DEPENDS:-util-linux bash curl bind-utils growpart parted ruby sudo shadow openssh gawk} # rubygem-json}
PROVIDES=${PROVIDES:-}
REPLACES=${REPLACES:-cloud-init cloud-init-config-suse}
CONFLICTS=${CONFLICTS:-${REPLACES} one-context}
POSTIN=${POSTINST:-pkg/postinstall.ec2 pkg/postinstall}
PREUN=${PREUN:-pkg/preuninstall.ec2 pkg/preuninstall}
POSTUN=${POSTUN:-pkg/postuninstall}
POSTUP=${POSTUP:-pkg/postupgrade}
;;
'deb')
NAME=${NAME:-one-context}
RELSUFFIX=${RELSUFFIX:-}
TYPE=${TYPE:-deb}
TAGS=${TAGS:-deb sysv systemd upstart one}
TAGS=${TAGS:-linux deb sysv systemd upstart one}
DEPENDS=${DEPENDS:-util-linux bash curl bind9-host cloud-utils parted ruby ifupdown|ifupdown2 acpid sudo passwd dbus openssh-server open-vm-tools qemu-guest-agent gawk}
PROVIDES=${PROVIDES:-}
REPLACES=${REPLACES:-cloud-init}
@ -186,26 +124,11 @@ case "${TARGET}" in
POSTUP=${POSTUP:-pkg/postupgrade}
;;
'deb_ec2')
NAME=${NAME:-one-context-ec2}
RELSUFFIX=${RELSUFFIX:-}
TYPE=${TYPE:-deb}
TAGS=${TAGS:-deb sysv systemd upstart ec2}
DEPENDS=${DEPENDS:-util-linux bash curl bind9-host cloud-utils parted ruby ifupdown|ifupdown2 sudo passwd dbus openssh-server resolvconf gawk}
PROVIDES=${PROVIDES:-}
REPLACES=${REPLACES:-cloud-init}
CONFLICTS=${CONFLICTS:-${REPLACES} one-context}
POSTIN=${POSTINST:-pkg/postinstall.ec2 pkg/postinstall}
PREUN=${PREUN:-pkg/preuninstall.ec2 pkg/preuninstall}
POSTUN=${POSTUN:-pkg/postuninstall}
POSTUP=${POSTUP:-pkg/postupgrade}
;;
'alpine')
NAME=${NAME:-one-context}
RELSUFFIX=${RELSUFFIX:-}
TYPE=${TYPE:-apk}
TAGS=${TAGS:-apk one}
TAGS=${TAGS:-linux apk one}
DEPENDS=${DEPENDS:-util-linux bash curl udev sfdisk parted e2fsprogs-extra sudo shadow ruby ruby-json bind-tools openssh open-vm-tools qemu-guest-agent gawk}
PROVIDES=${PROVIDES:-}
REPLACES=${REPLACES:-} #not respected
@ -216,21 +139,6 @@ case "${TARGET}" in
POSTUP=${POSTUP:-} # FPM 1.9.3 bug: https://github.com/jordansissel/fpm/blob/v1.9.3/lib/fpm/package/apk.rb#L149
;;
'alpine_ec2')
NAME=${NAME:-one-context-ec2}
RELSUFFIX=${RELSUFFIX:-}
TYPE=${TYPE:-apk}
TAGS=${TAGS:-apk ec2}
DEPENDS=${DEPENDS:-util-linux bash curl udev sfdisk parted e2fsprogs-extra sudo shadow ruby ruby-json bind-tools openssh gawk}
PROVIDES=${PROVIDES:-}
REPLACES=${REPLACES:-} #not respected
CONFLICTS=${CONFLICTS:-one-context}
POSTIN=${POSTINST:-pkg/postinstall.ec2 pkg/postinstall}
PREUN=${PREUN:-pkg/preuninstall.ec2 pkg/preuninstall}
POSTUN=${POSTUN:-pkg/postuninstall}
POSTUP=${POSTUP:-} # FPM 1.9.3 bug: https://github.com/jordansissel/fpm/blob/v1.9.3/lib/fpm/package/apk.rb#L149
;;
'iso')
NAME=${NAME:-one-context-linux}
TYPE=${TYPE:-iso}
@ -241,7 +149,7 @@ case "${TARGET}" in
RELSUFFIX=${RELSUFFIX:-}
TYPE=${TYPE:-pacman}
EXT=${EXT:-pkg.tar.xz}
TAGS=${TAGS:-arch systemd one networkd}
TAGS=${TAGS:-linux arch systemd one networkd}
# mkinitcpio-growrootfs ruby-json
DEPENDS=${DEPENDS:-filesystem util-linux bash curl bind-tools ruby sudo shadow open-vm-tools qemu-guest-agent gawk}
PROVIDES=${PROVIDES:-}

Loading…
Cancel
Save