grml-addtun: better bridge handling
authorMichael Gebetsroither <michael.geb@gmx.at>
Thu, 4 Oct 2007 10:48:20 +0000 (12:48 +0200)
committerMichael Gebetsroither <michael.geb@gmx.at>
Thu, 4 Oct 2007 10:48:20 +0000 (12:48 +0200)
sbin/grml-addtun

index 7c641f1..3b6d5b9 100755 (executable)
@@ -5,7 +5,6 @@
 # Bug-Reports:   see http://grml.org/bugs/
 # License:       This file is licensed under the GPL v2.
 ################################################################################
-# grml-addtun [OPTIONS] <tun0> <tun1> ...
 
 set -e
 #set -x
@@ -22,7 +21,7 @@ function printUsage()
     cat <<EOT
 Usage: "$PN_" [OPTIONS] <tun0> <tun1> ...
 
-$PN_ creates persistent tun/tap devices and optionally add them to a bridge
+$PN_ creates persistent tun/tap devices with bridge handling
 
 OPTIONS:
    -d           delete the given tun devices and remove them from the bridge if given
@@ -55,7 +54,6 @@ function createTun()
     if [[ $OPT_GROUP_ != '' ]]; then args_="$args_ -u $OPT_GROUP_"; fi
     tunctl -t "$1" $args_
     if [[ $OPT_BRIDGE_ != '' ]]; then
-        brctl showmacs "$OPT_BRIDGE_" &>/dev/null || brctl addbr "$OPT_BRIDGE_"
         brctl addif "$OPT_BRIDGE_" "$1"
     fi
 }
@@ -68,6 +66,11 @@ function trashTun()
     tunctl -d "$1"
 }
 
+function die()
+{
+    echo "$@" >&2
+    exit 1
+}
 
 ##
 # MAIN
@@ -93,7 +96,12 @@ if [[ $OPT_DEL_ == 'false' ]]; then
 else
     fromCmdline "trashTun" "$@"
     if [[ $OPT_BRIDGE_ != '' ]]; then
-        brctl showmacs "$OPT_BRIDGE_" &>/dev/null && brctl delbr "$OPT_BRIDGE_"
+        tmp_="`brctl showmacs "$OPT_BRIDGE_" |wc -l`"
+        if (( $tmp_ == 1 )); then
+            brctl delbr "$OPT_BRIDGE_"
+        else
+            die "E: bridge $OPT_BRIDGE_ not empty, not removing"
+        fi
     fi
 fi