début de réécriture du code de gestion des interfaces réseau

This commit is contained in:
Jephté Clain 2014-02-01 18:29:20 +04:00
parent 62a79e8c35
commit 55fdea8116
1 changed files with 71 additions and 50 deletions

View File

@ -114,19 +114,40 @@ END {
__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS=
function __network_backup() {
function debian_network_fix_confbrs() {
:
}
function debian_network_fix_confips() {
# $2==mainiface, l'interface associée aux adresses ip non qualifiées
:
}
function debian_network_fix_mainiface() {
# confips[0] est la configuration ip de l'interface principale
:
}
function debian_network_fix_confs() {
network_fix_confbrs "${1:-confbrs}"
network_fix_confips "${2:-confips}"
network_fix_mainiface "${1:-confbrs}" "${2:-confips}" "$3"
}
#XXX
function debian_network_backup() {
local file="$1"
local backup="$file.orig$(date +%Y%m%d)"
[ -f "$backup" ] || cat "$file" >"$backup" 2>/dev/null
}
function __network_hostname() {
function debian_network_hostname() {
local hostname="${1%%.*}"
[ -f /etc/hostname ] || touch /etc/hostname
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" ]; then
uecho "Setting /etc/hostname to $hostname"
elif [ "$(</etc/hostname)" != "$hostname" ]; then
__network_backup /etc/hostname
debian_network_backup /etc/hostname
if show_debug; then
edebug "Setting /etc/hostname to $hostname"
else
@ -136,13 +157,13 @@ function __network_hostname() {
fi
}
function __network_mailname() {
function debian_network_mailname() {
local host="$1"
[ -f /etc/mailname ] || touch /etc/mailname
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" ]; then
uecho "Setting /etc/mailname to $host"
elif [ "$(</etc/mailname)" != "$host" ]; then
__network_backup /etc/mailname
debian_network_backup /etc/mailname
if show_debug; then
edebug "Setting /etc/mailname to $host"
else
@ -152,7 +173,7 @@ function __network_mailname() {
fi
}
function __network_fix_exim4() {
function debian_network_fix_exim4() {
local host="$1" oldhost="$2"
local tmpfile; ac_set_tmpfile tmpfile
@ -167,7 +188,7 @@ dc_other_hostnames='$host'"
uecho "/etc/exim4/update-exim4.conf.conf: pas de modifications"
fi
elif testdiff "$tmpfile" /etc/exim4/update-exim4.conf.conf; then
__network_backup /etc/exim4/update-exim4.conf.conf
debian_network_backup /etc/exim4/update-exim4.conf.conf
if show_debug; then
edebug "Setting /etc/exim4/update-exim4.conf.conf to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
@ -180,7 +201,7 @@ dc_other_hostnames='$host'"
ac_clean "$tmpfile"
}
function __network_fix_postfix() {
function debian_network_fix_postfix() {
local host="$1" oldhost="$2"
local tmpfile; ac_set_tmpfile tmpfile
@ -201,7 +222,7 @@ function __network_fix_postfix() {
uecho "/etc/postfix/main.cf: pas de modifications"
fi
elif testdiff "$tmpfile" /etc/postfix/main.cf; then
__network_backup /etc/postfix/main.cf
debian_network_backup /etc/postfix/main.cf
if show_debug; then
edebug "Setting /etc/postfix/main.cf to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
@ -213,7 +234,7 @@ function __network_fix_postfix() {
ac_clean "$tmpfile"
}
function __network_hosts() {
function debian_network_hosts() {
local host="$1" hostname="${1%%.*}" ip="$2"
local shost="${host//./\\.}"
local tmpfile; ac_set_tmpfile tmpfile
@ -240,7 +261,7 @@ $ip$TAB$host $hostname" "$tmpfile"
uecho "/etc/hosts: pas de modifications"
fi
elif testdiff "$tmpfile" /etc/hosts; then
__network_backup /etc/hosts
debian_network_backup /etc/hosts
if show_debug; then
edebug "Setting /etc/hosts to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
@ -252,7 +273,7 @@ $ip$TAB$host $hostname" "$tmpfile"
ac_clean "$tmpfile"
}
function __network_parse_confip() {
function debian_network_parse_confip() {
# confip --> iface [ipspecs@] [ipspecs]
local __npc_tmp
splitfsep2 "$1" : "${2:-iface}" __npc_tmp
@ -260,14 +281,14 @@ function __network_parse_confip() {
[ -n "$4" ] && set_var "$4" "$__npc_tmp"
}
function __network_parse_ipspec() {
function debian_network_parse_ipspec() {
# ipspec --> ip suffix gateway
local __npi_tmp
splitfsep "$1" / "${2:-ip}" __npi_tmp
splitfsep "$__npi_tmp" + "${3:-suffix}" "${4:-gateway}"
}
function __network_parse_confbr() {
function debian_network_parse_confbr() {
# confbr --> br [ifaces@] [ifaces]
local __npc_tmp
splitfsep "$1" : "${2:-br}" __npc_tmp
@ -275,7 +296,7 @@ function __network_parse_confbr() {
[ -n "$4" ] && set_var "$4" "$__npc_tmp"
}
function __network_fix_confips() {
function debian_network_fix_confips() {
# confips@
local -a __nfc_confips __nfc_ipspecs __nfc_ifaces __nfc_ips
local __nfc_confip __nfc_iface __nfc_ipspec __nfc_ip __nfc_suffix __nfc_gateway __nfc_first
@ -283,15 +304,15 @@ function __network_fix_confips() {
array_copy __nfc_confips "${1:-confips}"
# recenser les interfaces et créer les tableaux __IFACE_ipspecs
for __nfc_confip in "${__nfc_confips[@]}"; do
__network_parse_confip "$__nfc_confip" __nfc_iface __nfc_ipspecs
debian_network_parse_confip "$__nfc_confip" __nfc_iface __nfc_ipspecs
array_addu __nfc_ifaces "$__nfc_iface"
eval "local -a __nfc_${__nfc_iface}_ipspecs"
done
# puis constuire la liste des adresses IP associées à chaque interface
for __nfc_confip in "${__nfc_confips[@]}"; do
__network_parse_confip "$__nfc_confip" __nfc_iface __nfc_ipspecs
debian_network_parse_confip "$__nfc_confip" __nfc_iface __nfc_ipspecs
for __nfc_ipspec in "${__nfc_ipspecs[@]}"; do
__network_parse_ipspec "$__nfc_ipspec" __nfc_ip __nfc_suffix __nfc_gateway
debian_network_parse_ipspec "$__nfc_ipspec" __nfc_ip __nfc_suffix __nfc_gateway
if ! array_contains __nfc_ips "$__nfc_ip"; then
[ -n "$__nfc_suffix" ] || __nfc_suffix=24
if [ -z "$__nfc_gateway" ] && array_isempty "__nfc_${__nfc_iface}_ipspecs"; then
@ -313,7 +334,7 @@ function __network_fix_confips() {
array_copy "${1:-confips}" __nfc_confips
}
function __network_fix_confbrs() {
function debian_network_fix_confbrs() {
# confbrs@
local -a __nfc_confbrs __nfc_ifaces __nfc_brs __nfc_ips
local __nfc_confbr __nfc_br __nfc_iface __nfc_ip __nfc_suffix __nfc_gateway
@ -321,13 +342,13 @@ function __network_fix_confbrs() {
array_copy __nfc_confbrs "${1:-confbrs}"
# recenser les bridges et créer les tableaux __BR_ifaces
for __nfc_confbr in "${__nfc_confbrs[@]}"; do
__network_parse_confbr "$__nfc_confbr" __nfc_br __nfc_ifaces
debian_network_parse_confbr "$__nfc_confbr" __nfc_br __nfc_ifaces
array_addu __nfc_brs "$__nfc_br"
eval "local -a __nfc_${__nfc_br}_ifaces"
done
# puis constuire la liste des interfaces associées à chaque bridge
for __nfc_confbr in "${__nfc_confbrs[@]}"; do
__network_parse_confbr "$__nfc_confbr" __nfc_br __nfc_ifaces
debian_network_parse_confbr "$__nfc_confbr" __nfc_br __nfc_ifaces
array_extendu "__nfc_${__nfc_br}_ifaces" __nfc_ifaces
done
# puis construire le tableau final
@ -339,14 +360,14 @@ function __network_fix_confbrs() {
array_copy "${1:-confbrs}" __nfc_confbrs
}
function __network_resolve_mainiface() {
function debian_network_resolve_mainiface() {
local __nrm_mainiface="${1:-mainiface}" __nrm_mainconfbr="$2" __nrm_mainconfip="$3"
[ -n "${!__nrm_mainiface}" ] || __network_parse_confbr "$__nrm_mainconfbr" "$__nrm_mainiface"
[ -n "${!__nrm_mainiface}" ] || __network_parse_confip "$__nrm_mainconfip" "$__nrm_mainiface"
[ -n "${!__nrm_mainiface}" ] || debian_network_parse_confbr "$__nrm_mainconfbr" "$__nrm_mainiface"
[ -n "${!__nrm_mainiface}" ] || debian_network_parse_confip "$__nrm_mainconfip" "$__nrm_mainiface"
[ -n "${!__nrm_mainiface}" ] || set_var "$__nrm_mainiface" eth0
}
function __network_set_confip() {
function debian_network_set_confip() {
eval "$(ip addr show dev "$1" | awk "BEGIN {
mainipvar = \"${2:-mainip}\"
supplipsvar = \"${3:-supplips}\"
@ -376,7 +397,7 @@ END {
set_var "$2" "$(array_join "$2" ,)"
}
function __network_set_gateway() {
function debian_network_set_gateway() {
eval "$(route -n | awk "BEGIN {
iface = \"$1\"
gatewayvar = \"${2:-gateway}\"
@ -389,20 +410,20 @@ $1 == "0.0.0.0" && $8 == iface {
}
#BUG)"
function __network_set_confips() {
function debian_network_set_confips() {
local -a __nsc_confips __nsc_ifaces __nsc_supplips
local __nsc_iface __nsc_mainip __nsc_gateway
array_from_lines __nsc_ifaces "$(ip link | grep -v '<.*LOOPBACK.*>' | grep '<.*UP.*>' | awk '{ sub(/:$/, "", $2); print $2}')"
for __nsc_iface in "${__nsc_ifaces[@]}"; do
__network_set_confip "$__nsc_iface" __nsc_mainip __nsc_supplips
debian_network_set_confip "$__nsc_iface" __nsc_mainip __nsc_supplips
[ -n "$__nsc_mainip" ] || continue
__network_set_gateway "$__nsc_iface" __nsc_gateway
debian_network_set_gateway "$__nsc_iface" __nsc_gateway
array_add __nsc_confips "$__nsc_iface:$__nsc_mainip${__nsc_gateway:++$__nsc_gateway}${__nsc_supplips:+,$__nsc_supplips}"
done
array_copy "${1:-confips}" __nsc_confips
}
function __network_set_confbrs() {
function debian_network_set_confbrs() {
local -a __nsc_ifaces __nsc_tmpconfbrs __nsc_confbrs
local __nsc_confbr __nsc_br __nsc_iface
array_from_lines __nsc_ifaces "$(ip link | grep -v '<.*LOOPBACK.*>' | grep '<.*UP.*>' | awk '{ sub(/:$/, "", $2); print $2}')"
@ -549,7 +570,7 @@ BEGIN {
}
'
function __network_update_bridge() {
function debian_network_update_bridge() {
# vérifier la configuration (interfaces de ifaces[@] en manual, présence du
# bridge, bridge en auto, adresse ip principale statique ou en dhcp,
# adresses ip supplémentaires), puis si nécessaire, supprimer l'ancienne
@ -783,7 +804,7 @@ END {
return 0
}
function __network_update_iface() {
function debian_network_update_iface() {
# vérifier la configuration (présence de l'interface, interface en auto,
# adresse ip principale statique ou en dhcp, adresses ip supplémentaires),
# puis si nécessaire, supprimer l'ancienne configuration et créer la
@ -946,16 +967,16 @@ function network_config() {
array_copy confips __nc_confips
array_copy confbrs __nc_confbrs
__network_fix_confips confips
__network_fix_confbrs confbrs
debian_network_fix_confips confips
debian_network_fix_confbrs confbrs
local mainbr # bridge principal
local mainip # adresse IP principale de l'interface principale
local confbr confip br iface suffix gateway
local -a ipspecs
__network_resolve_mainiface mainiface "${confbrs[0]}" "${confips[0]}"
__network_parse_confbr "${confbrs[0]}" mainbr
debian_network_resolve_mainiface mainiface "${confbrs[0]}" "${confips[0]}"
debian_network_parse_confbr "${confbrs[0]}" mainbr
[ -n "$mainbr" ] || mainbr=br0
if [ -n "${confips[*]}" -o -n "${confbrs[*]}" ]; then
@ -968,11 +989,11 @@ function network_config() {
local -a brifaces
ifaces_with_ips=()
for confip in "${confips[@]}"; do
__network_parse_confip "$confip" iface
debian_network_parse_confip "$confip" iface
array_addu ifaces_with_ips "$iface"
done
for confbr in "${confbrs[@]}"; do
__network_parse_confbr "$confbr" br ifaces
debian_network_parse_confbr "$confbr" br ifaces
array_add brifaces "$br"
for iface in "${ifaces[@]}"; do
if array_contains ifaces_with_ips "$iface"; then
@ -983,7 +1004,7 @@ function network_config() {
done
local -a confstdips confbrips
for confip in "${confips[@]}"; do
__network_parse_confip "$confip" iface
debian_network_parse_confip "$confip" iface
[ -n "$iface" ] || iface="$mainiface"
if array_contains brifaces "$iface"; then
array_add confbrips "$confip"
@ -1013,37 +1034,37 @@ iface lo inet loopback
local -a tmpifaces
local tmpbr
for confip in "${confbrips[@]}"; do
__network_parse_confip "$confip" br ipspecs
debian_network_parse_confip "$confip" br ipspecs
[ -n "$br" ] || br="$mainbr"
if [ -z "$mainip" -a "$br" == "$mainiface" ]; then
__network_parse_ipspec "${ipspecs[0]}" mainip suffix gateway
debian_network_parse_ipspec "${ipspecs[0]}" mainip suffix gateway
fi
ifaces=()
for confbr in "${confbrs[@]}"; do
__network_parse_confbr "$confbr" tmpbr tmpifaces
debian_network_parse_confbr "$confbr" tmpbr tmpifaces
if [ "$tmpbr" == "$br" ]; then
array_copy ifaces tmpifaces
break
fi
done
if __network_update_bridge "$interfaces" "$workfile" "$br" ipspecs ifaces; then
if debian_network_update_bridge "$interfaces" "$workfile" "$br" ipspecs ifaces; then
cat "$workfile" >"$interfaces"
fi
done
# configurer chaque interface classique
for confip in "${confstdips[@]}"; do
__network_parse_confip "$confip" iface ipspecs
debian_network_parse_confip "$confip" iface ipspecs
[ -n "$iface" ] || iface="$mainiface"
if [ -z "$mainip" -a "$iface" == "$mainiface" ]; then
__network_parse_ipspec "${ipspecs[0]}" mainip suffix gateway
debian_network_parse_ipspec "${ipspecs[0]}" mainip suffix gateway
fi
if __network_update_iface "$interfaces" "$workfile" "$iface" ipspecs; then
if debian_network_update_iface "$interfaces" "$workfile" "$iface" ipspecs; then
cat "$workfile" >"$interfaces"
fi
done
@ -1057,7 +1078,7 @@ iface lo inet loopback
uecho "/etc/network/interfaces: pas de modifications"
fi
elif testdiff "$interfaces" /etc/network/interfaces; then
__network_backup /etc/network/interfaces
debian_network_backup /etc/network/interfaces
if show_debug; then
edebug "Setting /etc/network/interfaces to:"
cat "$interfaces" | sed 's/^/ /g' 1>&2
@ -1091,10 +1112,10 @@ iface lo inet loopback
mainip="${mainips[0]}"
fi
__network_hostname "$host"
__network_mailname "$host"
debian_network_hostname "$host"
debian_network_mailname "$host"
if [ -n "$mainip" ]; then
__network_hosts "$host" "$mainip"
debian_network_hosts "$host" "$mainip"
fi
eend