From 03afcdce237b2dc52a6c1fea72b715cf6c34bf05 Mon Sep 17 00:00:00 2001 From: Jouke Witteveen Date: Thu, 27 Feb 2014 13:43:22 +0100 Subject: Uniform adding and deleting of interfaces The main benefit is the possibility to use interface hooks for links that are created at runtime. --- src/lib/connections/bond | 5 ++--- src/lib/connections/bridge | 5 ++--- src/lib/connections/dummy | 6 ++---- src/lib/connections/vlan | 4 ++-- src/lib/network | 21 +++++++++++++++++++++ 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/lib/connections/bond b/src/lib/connections/bond index c22fc82..9cc8b96 100644 --- a/src/lib/connections/bond +++ b/src/lib/connections/bond @@ -10,7 +10,7 @@ bond_up() { report_error "Interface '$Interface' already exists" return 1 else - ip link add name "$Interface" type bond + interface_add bond "$Interface" fi bring_interface_up "$Interface" @@ -28,8 +28,7 @@ bond_down() { done ip_unset - bring_interface_down "$Interface" - ip link delete "$Interface" + interface_delete "$Interface" } diff --git a/src/lib/connections/bridge b/src/lib/connections/bridge index 94a4802..b358e01 100644 --- a/src/lib/connections/bridge +++ b/src/lib/connections/bridge @@ -11,7 +11,7 @@ bridge_up() { return 1 fi else - ip link add name "$Interface" type bridge + interface_add bridge "$Interface" fi for member in "${BindsToInterfaces[@]}"; do @@ -34,8 +34,7 @@ bridge_down() { done ip_unset - bring_interface_down "$Interface" - ip link delete "$Interface" type bridge + interface_delete "$Interface" } diff --git a/src/lib/connections/dummy b/src/lib/connections/dummy index 879fc92..5a12d1e 100644 --- a/src/lib/connections/dummy +++ b/src/lib/connections/dummy @@ -10,16 +10,14 @@ dummy_up() { return 1 fi - ip link add "$Interface" type dummy - + interface_add dummy "$Interface" bring_interface_up "$Interface" ip_set } dummy_down() { ip_unset - bring_interface_down "$Interface" - ip link del "$Interface" + interface_delete "$Interface" } diff --git a/src/lib/connections/vlan b/src/lib/connections/vlan index e9ab08b..b34a67e 100644 --- a/src/lib/connections/vlan +++ b/src/lib/connections/vlan @@ -12,7 +12,7 @@ vlan_up() { return 1 else bring_interface_up "$BindsToInterfaces" - ip link add link "$BindsToInterfaces" name "$Interface" type vlan id "$VLANID" + interface_add vlan "$Interface" "$BindsToInterfaces" id "$VLANID" fi ethernet_up @@ -20,7 +20,7 @@ vlan_up() { vlan_down() { ethernet_down - ip link delete "$Interface" + interface_delete "$Interface" } diff --git a/src/lib/network b/src/lib/network index 7dfe0b5..b91b5e8 100755 --- a/src/lib/network +++ b/src/lib/network @@ -10,6 +10,27 @@ is_interface() { [[ -d "/sys/class/net/${1%%:?*}" ]] } +## Add an interface +# $1: interface type +# $2: interface name +# $3: interface link (optional) +# $4...: additional arguments +interface_add() { + local type="$1" name="$2" link="$3" + shift 3 + ip link add ${link:+link "$link"} name "$name" type "$type" "$@" + if [[ -x "$PROFILE_DIR/interfaces/$name" ]]; then + source "$PROFILE_DIR/interfaces/$name" + fi +} + +## Delete an interface +# $1: interface name +interface_delete() { + bring_interface_down "$1" + ip link delete "$1" +} + ## Check if an interface is up # $1: interface name interface_is_up() { -- cgit v1.2.3-24-g4f1b