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