vérifier le fichier /etc/network/interfaces

This commit is contained in:
Jephte CLAIN 2014-01-27 20:34:04 +04:00
parent aecf51721d
commit 01a837b103
1 changed files with 146 additions and 6 deletions

View File

@ -114,11 +114,18 @@ END {
__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS=
function __network_backup() {
local file="$1"
local backup="$file.orig$(date +%Y%m%d)"
[ -f "$backup" ] || cat "$file" >"$backup" 2>/dev/null
}
function __network_hostname() {
local hostname="${1%%.*}"
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" ]; then
uecho "Setting /etc/hostname to $hostname"
else
__network_backup /etc/hostname
echo "$hostname" >/etc/hostname
fi
}
@ -128,6 +135,7 @@ function __network_mailname() {
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" ]; then
uecho "Setting /etc/mailname to $host"
else
__network_backup /etc/mailname
echo "$host" >/etc/mailname
fi
}
@ -146,9 +154,11 @@ function __network_fix_exim4() {
uecho "/etc/exim4/update-exim4.conf.conf: pas de modifications"
fi
elif testdiff "$tmpfile" /etc/exim4/update-exim4.conf.conf; then
__network_backup /etc/exim4/update-exim4.conf.conf
cat "$tmpfile" >/etc/exim4/update-exim4.conf.conf
update-exim4.conf
fi
ac_clean "$tmpfile"
}
function __network_hosts() {
@ -171,11 +181,37 @@ $ip$TAB$host $hostname" "$tmpfile"
uecho "/etc/hosts: pas de modifications"
fi
elif testdiff "$tmpfile" /etc/hosts; then
__network_backup /etc/hosts
cat "$tmpfile" >/etc/hosts
fi
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() {
# confip --> iface [ipspecs@] [ipspecs]
local __npc_tmp
@ -265,6 +301,8 @@ function __network_resolve_mainiface() {
}
function __network_set_mainip() {
# XXX modifier pour en faire set_confips, qui initialise confips en fonction
# des adresses actuelles, principale et supplémentaire
eval "$(ip addr show dev "$1" | awk "BEGIN {
mainipvar = \"${2:-mainip}\"
netmaskvar = \"${3:-netmask}\"
@ -308,7 +346,9 @@ function __network_update_bridge() {
# 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.
awkrun -f iface="$1" ipspecs="${2:-ipspecs}[@]" ifaces="${3:-ifaces}[@]" '
local inf="$1"; shift
local outf="$1"; shift
awkrun <"$inf" >"$outf" -f iface="$1" ipspecs[@]="${2:-ipspecs}" ifaces[@]="${3:-ifaces}" '
{ print }
'
}
@ -318,9 +358,106 @@ function __network_update_iface() {
# adresse ip principale statique ou en dhcp, adresses ip supplémentaires),
# puis si nécessaire, supprimer l'ancienne configuration et créer la
# nouvelle.
awkrun -f iface="$1" ipspecs[@] '
local inf="$1"; shift
local outf="$1"; shift
local IPSPECS_SCRIPT='
method = "dhcp"
ips_count = ipspecs_count
array_new(ips)
array_new(masks)
array_new(gateways)
array_new(have_ups)
array_new(have_downs)
for (i = 1; i <= ips_count; i++) {
if (match(ipspecs[i], "^(.*)(/(.*))?(\\+(.*))?$", vs) != 0) {
ip = vs[1]
mask = vs[3]
gateway = vs[5]
} else {
ip = ""
mask = ""
gateway = ""
}
if (i == 1) {
if (ip == "dhcp") method = "dhcp"
else method = "static"
}
if (mask == "") mask = "255.255.255.0"
ips[i] = ip
#masks[i] = mask
#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++) {
if (ips[i] == ip) {
return i
}
}
return 0
}
($0 " ") ~ ("^allow-hotplug.* " iface " ") { have_hotplug = 1; next }
($0 " ") ~ ("^(allow-)?auto.* " iface " ") { have_auto = 1; next }
$0 ~ ("^iface " iface " inet ") {
have_iface = 1
if (($0 " ") ~ (" " method " ")) have_method = 1
in_iface = 1
next
}
$0 ~ ("^iface ") { in_inface = 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) {
i = indexof_ip(vs[1])
if (i != 0) have_ups[i] = 1
}
next
}
in_iface && $0 ~ "^[ ]*down (/sbin/)?ip addr del " {
if (match($0, "ip addr del (.*)/.* dev " iface, vs) != 0) {
i = indexof_ip(vs[1])
if (i != 0) have_downs[i] = 1
}
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_ups[i] || !have_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}" '
{ print }
'
return 0
}
function network_config() {
@ -430,8 +567,9 @@ function network_config() {
fi
done
__network_update_bridge "$br" ipspecs ifaces <"$interfaces" >"$workfile"
cat "$workfile" >"$interfaces"
if __network_update_bridge "$interfaces" "$workfile" "$br" ipspecs ifaces; then
cat "$workfile" >"$interfaces"
fi
done
# configurer chaque interface classique
@ -443,8 +581,9 @@ function network_config() {
__network_parse_ipspec "${ipspecs[0]}" mainip mask gateway
fi
__network_update_iface "$iface" ipspecs <"$interfaces" >"$workfile"
cat "$workfile" >"$interfaces"
if __network_update_iface "$interfaces" "$workfile" "$iface" ipspecs; then
cat "$workfile" >"$interfaces"
fi
done
# Fin de traitement
@ -456,6 +595,7 @@ function network_config() {
uecho "/etc/network/interfaces: pas de modifications"
fi
elif testdiff "$interfaces" /etc/network/interfaces; then
__network_backup /etc/network/interfaces
cat "$interfaces" >/etc/network/interfaces
fi
ac_clean "$interfaces" "$workfile"