maj implémentation

This commit is contained in:
Jephté Clain 2014-02-04 16:59:32 +04:00
parent e6154a4dfd
commit ca2fabf6ca
1 changed files with 229 additions and 84 deletions

View File

@ -5,7 +5,7 @@
##@require sysinfos ##@require sysinfos
##@require service ##@require service
uprovide debian uprovide debian
urequire base sysinfos service urequire base sysinfos service ipcalc
################################################################################ ################################################################################
# Gestion des packages # Gestion des packages
@ -67,51 +67,6 @@ function service_enable() {
################################################################################ ################################################################################
# Gestion des interfaces réseau # Gestion des interfaces réseau
function network_create_bridge() {
# Modifier le fichier /etc/network/interfaces pour créer un nouveau pont
# nommé $1 avec les paramètres $2. Si $2 est vide, sa valeur par défaut est
# bridge_ports none
# bridge_stp off
# bridge_fd 2
# bridge_maxwait 0
# OPTIONS:
# --static L'interface est statique plutôt que de recevoir son
# adresse par DHCP
# --auto Démarrer l'interface automatiquement
# Une définition existante de l'interface n'est jamais écrasée.
# Retourner vrai si la définition a été ajoutée dans /etc/network/interfaces
local args method auto ifname lines
method=dhcp
parse_opts \
--static method=static \
--auto auto \
@ args -- "$@" && set -- "${args[@]}" || {
eerror "$args"
return 1
}
ifname="${1:-br0}"
array_from_lines lines "$2"
lines="$(array_to_lines lines "" " ")"
doinplacef /etc/network/interfaces awkrun ifname="$ifname" lines="$lines" method="$method" auto="$auto" '
BEGIN {
foundif = 0
}
$0 ~ "^iface " ifname " " { foundif = 1 }
{ print }
END {
if (foundif) {
exit 1
} else {
print ""
if (auto != "") print "auto " ifname
print "iface " ifname " inet " method
if (lines != "") print lines
exit 0
}
}'
}
__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS= __DEBIAN_NETWORK_DEVEL_SHOW_MODIFS=
function __network_parse_confbr() { function __network_parse_confbr() {
@ -119,16 +74,16 @@ function __network_parse_confbr() {
splitpair "$1" br ifaces splitpair "$1" br ifaces
array_split ifaces "$ifaces" , array_split ifaces "$ifaces" ,
__npc_br="$br" __npc_destbr="$br"
array_copy __npc_ifaces ifaces array_copy __npc_destifaces ifaces
} }
function network_parse_confbr() { function network_parse_confbr() {
# network_parse_confbr "$confbr" br ifaces # network_parse_confbr "$confbr" br ifaces
local __npc_br; local -a __npc_ifaces local __npc_destbr; local -a __npc_destifaces
__network_parse_confbr "$1" __network_parse_confbr "$1"
set_var "${2:-br}" "$__npc_br" set_var "${2:-br}" "$__npc_destbr"
array_copy "${3:-ifaces}" __npc_ifaces array_copy "${3:-ifaces}" __npc_destifaces
} }
function network_format_confbr() { function network_format_confbr() {
@ -142,18 +97,18 @@ function __network_parse_confip() {
splitfsep "$tmpig" // iface gateway splitfsep "$tmpig" // iface gateway
array_split ipsuffixes "$ipsuffixes" , array_split ipsuffixes "$ipsuffixes" ,
__npc_iface="$iface" __npc_destiface="$iface"
__npc_gateway="$gateway" __npc_destgateway="$gateway"
array_copy __npc_ipsuffixes ipsuffixes array_copy __npc_destipsuffixes ipsuffixes
} }
function network_parse_confip() { function network_parse_confip() {
# network_parse_confip "$confip" iface gateway ipsuffixes # network_parse_confip "$confip" iface gateway ipsuffixes
local __npc_iface __npc_gateway; local -a __npc_ipsuffixes local __npc_destiface __npc_destgateway; local -a __npc_destipsuffixes
__network_parse_confip "$1" __network_parse_confip "$1"
set_var "${2:-iface}" "$__npc_iface" set_var "${2:-iface}" "$__npc_destiface"
set_var "${3:-gateway}" "$__npc_gateway" set_var "${3:-gateway}" "$__npc_destgateway"
array_copy "${4:-ipsuffixes}" __npc_ipsuffixes array_copy "${4:-ipsuffixes}" __npc_destipsuffixes
} }
function network_parse_ipsuffix() { function network_parse_ipsuffix() {
@ -179,6 +134,7 @@ function network_format_ipsuffix() {
function __network_fix_confbrs() { function __network_fix_confbrs() {
local -a confbrs ifaces brs ips local -a confbrs ifaces brs ips
local confbr br iface local confbr br iface
# recenser les bridges et créer les tableaux __BR_ifaces # recenser les bridges et créer les tableaux __BR_ifaces
for confbr in "${__nfc_confbrs[@]}"; do for confbr in "${__nfc_confbrs[@]}"; do
network_parse_confbr "$confbr" br ifaces network_parse_confbr "$confbr" br ifaces
@ -196,21 +152,23 @@ function __network_fix_confbrs() {
array_add confbrs "$(network_format_confbr "$br" "${br}_ifaces")" array_add confbrs "$(network_format_confbr "$br" "${br}_ifaces")"
done done
array_copy __nfc_confbrs confbrs array_copy __nfc_destconfbrs confbrs
} }
function network_fix_confbrs() { function network_fix_confbrs() {
# normaliser le tableau $1(=confbrs): fusionner les doublons # normaliser le tableau $1(=confbrs): fusionner les doublons
local -a __nfc_confbrs local -a __nfc_confbrs __nfc_destconfbrs
array_copy __nfc_confbrs "${1:-confbrs}" array_copy __nfc_confbrs "${1:-confbrs}"
__network_fix_confbrs __network_fix_confbrs
array_copy "${1:-confbrs}" __nfc_confbrs array_copy "${1:-confbrs}" __nfc_destconfbrs
} }
function __network_fix_confips() { function __network_fix_confips() {
local -a confips ipsuffixes ifaces ips local -a confips ipsuffixes ifaces ips
local confip iface gateway ip suffix mainip local confip iface gateway ip suffix mainip
local mainiface="$1"
# recenser les interfaces et créer les tableaux __IFACE_ipspecs # recenser les interfaces et créer les tableaux __IFACE_ipspecs
for confip in "${__nfc_confips[@]}"; do for confip in "${__nfc_confips[@]}"; do
network_parse_confip "$confip" iface gateway ipsuffixes network_parse_confip "$confip" iface gateway ipsuffixes
@ -255,24 +213,25 @@ function __network_fix_confips() {
array_add confips "$(network_format_confip "$iface" "$gateway" "${iface}_ipsuffixes")" array_add confips "$(network_format_confip "$iface" "$gateway" "${iface}_ipsuffixes")"
done done
array_copy __nfc_confips confips array_copy __nfc_destconfips confips
} }
function network_fix_confips() { function network_fix_confips() {
# normaliser le tableau $1(=confips): fusionner les doublons, spécifier le # normaliser le tableau $1(=confips): fusionner les doublons, spécifier le
# suffixe /24 par défaut, etc. $2 est le cas échéant l'interface associée # suffixe /24 par défaut, etc. $2 est le cas échéant l'interface associée
# aux adresses ip non qualifiées # aux adresses ip non qualifiées
local -a __nfc_confips local -a __nfc_confips __nfc_destconfips
array_copy __nfc_confips "${1:-confips}" array_copy __nfc_confips "${1:-confips}"
local mainiface="$2" __network_fix_confips "$2"
__network_fix_confips
array_copy "${1:-confips}" __nfc_confips array_copy "${1:-confips}" __nfc_destconfips
} }
function __network_fix_mainiface() { function __network_fix_mainiface() {
local -a confips ifaces ipsuffixes local -a confips ifaces ipsuffixes
local br iface gateway confip mainconfip local br iface gateway confip mainconfip
local mainiface="$1"
# déterminer mainiface # déterminer mainiface
if [ -z "$mainiface" -a -n "${__nfm_confbrs[0]}" ]; then if [ -z "$mainiface" -a -n "${__nfm_confbrs[0]}" ]; then
network_parse_confbr "${__nfm_confbrs[0]}" br ifaces network_parse_confbr "${__nfm_confbrs[0]}" br ifaces
@ -298,7 +257,7 @@ function __network_fix_mainiface() {
done done
[ -n "$mainconfip" ] && array_ins confips "$mainconfip" [ -n "$mainconfip" ] && array_ins confips "$mainconfip"
array_copy __nfm_confips confips array_copy __nfm_destconfips confips
} }
function network_fix_mainiface() { function network_fix_mainiface() {
# A partir des valeurs des tableaux $1(=confbrs) et $2(=confips), et de # A partir des valeurs des tableaux $1(=confbrs) et $2(=confips), et de
@ -308,13 +267,12 @@ function network_fix_mainiface() {
# sélectionnée. Sinon, on prend eth0. # sélectionnée. Sinon, on prend eth0.
# Ensuite, réorganiser les tableaux de façon que confips[0] devienne la # Ensuite, réorganiser les tableaux de façon que confips[0] devienne la
# configuration ip de l'interface principale. # configuration ip de l'interface principale.
local -a __nfm_confbrs __nfm_confips local -a __nfm_confbrs __nfm_confips __nfm_destconfips
array_copy __nfm_confbrs "${1:-confbrs}" array_copy __nfm_confbrs "${1:-confbrs}"
array_copy __nfm_confips "${2:-confips}" array_copy __nfm_confips "${2:-confips}"
local mainiface="$3" __network_fix_mainiface "$3"
__network_fix_mainiface
array_copy "${2:-confips}" __nfm_confips array_copy "${2:-confips}" __nfm_destconfips
} }
function network_fix_confs() { function network_fix_confs() {
@ -357,14 +315,14 @@ END {
array_add confbrs "$confbr" array_add confbrs "$confbr"
done done
array_copy __nsc_confbrs confbrs array_copy __nsc_destconfbrs confbrs
} }
function network_set_confbrs() { function network_set_confbrs() {
# initialiser $1(=confbrs) avec l'état des bridges sur le système courant # initialiser $1(=confbrs) avec l'état des bridges sur le système courant
local -a __nsc_confbrs local -a __nsc_destconfbrs
__network_set_confbrs __network_set_confbrs
array_copy "${1:-confbrs}" __nsc_confbrs array_copy "${1:-confbrs}" __nsc_destconfbrs
} }
function __network_set_gateway() { function __network_set_gateway() {
@ -373,40 +331,227 @@ function __network_set_gateway() {
gateway="$(route -n | awk -v iface="$1" '$1 == "0.0.0.0" && $8 == iface { print $2 }')" gateway="$(route -n | awk -v iface="$1" '$1 == "0.0.0.0" && $8 == iface { print $2 }')"
} }
function __network_set_confip() { function __network_set_confip() {
# initialiser la variable confip avec l'état de l'interface $1. # initialiser la variable confip avec l'état de l'interface $1, en assumant
# que la passerelle vaut $2.
# retourner 1 si l'interface n'a pas d'adresse ip associée # retourner 1 si l'interface n'a pas d'adresse ip associée
local -a ipsuffixes local -a ipsuffixes
array_from_lines ipsuffixes "$(ip addr show dev "$1" | awk '$1 == "inet" { print $2 }')" array_from_lines ipsuffixes "$(ip addr show dev "$1" | awk '$1 == "inet" { print $2 }')"
__network_set_gateway "$1" confip="$(network_format_confip "$1" "$2" ipsuffixes)"
confip="$(network_format_confip "$iface" "$gateway" ipsuffixes)"
[ "${#ipsuffixes[*]}" -gt 0 ] [ "${#ipsuffixes[*]}" -gt 0 ]
} }
function __network_set_confips() { function __network_set_confips() {
local -a confips ifaces local -a confips ifaces
local iface local iface gateway
array_from_lines ifaces "$(__network_valid_ifaces)" array_from_lines ifaces "$(__network_valid_ifaces)"
for iface in "${ifaces[@]}"; do for iface in "${ifaces[@]}"; do
__network_set_confip "$iface" || continue __network_set_gateway "$iface"
__network_set_confip "$iface" "$gateway" || continue
array_add confips "$confip" array_add confips "$confip"
done done
array_copy __nsc_confips confips array_copy __nsc_destconfips confips
} }
function network_set_confips() { function network_set_confips() {
# initialiser le tableau $1(=confips) avec l'état des interfaces sur le # initialiser le tableau $1(=confips) avec l'état des interfaces sur le
# système courant # système courant
local -a __nsc_confips local -a __nsc_destconfips
__network_set_confips __network_set_confips
array_copy "${1:-confips}" __nsc_confips array_copy "${1:-confips}" __nsc_destconfips
} }
function __network_interfaces_check_confbr() {
awkrun <"$nifile" -f br="$br" ifaces[@]=__nicc_ifaces '
BEGIN {
array_new(iface_have_autos)
array_new(iface_have_hotplugs)
array_new(iface_have_manuals)
for (i = 1; i <= ifaces_count; i++) {
iface_have_autos[i] = 0
iface_have_hotplugs[i] = 0
iface_have_manuals[i] = 0
}
br_have_auto = 0
br_have_hotplug = 0
have_br = 0
}
function get_iface_index(iface, i) {
for (i = 1; i <= ifaces_count; i++) {
if (ifaces[i] == iface) return i
}
return 0
}
$1 ~ /^(allow-)?auto$/ {
for (j = 2; j <= NF; j++) {
i = get_iface_index($j)
if (i != 0) iface_have_autos[i] = 1
if ($i == br) br_have_auto = 1
}
}
$1 == "allow-hotplug" {
for (j = 2; j <= NF; j++) {
i = get_iface_index($j)
if (i != 0) iface_have_hotplugs[i] = 1
if ($i == br) br_have_hotplug = 1
}
}
$1 == "iface" && $3 == "inet" && $4 == "manual" {
i = get_iface_index($2)
if (i != 0) iface_have_manuals[i] = 1
}
$1 == "iface" && $2 == br && $3 == "inet" {
have_br = 1
}
END {
check_auto_or_hotplug = br_have_auto || br_have_hotplug
check_manuals = 1
if (check_auto_or_hotplug) {
for (i = 1; i <= ifaces_count; i++) {
if (!iface_have_autos[i] && !iface_have_hotplugs[i]) {
check_auto_or_hotplug = 0
break
}
if (!iface_have_manuals[i]) {
check_manuals = 0
break
}
}
}
if (!check_auto_or_hotplug || !check_manuals || !have_br) {
exit 1
}
exit 0
}
'
}
function network_interfaces_check_confbr() { function network_interfaces_check_confbr() {
: # Vérifier que la configuration du bridge $1, dont les membres sont les
# interfaces du tableau $2(=ifaces) est faite dans le fichier
# $3(=/etc/network/interfaces)
local -a __nicc_ifaces
array_copy __nicc_ifaces "${2:-ifaces}"
local br="$1" nifile="${3:-/etc/network/interfaces}"
__network_interfaces_check_confbr
} }
function __network_interfaces_check_confip() {
awkrun <"$nifile" -f iface="$iface" gateway="$gateway" ipsuffixes[@]=__nicc_ipsuffixes '
BEGIN {
have_auto = 0
have_hotplug = 0
have_iface = 0
have_method = 0 # static ou dhcp
have_gateway = ipsuffixes[1] == "dhcp" || gateway == ""
array_new(have_ips)
array_new(have_ip_ups)
array_new(have_ip_downs)
for (i = 1; i <= ipsuffixes_count; i++) {
have_ips[i] = 1
have_ip_ups[i] = 0
have_ip_downs[i] = 0
}
have_ips[1] = 0
have_ip_ups[1] = 1
have_ip_downs[1] = 1
in_iface = 0
}
function get_ipsuffix_index(ipsuffix, i) {
ip0 = ipsuffix; sub(/\/[0-9]+$/, "", ip0)
for (i = 1; i <= ipsuffixes_count; i++) {
# XXX ne pas comparer les suffixes pour le moment
ipi = ipsuffixes[i]; sub(/\/[0-9]+$/, "", ipi)
if (ipi == ip0) return i
}
return 0
}
$1 == "iface" && $2 == iface && $3 == "inet" {
have_iface = 1
if (ipsuffixes[1] == "dhcp") {
have_method = $4 == "dhcp"
} else {
have_method = $4 == "static"
}
in_iface = 1
next
}
in_iface && $1 == "gateway" && $2 == gateway {
have_gateway = 1
next
}
in_iface && $1 == "address" && get_ipsuffix_index($2) == 1 {
have_ips[1] = 1
next
}
in_iface && $0 ~ "^[ \\t]*(post-)?up (/sbin/)?ip addr add " {
if (match($0, "ip addr add (.*/.*) dev " iface " ", vs) != 0) {
i = get_ipsuffix_index(vs[1])
if (i > 1) {
have_ip_ups[i] = 1
}
}
next
}
in_iface && $0 ~ "^[ \\t]*(pre-)?down (/sbin/)?ip addr del " {
if (match($0, "ip addr del (.*/.*) dev " iface " ", vs) != 0) {
i = get_ipsuffix_index(vs[1])
if (i > 1) {
have_ip_downs[i] = 1
}
}
next
}
$1 == "iface" {
in_iface = 0
next
}
$1 ~ /^(allow-)?auto$/ {
for (i = 2; i <= NF; i++) {
print "auto: " $i ", iface=" iface
if ($i == iface) {
have_auto = 1
}
}
in_iface = 0
next
}
$1 == "allow-hotplug" {
for (i = 2; i <= NF; i++) {
print "hotplug: " $i ", iface=" iface
if ($i == iface) {
have_hotplug = 1
}
}
in_iface = 0
next
}
END {
check_auto_or_hotplug = have_auto || have_hotplug
check_iface = have_iface && have_method && have_gateway
check_ips = 1
for (i = 1; i <= ipsuffixes_count; i++) {
if (!have_ips[i] || !have_ip_ups[i] || !have_ip_downs[i]) {
check_ips = 0
break
}
}
if (!check_auto_or_hotplug || !check_iface || !check_ips) {
exit 1
}
exit 0
}
'
}
function network_interfaces_check_confip() { function network_interfaces_check_confip() {
: # Vérifier que la configuration de l'interface $1, avec la passerelle $2,
# avec les adresses IP du tabbleau $3(=ipsuffixes) est faite dans le fichier
# $4(=/etc/network/interfaces)
local -a __nicc_ipsuffixes
array_copy __nicc_ipsuffixes "${3:-ipsuffixes}"
local iface="$1" gateway="$2" nifile="${4:-/etc/network/interfaces}"
__network_interfaces_check_confip
} }
function network_interfaces_remove_iface() { function network_interfaces_remove_iface() {