configuration des bridges

This commit is contained in:
Jephté Clain 2014-01-28 10:53:07 +04:00
parent ac46456273
commit 529a68ebd5
1 changed files with 387 additions and 111 deletions

View File

@ -194,31 +194,6 @@ $ip$TAB$host $hostname" "$tmpfile"
ac_clean "$tmpfile" ac_clean "$tmpfile"
} }
function __network_reset_interfaces() {
local tmpfile; ac_set_tmpfile tmpfile
echo >"$tmpfile" "\
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
"
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" ]; then
if testdiff "$tmpfile" /etc/network/interfaces; then
uecho "Setting /etc/network/interfaces to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
else
uecho "/etc/network/interfaces: pas de modifications"
fi
elif testdiff "$tmpfile" /etc/network/interfaces; then
__network_backup /etc/network/interfaces
cat "$tmpfile" >/etc/network/interfaces
fi
ac_clean "$tmpfile"
}
function __network_parse_confip() { function __network_parse_confip() {
# confip --> iface [ipspecs@] [ipspecs] # confip --> iface [ipspecs@] [ipspecs]
local __npc_tmp local __npc_tmp
@ -352,36 +327,82 @@ $1 == "0.0.0.0" && $8 == iface {
print gatewayvar "=\"" $2 "\"" print gatewayvar "=\"" $2 "\""
}')" }')"
} }
#BUG" #BUG")"
function __network_update_bridge() { __DEBIAN_NETWORK_UPDATE_BASE_SCRIPT='
# vérifier la configuration (interfaces de ifaces[@] en manual, présence du BEGIN {
# 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" >"$outf" -f iface="$1" ipspecs[@]="${2:-ipspecs}" ifaces[@]="${3:-ifaces}" '
{ print }
'
}
function __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
local IPSPECS_SCRIPT='BEGIN {
method = "dhcp" method = "dhcp"
ips_count = ipspecs_count array_new(have_iface_hotplugs)
array_new(have_iface_autos)
array_new(have_iface_manuals)
array_new(ips) array_new(ips)
array_new(suffixes) array_new(suffixes)
array_new(gateways) array_new(gateways)
array_new(have_ups) array_new(have_ip_ups)
array_new(have_downs) array_new(have_ip_downs)
for (i = 1; i <= ips_count; i++) { }
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) { if (match(ipspecs[i], "^([^/]*)(/([^+]*))?(\\+(.*))?$", vs) != 0) {
ip = vs[1] ip = vs[1]
suffix = vs[3] suffix = vs[3]
@ -395,23 +416,280 @@ function __network_update_iface() {
if (ip == "dhcp") method = "dhcp" if (ip == "dhcp") method = "dhcp"
else method = "static" else method = "static"
} }
if (suffix == "") suffix = "255.255.255.0" if (suffix == "") suffix = "24"
ips[i] = ip ips[i] = ip
suffixes[i] = suffix suffixes[i] = suffix
gateways[i] = gateway gateways[i] = gateway
have_ups[i] = 0 have_ip_ups[i] = 0
have_downs[i] = 0 have_ip_downs[i] = 0
} }
} }
function indexof_ip(ip, i) {
for (i = 1; i = ips_count; i++) {
if (ips[i] == ip) {
return i
}
}
return 0
}
' '
__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"
}
if (suffix == "") suffix = "24"
ips[i] = ip
suffixes[i] = suffix
gateways[i] = gateway
have_ip_ups[i] = 0
have_ip_downs[i] = 0
}
}
'
function __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])
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 __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}" ' awkrun <"$inf" -f iface="$1" ipspecs[@]="${2:-ipspecs}" '
BEGIN { BEGIN {
have_hotplug = 0 have_hotplug = 0
@ -421,32 +699,43 @@ BEGIN {
in_iface = 0 in_iface = 0
have_mainip = 0 have_mainip = 0
} }
'"$IPSPECS_SCRIPT"' '"
$__DEBIAN_NETWORK_UPDATE_BASE_SCRIPT
$__DEBIAN_NETWORK_UPDATE_IFACE_SCRIPT
"'
($0 " ") ~ ("^allow-hotplug.* " iface " ") { have_hotplug = 1; next } $1 == "iface" && $2 == iface && $3 == "inet" {
($0 " ") ~ ("^(allow-)?auto.* " iface " ") { have_auto = 1; next }
$0 ~ ("^iface " iface " inet ") {
have_iface = 1 have_iface = 1
if (($0 " ") ~ (" " method " ")) have_method = 1 if (($0 " ") ~ (" " method " ")) have_method = 1
in_iface = 1 in_iface = 1
next next
} }
$0 ~ ("^iface ") { in_iface = 0; next; } in_iface && ($0 " ") ~ "^[ \\t]*address " mainip " " { have_mainip = 1 }
in_iface && $0 ~ "^[ ]*address " mainip { have_mainip = 1 } in_iface && $0 ~ "^[ \\t]*up (/sbin/)?ip addr add " {
in_iface && $0 ~ "^[ ]*up (/sbin/)?ip addr add " {
if (match($0, "ip addr add (.*)/.* dev " iface, vs) != 0) { if (match($0, "ip addr add (.*)/.* dev " iface, vs) != 0) {
i = indexof_ip(vs[1]) i = indexof_ip(vs[1])
if (i != 0) have_ups[i] = 1 if (i != 0) have_ip_ups[i] = 1
} }
next next
} }
in_iface && $0 ~ "^[ ]*down (/sbin/)?ip addr del " { in_iface && $0 ~ "^[ \\t]*down (/sbin/)?ip addr del " {
if (match($0, "ip addr del (.*)/.* dev " iface, vs) != 0) { if (match($0, "ip addr del (.*)/.* dev " iface, vs) != 0) {
i = indexof_ip(vs[1]) i = indexof_ip(vs[1])
if (i != 0) have_downs[i] = 1 if (i != 0) have_ip_downs[i] = 1
} }
next 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 { END {
check_hotplug = !have_hotplug check_hotplug = !have_hotplug
@ -454,7 +743,7 @@ END {
check_iface = have_iface && have_method && have_mainip check_iface = have_iface && have_method && have_mainip
check_supplips = 1 check_supplips = 1
for (i = 1; i <= ipspecs_count; i++) { for (i = 1; i <= ipspecs_count; i++) {
if (!have_ups[i] || !have_downs[i]) { if (!have_ip_ups[i] || !have_ip_downs[i]) {
check_supplips = 0 check_supplips = 0
break break
} }
@ -474,60 +763,35 @@ BEGIN {
remove_auto = 0 remove_auto = 0
replace_conf = 1 replace_conf = 1
in_iface = 0 in_iface = 0
found_iface = 0
} }
'"$IPSPECS_SCRIPT"' '"
function remove_hotplug_iface() { $__DEBIAN_NETWORK_UPDATE_BASE_SCRIPT
line = $0 " "; gsub(" " iface " ", " ", line) $__DEBIAN_NETWORK_UPDATE_IFACE_SCRIPT
if (line ~ /^allow-hotplug *$/) { "'
# une seule interface sur la ligne: la commenter
print "#" $0
} else {
# supprimer l"interface de la ligne
sub(/ *$/, "", line)
print line
}
}
function remove_auto_iface() {
line = $0 " "; gsub(" " iface " ", " ", line)
if (line ~ /^(allow-)?auto *$/) {
# une seule interface sur la ligne: la commenter
print "#" $0
} else {
# supprimer l"interface de la ligne
sub(/ *$/, "", line)
print line
}
}
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 write_conf() { function write_conf() {
replace_conf = 0 replace_conf = 0
if (!have_auto) print "auto " iface if (!have_auto) print "auto " iface
print "iface " iface " inet " method print "iface " iface " inet " method
if (ips_count > 0 && method == "static") { if (ipspecs_count > 0 && method == "static") {
print " address " ips[1] print " address " ips[1]
print " netmask " get_netmask(suffixes[1]) print " netmask " get_netmask(suffixes[1])
print " gateway " gateways[1] print " gateway " gateways[1]
} }
serial = 0 serial = 0
for (i = 2; i <= ips_count; i++) { for (i = 2; i <= ipspecs_count; i++) {
print " up ip addr add " ips[i] "/" suffixes[i] " dev " iface " label " iface ":" serial 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 print " down ip addr del " ips[i] "/" suffixes[i] " dev " iface " label " iface ":" serial
serial++ serial++
} }
print ""
} }
($0 " ") ~ ("^allow-hotplug.* " iface " ") { remove_hotplug_iface(); next; } ($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 " ") { have_auto = 1; remove_auto = 1; next; }
remove_auto && ($0 " ") ~ ("^(allow-)?auto.* " iface " ") { remove_auto_iface(); 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; next; } replace_conf && !in_iface && $0 ~ ("^iface " iface " inet ") { in_iface = 1; found_iface = 1; next; }
replace_conf && in_iface && $0 ~ "^iface " { print; in_iface = 0; write_conf(); next; } replace_conf && found_iface && in_iface && $0 ~ "^iface " { print; in_iface = 0; write_conf(); next; }
{ print } { print }
END { END {
if (replace_conf) write_conf() if (replace_conf) write_conf()
@ -567,7 +831,7 @@ function network_config() {
[ -n "$2" ] && array_copy __nc_confips "$2" [ -n "$2" ] && array_copy __nc_confips "$2"
[ -n "$3" ] && array_copy __nc_confbrs "$3" [ -n "$3" ] && array_copy __nc_confbrs "$3"
local host="$1" mainiface="$4" local host="$1" mainiface="$4" reset_interfaces="$5"
local -a confips confbrs local -a confips confbrs
array_copy confips __nc_confips array_copy confips __nc_confips
array_copy confbrs __nc_confbrs array_copy confbrs __nc_confbrs
@ -610,6 +874,7 @@ function network_config() {
local -a confstdips confbrips local -a confstdips confbrips
for confip in "${confips[@]}"; do for confip in "${confips[@]}"; do
__network_parse_confip "$confip" iface __network_parse_confip "$confip" iface
[ -n "$iface" ] || iface="$mainiface"
if array_contains brifaces "$iface"; then if array_contains brifaces "$iface"; then
array_add confbrips "$confip" array_add confbrips "$confip"
else else
@ -623,6 +888,17 @@ function network_config() {
ac_set_tmpfile workfile ac_set_tmpfile workfile
cat /etc/network/interfaces >"$interfaces" cat /etc/network/interfaces >"$interfaces"
if [ -n "$reset_interfaces" ]; then
echo >"$interfaces" "\
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
"
fi
# configurer chaque bridge # configurer chaque bridge
local -a tmpifaces local -a tmpifaces
local tmpbr local tmpbr
@ -638,7 +914,7 @@ function network_config() {
for confbr in "${confbrs[@]}"; do for confbr in "${confbrs[@]}"; do
__network_parse_confbr "$confbr" tmpbr tmpifaces __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