grml-addtun: reworked interface activation code
[grml-network.git] / sbin / grml-addtun
index 3b6d5b9..109cc6f 100755 (executable)
@@ -14,6 +14,7 @@ OPT_DEL_='false'
 OPT_USER_=''
 OPT_GROUP_=''
 OPT_BRIDGE_=''
+OPT_AUTO_='false'
 
 
 function printUsage()
@@ -24,12 +25,13 @@ Usage: "$PN_" [OPTIONS] <tun0> <tun1> ...
 $PN_ creates persistent tun/tap devices with bridge handling
 
 OPTIONS:
-   -d           delete the given tun devices and remove them from the bridge if given
-   -u <user>    this user should be able to use the tun device
-   -g <group>   this group should be able to use the tun device
-   -b <bridge>  if given, all tun/tap devices are added/removed from the bridge
-                    bridge is created if not allready existent
-   -h           this help
+   -d             delete the tun devices and remove them from the bridge if given
+   -u <user>      this user should be able to use the tun device
+   -g <group>     this group should be able to use the tun device
+   -b <bridge>    if given, all tun/tap devices are added/removed from the bridge
+   -a             enable auto mode, eg. create the bridge if not already existing and
+                  delete it when empty after removing given tun devices
+   -h             this help
 EOT
 }
 
@@ -56,10 +58,12 @@ function createTun()
     if [[ $OPT_BRIDGE_ != '' ]]; then
         brctl addif "$OPT_BRIDGE_" "$1"
     fi
+    ip link set up dev "$1"
 }
 
 function trashTun()
 {
+    ip link set down dev "$1"
     if [[ $OPT_BRIDGE_ != '' ]]; then
         brctl delif "$OPT_BRIDGE_" "$1"
     fi
@@ -76,12 +80,13 @@ function die()
 # MAIN
 ##
 
-while getopts "du:g:b:h" opt; do
+while getopts "du:g:b:ah" opt; do
     case "$opt" in
         d) OPT_DEL_='true' ;;
         u) OPT_USER_="$OPTARG" ;;
         g) OPT_GROUP_="$OPTARG" ;;
         b) OPT_BRIDGE_="$OPTARG" ;;
+        a) OPT_AUTO_='true' ;;
         h) printUsage; exit 0 ;;
         ?) printUsage; exit 1 ;;
     esac
@@ -89,13 +94,14 @@ done
 shift $(($OPTIND - 1))
 
 if [[ $OPT_DEL_ == 'false' ]]; then
-    if [[ $OPT_BRIDGE_ != '' ]]; then
+    if [[ $OPT_BRIDGE_ != '' && $OPT_AUTO_ == 'true' ]]; then
         brctl showmacs "$OPT_BRIDGE_" &>/dev/null || brctl addbr "$OPT_BRIDGE_"
+        ip link set up dev "$OPT_BRIDGE_"
     fi
     fromCmdline "createTun" "$@"
 else
     fromCmdline "trashTun" "$@"
-    if [[ $OPT_BRIDGE_ != '' ]]; then
+    if [[ $OPT_BRIDGE_ != '' && $OPT_AUTO_ == 'true' ]]; then
         tmp_="`brctl showmacs "$OPT_BRIDGE_" |wc -l`"
         if (( $tmp_ == 1 )); then
             brctl delbr "$OPT_BRIDGE_"
@@ -105,4 +111,4 @@ else
     fi
 fi
 
-# vim: filetype=sh
+## END OF FILE #################################################################