]> git.proxmox.com Git - mirror_frr.git/blob - tools/frr.in
mgmtd: Bringup MGMTD daemon and datastore module support
[mirror_frr.git] / tools / frr.in
1 #!/bin/bash
2 #
3 ### BEGIN INIT INFO
4 # Provides: frr
5 # Required-Start: $local_fs $network $remote_fs $syslog
6 # Required-Stop: $local_fs $network $remote_fs $syslog
7 # Default-Start: 2 3 4 5
8 # Default-Stop: 0 1 6
9 # Short-Description: start and stop the Frr routing suite
10 # Description: Frr is a routing suite for IP routing protocols like
11 # BGP, OSPF, RIP and others. This script contols the main
12 # daemon "frr" as well as the individual protocol daemons.
13 ### END INIT INFO
14 #
15
16 PATH=/bin:/usr/bin:/sbin:/usr/sbin
17 D_PATH="@CFG_SBIN@" # /usr/lib/frr
18 C_PATH="@CFG_SYSCONF@" # /etc/frr
19 V_PATH="@CFG_STATE@" # /var/run/frr
20 VTYSH="@vtysh_bin@" # /usr/bin/vtysh
21 FRR_USER="@enable_user@" # frr
22 FRR_GROUP="@enable_group@" # frr
23 FRR_VTY_GROUP="@enable_vty_group@" # frrvty
24 FRR_CONFIG_MODE="@enable_configfile_mask@" # 0600
25 FRR_DEFAULT_PROFILE="@DFLT_NAME@" # traditional / datacenter
26
27 # Local Daemon selection may be done by using /etc/frr/daemons.
28 # See /usr/share/doc/frr/README.Debian.gz for further information.
29 # Keep zebra first and do not list watchfrr!
30 DAEMONS="mgmtd zebra bgpd ripd ripngd ospfd ospf6d isisd babeld pimd pim6d ldpd nhrpd eigrpd sharpd pbrd staticd bfdd fabricd vrrpd pathd"
31 MAX_INSTANCES=5
32 RELOAD_SCRIPT="$D_PATH/frr-reload.py"
33
34 if [ -e /lib/lsb/init-functions ]; then
35 . /lib/lsb/init-functions
36 fi
37
38 if [ -f $D_PATH/ssd ]; then
39 SSD=$D_PATH/ssd
40 else
41 SSD=`which start-stop-daemon`
42 fi
43
44 # Print the name of the pidfile.
45 pidfile()
46 {
47 echo "$V_PATH/$1.pid"
48 }
49
50 # Print the name of the vtysh.
51 vtyfile()
52 {
53 echo "$V_PATH/$1.vty"
54 }
55
56 # Check if daemon is started by using the pidfile.
57 started()
58 {
59 [ ! -e `pidfile $1` ] && return 3
60 if [ -n "$2" ] && [ "$2" == "log" ]; then
61 status_of_proc -p `pidfile $1` $1 $1 && return 0 || return $?
62 else
63 kill -0 `cat \`pidfile $1\`` 2> /dev/null || return 1
64 return 0
65 fi
66 }
67
68 # Loads the config via vtysh -b if configured to do so.
69 vtysh_b ()
70 {
71 # Rember, that all variables have been incremented by 1 in convert_daemon_prios()
72 if [ "$vtysh_enable" = 2 -a -f $C_PATH/frr.conf ]; then
73 $VTYSH -b
74 fi
75 }
76
77 # Check if the daemon is activated and if its executable and config files
78 # are in place.
79 # params: daemon name
80 # returns: 0=ok, 1=error
81 check_daemon()
82 {
83 if [ $1 != "watchfrr" -a $1 != "vtysh_enable" ]; then
84 # check for daemon binary
85 if [ ! -x "$D_PATH/$1" ]; then return 1; fi
86 fi
87
88 # If the integrated config file is used the others are not checked.
89 if [ -r "$C_PATH/frr.conf" ]; then
90 return 0
91 fi
92
93 # vtysh_enable has no config file nor binary so skip check.
94 # (Not sure why vtysh_enable is in this list but does not hurt)
95 if [ $1 != "watchfrr" -a $1 != "vtysh_enable" ]; then
96 # check for config file
97 if [ -n "$2" ]; then
98 if [ ! -r "$C_PATH/$1-$2.conf" ]; then
99 install -g "$FRR_GROUP" -o "$FRR_USER" -m "$FRR_CONFIG_MODE" /dev/null "$C_PATH/$1-$2.conf"
100 fi
101 elif [ ! -r "$C_PATH/$1.conf" ]; then
102 install -g "$FRR_GROUP" -o "$FRR_USER" -m "$FRR_CONFIG_MODE" /dev/null "$C_PATH/$1.conf"
103 fi
104 fi
105 return 0
106 }
107
108 # Starts the server if it's not alrady running according to the pid file.
109 # The Frr daemons creates the pidfile when starting.
110 start()
111 {
112 local dmn inst
113 dmn="$1"
114 inst="$2"
115
116 ulimit -n $MAX_FDS > /dev/null 2> /dev/null
117 if [ "$dmn" = "watchfrr" ]; then
118
119 # We may need to restart watchfrr if new daemons are added and/or
120 # removed
121 if started "$dmn" ; then
122 stop watchfrr
123 else
124 # Echo only once. watchfrr is printed in the stop above
125 echo -n " $dmn"
126 fi
127
128 eval "set - $watchfrr_options"
129 ${SSD} \
130 --start \
131 --pidfile=`pidfile $dmn` \
132 --exec "$D_PATH/$dmn" \
133 -- \
134 "$@"
135
136 elif [ -n "$inst" ]; then
137 echo -n " $dmn-$inst"
138 if ! check_daemon $dmn $inst ; then
139 echo -n " (binary does not exist)"
140 return;
141 fi
142
143 ${SSD} \
144 --start \
145 --pidfile=`pidfile $dmn-$inst` \
146 --exec "$D_PATH/$dmn" \
147 -- \
148 `eval echo "$""$dmn""_options"` $frr_global_options -n "$inst"
149 else
150 if ! check_daemon $dmn; then
151 echo -n " (binary does not exist)"
152 return;
153 fi
154
155 if [ "$valgrind_enable" = "yes" ]; then
156 ${SSD} \
157 --start \
158 --pidfile=`pidfile $dmn` \
159 --exec "$valgrind" \
160 -- --trace-children=no --leak-check=full --log-file=/var/log/frr/$dmn-valgrind.log $D_PATH/$dmn \
161 `eval echo "$""$dmn""_options"` $frr_global_options
162 else
163 ${SSD} \
164 --start \
165 --pidfile=`pidfile $dmn` \
166 --exec "$D_PATH/$dmn" \
167 -- \
168 `eval echo "$""$dmn""_options"` $frr_global_options
169 fi
170 fi
171
172 # Start the staticd automatically
173 if [ "$dmn" = "zebra" ]; then
174 echo -n "starting staticd since zebra is running"
175 if ! check_daemon staticd ; then
176 echo -n " (binary does not exist)"
177 return;
178 fi
179
180 ${SSD} \
181 --start \
182 --pidfile=`pidfile staticd` \
183 --exec "$D_PATH/staticd" \
184 -- \
185 `eval echo "$"staticd"_options"` $frr_global_options
186 fi
187 }
188
189 # Stop the daemon given in the parameter, printing its name to the terminal.
190 stop()
191 {
192 local inst
193
194 if [ -n "$2" ]; then
195 inst="$1-$2"
196 else
197 inst="$1"
198 fi
199
200 if ! started "$inst" ; then
201 echo -n " ($inst)"
202 return 0
203 else
204 PIDFILE=`pidfile $inst`
205 PID=`cat $PIDFILE 2>/dev/null`
206 kill -2 $PID 2>/dev/null
207 #
208 # Now we have to wait until $DAEMON has _really_ stopped.
209 #
210 if test -n "$PID" && kill -0 $PID 2>/dev/null; then
211 cnt=0
212 while kill -0 $PID 2>/dev/null; do
213 cnt=`expr $cnt + 1`
214 if [ $cnt -gt 60 ]; then
215 # Waited 120 secs now, fail.
216 echo -n "Failed.. "
217 break
218 fi
219 sleep 2
220 done
221 fi
222 rm -f `pidfile $inst`
223 rm -f `vtyfile $inst`
224
225 if [ "$1" = "zebra" ]; then
226 echo -n "Stopping staticd since zebra is running"
227 stop staticd
228 fi
229 fi
230 }
231
232 # Converts values from /etc/frr/daemons to all-numeric values.
233 convert_daemon_prios()
234 {
235 for name in $DAEMONS zebra vtysh_enable watchfrr_enable; do
236 # First, assign the value set by the user to $value
237 eval value=\${${name}:0:3}
238
239 # Daemon not activated or entry missing?
240 if [ "$value" = "no" -o "$value" = "" ]; then value=0; fi
241
242 # These strings parsed for backwards compatibility.
243 if [ "$value" = "yes" -o "$value" = "true" ]; then
244 value=1;
245 fi
246
247 # Zebra is threatened special. It must be between 0=off and the first
248 # user assigned value "1" so we increase all other enabled daemons' values.
249 if [ "$name" != "zebra" -a "$value" -gt 0 ]; then value=`expr "$value" + 1`; fi
250
251 # If e.g. name is zebra then we set "zebra=yes".
252 eval $name=$value
253 done
254 }
255
256 # Starts watchfrr for all wanted daemons.
257 start_watchfrr()
258 {
259 local daemon_name
260 local daemon_prio
261 local found_one
262 local daemon_inst
263
264 # Start the monitor daemon only if desired.
265 if [ 0 -eq "$watchfrr_enable" ]; then
266 return
267 fi
268
269 # Check variable type
270 if declare -p watchfrr_options | grep -q '^declare \-a'; then
271 # old array support
272 watchfrr_options="${watchfrr_options[@]}"
273 fi
274
275 # Which daemons have been started?
276 found_one=0
277 for daemon_name in $DAEMONS; do
278 eval daemon_prio=\$$daemon_name
279 if [ "$daemon_prio" -gt 0 ]; then
280 eval "daemon_inst=\${${daemon_name}_instances//,/ }"
281 if [ -n "$daemon_inst" ]; then
282 for inst in ${daemon_inst}; do
283 eval "inst_disable=\${${daemon_name}_${inst}}"
284 if [ -z ${inst_disable} ] || [ ${inst_disable} != 0 ]; then
285 if check_daemon $daemon_name $inst; then
286 watchfrr_options="$watchfrr_options ${daemon_name}-${inst}"
287 fi
288 fi
289 done
290 else
291 if check_daemon $daemon_name; then
292 watchfrr_options="$watchfrr_options $daemon_name"
293 fi
294 fi
295 found_one=1
296 fi
297 done
298
299 # Start if at least one daemon is activated.
300 if [ $found_one -eq 1 ]; then
301 start watchfrr
302 echo "."
303 fi
304 }
305
306 # Stopps watchfrr.
307 stop_watchfrr()
308 {
309 echo -n "Stopping Frr monitor daemon:"
310 stop watchfrr
311 echo "."
312 }
313
314 # Stops all daemons that have a lower level of priority than the given.
315 # (technically if daemon_prio >= wanted_prio)
316 stop_prio()
317 {
318 local wanted_prio
319 local daemon_prio
320 local daemon_list
321 local daemon_inst
322 local inst
323
324 if [ -n "$2" ] && [[ "$2" =~ (.*)-(.*) ]]; then
325 daemon=${BASH_REMATCH[1]}
326 inst=${BASH_REMATCH[2]}
327 else
328 daemon="$2"
329 fi
330
331 wanted_prio=$1
332 daemon_list=${daemon:-$DAEMONS}
333
334 echo -n "Stopping Frr daemons (prio:$wanted_prio):"
335
336 for prio_i in `seq 10 -1 $wanted_prio`; do
337 for daemon_name in $daemon_list; do
338 eval daemon_prio=\${${daemon_name}:0:3}
339 daemon_inst=""
340 if [ $daemon_prio -eq $prio_i ]; then
341 eval "daemon_inst=\${${daemon_name}_instances//,/ }"
342 if [ -n "$daemon_inst" ]; then
343 for i in ${daemon_inst}; do
344 if [ -n "$inst" ] && [ "$i" == "$inst" ]; then
345 stop "$daemon_name" "$inst"
346 elif [ x"$inst" == x ]; then
347 stop "$daemon_name" "$i"
348 fi
349 done
350 else
351 stop "$daemon_name"
352 fi
353 fi
354 done
355 done
356
357 echo "."
358 if [ -z "$inst" ]; then
359 # Now stop other daemons that're prowling, coz the daemons file changed
360 echo -n "Stopping other frr daemons"
361 if [ -n "$daemon" ]; then
362 eval "file_list_suffix="$V_PATH"/"$daemon*""
363 else
364 eval "file_list_suffix="$V_PATH/*""
365 fi
366 for pidfile in $file_list_suffix.pid; do
367 PID=`cat $pidfile 2>/dev/null`
368 ${SSD} --stop --quiet --oknodo --pidfile "$pidfile"
369 echo -n "."
370 rm -rf "$pidfile"
371 done
372 echo "."
373
374 echo -n "Removing remaining .vty files"
375 for vtyfile in $file_list_suffix.vty; do
376 rm -rf "$vtyfile"
377 done
378 echo "."
379 fi
380 }
381
382 # Starts all daemons that have a higher level of priority than the given.
383 # (technically if daemon_prio <= wanted_prio)
384 start_prio()
385 {
386 local wanted_prio
387 local daemon_prio
388 local daemon_list
389 local daemon_name
390 local daemon_inst
391 local inst
392
393 if [ -n "$2" ] && [[ "$2" =~ (.*)-(.*) ]]; then
394 daemon=${BASH_REMATCH[1]}
395 inst=${BASH_REMATCH[2]}
396 else
397 daemon="$2"
398 fi
399
400 wanted_prio=$1
401 daemon_list=${daemon:-$DAEMONS}
402
403 for prio_i in `seq 1 $wanted_prio`; do
404 for daemon_name in $daemon_list; do
405 eval daemon_prio=\$${daemon_name}
406 daemon_inst=""
407 if [ $daemon_prio -eq $prio_i ]; then
408 eval "daemon_inst=\${${daemon_name}_instances//,/ }"
409 if [ -n "$daemon_inst" ]; then
410 if [ `echo "$daemon_inst" | wc -w` -gt ${MAX_INSTANCES} ]; then
411 echo "Max instances supported is ${MAX_INSTANCES}. Aborting"
412 exit 1
413 fi
414 # Check if we're starting again by switching from single instance
415 # to MI version
416 if started "$daemon_name"; then
417 PIDFILE=`pidfile $daemon_name`
418 ${SSD} \
419 --stop --quiet --oknodo \
420 --pidfile "$PIDFILE" \
421 --exec "$D_PATH/$daemon_name"
422
423 rm -f `pidfile $1`
424 rm -f `vtyfile $1`
425 fi
426
427 for i in ${daemon_inst}; do
428 if [ -n "$inst" ] && [ "$i" == "$inst" ]; then
429 start "$daemon_name" "$inst"
430 elif [ x"$inst" == x ]; then
431 start "$daemon_name" "$i"
432 fi
433 done
434 else
435 # Check if we're starting again by switching from
436 # single instance to MI version
437 eval "file_list_suffix="$V_PATH"/"$daemon_name-*""
438 for pidfile in $file_list_suffix.pid; do
439 ${SSD} --stop --quiet --oknodo --pidfile "$pidfile"
440 rm -rf "$pidfile"
441 done
442 for vtyfile in $file_list_suffix.vty; do
443 rm -rf "$vtyfile"
444 done
445
446 start "$daemon_name"
447 fi
448 fi
449 done
450 done
451 }
452
453 check_status()
454 {
455 local daemon_name
456 local daemon_prio
457 local daemon_inst
458 local failed_status=0
459
460 if [ -n "$1" ] && [[ "$1" =~ (.*)-(.*) ]]; then
461 daemon=${BASH_REMATCH[1]}
462 inst=${BASH_REMATCH[2]}
463 else
464 daemon="$1"
465 fi
466
467 daemon_list=${daemon:-$DAEMONS}
468
469 # Which daemons have been started?
470 for daemon_name in $daemon_list; do
471 eval daemon_prio=\$$daemon_name
472 if [ "$daemon_prio" -gt 0 ]; then
473 eval "daemon_inst=\${${daemon_name}_instances//,/ }"
474 if [ -n "$daemon_inst" ]; then
475 for i in ${daemon_inst}; do
476 if [ -n "$inst" -a "$inst" = "$i" ]; then
477 started "$1" "log" || failed_status=$?
478 elif [ -z "$inst" ]; then
479 started "$daemon_name-$i" "log" || failed_status=$?
480 fi
481 done
482 else
483 started "$daemon_name" "log" || failed_status=$?
484 fi
485 fi
486 done
487
488 # All daemons that need to have been started are up and running
489 return $failed_status
490 }
491
492 #########################################################
493 # Main program #
494 #########################################################
495
496 # Config broken but script must exit silently.
497 [ ! -r "$C_PATH/daemons" ] && exit 0
498
499 # Load configuration
500 . "$C_PATH/daemons"
501 if [ -e "$C_PATH/daemons.conf" ]; then
502 . "$C_PATH/daemons.conf"
503 fi
504
505 # Read configuration variable file if it is present
506 [ -r /etc/default/frr ] && . /etc/default/frr
507
508 if test -z "$frr_profile"; then
509 # try to autodetect config profile
510 if test -d /etc/cumulus; then
511 frr_profile=datacenter
512 # elif test ...; then
513 # -- add your distro/system here
514 elif test -n "$FRR_DEFAULT_PROFILE"; then
515 frr_profile="$FRR_DEFAULT_PROFILE"
516 fi
517 fi
518 test -n "$frr_profile" && frr_global_options="$frr_global_options -F $frr_profile"
519
520 MAX_INSTANCES=${MAX_INSTANCES:=5}
521
522 # Set priority of un-startable daemons to 'no' and substitute 'yes' to '0'
523 convert_daemon_prios
524
525 if [ ! -d $V_PATH ]; then
526 echo "Creating $V_PATH"
527 install -g "$FRR_GROUP" -o "$FRR_USER" -m "$FRR_CONFIG_MODE" -d "$V_PATH"
528 chmod gu+x "${V_PATH}"
529 fi
530
531 if [ -n "$3" ] && [ "$3" != "all" ]; then
532 dmn="$2"-"$3"
533 elif [ -n "$2" ] && [ "$2" != "all" ]; then
534 dmn="$2"
535 fi
536
537 case "$1" in
538 start)
539 # Try to load this necessary (at least for 2.6) module.
540 if [ -d /lib/modules/`uname -r` ] ; then
541 echo "Loading capability module if not yet done."
542 set +e; LC_ALL=C modprobe -a capability 2>&1 | egrep -v "(not found|Can't locate)"; set -e
543 fi
544
545 # Start all daemons
546 cd $C_PATH/
547 if [ "$2" != "watchfrr" ]; then
548 start_prio 10 $dmn
549 fi
550 start_watchfrr
551 vtysh_b
552 ;;
553
554 1|2|3|4|5|6|7|8|9|10)
555 # Stop/start daemons for the appropriate priority level
556 stop_prio $1
557 start_prio $1
558 vtysh_b
559 ;;
560
561 stop|0)
562 # Stop all daemons at level '0' or 'stop'
563 stop_watchfrr
564 if [ "$dmn" != "watchfrr" ]; then
565 [ -n "${dmn}" ] && eval "${dmn/-/_}=0"
566 stop_prio 0 $dmn
567 fi
568
569 if [ -n "$dmn" -a "$dmn" != "zebra" ]; then
570 [ -n "$dmn" ] && eval "${dmn/-/_}=0"
571 start_watchfrr
572 fi
573 ;;
574
575 reload)
576 # Just apply the commands that have changed, no restart necessary
577 if [ ! -x "$RELOAD_SCRIPT" ]; then
578 echo "Please install frr-pythontools package. Required for reload"
579 exit 0
580 fi
581
582 NEW_CONFIG_FILE="${2:-$C_PATH/frr.conf}"
583 [ ! -r $NEW_CONFIG_FILE ] && echo "Unable to read new configuration file $NEW_CONFIG_FILE" && exit 1
584 echo "Applying only incremental changes to running configuration from frr.conf"
585 "$RELOAD_SCRIPT" --reload --bindir "$D_PATH" --confdir "$C_PATH" --rundir "$V_PATH" "$C_PATH/frr.conf"
586 exit $?
587 ;;
588
589 status)
590 check_status $dmn
591 exit $?
592 ;;
593
594 restart|force-reload)
595 $0 stop $dmn
596 sleep 1
597 $0 start $dmn
598 ;;
599
600 *)
601 echo "Usage: /etc/init.d/frr {start|stop|status|reload|restart|force-reload|<priority>} [daemon]"
602 echo " E.g. '/etc/init.d/frr 5' would start all daemons with a prio 1-5."
603 echo " reload applies only modifications from the running config to all daemons."
604 echo " reload neither restarts starts any daemon nor starts any new ones."
605 echo " Read /usr/share/doc/frr/README.Debian for details."
606 exit 1
607 ;;
608 esac
609
610 echo "Exiting from the script"
611 exit 0