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