added ifupdown hook script
[rrq/rrqnet.git] / rrqnet-ifupdown.sh
1 #!/bin/sh
2 #
3 # Control script for starting or stopping an rrqnet virtual cable via
4 # ifup/ifdown. To use this, you need firstly to links to this script
5 # set up as /etc/network/if-pre-up.d/rrqnet and
6 # /etc/network/if-down.d/rrqnet. Secondly, you need a stanza in
7 # /etc/network/interfaces for the cabling tap and its associated
8 # configuration settings.
9 #
10 # "rrqnet name" is the primary stanza key, which result in
11 # a creation of a tap by that name, supported by an rrqnet plug
12 # according to the setup in /etc/rrqnet/conf.d/name.conf
13
14 #echo '===========' >> /tmp/FOO
15 #env >> /tmp/FOO
16
17 # Verify that it's an rrqnet stanza
18 [ -z "$IF_RRQNET_PORT" ] && exit 0
19
20 # An rrqnet stanza may have the following settings:
21 # rrqnet_port <port>
22 # rrqnet_remote <remote declaration>
23 # rrqnet_bridge <bridge>
24 # rrqnet_log <level> <pathname>
25
26 : ${RRQDAEMON:=/usr/local/sbin/rrqnet}
27
28 #function
29 configure_tap_bridge() {
30     [ -z "$IF_RRQNET_BRIDGE" ] && return 0
31     brctl show $IF_RRQNET_BRIDGE | grep -wq $IFACE && return 0
32     brctl addif $IF_RRQNET_BRIDGE $IFACE
33 }
34
35 #function
36 configure_tap_up() {
37     ( ip link show $IFACE 2>/dev/null || ip tuntap add $IFACE mode tap ) | \
38         grep -q "state UP" || ip link set dev $IFACE up
39 }
40
41 #function <env> start_cable <loglevel> <logfile>
42 start_cable_pre_up() {
43     configure_tap_up
44     NAME="rrqnet-$IFACE"
45     if [ -z "$IF_RRQNET_LOG" ] ; then
46         daemon -U -r -n $NAME -- \
47                $RRQDAEMON -4 $IF_RRQNET_OPTIONS \
48                -t $IFACE $IF_RRQNET $IF_RRQNET_REMOTE
49     else
50         daemon -U -r -n $NAME -E "${IF_RRQNET_LOG#* }" -- \
51                $RRQDAEMON ${IF_RRQNET_LOG%% *} -4 $IF_RRQNET_OPTIONS \
52                -t $IFACE $IF_RRQNET_PORT $IF_RRQNET_REMOTE
53     fi
54 }
55
56 #function
57 start_cable_post_up() {
58     configure_tap_bridge
59 }
60
61 #function
62 stop_cable_pre_down() {
63     :
64 }
65
66 #function
67 stop_cable_post_down() {
68     NAME="rrqnet-$IFACE"
69     daemon -n $NAME --stop
70 }
71
72 # main script body
73
74 case "$MODE-$PHASE" in
75     start-pre-up)
76         start_cable_pre_up
77         ;;
78     start-post-up)
79         start_cable_post_up
80         ;;
81     stop-pre-down)
82         :
83         ;;
84     stop-post-down)
85         stop_cable_post_down
86         ;;
87 esac