suite implémentation

This commit is contained in:
Jephte CLAIN 2014-02-07 22:48:03 +04:00
parent cb24217682
commit 226b7aeb62
2 changed files with 127 additions and 530 deletions

View File

@ -360,7 +360,16 @@ function network_set_confips() {
array_copy "${1:-confips}" __nsc_destconfips
}
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"
local -a ifaces; array_copy ifaces __nicc_ifaces
local nifile="${3:-/etc/network/interfaces}"
awkrun <"$nifile" -f br="$br" ifaces[@]=__nicc_ifaces '
BEGIN {
array_new(iface_have_autos)
@ -422,21 +431,20 @@ END {
exit 1
}
exit 0
}
'
}
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 '
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"
local -a ipsuffixes; array_copy ipsuffixes __nicc_ipsuffixes
local nifile="${4:-/etc/network/interfaces}"
awkrun <"$nifile" -f iface="$iface" gateway="$gateway" ipsuffixes[@]=ipsuffixes '
BEGIN {
have_auto = 0
have_hotplug = 0
@ -539,17 +547,7 @@ END {
exit 1
}
exit 0
}
'
}
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() {
@ -630,6 +628,7 @@ function network_interfaces_remove_iface() {
local iface="$1" nifile="${2:-/etc/network/interfaces}"
local tmpfile; ac_set_tmpfile tmpfile
local modified
if __network_interfaces_remove_iface "$iface" <"$nifile" >"$tmpfile"; then
cat "$tmpfile" >"$nifile"
modified=1
@ -641,10 +640,13 @@ function network_interfaces_remove_ifaces() {
# Supprimer dans le fichier $2(=/etc/network/interfaces) toute la
# configuration qui concerne les interfaces du tableau $1=(ifaces)
local -a __niri_ifaces; array_copy __niri_ifaces "${1:-ifaces}"
local -a ifaces; array_copy ifaces __niri_ifaces
local nifile="${2:-/etc/network/interfaces}"
local workfile; ac_set_tmpfile workfile
local tmpfile; ac_set_tmpfile tmpfile
local iface modified
cat "$nifile" >"$workfile"
for iface in "${__niri_ifaces[@]}"; do
if __network_interfaces_remove_iface "$iface" <"$workfile" >"$tmpfile"; then
@ -661,8 +663,48 @@ function network_interfaces_remove_confbr() {
# configuration qui concerne le bridge $1, et dont les interfaces sont
# listées dans le tableau $2(=ifaces)
local -a __nirc_ifaces; array_copy __nirc_ifaces "${2:-ifaces}"
array_ins __nirc_ifaces "$1"
network_interfaces_remove_ifaces __nirc_ifaces "$3"
local -a ifaces; array_copy ifaces __nirc_ifaces
array_ins ifaces "$1"
network_interfaces_remove_ifaces ifaces "$3"
}
function network_interfaces_add_confip() {
# ajouter dans le fichier $4(=/etc/network/interfaces) la configuration pour
# l'interface $1, avec éventuellement la passerelle $2, et les adresses ips
# telles qu'elles sont définies dans le table $3(=ipsuffixes)
local -a __niac_ipsuffixes; array_copy __niac_ipsuffixes "${3:-ipsuffixes}"
local iface="$1" gateway="$2"
local -a ipsuffixes; array_copy ipsuffixes __niac_ipsuffixes
local nifile="${4:-/etc/network/interfaces}"
local mainip netmask method
network_parse_ipsuffix "${ipsuffixes[0]}" mainip netmask
if [ "$mainip" == "dhcp" ]; then
mainip=
netmask=
method=dhcp
else
netmask="$(ipcalc_netmask "$netmask")"
[ -n "$netmask" ] || netmask=255.255.255.0
method=static
fi
echo >>"$nifile" "
auto $iface
iface $iface inet $method${mainip:+
address $mainip
netmask $netmask${gateway:+
gateway $gateway}}"
local i=0 ipsuffix
for ipsuffix in "${ipsuffixes[@]:1}"; do
echo >>"$nifile" "\
up ip addr add $ipsuffix dev $iface label $iface:$i
down ip addr del $ipsuffix dev $iface label $iface:$i"
i=$(($i + 1))
done
}
function network_interfaces_add_confbr() {
@ -670,17 +712,42 @@ function network_interfaces_add_confbr() {
# le bridge $1, avec la liste des interfaces dans le tableau $2(=ifaces) et
# la liste des configurations des adresses des interfaces dans le tableau
# $3(=confips)
:
local -a __niac_ifaces; array_copy __niac_ifaces "${2:-ifaces}"
local -a __niac_confips; array_copy __niac_confips "${3:-confips}"
local br="$1"
local -a ifaces; array_copy ifaces __niac_ifaces
local -a confips; array_copy confips __niac_confips
local nifile="${4:-/etc/network/interfaces}"
local iface confip
for iface in "${ifaces[@]}"; do
echo >>"$nifile" "
auto $iface
iface $iface inet manual"
done
local found=
for confip in "${confips[@]}"; do
network_parse_confip "$confip" iface gateway ipsuffixes
if [ "$iface" == "$br" ]; then
found=1
break
fi
done
[ -n "$found" ] || return 1
network_interfaces_add_confip "$br" "$gateway" ipsuffixes "$nifile"
ifaces="${ifaces[*]}"
[ -n "$ifaces" ] || ifaces="none"
echo >>"$nifile" "\
bridge_ports $ifaces
bridge_stp off
bridge_fd 2
bridge_maxwait 0"
}
function network_interfaces_add_confip() {
# ajouter dans le fichier $4(=/etc/network/interfaces) la configuration pour
# l'interface $1, avec éventuellement la passerelle $2, et les adresses ips
# telles qu'elles sont définies dans le table $3(=ipsuffixes)
:
}
#XXX
function __old_network_backup() {
local file="$1"
local backup="$file.orig$(date +%Y%m%d)"
@ -819,492 +886,6 @@ $ip$TAB$host $hostname" "$tmpfile"
ac_clean "$tmpfile"
}
__DEBIAN_NETWORK_UPDATE_BASE_SCRIPT='
BEGIN {
method = "dhcp"
array_new(have_iface_hotplugs)
array_new(have_iface_autos)
array_new(have_iface_manuals)
array_new(ips)
array_new(suffixes)
array_new(gateways)
array_new(have_ip_ups)
array_new(have_ip_downs)
}
function indexof_iface(iface, i) {
for (i = 1; i <= ifaces_count; i++) {
if (ifaces[i] == iface) {
return i
}
}
return 0
}
function indexof_ip(ip, i) {
for (i = 1; i <= ipspecs_count; i++) {
if (ips[i] == ip) {
return i
}
}
return 0
}
function get_netmask(suffix) {
if (suffix == 0) return "0.0.0.0"
else if (suffix == 8) return "255.0.0.0"
else if (suffix == 16) return "255.255.0.0"
else if (suffix == 24) return "255.255.255.0"
else if (suffix == 32) return "255.255.255.255"
else return suffix
}
function remove_hotplug_iface(iface, line) {
if (line == "") line = $0
if (line !~ / $/) line = line " "
gsub(" " iface " ", " ", line)
return line
}
function print_hotplug_iface(line) {
if (line ~ /^allow-hotplug *$/) {
# une seule interface sur la ligne: ne pas l"afficher
} else {
# supprimer l"interface de la ligne
sub(/ *$/, "", line)
print line
}
}
function remove_auto_iface(iface, line) {
if (line == "") line = $0 " "
gsub(" " iface " ", " ", line)
return line
}
function print_auto_iface(line) {
if (line ~ /^(allow-)?auto *$/) {
# une seule interface sur la ligne: ne pas l"afficher
} else {
# supprimer l"interface de la ligne
sub(/ *$/, "", line)
print line
}
}
'
__DEBIAN_NETWORK_UPDATE_BRIDGE_SCRIPT='
BEGIN {
for (i = 1; i <= ifaces_count; i++) {
have_iface_hotplugs[i] = 0
have_iface_autos[i] = 0
have_iface_manuals[i] = 0
}
for (i = 1; i <= ipspecs_count; i++) {
if (match(ipspecs[i], "^([^/]*)(/([^+]*))?(\\+(.*))?$", vs) != 0) {
ip = vs[1]
suffix = vs[3]
gateway = vs[5]
} else {
ip = ""
suffix = ""
gateway = ""
}
if (i == 1) {
if (ip == "dhcp") {
method = "dhcp"
} else {
method = "static"
mainip = ip
}
}
if (suffix == "") suffix = "24"
ips[i] = ip
suffixes[i] = suffix
gateways[i] = gateway
have_ip_ups[i] = i == 1
have_ip_downs[i] = i == 1
}
}
'
__DEBIAN_NETWORK_UPDATE_IFACE_SCRIPT='
BEGIN {
for (i = 1; i <= ipspecs_count; i++) {
if (match(ipspecs[i], "^([^/]*)(/([^+]*))?(\\+(.*))?$", vs) != 0) {
ip = vs[1]
suffix = vs[3]
gateway = vs[5]
} else {
ip = ""
suffix = ""
gateway = ""
}
if (i == 1) {
if (ip == "dhcp") {
method = "dhcp"
} else {
method = "static"
mainip = ip
}
}
if (suffix == "") suffix = "24"
ips[i] = ip
suffixes[i] = suffix
gateways[i] = gateway
have_ip_ups[i] = i == 1
have_ip_downs[i] = i == 1
}
}
'
function __old_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
# configuration et créer la nouvelle.
local inf="$1"; shift
local outf="$1"; shift
awkrun <"$inf" -f iface="$1" ipspecs[@]="${2:-ipspecs}" ifaces[@]="${3:-ifaces}" '
BEGIN {
have_hotplug = 0
have_auto = 0
have_iface = 0
have_method = 0 # static ou dhcp
in_iface = 0
have_mainip = 0
}
'"
$__DEBIAN_NETWORK_UPDATE_BASE_SCRIPT
$__DEBIAN_NETWORK_UPDATE_BRIDGE_SCRIPT
"'
$1 == "iface" && $2 == iface && $3 == "inet" {
have_iface = 1
if (($0 " ") ~ (" " method " ")) have_method = 1
in_iface = 1
next
}
in_iface && ($0 " ") ~ "^[ \\t]*address " mainip " " { have_mainip = 1 }
in_iface && $0 ~ "^[ \\t]*up (/sbin/)?ip addr add " {
if (match($0, "ip addr add (.*)/.* dev " iface, vs) != 0) {
i = indexof_ip(vs[1])
if (i != 0) have_ip_ups[i] = 1
}
next
}
in_iface && $0 ~ "^[ \\t]*down (/sbin/)?ip addr del " {
if (match($0, "ip addr del (.*)/.* dev " iface, vs) != 0) {
i = indexof_ip(vs[1])
if (i != 0) have_ip_downs[i] = 1
}
next
}
$1 == "iface" {
i = indexof_iface($2)
if (i != 0 && $3 == "inet" && $4 == "manual") {
have_iface_manuals[i] = 1
}
in_iface = 0
next
}
$1 == "allow-hotplug" {
if (($0 " ") ~ (" " iface " ")) have_hotplug = 1
for (i = 1; i <= ifaces_count; i++) {
if (($0 " ") ~ (" " ifaces[i] " ")) iface_have_hotplugs[i] = 1
}
in_iface = 0
next
}
$1 ~ /^(allow-)?auto$/ {
if (($0 " ") ~ (" " iface " ")) have_auto = 1
for (i = 1; i <= ifaces_count; i++) {
if (($0 " ") ~ (" " ifaces[i] " ")) have_iface_autos[i] = 1
}
in_iface = 0
next
}
END {
check_hotplug = !have_hotplug
if (check_hotplug) for (i = 1; i <= ifaces_count; i++) {
if (iface_have_hotplugs[i]) {
check_hotplug = 0
break
}
}
check_auto = have_auto
if (check_auto) for (i = 1; i <= ifaces_count; i++) {
if (!have_iface_autos[i]) {
check_auto = 0
break
}
}
check_iface = have_iface && have_method && have_mainip
check_supplips = 1
for (i = 1; i <= ipspecs_count; i++) {
if (!have_ip_ups[i] || !have_ip_downs[i]) {
check_supplips = 0
break
}
}
if (!check_hotplug || !check_auto || !check_iface || !check_supplips) {
exit 1 # il faut refaire la configuration
}
exit 0 # tout est ok
}
' && return 1
# il faut refaire la configuration
awkrun <"$inf" >"$outf" -f iface="$1" ipspecs[@]="${2:-ipspecs}" ifaces[@]="${3:-ifaces}" '
BEGIN {
have_auto = 0
remove_auto = 0
array_new(remove_iface_autos)
for (i = 1; i <= ifaces_count; i++) {
remove_iface_autos[i] = 0
}
replace_conf = 1
in_iface = 0
found_iface = 0
remove_old_iface = 0
}
'"
$__DEBIAN_NETWORK_UPDATE_BASE_SCRIPT
$__DEBIAN_NETWORK_UPDATE_BRIDGE_SCRIPT
"'
function write_conf() {
replace_conf = 0
for (i = 1; i <= ifaces_count; i++) {
printed = 0
if (!have_iface_autos[i]) {
print "auto " ifaces[i]
printed = 1
}
if (!have_iface_manuals[i]) {
print "iface " ifaces[i] " inet manual"
printed = 1
}
if (printed) print ""
}
if (!have_auto) print "auto " iface
print "iface " iface " inet " method
if (ipspecs_count > 0 && method == "static") {
print " address " ips[1]
print " netmask " get_netmask(suffixes[1])
if (gateways[1] != "" && gateways[1] != ips[1]) {
print " gateway " gateways[1]
}
}
ports = " bridge_ports"
if (ifaces_count == 0) {
ports = ports " none"
} else {
for (i = 1; i <= ifaces_count; i++) {
ports = ports " " ifaces[i]
}
}
print ports
print " bridge_stp off"
print " bridge_fd 2"
print " bridge_maxwait 0"
serial = 0
for (i = 2; i <= ipspecs_count; i++) {
print " up ip addr add " ips[i] "/" suffixes[i] " dev " iface " label " iface ":" serial
print " down ip addr del " ips[i] "/" suffixes[i] " dev " iface " label " iface ":" serial
serial++
}
print ""
}
remove_old_iface && $1 == "iface" {
remove_old_iface = 0
}
!remove_old_iface && replace_conf && !in_iface && $1 == "iface" && $2 == iface && $3 == "inet" {
in_iface = 1
found_iface = 1
next
}
!remove_old_iface && $1 == "iface" {
in_iface = 0
for (i = 1; i <= ifaces_count; i++) {
if ($2 == ifaces[i]) {
if ($3 == "inet" && $4 == "manual") {
have_iface_manuals[i] = 1
break
} else {
remove_old_iface = 1
}
}
}
if (replace_conf && found_iface) write_conf()
if (!remove_old_iface) print
next
}
$1 == "allow-hotplug" {
in_iface = 0
remove_old_iface = 0
if (replace_conf && found_iface) write_conf()
line = $0
if (($0 " ") ~ (" " iface " ")) line = remove_hotplug_iface(iface, line)
for (i = 1; i <= ifaces_count; i++) {
if (($0 " ") ~ (" " ifaces[i] " ")) line = remove_hotplug_iface(ifaces[i], line)
}
print_hotplug_iface(line)
next
}
$1 ~ /(allow-)?auto/ {
in_iface = 0
remove_old_iface = 0
if (replace_conf && found_iface) write_conf()
line = $0
if (($0 " ") ~ (" " iface " ")) {
if (!remove_auto) {
have_auto = 1
remove_auto = 1
} else {
line = remove_auto_iface(iface, line)
}
}
for (i = 1; i <= ifaces_count; i++) {
if (($0 " ") ~ (" " ifaces[i] " ")) {
if (!remove_iface_autos[i]) {
have_iface_autos[i] = 1
remove_iface_autos[i] = 1
} else {
line = remove_auto_iface(ifaces[i], line)
}
}
}
print_auto_iface(line)
next
}
{ print }
END {
if (replace_conf) write_conf()
}
'
return 0
}
function __old_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
# nouvelle.
local inf="$1"; shift
local outf="$1"; shift
awkrun <"$inf" -f iface="$1" ipspecs[@]="${2:-ipspecs}" '
BEGIN {
have_hotplug = 0
have_auto = 0
have_iface = 0
have_method = 0 # static ou dhcp
in_iface = 0
have_mainip = 0
}
'"
$__DEBIAN_NETWORK_UPDATE_BASE_SCRIPT
$__DEBIAN_NETWORK_UPDATE_IFACE_SCRIPT
"'
$1 == "iface" && $2 == iface && $3 == "inet" {
have_iface = 1
if (($0 " ") ~ (" " method " ")) have_method = 1
in_iface = 1
next
}
in_iface && ($0 " ") ~ "^[ \\t]*address " mainip " " { have_mainip = 1 }
in_iface && $0 ~ "^[ \\t]*up (/sbin/)?ip addr add " {
if (match($0, "ip addr add (.*)/.* dev " iface, vs) != 0) {
i = indexof_ip(vs[1])
if (i != 0) have_ip_ups[i] = 1
}
next
}
in_iface && $0 ~ "^[ \\t]*down (/sbin/)?ip addr del " {
if (match($0, "ip addr del (.*)/.* dev " iface, vs) != 0) {
i = indexof_ip(vs[1])
if (i != 0) have_ip_downs[i] = 1
}
next
}
$1 == "iface" {
in_iface = 0
next
}
$1 == "allow-hotplug" {
if (($0 " ") ~ (" " iface " ")) have_hotplug = 1
in_iface = 0
next
}
$1 ~ /^(allow-)?auto$/ {
if (($0 " ") ~ (" " iface " ")) have_auto = 1
in_iface = 0
next
}
END {
check_hotplug = !have_hotplug
check_auto = have_auto
check_iface = have_iface && have_method && have_mainip
check_supplips = 1
for (i = 1; i <= ipspecs_count; i++) {
if (!have_ip_ups[i] || !have_ip_downs[i]) {
check_supplips = 0
break
}
}
if (!check_hotplug || !check_auto || !check_iface || !check_supplips) {
exit 1 # il faut refaire la configuration
}
exit 0 # tout est ok
}
' && return 1
# il faut refaire la configuration
awkrun <"$inf" >"$outf" -f iface="$1" ipspecs[@]="${2:-ipspecs}" '
BEGIN {
have_auto = 0
remove_auto = 0
replace_conf = 1
in_iface = 0
found_iface = 0
}
'"
$__DEBIAN_NETWORK_UPDATE_BASE_SCRIPT
$__DEBIAN_NETWORK_UPDATE_IFACE_SCRIPT
"'
function write_conf() {
replace_conf = 0
if (!have_auto) print "auto " iface
print "iface " iface " inet " method
if (ipspecs_count > 0 && method == "static") {
print " address " ips[1]
print " netmask " get_netmask(suffixes[1])
if (gateways[1] != "" && gateways[1] != ips[1]) {
print " gateway " gateways[1]
}
}
serial = 0
for (i = 2; i <= ipspecs_count; i++) {
print " up ip addr add " ips[i] "/" suffixes[i] " dev " iface " label " iface ":" serial
print " down ip addr del " ips[i] "/" suffixes[i] " dev " iface " label " iface ":" serial
serial++
}
print ""
}
($0 " ") ~ ("^allow-hotplug.* " iface " ") { print_hotplug_iface(remove_hotplug_iface(iface)); next; }
!remove_auto && ($0 " ") ~ ("^(allow-)?auto.* " iface " ") { have_auto = 1; remove_auto = 1; next; }
remove_auto && ($0 " ") ~ ("^(allow-)?auto.* " iface " ") { print_auto_iface(remove_auto_iface(iface)); next; }
replace_conf && !in_iface && $0 ~ ("^iface " iface " inet ") { in_iface = 1; found_iface = 1; next; }
replace_conf && found_iface && in_iface && $0 ~ "^iface " { print; in_iface = 0; write_conf(); next; }
{ print }
END {
if (replace_conf) write_conf()
}
'
return 0
}
function network_config() {
# (Re)configurer le réseau sur l'hôte courant.
# $1 (host) est le nom d'hôte.

View File

@ -57,14 +57,30 @@ END { exit status }' <<<"$1"
function ipcalc_checkmask() {
# vérifier le masque de sous-réseau $1 pour voir si elle est valide. Si oui,
# afficher le suffixe (0, 8, 16, 24, 32) associé. Sinon retourner 1
local mask="$1"
[ "$mask" == 0.0.0.0 ] && mask=0
[ "$mask" == 255.0.0.0 ] && mask=8
[ "$mask" == 255.255.0.0 ] && mask=16
[ "$mask" == 255.255.255.0 ] && mask=24
[ "$mask" == 255.255.255.255 ] && mask=32
[ "$mask" == 0 -o "$mask" == 8 -o "$mask" == 16 -o "$mask" == 24 -o "$mask" == 32 ] || return 1
echo "$mask"
case "$1" in
0|0.0.0.0) echo 0;;
8|255.0.0.0) echo 8;;
16|255.255.0.0) echo 16;;
24|255.255.255.0) echo 24;;
32|255.255.255.255) echo 32;;
*) return 1;;
esac
return 0
}
function ipcalc_netmask() {
# à partir d'un suffixe (0, 8, 16, 24, 32) ou d'un masque de sous-réseau,
# afficher le masque de sous-réseau. si le suffixe ou le masque ne sont pas
# reconnus, retourner 1
case "$1" in
0|0.0.0.0) echo 0.0.0.0;;
8|255.0.0.0) echo 255.0.0.0;;
16|255.255.0.0) echo 255.255.0.0;;
24|255.255.255.0) echo 255.255.255.0;;
32|255.255.255.255) echo 255.255.255.255;;
*) return 1;;
esac
return 0
}
function ipcalc_broadcast() {