F #227: Refactor loc-10-network
Deprecate EC2 Introduce one-context-online service Support NetPlan, NetworkManager Various fixes DHCP WIPpull/244/head
parent
688c04e14c
commit
914ceae299
@ -1,4 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
NETWORK_EC2=yes
|
||||
SERVICES='one-context'
|
@ -1,3 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
SERVICES='one-context'
|
@ -0,0 +1,3 @@
|
||||
[main]
|
||||
no-auto-default=*
|
||||
dns=none
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
@ -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
|
Loading…
Reference in new issue