]> git.proxmox.com Git - ceph.git/blame - ceph/src/init-ceph.in
import 15.2.0 Octopus source
[ceph.git] / ceph / src / init-ceph.in
CommitLineData
7c673cae
FG
1#!/bin/sh
2# Start/stop ceph daemons
3# chkconfig: 2345 60 80
4
5### BEGIN INIT INFO
6# Provides: ceph
7# Default-Start: 2 3 4 5
8# Default-Stop: 0 1 6
9# Required-Start: $remote_fs $named $network $time
10# Required-Stop: $remote_fs $named $network $time
11# Short-Description: Start Ceph distributed file system daemons at boot time
12# Description: Enable Ceph distributed file system services.
13### END INIT INFO
14
15# TODO: on FreeBSD/OSX, use equivalent script file
16if [ -e /lib/lsb/init-functions ]; then
17 . /lib/lsb/init-functions
18fi
19
9f95a23c
TL
20[ -z "$BUILD_DIR" ] && BUILD_DIR=build
21
7c673cae
FG
22if [ `dirname $0` = "." ] && [ $PWD != "/etc/init.d" ]; then
23 # looks like an autotools src dir build
24 BINDIR=.
25 SBINDIR=.
26 LIBEXECDIR=.
27 ETCDIR=.
28 ASSUME_DEV=1
29else
30 if [ -e CMakeCache.txt ] && [ -e bin/init-ceph ]; then
31 # looks like a cmake build directory
32 CEPH_ROOT=`grep ceph_SOURCE_DIR CMakeCache.txt | cut -d "=" -f 2`
33 BINDIR=bin
34 SBINDIR=bin
35 LIBEXECDIR=$CEPH_ROOT/src
36 ETCDIR=.
37 ASSUME_DEV=1
9f95a23c
TL
38 CEPH_LIB=$CEPH_ROOT/${BUILD_DIR}/lib
39 echo "$PYTHONPATH" | grep -q $CEPH_LIB || export PYTHONPATH=$CEPH_LIB/cython_modules/lib.3:$PYTHONPATH
7c673cae
FG
40 echo "$LD_LIBRARY_PATH" | grep -q $CEPH_LIB || export LD_LIBRARY_PATH=$CEPH_LIB:$LD_LIBRARY_PATH
41 echo "$DYLD_LIBRARY_PATH" | grep -q $CEPH_LIB || export DYLD_LIBRARY_PATH=$CEPH_LIB:$DYLD_LIBRARY_PATH
42 else
43 BINDIR=@bindir@
44 SBINDIR=@sbindir@
45 LIBEXECDIR=@libexecdir@/ceph
46 ETCDIR=@sysconfdir@/ceph
47 ASSUME_DEV=0
48 fi
49fi
50
51if [ -n "$CEPH_BIN" ] && [ -n "$CEPH_ROOT" ] && [ -n "$CEPH_BUILD_DIR" ]; then
52 BINDIR=$CEPH_BIN
53 SBINDIR=$CEPH_ROOT/src
54 ETCDIR=$CEPH_BIN
55 LIBEXECDIR=$CEPH_ROOT/src
56 ASSUME_DEV=1
57fi
58
59if [ `uname` = FreeBSD ]; then
60 GETOPT=/usr/local/bin/getopt
61else
62 GETOPT=getopt
63fi
64
65if id ceph > /dev/null 2>&1; then
66 SET_CEPHUSER_ARGS=" --setuser ceph --setgroup ceph"
67fi
68
69usage_exit() {
70 echo "usage: $0 [options] {start|stop|restart|condrestart} [mon|osd|mds]..."
71 printf "Core options:\n"
72 printf "\t--allhosts / -a execute (via ssh) on all hosts in conf file\n"
73 printf "\t--cluster [cluster name] define the cluster name\n"
74 printf "\t--conf / -c [conf file] use [conf file] instead of default\n"
75 printf "\t--help / -h show this usage message\n"
76 printf "\t--hostname [hostname] override hostname lookup\n"
77 printf "\t-m [mon addr] mon address\n"
78 printf "\n"
79 printf "Other options:\n"
80 printf "\t--btrfs btrfs\n"
81 printf "\t--nobtrfs no btrfs\n"
82 printf "\t--btrfsumount btrfs umount\n"
83 printf "\t--fsmount fsmount\n"
84 printf "\t--nofsmount no fsmount\n"
85 printf "\t--fsumount fsumount\n"
86 printf "\t--restart restart on core dump\n"
87 printf "\t--norestart do not restart on core dump\n"
88 printf "\t--valgrind run via valgrind\n"
89 printf "\t--novalgrind do not run via valgrind\n"
90 printf "\t--verbose / -v be verbose\n"
91 exit
92}
93
94# behave if we are not completely installed (e.g., Debian "removed,
95# config remains" state)
96test -f $LIBEXECDIR/ceph_common.sh || exit 0
97
98. $LIBEXECDIR/ceph_common.sh
99
100EXIT_STATUS=0
101
102signal_daemon() {
103 name=$1
104 daemon=$2
105 pidfile=$3
106 signal=$4
107 action=$5
108 [ -z "$action" ] && action="Stopping"
109 printf "$action Ceph $name on $host..."
110 do_cmd "if [ -e $pidfile ]; then
111 pid=\`cat $pidfile\`
112 if ps -p \$pid -o args= | grep -q $daemon; then
113 cmd=\"kill $signal \$pid\"
114 printf \"\$cmd...\"
115 \$cmd
116 fi
117 fi"
118 echo done
119}
120
121daemon_is_running() {
122 name=$1
123 daemon=$2
124 daemon_id=$3
125 pidfile=$4
126 do_cmd "[ -e $pidfile ] || exit 1 # no pid, presumably not running
127 pid=\`cat $pidfile\`
128 ps -p \$pid -o args= | grep $daemon | grep -qwe -i.$daemon_id && exit 0 # running
129 exit 1 # pid is something else" "" "okfail"
130}
131
132stop_daemon() {
133 name=$1
134 daemon=$2
135 pidfile=$3
136 signal=$4
137 action=$5
138 [ -z "$action" ] && action="Stopping"
139 printf "$action Ceph $name on $host..."
140 do_cmd "if [ -e $pidfile ] ; then
141 pid=\`cat $pidfile\`
142 while ps -p \$pid -o args= | grep -q $daemon; do
143 cmd=\"kill $signal \$pid\"
144 printf \"\$cmd...\"
145 \$cmd
9f95a23c 146 sleep 2
7c673cae
FG
147 continue
148 done
149 fi"
150 echo done
151}
152
153## command line options
154options=
155
156OPTS=$(${GETOPT} -n 'init-ceph' -o 'hvam:c:' -l 'help,verbose,valgrind,novalgrind,allhosts,restart,norestart,btrfs,nobtrfs,fsmount,nofsmount,btrfsumount,fsumount,conf:,cluster:,hostname:' -- "$@")
157if [ $? != 0 ]
158then
159 exit 1
160fi
161
162eval set -- "$OPTS"
163
164dovalgrind=
165docrun=
166allhosts=0
167monaddr=
168dofsmount=1
169dofsumount=0
170verbose=0
171use_default_conf=1
172
173## set variables like cluster or conf
174[ -e /etc/sysconfig/ceph ] && . /etc/sysconfig/ceph
175[ -e /etc/default/ceph ] && . /etc/default/ceph
176
177
178while echo $1 | grep -q '^-'; do # FIXME: why not '^-'?
179case $1 in
180 -v | --verbose)
181 verbose=1
182 ;;
183 --valgrind)
184 dovalgrind=1
185 ;;
186 --novalgrind)
187 dovalgrind=0
188 ;;
189 --allhosts | -a)
190 allhosts=1;
191 ;;
192 --restart)
193 docrun=1
194 ;;
195 --norestart)
196 docrun=0
197 ;;
198 -h | --help)
199 usage_exit
200 ;;
201 -m )
202 [ -z "$2" ] && usage_exit
203 options="$options $1"
204 shift
205 MON_ADDR=$1
206 ;;
207 --btrfs | --fsmount)
208 dofsmount=1
209 ;;
210 --nobtrfs | --nofsmount)
211 dofsmount=0
212 ;;
213 --btrfsumount | --fsumount)
214 dofsumount=1
215 ;;
216 --conf | -c)
217 [ -z "$2" ] && usage_exit
218 options="$options $1"
219 shift
220 use_default_conf=0
221 conf=$1
222 ;;
223 --cluster )
224 [ -z "$2" ] && usage_exit
225 options="$options $1"
226 shift
227 cluster=$1
228 ;;
229 --hostname )
230 [ -z "$2" ] && usage_exit
231 options="$options $1"
232 shift
233 hostname=$1
234 ;;
235 -- )
236 shift
237 break
238 ;;
239 *)
240 echo unrecognized option \'$1\'
241 usage_exit
242 ;;
243esac
244options="$options $1"
245shift
246done
247
248# if `--cluster` was not passed in, fallback to looking at the config name
249if [ -z "$cluster" ]; then
250 cluster=`echo $conf | awk -F'/' '{print $(NF)}' | cut -d'.' -f 1`
251else
252 # if we were told to use a given cluster name then $conf needs to be updated
253 # but just define it if `--conf` was not specified, otherwise we would be silently
254 # overriding $conf even if it was defined with `--conf`
255 if [ $use_default_conf -eq 1 ]; then
256 conf="/etc/ceph/$cluster.conf"
257 fi
258fi
259
260
261verify_conf
262
263command=$1
264[ -n "$*" ] && shift
265
266get_local_name_list
267get_name_list "$@"
268
269# Reverse the order if we are stopping
270
271if [ "$command" = "stop" -o "$command" = "onestop" ]; then
272 for f in $what; do
273 new_order="$f $new_order"
274 done
275 what="$new_order"
276fi
277
278for name in $what; do
279 type=`echo $name | cut -c 1-3` # e.g. 'mon', if $item is 'mon1'
280 id=`echo $name | cut -c 4- | sed 's/^\\.//'`
281 num=$id
282 name="$type.$id"
283
284 check_host $cluster || continue
285
286 binary="$BINDIR/ceph-$type"
287 cmd="$binary -i $id"
288 if [ $ASSUME_DEV -eq 1 ]; then
289 cmd="PATH=$PWD:$PATH $cmd"
290 fi
291
292 get_conf run_dir "/var/run/ceph" "run dir"
293
294 get_conf pid_file "$run_dir/$type.$id.pid" "pid file"
295
296 if [ "$command" = "start" -o "$command" = "onestart" ]; then
297 if [ -n "$pid_file" ]; then
298 do_cmd "mkdir -p "`dirname $pid_file`
299 cmd="$cmd --pid-file $pid_file"
300 fi
301
302 get_conf log_dir "" "log dir"
303 [ -n "$log_dir" ] && do_cmd "mkdir -p $log_dir"
304
305 get_conf auto_start "" "auto start"
306 if [ "$auto_start" = "no" ] || [ "$auto_start" = "false" ] || [ "$auto_start" = "0" ]; then
307 if [ -z "$@" ]; then
308 echo "Skipping Ceph $name on $host... auto start is disabled"
309 continue
310 fi
311 fi
312
313 if daemon_is_running $name ceph-$type $id $pid_file; then
314 echo "Starting Ceph $name on $host...already running"
315 continue
316 fi
317
318 get_conf copy_executable_to "" "copy executable to"
319 if [ -n "$copy_executable_to" ]; then
320 scp $binary "$host:$copy_executable_to"
321 binary="$copy_executable_to"
322 fi
323 fi
324
325 # conf file
326 cmd="$cmd -c $conf"
327
328 if echo $name | grep -q ^osd; then
329 get_conf osd_data "/var/lib/ceph/osd/$cluster-$id" "osd data"
330 get_conf fs_path "$osd_data" "fs path" # mount point defaults so osd data
331 get_conf fs_devs "" "devs"
332 if [ -z "$fs_devs" ]; then
333 # try to fallback to old keys
334 get_conf tmp_btrfs_devs "" "btrfs devs"
335 if [ -n "$tmp_btrfs_devs" ]; then
336 fs_devs="$tmp_btrfs_devs"
337 fi
338 fi
339 first_dev=`echo $fs_devs | cut '-d ' -f 1`
340 fi
341
342 # do lockfile, if RH
343 get_conf lockfile "/var/lock/subsys/ceph" "lock file"
344 lockdir=`dirname $lockfile`
345 if [ ! -d "$lockdir" ]; then
346 lockfile=""
347 fi
348
349 get_conf asok "$run_dir/$cluster-$type.$id.asok" "admin socket"
350
351 case "$command" in
352 start|onestart)
353 # Increase max_open_files, if the configuration calls for it.
354 get_conf max_open_files "32768" "max open files"
355
356 # build final command
357 wrap=""
358 runmode=""
359 runarg=""
360
361 [ -z "$docrun" ] && get_conf_bool docrun "0" "restart on core dump"
362 [ "$docrun" -eq 1 ] && wrap="$BINDIR/ceph-run"
363
364 [ -z "$dovalgrind" ] && get_conf_bool valgrind "" "valgrind"
365 [ -n "$valgrind" ] && wrap="$wrap valgrind $valgrind"
366
367 [ -n "$wrap" ] && runmode="-f &" && runarg="-f"
368 [ -n "$max_open_files" ] && files="ulimit -n $max_open_files;"
369
370 [ -n "$TCMALLOC_MAX_TOTAL_THREAD_CACHE_BYTES" ] && tcmalloc="TCMALLOC_MAX_TOTAL_THREAD_CACHE_BYTES=$TCMALLOC_MAX_TOTAL_THREAD_CACHE_BYTES"
371
372 cmd="$files $tcmalloc $wrap $cmd --cluster $cluster ${SET_CEPHUSER_ARGS} $runmode"
373
374 if [ $dofsmount -eq 1 ] && [ -n "$fs_devs" ]; then
375 get_conf pre_mount "true" "pre mount command"
376 get_conf fs_type "" "osd mkfs type"
377
378 if [ -z "$fs_type" ]; then
379 # try to fallback to old keys
380 get_conf tmp_devs "" "btrfs devs"
381 if [ -n "$tmp_devs" ]; then
382 fs_type="btrfs"
383 else
384 echo No filesystem type defined!
385 exit 0
386 fi
387 fi
388
389 get_conf fs_opt "" "osd mount options $fs_type"
390 if [ -z "$fs_opt" ]; then
391 if [ "$fs_type" = "btrfs" ]; then
392 #try to fallback to old keys
393 get_conf fs_opt "" "btrfs options"
394 fi
395
396 if [ -z "$fs_opt" ]; then
397 if [ "$fs_type" = "xfs" ]; then
398 fs_opt="rw,noatime,inode64"
399 else
400 #fallback to use at least noatime
401 fs_opt="rw,noatime"
402 fi
403 fi
404 fi
405
406 [ -n "$fs_opt" ] && fs_opt="-o $fs_opt"
407 [ -n "$pre_mount" ] && do_cmd "$pre_mount"
408
409 do_root_cmd_okfail "mkdir -p $fs_path"
410 if [ "$fs_type" = "btrfs" ]; then
411 echo Mounting Btrfs on $host:$fs_path
412 do_root_cmd_okfail "modprobe btrfs ; btrfs device scan || btrfsctl -a ; egrep -q '^[^ ]+ $fs_path ' /proc/mounts && umount $fs_path ; mount -t btrfs $fs_opt $first_dev $fs_path"
413 else
414 echo Mounting $fs_type on $host:$fs_path
415 do_root_cmd_okfail "modprobe $fs_type ; egrep -q '^[^ ]+ $fs_path ' /proc/mounts && umount $fs_path ; mount -t $fs_type $fs_opt $first_dev $fs_path"
416 fi
417 if [ "$ERR" != "0" ]; then
418 EXIT_STATUS=$ERR
419 continue
420 fi
421 fi
422
7c673cae
FG
423 echo Starting Ceph $name on $host...
424 if [ ! -d $run_dir ]; then
425 # assume /var/run exists
426 install -d -m0770 -o ceph -g ceph /var/run/ceph
427 fi
428 get_conf pre_start_eval "" "pre start eval"
429 [ -n "$pre_start_eval" ] && $pre_start_eval
430 get_conf pre_start "" "pre start command"
431 get_conf post_start "" "post start command"
432 [ -n "$pre_start" ] && do_cmd "$pre_start"
433 do_cmd_okfail "$cmd" $runarg
434 if [ "$ERR" != "0" ]; then
435 EXIT_STATUS=$ERR
436 continue
437 fi
438
439 [ -n "$post_start" ] && do_cmd "$post_start"
440 [ -n "$lockfile" ] && [ "$?" -eq 0 ] && touch $lockfile
441 ;;
442
443 stop|onestop)
444 get_conf pre_stop "" "pre stop command"
445 get_conf post_stop "" "post stop command"
446 [ -n "$pre_stop" ] && do_cmd "$pre_stop"
447 stop_daemon $name ceph-$type $pid_file
448 [ -n "$pidfile" ] && rm -f $pidfile
449 [ -n "$asok" ] && rm -f $asok
450 [ -n "$post_stop" ] && do_cmd "$post_stop"
451 [ -n "$lockfile" ] && [ "$?" -eq 0 ] && rm -f $lockfile
452 if [ $dofsumount -eq 1 ] && [ -n "$fs_devs" ]; then
453 echo Unmounting OSD volume on $host:$fs_path
454 do_root_cmd "umount $fs_path || true"
455 fi
456 ;;
457
458 status)
459 if daemon_is_running $name ceph-$type $id $pid_file; then
460 printf "$name: running "
461 do_cmd "$BINDIR/ceph daemon $name version 2>/dev/null" || printf unknown
462 printf "\n"
463 elif [ -e "$pid_file" ]; then
464 # daemon is dead, but pid file still exists
465 echo "$name: dead."
466 EXIT_STATUS=1
467 else
468 # daemon is dead, and pid file is gone
469 echo "$name: not running."
470 EXIT_STATUS=3
471 fi
472 ;;
473
474 ssh)
475 $ssh
476 ;;
477
478 forcestop)
479 get_conf pre_forcestop "" "pre forcestop command"
480 get_conf post_forcestop "" "post forcestop command"
481 [ -n "$pre_forcestop" ] && do_cmd "$pre_forcestop"
482 stop_daemon $name ceph-$type $pid_file -9
483 [ -n "$post_forcestop" ] && do_cmd "$post_forcestop"
484 [ -n "$lockfile" ] && [ "$?" -eq 0 ] && rm -f $lockfile
485 ;;
486
487 killall)
488 echo "killall ceph-$type on $host"
489 do_cmd "pkill ^ceph-$type || true"
490 [ -n "$lockfile" ] && [ "$?" -eq 0 ] && rm -f $lockfile
491 ;;
492
493 force-reload | reload)
494 signal_daemon $name ceph-$type $pid_file -1 "Reloading"
495 ;;
496
497 restart|onerestart)
498 $0 $options stop $name
499 $0 $options start $name
500 ;;
501
502 condrestart)
503 if daemon_is_running $name ceph-$type $id $pid_file; then
504 $0 $options stop $name
505 $0 $options start $name
506 else
507 echo "$name: not running."
508 fi
509 ;;
510
511 cleanlogs)
512 echo removing logs
513 [ -n "$log_dir" ] && do_cmd "rm -f $log_dir/$type.$id.*"
514 ;;
515
516 cleanalllogs)
517 echo removing all logs
518 [ -n "$log_dir" ] && do_cmd "rm -f $log_dir/* || true"
519 ;;
520
521 *)
522 usage_exit
523 ;;
524 esac
525done
526
7c673cae 527exit $EXIT_STATUS