update for new -tpg argument
[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 [ notap ] <port> 
22 # rrqnet_nice <nice>
23 # rrqnet_remote <remote declaration>
24 # rrqnet_options <options>
25 # rrqnet_log <level> <pathname>
26 # rrqnet_bridge <bridge>
27 # rrqnet_dhcp <options>
28
29 : ${RRQDAEMON:=/usr/sbin/rrqnet}
30 : ${NAME:=rrqnet-${IFACE}}
31
32 NOTAP="${IF_RRQNET_PORT##notap *}" # empty if 'notap' is used
33 IF_RRQNET_PORT="${IF_RRQNET_PORT#notap }"
34
35 #function
36 configure_tap_bridge() {
37     [ -z "$IF_RRQNET_BRIDGE" ] && return 0
38     brctl show $IF_RRQNET_BRIDGE | grep -wq $IFACE && return 0
39     brctl addif $IF_RRQNET_BRIDGE $IFACE
40 }
41
42 #function
43 configure_tap_up() {
44     ( ip link show $IFACE 2>/dev/null || ip tuntap add $IFACE mode tap ) | \
45         grep -q "state UP" || ip link set dev $IFACE up
46 }
47
48 ############################################################
49 ## DHCP support
50 : ${LEASES:=/var/lib/dhcp/dhclient.$IFACE.leases}
51 : ${DHCPARGS:="-4 -cf /dev/null"}
52 : ${PIDFILE:=/var/run/dhclient.$IFACE}
53
54 #function
55 start_dhclient() {
56     shift 1
57     [ -z "$*" ] || DHCPARGS="$*"
58     /sbin/dhclient -pf $PIDFILE $DHCPARGS -lf $LEASES $IFACE
59 }
60
61 #function
62 stop_dhclient() {
63     shift 1
64     [ -z "$*" ] || DHCPARGS="$*"
65     /sbin/dhclient -x -pf $PIDFILE $DHCPARGS -lf $LEASES $IFACE 2>/dev/null
66 }
67
68 ############################################################
69 ## The action functions
70
71 #function
72 start_cable_pre_up() {
73     local TAP
74     TAP="-t $IFACE"
75     if [ -z "$NOTAP" ] ; then
76         echo "Note: $IFACE is an rrqnet without local interface" >&2
77         TAP=""
78     else
79         configure_tap_up || return 1
80         configure_tap_bridge || return 1
81     fi
82     [ -z "$IF_RRQNET_NICE" ] || \
83         RRQDAEMON="/usr/bin/nice -n $IF_RRQNET_NICE $RRQDAEMON"
84     if [ -z "$IF_RRQNET_LOG" ] ; then
85         daemon -U -r -a 10 -n $NAME -- \
86                $RRQDAEMON $IF_RRQNET_OPTIONS \
87                $TAP $IF_RRQNET_PORT $IF_RRQNET_REMOTE
88     else
89         daemon -U -r -a 10 -n $NAME -E "${IF_RRQNET_LOG#* }" -- \
90                $RRQDAEMON ${IF_RRQNET_LOG%% *} $IF_RRQNET_OPTIONS \
91                $TAP $IF_RRQNET_PORT $IF_RRQNET_REMOTE
92     fi
93 }
94
95 #function
96 start_cable_post_up() {
97     case "$IF_RRQNET_DHCP" in
98         dhclient*)
99             start_dhclient $IF_RRQNET_DHCP
100             ;;
101         *)
102             : # no or unkown dhcp option
103             ;;
104     esac
105 }
106
107 #function
108 stop_cable_pre_down() {
109     case "$IF_RRQNET_DHCP" in
110         dhclient*)
111             stop_dhclient $IF_RRQNET_DHCP
112             ;;
113         *)
114             : # no or unkown dhcp option
115             ;;
116     esac
117     daemon -n $NAME --stop
118 }
119
120 #function
121 stop_cable_post_down() {
122     [ -z "$NOTAP" ] || ip link del $IFACE
123 }
124
125 # main script body
126 case "$MODE-$PHASE" in
127     start-pre-up) start_cable_pre_up ;;
128     start-post-up) start_cable_post_up ;;
129     stop-pre-down) stop_cable_pre_down ;;
130     stop-post-down) stop_cable_post_down ;;
131 esac