2013-08-27 15:14:44 +04:00
|
|
|
##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
|
|
|
## Outils pour les distributions debian
|
|
|
|
##@cooked nocomments
|
|
|
|
##@require base
|
|
|
|
##@require sysinfos
|
|
|
|
##@require service
|
|
|
|
uprovide debian
|
2014-02-04 16:59:32 +04:00
|
|
|
urequire base sysinfos service ipcalc
|
2013-08-27 15:14:44 +04:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Gestion des packages
|
|
|
|
|
|
|
|
function pkg_check() {
|
|
|
|
# Vérifier que les packages sont installés sur le système
|
|
|
|
local pkg status
|
|
|
|
for pkg in "$@"; do
|
2017-07-26 11:44:17 +04:00
|
|
|
LANG=C setx status=dpkg -s "$pkg" 2>/dev/null || return 1
|
|
|
|
setx status=grep '^Status:' <<<"$status"
|
2013-08-27 15:14:44 +04:00
|
|
|
[[ "$status" == *not-installed* ]] && return 1
|
|
|
|
[[ "$status" == *deinstall* ]] && return 1
|
|
|
|
done
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function pkg_update() {
|
|
|
|
# Mettre à jour la liste des packages silencieusement sans confirmation
|
|
|
|
LANG=C apt-get update -qy
|
|
|
|
}
|
|
|
|
|
|
|
|
function pkg_upgrade() {
|
|
|
|
# Mettre à jour la liste des packages silencieusement sans confirmation
|
|
|
|
LANG=C apt-get upgrade -qy
|
|
|
|
}
|
|
|
|
|
|
|
|
function pkg_install() {
|
|
|
|
# Installer les packages silencieusement et sans confirmation
|
|
|
|
LANG=C apt-get install -qy "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
function pkg_installm() {
|
|
|
|
# Installer les packages silencieusement et sans confirmation
|
|
|
|
# Retourner 0 si au moins un des packages a été installé. Sinon, les
|
|
|
|
# packages n'ont pas été installés, soit parce qu'ils sont déjà installé,
|
|
|
|
# soit parce qu'il y a eu une erreur.
|
|
|
|
if ! pkg_check "$@"; then
|
|
|
|
# essayer d'installer les packages
|
|
|
|
pkg_install "$@"
|
|
|
|
else
|
|
|
|
# aucun package n'a été installé
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-04-05 12:23:10 +04:00
|
|
|
function pkg_check_install() {
|
|
|
|
# Si le programme $1 n'existe pas, alors installer les packages $2..$@
|
|
|
|
# S'il n'y a pas d'arguments $2..$@ utiliser $1 comme nom de package
|
|
|
|
# Retourner 0 si au moins un des packages a été installé
|
|
|
|
progexists "$1" && return 1
|
|
|
|
[ $# -gt 1 ] && shift
|
|
|
|
pkg_installm "$@"
|
|
|
|
}
|
|
|
|
|
2017-10-12 19:47:53 +04:00
|
|
|
function require_pkgs() {
|
|
|
|
# installer les packages spécifiés silencieusement et sans confirmation.
|
|
|
|
# si les packages n'existent pas ou ne peuvent pas être installé, c'est une
|
|
|
|
# erreur fatale.
|
|
|
|
if ! pkg_check "$@"; then
|
|
|
|
# essayer d'installer les packages
|
|
|
|
enote "Installation de packages $*"
|
|
|
|
pkg_install "$@" || die
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-08-27 15:14:44 +04:00
|
|
|
################################################################################
|
|
|
|
# Gestion des services
|
|
|
|
|
|
|
|
function service_disable() {
|
|
|
|
# Désactiver le service $1 pour qu'il ne se lance pas automatiquement au
|
|
|
|
# démarrage
|
2017-07-18 09:31:50 +04:00
|
|
|
if [ -f "/etc/init.d/$1" ]; then
|
|
|
|
update-rc.d -f "$1" remove
|
|
|
|
elif [ -f "/etc/systemd/system/$1.service" ]; then
|
|
|
|
systemctl disable "$1"
|
|
|
|
fi
|
2013-08-27 15:14:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function service_enable() {
|
|
|
|
# Activer le service $1 pour qu'il se lance automatiquement au démarrage
|
2017-07-18 09:31:50 +04:00
|
|
|
if [ -f "/etc/init.d/$1" ]; then
|
|
|
|
update-rc.d "$1" defaults
|
|
|
|
elif [ -f "/etc/systemd/system/$1.service" ]; then
|
|
|
|
systemctl daemon-reload
|
|
|
|
systemctl enable "$1"
|
|
|
|
fi
|
2013-08-27 15:14:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Gestion des interfaces réseau
|
|
|
|
|
2014-01-27 17:13:57 +04:00
|
|
|
__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS=
|
2014-02-26 23:29:30 +04:00
|
|
|
__DEBIAN_NETWORK_INTERFACES=/etc/network/interfaces
|
2014-01-27 17:13:57 +04:00
|
|
|
|
2014-02-07 22:48:03 +04:00
|
|
|
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
|
2014-02-26 23:29:30 +04:00
|
|
|
local nifile="${3:-$__DEBIAN_NETWORK_INTERFACES}"
|
2014-02-07 22:48:03 +04:00
|
|
|
|
2014-02-04 16:59:32 +04:00
|
|
|
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
|
2014-02-07 22:48:03 +04:00
|
|
|
}'
|
2014-02-04 16:59:32 +04:00
|
|
|
}
|
|
|
|
|
2014-02-07 22:48:03 +04:00
|
|
|
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
|
2014-02-26 23:29:30 +04:00
|
|
|
local nifile="${4:-$__DEBIAN_NETWORK_INTERFACES}"
|
2014-02-07 22:48:03 +04:00
|
|
|
|
|
|
|
awkrun <"$nifile" -f iface="$iface" gateway="$gateway" ipsuffixes[@]=ipsuffixes '
|
2014-02-04 16:59:32 +04:00
|
|
|
BEGIN {
|
|
|
|
have_auto = 0
|
|
|
|
have_hotplug = 0
|
|
|
|
have_iface = 0
|
|
|
|
have_method = 0 # static ou dhcp
|
|
|
|
have_gateway = ipsuffixes[1] == "dhcp" || gateway == ""
|
2016-12-30 18:14:01 +04:00
|
|
|
if (gateway == "none" || gateway == "-") gateway = ""
|
2014-02-04 16:59:32 +04:00
|
|
|
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
|
2014-02-01 18:40:59 +04:00
|
|
|
}
|
|
|
|
|
2014-02-04 16:59:32 +04:00
|
|
|
$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++) {
|
|
|
|
if ($i == iface) {
|
|
|
|
have_auto = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
in_iface = 0
|
|
|
|
next
|
|
|
|
}
|
|
|
|
$1 == "allow-hotplug" {
|
|
|
|
for (i = 2; i <= NF; i++) {
|
|
|
|
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
|
2014-02-07 22:48:03 +04:00
|
|
|
}'
|
2014-02-01 18:40:59 +04:00
|
|
|
}
|
|
|
|
|
2014-02-07 14:52:17 +04:00
|
|
|
function __network_interfaces_remove_iface() {
|
|
|
|
awkrun -f iface="$1" '
|
|
|
|
function match_iface(iface, line) {
|
|
|
|
if (line == "") line = $0 " "
|
|
|
|
return line ~ (" " iface " ")
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
in_iface = 0
|
|
|
|
found_iface = 0
|
|
|
|
modified = 0
|
|
|
|
}
|
|
|
|
!found_iface && $1 == "iface" && $2 == iface && $3 == "inet" {
|
|
|
|
in_iface = 1
|
|
|
|
found_iface = 1
|
|
|
|
modified = 1
|
|
|
|
next
|
|
|
|
}
|
|
|
|
$1 == "iface" {
|
|
|
|
in_iface = 0
|
|
|
|
}
|
|
|
|
$1 ~ /^(allow-)?auto$/ {
|
|
|
|
in_iface = 0
|
|
|
|
if (match_iface(iface)) {
|
|
|
|
print_auto_iface(remove_auto_iface(iface))
|
|
|
|
modified = 1
|
|
|
|
next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$1 == "allow-hotplug" {
|
|
|
|
in_iface = 0
|
|
|
|
if (match_iface(iface)) {
|
|
|
|
print_hotplug_iface(remove_hotplug_iface(iface))
|
|
|
|
modified = 1
|
|
|
|
next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
!in_iface { print }
|
|
|
|
END {
|
|
|
|
if (modified) exit 0
|
|
|
|
else exit 1
|
|
|
|
}'
|
|
|
|
}
|
2014-02-01 18:40:59 +04:00
|
|
|
function network_interfaces_remove_iface() {
|
2014-02-07 14:52:17 +04:00
|
|
|
# Supprimer dans le fichier $2(=/etc/network/interfaces) toute la
|
|
|
|
# configuration qui concerne l'interface $1
|
2014-02-26 23:29:30 +04:00
|
|
|
local iface="$1" nifile="${2:-$__DEBIAN_NETWORK_INTERFACES}"
|
2014-02-07 14:52:17 +04:00
|
|
|
local tmpfile; ac_set_tmpfile tmpfile
|
|
|
|
local modified
|
2014-02-07 22:48:03 +04:00
|
|
|
|
2014-02-07 14:52:17 +04:00
|
|
|
if __network_interfaces_remove_iface "$iface" <"$nifile" >"$tmpfile"; then
|
|
|
|
cat "$tmpfile" >"$nifile"
|
|
|
|
modified=1
|
|
|
|
fi
|
|
|
|
ac_clean "$tmpfile"
|
|
|
|
[ -n "$modified" ]
|
|
|
|
}
|
|
|
|
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}"
|
2014-02-07 22:48:03 +04:00
|
|
|
|
|
|
|
local -a ifaces; array_copy ifaces __niri_ifaces
|
2014-02-26 23:29:30 +04:00
|
|
|
local nifile="${2:-$__DEBIAN_NETWORK_INTERFACES}"
|
2014-02-07 14:52:17 +04:00
|
|
|
local workfile; ac_set_tmpfile workfile
|
|
|
|
local tmpfile; ac_set_tmpfile tmpfile
|
|
|
|
local iface modified
|
2014-02-07 22:48:03 +04:00
|
|
|
|
2014-02-07 14:52:17 +04:00
|
|
|
cat "$nifile" >"$workfile"
|
|
|
|
for iface in "${__niri_ifaces[@]}"; do
|
|
|
|
if __network_interfaces_remove_iface "$iface" <"$workfile" >"$tmpfile"; then
|
|
|
|
cat "$tmpfile" >"$workfile"
|
|
|
|
modified=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
[ -n "$modified" ] && cat "$workfile" >"$nifile"
|
|
|
|
ac_clean "$workfile" "$tmpfile"
|
|
|
|
[ -n "$modified" ]
|
2014-02-01 18:40:59 +04:00
|
|
|
}
|
2014-02-07 19:46:05 +04:00
|
|
|
function network_interfaces_remove_confbr() {
|
|
|
|
# Supprimer dans le fichier $3(=/etc/network/interfaces) toute la
|
|
|
|
# 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}"
|
2014-02-07 22:48:03 +04:00
|
|
|
|
|
|
|
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
|
2014-02-26 23:29:30 +04:00
|
|
|
local nifile="${4:-$__DEBIAN_NETWORK_INTERFACES}"
|
2014-02-07 22:48:03 +04:00
|
|
|
local mainip netmask method
|
|
|
|
|
2016-12-30 18:14:01 +04:00
|
|
|
[ "$gateway" == "none" -o "$gateway" == "-" ] && gateway=
|
|
|
|
|
2014-02-07 22:48:03 +04:00
|
|
|
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
|
|
|
|
|
2014-03-01 14:20:16 +04:00
|
|
|
echo >>"$nifile" "\
|
2014-02-07 22:48:03 +04:00
|
|
|
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
|
2014-02-07 19:46:05 +04:00
|
|
|
}
|
2014-02-01 18:40:59 +04:00
|
|
|
|
|
|
|
function network_interfaces_add_confbr() {
|
2014-02-07 19:46:05 +04:00
|
|
|
# ajouter dans le fichier $4(=/etc/network/interfaces) la configuration pour
|
|
|
|
# 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)
|
2014-02-07 22:48:03 +04:00
|
|
|
local -a __niac_ifaces; array_copy __niac_ifaces "${2:-ifaces}"
|
|
|
|
local -a __niac_confips; array_copy __niac_confips "${3:-confips}"
|
2014-02-01 18:40:59 +04:00
|
|
|
|
2014-02-07 22:48:03 +04:00
|
|
|
local br="$1"
|
|
|
|
local -a ifaces; array_copy ifaces __niac_ifaces
|
|
|
|
local -a confips; array_copy confips __niac_confips
|
2014-02-26 23:29:30 +04:00
|
|
|
local nifile="${4:-$__DEBIAN_NETWORK_INTERFACES}"
|
2014-02-07 22:48:03 +04:00
|
|
|
local iface confip
|
|
|
|
|
|
|
|
for iface in "${ifaces[@]}"; do
|
2014-03-01 14:20:16 +04:00
|
|
|
echo >>"$nifile" "\
|
2014-02-07 22:48:03 +04:00
|
|
|
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"
|
2014-02-01 18:40:59 +04:00
|
|
|
}
|
|
|
|
|
2014-02-09 21:55:25 +04:00
|
|
|
function network_fix_hostname() {
|
2014-01-27 17:13:57 +04:00
|
|
|
local hostname="${1%%.*}"
|
2014-02-09 21:55:25 +04:00
|
|
|
local modified
|
2014-01-28 12:43:24 +04:00
|
|
|
[ -f /etc/hostname ] || touch /etc/hostname
|
2017-02-13 08:15:35 +04:00
|
|
|
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" -o -n "$__NETWORK_DEVEL_SHOW_MODIFS" ]; then
|
2014-01-27 17:13:57 +04:00
|
|
|
uecho "Setting /etc/hostname to $hostname"
|
2014-01-28 12:43:24 +04:00
|
|
|
elif [ "$(</etc/hostname)" != "$hostname" ]; then
|
2014-02-09 21:55:25 +04:00
|
|
|
__network_backup /etc/hostname
|
2014-01-28 16:29:37 +04:00
|
|
|
if show_debug; then
|
|
|
|
edebug "Setting /etc/hostname to $hostname"
|
|
|
|
else
|
|
|
|
estep /etc/hostname
|
|
|
|
fi
|
2014-01-27 17:13:57 +04:00
|
|
|
echo "$hostname" >/etc/hostname
|
2014-02-09 21:55:25 +04:00
|
|
|
modified=1
|
2014-01-27 17:13:57 +04:00
|
|
|
fi
|
2014-02-09 21:55:25 +04:00
|
|
|
[ -n "$modified" ]
|
2014-01-24 16:26:22 +04:00
|
|
|
}
|
|
|
|
|
2014-02-09 21:55:25 +04:00
|
|
|
function network_fix_mailname() {
|
2014-01-27 17:13:57 +04:00
|
|
|
local host="$1"
|
2014-02-09 21:55:25 +04:00
|
|
|
local modified
|
2014-01-28 12:43:24 +04:00
|
|
|
[ -f /etc/mailname ] || touch /etc/mailname
|
2017-02-13 08:15:35 +04:00
|
|
|
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" -o -n "$__NETWORK_DEVEL_SHOW_MODIFS" ]; then
|
2014-01-27 17:13:57 +04:00
|
|
|
uecho "Setting /etc/mailname to $host"
|
2014-01-28 12:43:24 +04:00
|
|
|
elif [ "$(</etc/mailname)" != "$host" ]; then
|
2014-02-09 21:55:25 +04:00
|
|
|
__network_backup /etc/mailname
|
2014-01-28 16:29:37 +04:00
|
|
|
if show_debug; then
|
|
|
|
edebug "Setting /etc/mailname to $host"
|
|
|
|
else
|
|
|
|
estep /etc/mailname
|
|
|
|
fi
|
2014-01-27 17:13:57 +04:00
|
|
|
echo "$host" >/etc/mailname
|
2014-02-09 21:55:25 +04:00
|
|
|
modified=1
|
2014-01-27 17:13:57 +04:00
|
|
|
fi
|
2014-02-09 21:55:25 +04:00
|
|
|
[ -n "$modified" ]
|
2014-01-27 17:13:57 +04:00
|
|
|
}
|
|
|
|
|
2014-01-24 16:26:22 +04:00
|
|
|
function network_config() {
|
2016-12-30 18:14:01 +04:00
|
|
|
# (Re)configurer le réseau sur l'hôte courant. Des efforts sont faits pour
|
|
|
|
# ne mettre à jour les fichiers que si c'est nécessaire. Si un des arguments
|
|
|
|
# $1..$6 n'est pas spécifié, il est ignoré.
|
2014-01-28 15:55:40 +04:00
|
|
|
# $1 (host) est le nom d'hôte.
|
|
|
|
# $2 (confips) est le nom d'un tableau contenant la configuration des
|
|
|
|
# adresses ips pour les interfaces.
|
|
|
|
# $3 (confbrs) est le nom d'un tableau contenant la configuration des
|
|
|
|
# bridges à créer/mettre à jour.
|
|
|
|
# $4 (mainiface) est le nom de l'interface principale, c'est à dire
|
|
|
|
# l'interface qui est sélectionnée si une adresse ip n'est pas préfixée de
|
|
|
|
# son interface. En principe, l'interface principale est le premier bridge
|
2014-01-27 17:13:57 +04:00
|
|
|
# défini ou la première interface définie.
|
2014-01-28 15:55:40 +04:00
|
|
|
# $5 (reset_interfaces) spécifie de ne pas chercher à mettre à jour le
|
2016-12-30 18:14:01 +04:00
|
|
|
# fichier /etc/network/interfaces, mais de le recréer depuis zéro. la valeur
|
|
|
|
# 'force' spécifie de recréer le fichier même si ce n'est pas nécessaire à
|
|
|
|
# priori.
|
2014-02-09 22:44:22 +04:00
|
|
|
# $6 (oldhost) est le nom d'hôte actuel, avant la modification
|
2016-12-30 18:14:01 +04:00
|
|
|
# $7(=/etc/network/interfaces) est le fichier à mettre à jour
|
|
|
|
#
|
2014-01-27 17:13:57 +04:00
|
|
|
# Le tableau confips doit contenir des définitions d'une des formes
|
|
|
|
# suivantes:
|
2014-02-09 22:44:22 +04:00
|
|
|
# [[iface][//gateway]:]address[/suffix],...
|
2014-01-27 17:13:57 +04:00
|
|
|
# [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
|
|
|
|
# existe, auquel cas il s'agit du premier bridge défini. Pour chaque
|
|
|
|
# interface, seule la première spécification d'adresse IP tient compte de
|
|
|
|
# l'argument gateway. Les autres spécifications définissent des adresses IP
|
|
|
|
# supplémentaires pour l'interface.
|
2016-12-30 18:14:01 +04:00
|
|
|
#
|
2014-01-27 17:13:57 +04:00
|
|
|
# Le tableau brs doit contenir des définitions de la forme suivante:
|
|
|
|
# br:ifaces,...
|
|
|
|
# br est le nom du bridge, e.g. br0. ifaces est une liste d'interfaces
|
|
|
|
# séparées par une virgule. e.g. br0:eth0,eth1
|
|
|
|
# Bien que ce soit techniquement possible, ce script interdit que l'on
|
|
|
|
# définisse une adresse IP pour une interface faisant partie d'un bridge.
|
|
|
|
local -a __nc_confips __nc_confbrs
|
|
|
|
[ -n "$2" ] && array_copy __nc_confips "$2"
|
|
|
|
[ -n "$3" ] && array_copy __nc_confbrs "$3"
|
|
|
|
|
2014-02-09 22:44:22 +04:00
|
|
|
local host="$1"
|
|
|
|
local -a confips; array_copy confips __nc_confips
|
|
|
|
local -a confbrs; array_copy confbrs __nc_confbrs
|
|
|
|
local mainiface="$4" reset_interfaces="$5" oldhost="$6"
|
|
|
|
local modified
|
2016-12-30 18:14:01 +04:00
|
|
|
local nifile="${7:-$__DEBIAN_NETWORK_INTERFACES}"
|
2014-01-27 17:13:57 +04:00
|
|
|
|
2014-02-09 22:44:22 +04:00
|
|
|
network_fix_confs confbrs confips "$mainiface"
|
2014-01-27 17:13:57 +04:00
|
|
|
|
2014-02-09 22:44:22 +04:00
|
|
|
local confbr br; local -a ifaces
|
|
|
|
local confip iface gateway mainip suffix; local -a ipsuffixes
|
|
|
|
network_parse_confip "${confips[0]}" iface gateway ipsuffixes
|
|
|
|
network_parse_ipsuffix "${ipsuffixes[0]}" mainip suffix
|
2014-01-27 17:13:57 +04:00
|
|
|
|
|
|
|
if [ -n "${confips[*]}" -o -n "${confbrs[*]}" ]; then
|
|
|
|
# Vérifier qu'une interface avec une adresse IP ne figure pas dans les
|
|
|
|
# bridges à configurer. en profiter pour classer les interfaces par
|
|
|
|
# type: standard et bridges
|
|
|
|
etitle "Configuration des interfaces réseau"
|
|
|
|
|
|
|
|
local -a ifaces_with_ips ifaces
|
|
|
|
local -a brifaces
|
|
|
|
ifaces_with_ips=()
|
|
|
|
for confip in "${confips[@]}"; do
|
2014-02-09 22:44:22 +04:00
|
|
|
network_parse_confip "$confip" iface gateway ipsuffixes
|
2014-01-27 17:13:57 +04:00
|
|
|
array_addu ifaces_with_ips "$iface"
|
|
|
|
done
|
|
|
|
for confbr in "${confbrs[@]}"; do
|
2014-02-09 22:44:22 +04:00
|
|
|
network_parse_confbr "$confbr" br ifaces
|
2014-01-27 17:13:57 +04:00
|
|
|
array_add brifaces "$br"
|
|
|
|
for iface in "${ifaces[@]}"; do
|
|
|
|
if array_contains ifaces_with_ips "$iface"; then
|
|
|
|
eerror "$iface: Impossible de configurer une interface dans un bridge ET avec une adresse IP"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
2014-02-09 22:44:22 +04:00
|
|
|
|
|
|
|
# vérifier si une modification est nécessaire
|
|
|
|
local modify
|
2016-12-30 18:14:01 +04:00
|
|
|
[ "$reset_interfaces" == force ] && modify=1
|
|
|
|
if [ -z "$modify" ]; then
|
|
|
|
for confbr in "${confbrs[@]}"; do
|
|
|
|
network_parse_confbr "$confbr" br ifaces
|
|
|
|
if ! network_interfaces_check_confbr "$br" iface; then
|
|
|
|
modify=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2014-02-09 22:44:22 +04:00
|
|
|
if [ -z "$modify" ]; then
|
|
|
|
for confip in "${confips[@]}"; do
|
|
|
|
network_parse_confip "$confip" iface gateway ipsuffixes
|
|
|
|
if ! network_interfaces_check_confip "$iface" "$gateway" ipsuffixes; then
|
|
|
|
modify=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2014-01-27 17:13:57 +04:00
|
|
|
|
2014-02-09 22:44:22 +04:00
|
|
|
# yes! il faut refaire la configuration
|
|
|
|
if [ -n "$modify" ]; then
|
|
|
|
# faire une copie de travail
|
|
|
|
local interfaces; ac_set_tmpfile interfaces
|
2016-12-30 18:14:01 +04:00
|
|
|
cat "$nifile" >"$interfaces"
|
2014-01-27 17:13:57 +04:00
|
|
|
|
2014-02-09 22:44:22 +04:00
|
|
|
if [ -n "$reset_interfaces" ]; then
|
2016-12-30 18:14:01 +04:00
|
|
|
local source_itfd
|
|
|
|
check_sysinfos -d debian -v jessie+ && source_itfd=1
|
2014-02-09 22:44:22 +04:00
|
|
|
echo >"$interfaces" "\
|
2014-01-28 10:53:07 +04:00
|
|
|
# This file describes the network interfaces available on your system
|
|
|
|
# and how to activate them. For more information, see interfaces(5).
|
2016-12-30 18:14:01 +04:00
|
|
|
${source_itfd:+
|
|
|
|
source /etc/network/interfaces.d/*
|
|
|
|
}
|
2014-01-28 10:53:07 +04:00
|
|
|
# The loopback network interface
|
|
|
|
auto lo
|
|
|
|
iface lo inet loopback
|
|
|
|
"
|
2014-01-27 17:13:57 +04:00
|
|
|
fi
|
|
|
|
|
2014-02-09 22:44:22 +04:00
|
|
|
# d'abord supprimer l'ancienne configuration
|
2014-01-27 17:13:57 +04:00
|
|
|
for confbr in "${confbrs[@]}"; do
|
2014-02-09 22:44:22 +04:00
|
|
|
network_parse_confbr "$confbr" br ifaces
|
|
|
|
network_interfaces_remove_confbr "$br" ifaces "$interfaces"
|
|
|
|
done
|
|
|
|
for confip in "${confips[@]}"; do
|
|
|
|
network_parse_confip "$confip" iface gateway ipsuffixes
|
2014-02-26 23:29:30 +04:00
|
|
|
network_interfaces_remove_iface "$iface" "$interfaces"
|
2014-01-27 17:13:57 +04:00
|
|
|
done
|
|
|
|
|
2014-02-09 22:44:22 +04:00
|
|
|
# puis recréer la configuration
|
|
|
|
for confbr in "${confbrs[@]}"; do
|
|
|
|
network_parse_confbr "$confbr" br ifaces
|
|
|
|
network_interfaces_add_confbr "$br" ifaces confips "$interfaces"
|
|
|
|
done
|
|
|
|
for confip in "${confips[@]}"; do
|
|
|
|
network_parse_confip "$confip" iface gateway ipsuffixes
|
|
|
|
if ! array_contains brifaces "$iface"; then
|
|
|
|
network_interfaces_add_confip "$iface" "$gateway" ipsuffixes "$interfaces"
|
|
|
|
fi
|
|
|
|
done
|
2014-02-09 23:11:21 +04:00
|
|
|
|
2014-02-09 22:44:22 +04:00
|
|
|
# Fin de traitement
|
2017-02-13 08:15:35 +04:00
|
|
|
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" -o -n "$__NETWORK_DEVEL_SHOW_MODIFS" ]; then
|
2016-12-30 18:14:01 +04:00
|
|
|
if testdiff "$interfaces" "$nifile"; then
|
|
|
|
uecho "Setting $nifile to:"
|
2014-02-09 22:44:22 +04:00
|
|
|
cat "$interfaces" | sed 's/^/ /g' 1>&2
|
|
|
|
else
|
2016-12-30 18:14:01 +04:00
|
|
|
uecho "$nifile: pas de modifications"
|
2014-02-09 22:44:22 +04:00
|
|
|
fi
|
2016-12-30 18:14:01 +04:00
|
|
|
elif testdiff "$interfaces" "$nifile"; then
|
|
|
|
__network_backup "$nifile"
|
2014-02-09 22:44:22 +04:00
|
|
|
if show_debug; then
|
2016-12-30 18:14:01 +04:00
|
|
|
edebug "Setting $nifile to:"
|
2014-02-09 22:44:22 +04:00
|
|
|
cat "$interfaces" | sed 's/^/ /g' 1>&2
|
|
|
|
else
|
2016-12-30 18:14:01 +04:00
|
|
|
estep "$nifile"
|
2014-02-09 22:44:22 +04:00
|
|
|
fi
|
2016-12-30 18:14:01 +04:00
|
|
|
cat "$interfaces" >"$nifile"
|
2014-02-09 22:44:22 +04:00
|
|
|
modified=1
|
2014-01-28 16:29:37 +04:00
|
|
|
fi
|
2014-02-09 22:44:22 +04:00
|
|
|
ac_clean "$interfaces"
|
2014-01-27 17:13:57 +04:00
|
|
|
fi
|
|
|
|
|
|
|
|
eend
|
|
|
|
fi
|
2014-01-24 16:26:22 +04:00
|
|
|
|
|
|
|
# configurer le nom d'hôte
|
|
|
|
if [ -n "$host" ]; then
|
|
|
|
host="$(ipcalc_fqdn_maybe "$host")"
|
|
|
|
etitle "Configuration du nom d'hôte: $host"
|
|
|
|
|
|
|
|
# si on ne spécifie pas l'adresse ip principale, la résoudre ici. ceci
|
|
|
|
# est nécessaire pour mettre à jour /etc/hosts
|
2014-01-27 17:13:57 +04:00
|
|
|
if [ "$mainip" == dhcp ]; then
|
|
|
|
# si l'adresse obtenue est en DHCP, ne pas mettre à jour /etc/hosts
|
|
|
|
mainip=
|
|
|
|
elif [ -z "$mainip" ]; then
|
|
|
|
local -a mainips
|
|
|
|
resolv_ips mainips "$host"
|
|
|
|
[ -n "${mainips[*]}" ] || {
|
2014-01-24 16:26:22 +04:00
|
|
|
eerror "$host: Impossible de résoudre ce nom d'hôte"
|
|
|
|
return 1
|
|
|
|
}
|
2014-01-27 17:13:57 +04:00
|
|
|
mainip="${mainips[0]}"
|
2014-01-24 16:26:22 +04:00
|
|
|
fi
|
|
|
|
|
2014-02-09 21:55:25 +04:00
|
|
|
network_fix_hostname "$host"
|
|
|
|
network_fix_mailname "$host"
|
2014-04-23 16:17:08 +04:00
|
|
|
[ -n "$mainip" ] && network_fix_hosts "$host" "$mainip" "$oldhost"
|
2014-01-24 16:26:22 +04:00
|
|
|
|
|
|
|
eend
|
|
|
|
fi
|
|
|
|
|
2014-02-09 22:44:22 +04:00
|
|
|
[ -z "$modified" ] && return 10
|
2014-01-24 16:26:22 +04:00
|
|
|
return 0
|
|
|
|
}
|
2016-12-30 18:14:01 +04:00
|
|
|
|
|
|
|
function network_config_partial() {
|
|
|
|
# une version allégée de network_config() qui ne permet que de rajouter des
|
|
|
|
# adresses IPs en plus de celles qui sont déjà configurées.
|
|
|
|
# $1(=confips) est le nom d'un tableau contenant la configuration des
|
|
|
|
# adresses ips pour les interfaces.
|
|
|
|
# $2(=/etc/network/interfaces) est le fichier à mettre à jour
|
|
|
|
local -a __ncp_newconfips; array_copy __ncp_newconfips "${1:-confips}"
|
|
|
|
local nifile="${2:-$__DEBIAN_NETWORK_INTERFACES}"
|
|
|
|
|
|
|
|
local -a confips confbrs
|
|
|
|
network_set_confips
|
|
|
|
network_set_confbrs
|
|
|
|
network_fix_confs
|
|
|
|
|
|
|
|
array_extend confips __ncp_newconfips
|
|
|
|
network_config "" confips confbrs "" "" "" "$nifile" || return
|
|
|
|
|
|
|
|
# des modifications ont eu lieu, tenter de configurer les nouvelles adresses
|
|
|
|
# d'abord calculer les adresses ips qui sont activées
|
|
|
|
network_set_confips
|
|
|
|
local -a ips
|
|
|
|
local ipsuffix ip suffix
|
|
|
|
for confip in "${confips[@]}"; do
|
|
|
|
network_parse_confip "$confip" iface gateway ipsuffixes
|
|
|
|
for ipsuffix in "${ipsuffixes[@]}"; do
|
|
|
|
network_parse_ipsuffix "$ipsuffix" ip suffix
|
|
|
|
array_addu ips "$ip"
|
|
|
|
done
|
|
|
|
done
|
|
|
|
# puis lister les commandes correspondant aux adresses ips non activées
|
|
|
|
local -a ups; local up
|
|
|
|
array_from_lines ups "$(awkrun <"$nifile" -f ips[@] '
|
|
|
|
$1 == "up" && $2 == "ip" && $3 == "addr" && $4 == "add" {
|
|
|
|
ip = $5; sub(/(\/[0-9.]+)?$/, "", ip) # enlever suffixe
|
|
|
|
if (! in_array(ip, ips)) {
|
|
|
|
$1 = ""
|
|
|
|
print
|
|
|
|
}
|
|
|
|
}')"
|
|
|
|
if [ ${#ups[*]} -gt 0 ]; then
|
|
|
|
etitle "Configuration des nouvelles adresses"
|
|
|
|
for up in "${ups[@]}"; do
|
|
|
|
estep "$up"
|
|
|
|
eval "$up"
|
|
|
|
done
|
|
|
|
eend
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function network_update_etc_networks() {
|
|
|
|
# écraser le contenu du fichier /etc/networks avec la chaine $1
|
|
|
|
# si $1 est vide, initialiser le fichier avec un contenu par défaut.
|
|
|
|
local contents="$1" TAB=$'\t'
|
|
|
|
[ -n "$contents" ] || contents="\
|
|
|
|
default${TAB}${TAB}0.0.0.0
|
|
|
|
loopback${TAB}127.0.0.0
|
|
|
|
link-local${TAB}169.254.0.0"
|
|
|
|
if [ "$(</etc/networks)" != "$contents" ]; then
|
|
|
|
__network_backup /etc/networks
|
|
|
|
echo "$contents" >/etc/networks
|
|
|
|
fi
|
|
|
|
}
|