7fa0d95c4440395ac6cb55412da22023315401de
[grml-network.git] / sbin / grml-vnet
1 #!/bin/bash
2 # Filename:      grml-vnet
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 ################################################################################
8
9 set -e
10 #set -x
11
12 PN_="`basename $0`"
13 OPT_DEL_='false'
14 OPT_USER_=''
15 OPT_GROUP_=''
16 OPT_BRIDGE_=''
17 OPT_AUTO_='false'
18
19 if [ $(id -ru) -ne 0 ] ; then
20   echo 1>&2 "Error: please run this script with uid 0 (root)." ; exit 1
21 fi
22
23 function printUsage()
24 {
25     cat <<EOT
26 Usage: "$PN_" [OPTIONS] <tun0> <tun1> ...
27
28 $PN_ creates persistent tun/tap devices with bridge handling
29
30 OPTIONS:
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
37    -h             this help
38 EOT
39 }
40
41 function fromCmdline()
42 {
43     local action_="$1"
44     shift
45
46     while (( $# != 0 )); do
47         case "$1" in
48             "") continue ;;
49         esac
50         $action_ "$1"
51         shift
52     done
53 }
54
55 function createTun()
56 {
57     local args_=''
58     if [[ $OPT_USER_ != '' ]]; then args_="$args_ -u $OPT_USER_"; fi
59     if [[ $OPT_GROUP_ != '' ]]; then args_="$args_ -u $OPT_GROUP_"; fi
60     tunctl -t "$1" $args_
61     if [[ $OPT_BRIDGE_ != '' ]]; then
62         brctl addif "$OPT_BRIDGE_" "$1"
63     fi
64     ip link set up dev "$1"
65 }
66
67 function trashTun()
68 {
69     ip link set down dev "$1"
70     if [[ $OPT_BRIDGE_ != '' ]]; then
71         brctl delif "$OPT_BRIDGE_" "$1"
72     fi
73     tunctl -d "$1"
74 }
75
76 function die()
77 {
78     echo "$@" >&2
79     exit 1
80 }
81
82 ##
83 # MAIN
84 ##
85
86 while getopts "du:g:b:ah" opt; do
87     case "$opt" in
88         d) OPT_DEL_='true' ;;
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 ;;
95     esac
96 done
97 shift $(($OPTIND - 1))
98
99 if [[ $# < 1 ]]; then
100     echo "Error: Please give at least one device" >&2
101     printUsage
102     exit 1
103 fi
104
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_"
109     fi
110     fromCmdline "createTun" "$@"
111 else
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_"
118         else
119             die "E: bridge $OPT_BRIDGE_ not empty, not removing"
120         fi
121     fi
122 fi
123
124 ## END OF FILE #################################################################