195 lines
6.5 KiB
Bash
195 lines
6.5 KiB
Bash
# ------------------------------------------------------------------------------
|
|
# Configure network
|
|
# This file is part of the init.sh project
|
|
# Copyright (c) 2019-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
|
# ------------------------------------------------------------------------------
|
|
# This file is distributed under 3-clause BSD license.
|
|
# The complete license agreement can be obtained at:
|
|
# https://opensource.org/licenses/BSD-3-Clause
|
|
# ------------------------------------------------------------------------------
|
|
# Variables:
|
|
# * IPV4_IFACES: list of the interfaces with IPv4 configuration
|
|
# * IPV6_IFACES: list of the interfaces with IPv6 configuration
|
|
# * NET{4,6}_MODE_$iface: mode of configuration for $iface: static, manual or dhcp
|
|
# - In static mode only:
|
|
# * NET{4,6}_IP_$iface: IP address of the interface $iface
|
|
# * NET{4,6}_GW_$iface: Gateway of the interface $iface
|
|
# * NET{4,6}_NS_$iface: Name servers of the interface $iface
|
|
# * NET{4,6}_NS_SEARCH_$iface: Domain name research the interface $iface
|
|
# - In manual mode only
|
|
# * NET{4,6}_MANUAL_FILE_$iface: filename for manual configuration of $iface
|
|
# ------------------------------------------------------------------------------
|
|
|
|
export VER_conf_network="0.0.8"
|
|
export DEP_conf_network=""
|
|
|
|
conf_network()
|
|
{
|
|
local if_file="/etc/network/interfaces"
|
|
backup_dist $if_file
|
|
|
|
# The interfaces header contain loopback interface declaration
|
|
install_file interfaces.head $if_file
|
|
tag_file $if_file
|
|
|
|
# First configure IPv4 ifaces
|
|
local iface=
|
|
for iface in $IPV4_IFACES; do
|
|
echo -e "\n# --------------------------------\n" >> $if_file
|
|
|
|
if [[ $(eval echo \$NET4_MODE_$iface) == static ]]; then
|
|
prnt I "Configuring IPv4 network interface $iface in static mode..."
|
|
echo "auto $iface" >> $if_file
|
|
echo "iface $iface inet static" >> $if_file
|
|
echo -e "\taddress $(eval echo \$NET4_IP_$iface)" >> $if_file
|
|
|
|
if [[ -n $(eval echo \$NET4_GW_$iface) ]]; then
|
|
echo -e "\tgateway $(eval echo \$NET4_GW_$iface)" >> $if_file
|
|
fi
|
|
|
|
if [[ -n $(eval echo \$NET4_NS_$iface) ]]; then
|
|
echo -e "\tdns_nameservers $(eval echo \$NET4_NS_$iface)" >> $if_file
|
|
fi
|
|
|
|
if [[ -n $(eval echo \$NET4_NS_SEARCH_$iface) ]]; then
|
|
echo -e "\tdns_search $(eval echo \$NET4_NS_SEARCH_$iface)" >> $if_file
|
|
fi
|
|
|
|
elif [[ $(eval echo \$NET4_MODE_$iface) == dhcp ]]; then
|
|
prnt I "Configuring IPv4 network interface $iface in DHCP mode..."
|
|
echo "auto $iface" >> $if_file
|
|
echo "iface $iface inet dhcp" >> $if_file
|
|
|
|
elif [[ $(eval echo \$NET4_MODE_$iface) == manual ]]; then
|
|
local fname=$(eval echo \$NET4_MANUAL_FILE_$iface)
|
|
append_file $fname $if_file
|
|
unset fname
|
|
fi
|
|
done
|
|
|
|
for iface in $IPV6_IFACES; do
|
|
echo -e "\n# --------------------------------\n" >> $if_file
|
|
|
|
if [[ $(eval echo \$NET6_MODE_$iface) == static ]]; then
|
|
prnt I "Configuring IPv6 network interface $iface in static mode..."
|
|
echo "auto $iface" >> $if_file
|
|
echo "iface $iface inet6 static" >> $if_file
|
|
echo -e "\taddress $(eval echo \$NET6_IP_$iface)" >> $if_file
|
|
|
|
if [[ -n $(eval echo \$NET6_GW_$iface) ]]; then
|
|
echo -e "\tgateway $(eval echo \$NET6_GW_$iface)" >> $if_file
|
|
fi
|
|
|
|
if [[ -n $(eval echo \$NET6_NS_$iface) ]]; then
|
|
echo -e "\tdns_nameservers $(eval echo \$NET6_NS_$iface)" >> $if_file
|
|
fi
|
|
|
|
if [[ -n $(eval echo \$NET6_NS_SEARCH_$iface) ]]; then
|
|
echo -e "\tdns_search $(eval echo \$NET6_NS_SEARCH_$iface)" >> $if_file
|
|
fi
|
|
|
|
elif [[ $(eval echo \$NET6_MODE_$iface) == dhcp ]]; then
|
|
prnt I "Configuring IPv6 network interface $iface in DHCP mode..."
|
|
echo "auto $iface" >> $if_file
|
|
echo "iface $iface inet6 dhcp" >> $if_file
|
|
|
|
elif [[ $(eval echo \$NET6_MODE_$iface) == manual ]]; then
|
|
local fname=$(eval echo \$NET6_MANUAL_FILE_$iface)
|
|
append_file $fname $if_file
|
|
unset fname
|
|
fi
|
|
done
|
|
|
|
prnt I "Trying to raise down iface up. Allready configured iface will require a reboot"
|
|
ifup -a || true && prnt W "Ignoring errors here."
|
|
|
|
unset iface if_file
|
|
NEED_REBOOT=true
|
|
}
|
|
|
|
precheck_conf_network()
|
|
{
|
|
file_must_exists interfaces.head
|
|
if [[ -z $IPV4_IFACES ]]; then
|
|
prnt W "No IPv4 interfaces to configure."
|
|
else
|
|
for iface in $IPV4_IFACES; do
|
|
if [[ ! -d /sys/class/net/$iface ]]; then
|
|
prnt E "The iface $iface, asked to configure, do not exist!"
|
|
die 175
|
|
else
|
|
if [[ $(grep "up" /sys/class/net/$iface/operstate) ]]; then
|
|
prnt W "The IPv4 iface $iface, is already configured, a reboot will be required."
|
|
fi
|
|
fi
|
|
if [[ -z $(eval echo \$NET4_MODE_$iface) ]]; then
|
|
prnt E "Interfaces $iface have no mode set!"
|
|
die 175
|
|
else
|
|
case $(eval echo \$NET4_MODE_$iface) in
|
|
"static")
|
|
if [[ -z $(eval echo \$NET4_IP_$iface) ]]; then
|
|
prnt E "No IPv4 have been assigned to static interface $iface!"
|
|
fi
|
|
prnt m " * Interface $iface will have $(eval echo \$NET4_IP_$iface) static IPv4 address."
|
|
;;
|
|
"dhcp")
|
|
prnt m " * Interface $iface will use DHCP."
|
|
;;
|
|
"manual")
|
|
file_must_exists $(eval echo \$NET4_MANUAL_FILE_$iface)
|
|
prnt m " * Interface $iface will use manual IPv4 configuration in a file."
|
|
;;
|
|
*)
|
|
prnt E "Interfaces mode \"$(eval echo \$NET4_MODE_$iface)\" unsuported!"
|
|
die 175
|
|
;;
|
|
esac
|
|
fi
|
|
done
|
|
fi
|
|
if [[ -z $IPV6_IFACES ]]; then
|
|
prnt W "No IPv6 interfaces to configure."
|
|
else
|
|
for iface in $IPV6_IFACES; do
|
|
if [[ ! -d /sys/class/net/$iface ]]; then
|
|
prnt E "The iface $iface, asked to configure, do not exist!"
|
|
die 175
|
|
else
|
|
if [[ $(grep "up" /sys/class/net/$iface/operstate) ]]; then
|
|
prnt W "The IPv6 iface $iface, is already configured, a reboot will be required."
|
|
fi
|
|
fi
|
|
if [[ -z $(eval echo \$NET6_MODE_$iface) ]]; then
|
|
prnt E "Interfaces $iface have no mode set!"
|
|
die 175
|
|
else
|
|
case $(eval echo \$NET6_MODE_$iface) in
|
|
"static")
|
|
if [[ -z $(eval echo \$NET6_IP_$iface) ]]; then
|
|
prnt E "No IPv6 have been assigned to static interface $iface!"
|
|
fi
|
|
prnt m " * Interface $iface will have $(eval echo \$NET6_IP_$iface) static IPv6 address."
|
|
;;
|
|
"dhcp")
|
|
prnt m " * Interface $iface will use DHCPv6."
|
|
;;
|
|
"manual")
|
|
file_must_exists $(eval echo \$NET6_MANUAL_FILE_$iface)
|
|
prnt m " * Interface $iface will use manual IPv6 configuration in a file."
|
|
;;
|
|
*)
|
|
prnt E "Interfaces mode \"$(eval echo \$NET6_MODE_$iface)\" unsuported!"
|
|
die 175
|
|
;;
|
|
esac
|
|
fi
|
|
done
|
|
fi
|
|
}
|
|
|
|
export -f conf_network
|
|
export -f precheck_conf_network
|
|
|
|
# EOF
|