3 # Purpose: program to create tun/tap devices and add them to a bridge
4 # Authors: Michael Gebetsroither <gebi@grml.org>
5 # Bug-Reports: see http://grml.org/bugs/
6 # License: This file is licensed under the GPL v2.
7 ################################################################################
19 if [ $(id -ru) -ne 0 ] ; then
20 echo 1>&2 "Error: please run this script with uid 0 (root)." ; exit 1
26 Usage: "$PN_" [OPTIONS] <tun0> <tun1> ...
28 $PN_ creates persistent tun/tap devices with bridge handling
31 -d delete the tun devices and remove them from the bridge if given
32 -u <user> this user should be able to use the tun device
33 -g <group> this group should be able to use the tun device
34 -b <bridge> if given, all tun/tap devices are added/removed from the bridge
35 -a enable auto mode, eg. create the bridge if not already existing and
36 delete it when empty after removing given tun devices
41 function fromCmdline()
46 while (( $# != 0 )); do
58 if [[ $OPT_USER_ != '' ]]; then args_="$args_ -u $OPT_USER_"; fi
59 if [[ $OPT_GROUP_ != '' ]]; then args_="$args_ -u $OPT_GROUP_"; fi
61 if [[ $OPT_BRIDGE_ != '' ]]; then
62 brctl addif "$OPT_BRIDGE_" "$1"
64 ip link set up dev "$1"
69 ip link set down dev "$1"
70 if [[ $OPT_BRIDGE_ != '' ]]; then
71 brctl delif "$OPT_BRIDGE_" "$1"
86 while getopts "du:g:b:ah" opt; do
89 u) OPT_USER_="$OPTARG" ;;
90 g) OPT_GROUP_="$OPTARG" ;;
91 b) OPT_BRIDGE_="$OPTARG" ;;
92 a) OPT_AUTO_='true' ;;
93 h) printUsage; exit 0 ;;
94 ?) printUsage; exit 1 ;;
97 shift $(($OPTIND - 1))
100 echo "Error: Please give at least one device" >&2
105 if [[ $OPT_DEL_ == 'false' ]]; then
106 if [[ $OPT_BRIDGE_ != '' && $OPT_AUTO_ == 'true' ]]; then
107 brctl showmacs "$OPT_BRIDGE_" &>/dev/null || brctl addbr "$OPT_BRIDGE_"
108 ip link set up dev "$OPT_BRIDGE_"
110 fromCmdline "createTun" "$@"
112 fromCmdline "trashTun" "$@"
113 if [[ $OPT_BRIDGE_ != '' && $OPT_AUTO_ == 'true' ]]; then
114 tmp_="`brctl showmacs "$OPT_BRIDGE_" |wc -l`"
115 if (( $tmp_ == 1 )); then
116 ip link set down dev "$OPT_BRIDGE_"
117 brctl delbr "$OPT_BRIDGE_"
119 die "E: bridge $OPT_BRIDGE_ not empty, not removing"
124 ## END OF FILE #################################################################