~upstart-devel/upstart/upstart-jobs

« back to all changes in this revision

Viewing changes to vivid/etc/init.d/networking-routes

  • Committer: Dimitri John Ledkov
  • Date: 2014-11-19 12:58:41 UTC
  • Revision ID: dimitri.j.ledkov@intel.com-20141119125841-98dr37roy8dvcv3b
auto update

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh -e
 
2
# Script to add global static routes to the system
 
3
#
 
4
 
5
### BEGIN INIT INFO
 
6
# Provides:          networking-routes
 
7
# Required-Start:    $network $local_fs
 
8
# Required-Stop:     $local_fs
 
9
# Default-Start:     S
 
10
# Default-Stop:      
 
11
# Short-Description: Establish global networking routes for the system
 
12
# Description:       Define global network routes for the system using
 
13
#                    the configuration defined in /etc/network/routes
 
14
#                    Global routes can be either routes for which
 
15
#                    the associated interface cannot be determined
 
16
#                    beforehand (maybe the device name is not known)
 
17
#                    or 'reject' routes to prevent the system from
 
18
#                    communicating with remote networks.
 
19
### END INIT INFO
 
20
#
 
21
# Copyright (c) 2011 Javier Fernandez-Sanguino <jfs@debian.org>
 
22
#
 
23
# Some portions (specifically the code of the run_route() function) are derived
 
24
# from the ifup-route script in SuSE's sysconfig package.
 
25
# These portions are:
 
26
#
 
27
# - Copyright (c) 2002 SuSE Linux AG Nuernberg, Germany. All rights reserved.
 
28
# -  Author: Christian Zoz <zoz@suse.de>, 2002
 
29
# -  Based on rcroute: Burchard Steinbild <bs@suse.de>, 1996
 
30
#                Werner Fink <werner@suse.de>, 1996-2000
 
31
#
 
32
#
 
33
# This program is free software; you can redistribute it and/or modify it under
 
34
# the terms of the GNU General Public License as published by the Free Software
 
35
# Foundation; either version 2 of the License, or (at your option) any later
 
36
# version.
 
37
#
 
38
# This program is distributed in the hope that it will be useful, but WITHOUT
 
39
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
40
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
41
# details.
 
42
#
 
43
# You should have received a copy of the GNU General Public License along with
 
44
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
45
# Place, Suite 330, Boston, MA 02111-1307 USA
 
46
#
 
47
 
 
48
[ -x /sbin/route ] || exit 0
 
49
ROUTEFILE="/etc/network/routes"
 
50
# Abort (without error) if the configuration file does not exist
 
51
[ ! -r "$ROUTEFILE" ] && exit 0
 
52
 
 
53
. /lib/lsb/init-functions
 
54
# Default value
 
55
VERBOSITY=${VERBOSITY:-0}
 
56
 
 
57
 
 
58
# Functions to read the route file and process it
 
59
 
 
60
 
 
61
run_route() {
 
62
        local COMMAND="route $*"
 
63
        export LC_MESSAGES=C # We need the return messages to be in english
 
64
        RETMESSAGE="$($COMMAND 2>&1)"
 
65
        RETVALUE=$?
 
66
        if test $RETVALUE -ne 0 ; then
 
67
                [ "$VERBOSITY" -eq 1 ] && echo "DEBUG: calling: '$COMMAND' FAILED"
 
68
                # Process the messages and omits those that are not
 
69
                # relevant.
 
70
                case "$RETMESSAGE" in
 
71
                # Omit 'File exists' since the route is already there..
 
72
                        *File*exists) return ;;
 
73
                # 'No such process' is only omitted if the route is being
 
74
                # deleted.  If the route is being created, this error message
 
75
                # might appear if the gateway is not reachable.
 
76
                        *No*such*process) [ "$1" = "del" ] && return ;;
 
77
                        *)
 
78
                esac
 
79
                log_failure_msg "Error while executing:" \
 
80
                         "  Command '$COMMAND' returned:  ${RETMESSAGE%%Usage:*}"\
 
81
                         "  Configuration line: $LINE"
 
82
        else
 
83
                [ "$VERBOSITY" -eq 1 ] && echo "DEBUG: calling: '$COMMAND' SUCCEEDED"
 
84
        fi
 
85
 
 
86
 
 
87
 
 
88
 
89
 
 
90
del_global_routes() {
 
91
        ret=0
 
92
        cat $ROUTEFILE | egrep "^[^#].*any$" | 
 
93
        while read network netmask gateway interface ; do
 
94
            if [ -n "$interface" ] && [ -n "$network" ] && [ -n "$netmask" ] && [ -n "$gateway" ] ; then
 
95
                if [ "$gateway" != "reject" ] ; then
 
96
                    [ "$VERBOSITY" -eq 1 ] && echo "DEBUG: Deleting global route for $network / $netmask through gateway $gateway"
 
97
                    run_route del -net $network netmask $netmask gw $gateway 
 
98
                    [ $? -ne 0 ] && ret=$?
 
99
                else
 
100
                    [ "$VERBOSITY" -eq 1 ] && echo "DEBUG: Deleting reject route for $network / $netmask"
 
101
                    run_route del -net $network netmask $netmask reject
 
102
                    [ $? -ne 0 ] && ret=$?
 
103
                fi
 
104
 
 
105
            else
 
106
                echo "ERROR: Incorrect line for global network routes in $ROUTEFILE: '$network $netmask $gateway $interface'"
 
107
                ret=1
 
108
            fi
 
109
        done
 
110
        return $ret
 
111
}
 
112
 
 
113
add_global_routes() {
 
114
        ret=0
 
115
        cat $ROUTEFILE | egrep "^[^#].*any$" | 
 
116
        while read network netmask gateway interface ; do
 
117
            if [ -n "$interface" ] && [ -n "$network" ] && [ -n "$netmask" ] && [ -n "$gateway" ] ; then
 
118
                if [ "$gateway" != "reject" ] ; then
 
119
                    [ "$VERBOSITY" -eq 1 ] && echo "DEBUG: Adding global route for $network / $netmask through gateway $gateway"
 
120
                    run_route add -net $network netmask $netmask gw $gateway
 
121
                    [ $? -ne 0 ] && ret=$?
 
122
                else
 
123
                    [ "$VERBOSITY" -eq 1 ] && echo "DEBUG: Adding global reject route for $network / $netmask"
 
124
                    run_route add -net $network netmask $netmask reject
 
125
                    [ $? -ne 0 ] && ret=$?
 
126
                fi
 
127
 
 
128
            else
 
129
                echo "ERROR: Incorrect line for global network routes in $ROUTEFILE: '$network $netmask $gateway $interface'"
 
130
                ret=1
 
131
            fi
 
132
        done
 
133
        return $ret
 
134
}
 
135
 
 
136
case "$1" in
 
137
start)
 
138
        log_action_begin_msg "Configuring network routes"
 
139
        if add_global_routes; then
 
140
            log_action_end_msg $?
 
141
        else
 
142
            log_action_end_msg $?
 
143
        fi
 
144
        ;;
 
145
 
 
146
stop)
 
147
        log_action_begin_msg "Deconfiguring network routes"
 
148
        if del_global_routes; then
 
149
            log_action_end_msg $?
 
150
        else
 
151
            log_action_end_msg $?
 
152
        fi
 
153
        ;;
 
154
 
 
155
reload|force-reload|restart)
 
156
        log_action_begin_msg "Reconfiguring network routes"
 
157
        del_global_routes
 
158
        if add_global_routes; then
 
159
            log_action_end_msg $?
 
160
        else
 
161
            log_action_end_msg $?
 
162
        fi
 
163
        ;;
 
164
 
 
165
*)
 
166
        echo "Usage: /etc/init.d/networking-routes {start|stop|restart|reload}"
 
167
        exit 1
 
168
        ;;
 
169
esac
 
170
 
 
171
exit 0
 
172