X-Git-Url: http://git.grml.org/?a=blobdiff_plain;f=sbin%2Fgrml-addtun;h=109cc6ff9180a273e282f677b92ebbf5a46c30d8;hb=a9e6e3a985c3d5103a6dd2d0b435b9a018090a32;hp=7c641f1bc07e63d78b7324250fca0bf622a1a7e0;hpb=e55c95bdd7cd5382037052542d37e63b846e31cd;p=grml-network.git diff --git a/sbin/grml-addtun b/sbin/grml-addtun index 7c641f1..109cc6f 100755 --- a/sbin/grml-addtun +++ b/sbin/grml-addtun @@ -5,7 +5,6 @@ # Bug-Reports: see http://grml.org/bugs/ # License: This file is licensed under the GPL v2. ################################################################################ -# grml-addtun [OPTIONS] ... set -e #set -x @@ -15,6 +14,7 @@ OPT_DEL_='false' OPT_USER_='' OPT_GROUP_='' OPT_BRIDGE_='' +OPT_AUTO_='false' function printUsage() @@ -22,15 +22,16 @@ function printUsage() cat < ... -$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 - -u this user should be able to use the tun device - -g this group should be able to use the tun device - -b 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 this user should be able to use the tun device + -g this group should be able to use the tun device + -b 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 } @@ -55,30 +56,37 @@ 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 + ip link set up dev "$1" } function trashTun() { + ip link set down dev "$1" if [[ $OPT_BRIDGE_ != '' ]]; then brctl delif "$OPT_BRIDGE_" "$1" fi tunctl -d "$1" } +function die() +{ + echo "$@" >&2 + exit 1 +} ## # 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 @@ -86,15 +94,21 @@ 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 - brctl showmacs "$OPT_BRIDGE_" &>/dev/null && brctl delbr "$OPT_BRIDGE_" + if [[ $OPT_BRIDGE_ != '' && $OPT_AUTO_ == 'true' ]]; then + 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 -# vim: filetype=sh +## END OF FILE #################################################################