maj du fichier interfaces

This commit is contained in:
Jephte CLAIN 2014-01-28 00:13:37 +04:00
parent 01a837b103
commit a0130ab206
1 changed files with 98 additions and 29 deletions

View File

@ -221,10 +221,10 @@ function __network_parse_confip() {
}
function __network_parse_ipspec() {
# ipspec --> ip mask gateway
# ipspec --> ip suffix gateway
local __npi_tmp
splitfsep "$1" / "${2:-ip}" __npi_tmp
splitfsep "$__npi_tmp" + "${3:-mask}" "${4:-gateway}"
splitfsep "$__npi_tmp" + "${3:-suffix}" "${4:-gateway}"
}
function __network_parse_confbr() {
@ -238,7 +238,7 @@ function __network_parse_confbr() {
function __network_fix_confips() {
# confips@
local -a __nfc_confips __nfc_ipspecs __nfc_ifaces __nfc_ips
local __nfc_confip __nfc_iface __nfc_ipspec __nfc_ip __nfc_mask __nfc_gateway
local __nfc_confip __nfc_iface __nfc_ipspec __nfc_ip __nfc_suffix __nfc_gateway __nfc_first
array_copy __nfc_confips "${1:-confips}"
# recenser les interfaces et créer les tableaux __IFACE_ipspecs
@ -251,9 +251,15 @@ function __network_fix_confips() {
for __nfc_confip in "${__nfc_confips[@]}"; do
__network_parse_confip "$__nfc_confip" __nfc_iface __nfc_ipspecs
for __nfc_ipspec in "${__nfc_ipspecs[@]}"; do
__network_parse_ipspec "$__nfc_ipspec" __nfc_ip __nfc_mask __nfc_gateway
__network_parse_ipspec "$__nfc_ipspec" __nfc_ip __nfc_suffix __nfc_gateway
if ! array_contains __nfc_ips "$__nfc_ip"; then
eval "array_add __nfc_${__nfc_iface}_ipspecs \"$__nfc_ipspec\""
[ -n "$__nfc_suffix" ] || __nfc_suffix=24
if [ -z "$__nfc_gateway" ] && array_isempty "__nfc_${__nfc_iface}_ipspecs"; then
# premier ipspec, il faut spécifier le gateway
__nfc_gateway="$(ipcalc_gateway "$__nfc_ip" "$__nfc_suffix")"
fi
eval "array_add __nfc_${__nfc_iface}_ipspecs \"$__nfc_ip/$__nfc_suffix${__nfc_gateway:++$__nfc_gateway}\""
array_add __nfc_ips "$__nfc_ip"
fi
done
@ -270,7 +276,7 @@ function __network_fix_confips() {
function __network_fix_confbrs() {
# confbrs@
local -a __nfc_confbrs __nfc_ifaces __nfc_brs __nfc_ips
local __nfc_confbr __nfc_br __nfc_iface __nfc_ip __nfc_mask __nfc_gateway
local __nfc_confbr __nfc_br __nfc_iface __nfc_ip __nfc_suffix __nfc_gateway
array_copy __nfc_confbrs "${1:-confbrs}"
# recenser les bridges et créer les tableaux __BR_ifaces
@ -360,45 +366,35 @@ function __network_update_iface() {
# nouvelle.
local inf="$1"; shift
local outf="$1"; shift
local IPSPECS_SCRIPT='
local IPSPECS_SCRIPT='BEGIN {
method = "dhcp"
ips_count = ipspecs_count
array_new(ips)
array_new(masks)
array_new(suffixes)
array_new(gateways)
array_new(have_ups)
array_new(have_downs)
for (i = 1; i <= ips_count; i++) {
if (match(ipspecs[i], "^(.*)(/(.*))?(\\+(.*))?$", vs) != 0) {
if (match(ipspecs[i], "^([^/]*)(/([^+]*))?(\\+(.*))?$", vs) != 0) {
ip = vs[1]
mask = vs[3]
suffix = vs[3]
gateway = vs[5]
} else {
ip = ""
mask = ""
suffix = ""
gateway = ""
}
if (i == 1) {
if (ip == "dhcp") method = "dhcp"
else method = "static"
}
if (mask == "") mask = "255.255.255.0"
if (suffix == "") suffix = "255.255.255.0"
ips[i] = ip
#masks[i] = mask
#gateways[i] = gateway
suffixes[i] = suffix
gateways[i] = gateway
have_ups[i] = 0
have_downs[i] = 0
}
'
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
'"$IPSPECS_SCRIPT"'
}
function indexof_ip(ip, i) {
for (i = 1; i = ips_count; i++) {
@ -408,6 +404,17 @@ function indexof_ip(ip, i) {
}
return 0
}
'
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
}
'"$IPSPECS_SCRIPT"'
($0 " ") ~ ("^allow-hotplug.* " iface " ") { have_hotplug = 1; next }
($0 " ") ~ ("^(allow-)?auto.* " iface " ") { have_auto = 1; next }
@ -417,7 +424,7 @@ $0 ~ ("^iface " iface " inet ") {
in_iface = 1
next
}
$0 ~ ("^iface ") { in_inface = 0; next; }
$0 ~ ("^iface ") { in_iface = 0; next; }
in_iface && $0 ~ "^[ ]*address " mainip { have_mainip = 1 }
in_iface && $0 ~ "^[ ]*up (/sbin/)?ip addr add " {
if (match($0, "ip addr add (.*)/.* dev " iface, vs) != 0) {
@ -455,7 +462,69 @@ END {
# 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
}
'"$IPSPECS_SCRIPT"'
function remove_hotplug_iface() {
line = $0 " "; gsub(" " iface " ", " ", line)
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() {
replace_conf = 0
if (!have_auto) print "auto " iface
print "iface " iface " inet " method
if (ips_count > 0 && method == "static") {
print " address " ips[1]
print " netmask " get_netmask(suffixes[1])
print " gateway " gateways[1]
}
serial = 0
for (i = 2; i <= ips_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++
}
}
($0 " ") ~ ("^allow-hotplug.* " iface " ") { remove_hotplug_iface(); next; }
!remove_auto && ($0 " ") ~ ("^(allow-)?auto.* " iface " ") { have_auto = 1; remove_auto = 1; next; }
remove_auto && ($0 " ") ~ ("^(allow-)?auto.* " iface " ") { remove_auto_iface(); next; }
replace_conf && !in_iface && $0 ~ ("^iface " iface " inet ") { in_iface = 1; next; }
replace_conf && in_iface && $0 ~ "^iface " { print; in_iface = 0; write_conf(); next; }
{ print }
END {
if (replace_conf) write_conf()
}
'
return 0
}
@ -471,7 +540,7 @@ function network_config() {
# Si un des arguments n'est pas spécifié, il est ignoré.
# Le tableau confips doit contenir des définitions d'une des formes
# suivantes:
# [iface:]address[/mask][+gateway],...
# [iface:]address[/suffix][+gateway],...
# [iface:]dhcp
# La deuxième forme est pour spécifier qu'une interface est configurée par
# DHCP. iface vaut par défaut eth0, sauf si une définition de bridge
@ -501,7 +570,7 @@ function network_config() {
local mainbr # bridge principal
local mainip # adresse IP principale de l'interface principale
local confbr confip br iface mask gateway
local confbr confip br iface suffix gateway
local -a ipspecs
__network_resolve_mainiface mainiface "${confbrs[0]}" "${confips[0]}"
@ -555,7 +624,7 @@ function network_config() {
[ -n "$br" ] || br="$mainbr"
if [ -z "$mainip" -a "$br" == "$mainiface" ]; then
__network_parse_ipspec "${ipspecs[0]}" mainip mask gateway
__network_parse_ipspec "${ipspecs[0]}" mainip suffix gateway
fi
ifaces=()
@ -578,7 +647,7 @@ function network_config() {
[ -n "$iface" ] || iface="$mainiface"
if [ -z "$mainip" -a "$iface" == "$mainiface" ]; then
__network_parse_ipspec "${ipspecs[0]}" mainip mask gateway
__network_parse_ipspec "${ipspecs[0]}" mainip suffix gateway
fi
if __network_update_iface "$interfaces" "$workfile" "$iface" ipspecs; then