summaryrefslogtreecommitdiff
path: root/src/init-ceph.in
blob: 7344bf8f02b7e2c958d2029c3b5736b494f594fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/sh
# Start/stop ceph daemons
# chkconfig: 2345 60 80

### BEGIN INIT INFO
# Provides:          ceph
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Required-Start:    $remote_fs $named $network $time
# Required-Stop:     $remote_fs $named $network $time
# Short-Description: Start Ceph distributed file system daemons at boot time
# Description:       Enable Ceph distributed file system services.
### END INIT INFO

RUN_DIR=/var/run/ceph

# if we start up as ./mkcephfs, assume everything else is in the
# current directory too.
if [ `dirname $0` = "." ] && [ $PWD != "/etc/init.d" ]; then
    BINDIR=.
    LIBDIR=.
    ETCDIR=.
else
    BINDIR=@bindir@
    LIBDIR=@libdir@/ceph
    ETCDIR=@sysconfdir@/ceph
fi

usage_exit() {
    echo "usage: $0 [options] {start|stop|restart} [mon|osd|mds]..."
    printf "\t-c ceph.conf\n"
    printf "\t--valgrind\trun via valgrind\n"
    printf "\t--hostname [hostname]\toverride hostname lookup\n"
    exit
}

. $LIBDIR/ceph_common.sh

EXIT_STATUS=0

signal_daemon() {
    name=$1
    daemon=$2
    pidfile=$3
    signal=$4
    action=$5
    [ -z "$action" ] && action="Stopping"
    echo -n "$action Ceph $name on $host..."
    do_cmd "if [ -e $pidfile ]; then
        pid=`cat $pidfile`
        if [ -e /proc/\$pid ] && grep -q $daemon /proc/\$pid/cmdline ; then
	    cmd=\"kill $signal \$pid\"
	    echo -n \$cmd...
	    \$cmd
        fi
    fi"
    echo done
}

daemon_is_running() {
    name=$1
    daemon=$2
    daemon_id=$3
    pidfile=$4
    do_cmd "[ -e $pidfile ] || exit 1   # no pid, presumably not running
	pid=\`cat $pidfile\`
	[ -e /proc/\$pid ] && grep -q $daemon /proc/\$pid/cmdline && grep -qwe -i.$daemon_id /proc/\$pid/cmdline && exit 0 # running
        exit 1  # pid is something else" "" "okfail"
}

stop_daemon() {
    name=$1
    daemon=$2
    pidfile=$3
    signal=$4
    action=$5
    [ -z "$action" ] && action="Stopping"
    echo -n "$action Ceph $name on $host..."
    do_cmd "while [ 1 ]; do 
	[ -e $pidfile ] || break
	pid=\`cat $pidfile\`
	while [ -e /proc/\$pid ] && grep -q $daemon /proc/\$pid/cmdline ; do
	    cmd=\"kill $signal \$pid\"
	    echo -n \$cmd...
	    \$cmd
	    sleep 1
	    continue
	done
	break
    done"
    echo done
}

## command line options
options=

version=0
dovalgrind=
docrun=
allhosts=0
debug=0
monaddr=
dobtrfs=1
dobtrfsumount=0
verbose=0

while echo $1 | grep -q '^-'; do     # FIXME: why not '^-'?
case $1 in
    -v | --verbose)
	    verbose=1
	    ;;
    --valgrind)
	    dovalgrind=1
	    ;;
    --novalgrind)
	    dovalgrind=0
	    ;;
    --allhosts | -a)
	    allhosts=1;
	    ;;
    --restart)
	    docrun=1
	    ;;
    --norestart)
	    docrun=0
	    ;;
    -m )
	    [ -z "$2" ] && usage_exit
	    options="$options $1"
	    shift
	    MON_ADDR=$1
	    ;;
    --btrfs)
	    dobtrfs=1
	    ;;
    --nobtrfs)
	    dobtrfs=0
	    ;;
    --btrfsumount)
	    dobtrfsumount=1
	    ;;
    --conf | -c)
	    [ -z "$2" ] && usage_exit
	    options="$options $1"
	    shift
	    conf=$1
	    ;;
    --hostname )
	    [ -z "$2" ] && usage_exit
	    options="$options $1"
	    shift
	    hostname=$1
            ;;
    *)
	    echo unrecognized option \'$1\'
	    usage_exit
	    ;;
esac
options="$options $1"
shift
done

verify_conf

command=$1
[ -n "$*" ] && shift

get_name_list "$@"

for name in $what; do
    type=`echo $name | cut -c 1-3`   # e.g. 'mon', if $item is 'mon1'
    id=`echo $name | cut -c 4- | sed 's/^\\.//'`
    num=$id
    name="$type.$id"

    check_host || continue

    binary="$BINDIR/ceph-$type"
    cmd="$binary -i $id"

    get_conf pid_file "$RUN_DIR/$type.$id.pid" "pid file"
    if [ -n "$pid_file" ]; then
	do_cmd "mkdir -p "`dirname $pid_file`
	cmd="$cmd --pid-file $pid_file"
    fi

    get_conf log_dir "" "log dir"
    [ -n "$log_dir" ] && do_cmd "mkdir -p $log_dir"

    if [ "$command" = "start" ]; then
        get_conf auto_start "" "auto start"
        if [ "$auto_start" = "no" ] || [ "$auto_start" = "false" ] || [ "$auto_start" = "0" ]; then
            if [ -z "$@" ]; then
                echo "Skipping Ceph $name on $host... auto start is disabled"
                continue
            fi
        fi

	if daemon_is_running $name ceph-$type $id $pid_file; then
	    echo "Starting Ceph $name on $host...already running"
	    continue
	fi

	get_conf copy_executable_to "" "copy executable to"
	if [ -n "$copy_executable_to" ]; then
	    scp $binary "$host:$copy_executable_to"
	    binary="$copy_executable_to"
	fi
    fi

    # conf file
    if [ "$host" = "$hostname" ]; then
	cur_conf=$conf
    else
	if echo $pushed_to | grep -v -q " $host "; then
	    scp -q $conf $host:/tmp/ceph.conf.$$
	    pushed_to="$pushed_to $host "
	fi
	cur_conf="/tmp/ceph.conf.$$"
    fi
    cmd="$cmd -c $cur_conf"

    if echo $name | grep -q ^osd; then
	get_conf osd_data "" "osd data"
	get_conf btrfs_path "$osd_data" "btrfs path"  # mount point defaults so osd data
	get_conf btrfs_devs "" "btrfs devs"
	first_dev=`echo $btrfs_devs | cut '-d ' -f 1`
    fi

    # do lockfile, if RH
    get_conf lockfile "/var/lock/subsys/ceph" "lock file"
    lockdir=`dirname $lockfile`
    if [ ! -d "$lockdir" ]; then
	lockfile=""
    fi

    case "$command" in
	start)
            # Increase max_open_files, if the configuration calls for it.
            get_conf max_open_files "0" "max open files"
            if [ $max_open_files != "0" ]; then
                # Note: Don't try to do math with these numbers, because POSIX shells
                # can't do 64-bit math (natively). Just treat them as strings.
                cur=`ulimit -n`
                if [ "x$max_open_files" != "x$cur" ]; then
                    ulimit -n $max_open_files
                fi
            fi

            # build final command
	    wrap=""
	    runmode=""
	    runarg=""
	    
	    [ -z "$crun" ] && get_conf_bool crun "0" "restart on core dump"
	    [ "$crun" -eq 1 ] && wrap="$BINDIR/ceph-run"
	    
	    [ -z "$dovalgrind" ] && get_conf_bool valgrind "" "valgrind"
	    [ -n "$valgrind" ] && wrap="$wrap valgrind $valgrind"
	    
	    [ -n "$wrap" ] && runmode="-f &" && runarg="-f"

	    cmd="$wrap $cmd $runmode"
	    
	    if [ $dobtrfs -eq 1 ] && [ -n "$btrfs_devs" ]; then
		get_conf pre_mount "true" "pre mount command"
		get_conf btrfs_opt "noatime" "btrfs options"
		[ -n "$btrfs_opt" ] && btrfs_opt="-o $btrfs_opt"
		[ -n "$pre_mount" ] && do_cmd "$pre_mount"
		echo Mounting Btrfs on $host:$btrfs_path
		do_root_cmd "modprobe btrfs ; btrfs device scan || btrfsctl -a ; egrep -q '^[^ ]+ $btrfs_path' /proc/mounts || mount -t btrfs $btrfs_opt $first_dev $btrfs_path"
	    fi
	    echo Starting Ceph $name on $host...
	    mkdir -p $RUN_DIR
	    get_conf pre_start_eval "" "pre start eval"
	    [ -n "$pre_start_eval" ] && $pre_start_eval
	    get_conf pre_start "" "pre start command"
	    get_conf post_start "" "post start command"
	    [ -n "$pre_start" ] && do_cmd "$pre_start"
	    do_cmd "$cmd" $runarg
	    [ -n "$post_start" ] && do_cmd "$post_start"
	    [ -n "$lockfile" ] && [ "$?" -eq 0 ] && touch $lockfile
	    ;;
	
	stop)
	    get_conf pre_stop "" "pre stop command"
	    get_conf post_stop "" "post stop command"
	    [ -n "$pre_stop" ] && do_cmd "$pre_stop"
	    stop_daemon $name ceph-$type $pid_file
	    [ -n "$post_stop" ] && do_cmd "$post_stop"
	    [ -n "$lockfile" ] && [ "$?" -eq 0 ] && rm -f $lockfile
	    if [ $dobtrfsumount -eq 1 ] && [ -n "$btrfs_devs" ]; then
		echo Unmounting Btrfs on $host:$btrfs_path
		do_root_cmd "umount $btrfs_path || true"
	    fi
	    ;;

	status)
	    if daemon_is_running $name ceph-$type $id $pid_file; then
		get_conf asok "/var/run/ceph/ceph-$type.$id.asok" "admin socket"
		echo -n "$name: running "
		do_cmd "$BINDIR/ceph --admin-daemon $asok version 2>/dev/null" || echo unknown
            elif [ -e "$pid_file" ]; then
                # daemon is dead, but pid file still exists
                echo "$name: dead."
                EXIT_STATUS=1
            else
                # daemon is dead, and pid file is gone
                echo "$name: not running."
                EXIT_STATUS=3
            fi
	    ;;

	ssh)
	    $ssh
	    ;;

	forcestop)
	    get_conf pre_forcestop "" "pre forcestop command"
	    get_conf post_forcestop "" "post forcestop command"
	    [ -n "$pre_forcestop" ] && do_cmd "$pre_forcestop"
	    stop_daemon $name ceph-$type $pid_file -9
	    [ -n "$post_forcestop" ] && do_cmd "$post_forcestop"
	    [ -n "$lockfile" ] && [ "$?" -eq 0 ] && rm -f $lockfile
	    ;;
	    
	killall)
	    echo "killall ceph-$type on $host"
	    do_cmd "pkill ^ceph-$type || true"
	    [ -n "$lockfile" ] && [ "$?" -eq 0 ] && rm -f $lockfile
	    ;;
	
	force-reload | reload)
	    signal_daemon $name ceph-$type $pid_file -1 "Reloading"
	    ;;

	restart)
	    $0 $options stop $name
	    $0 $options start $name
	    ;;

	cleanlogs)
	    echo removing logs
	    [ -n "$log_dir" ] && do_cmd "rm -f $log_dir/$type.$id.*"
	    ;;

	cleanalllogs)
	    echo removing all logs
	    [ -n "$log_dir" ] && do_cmd "rm -f $log_dir/* || true"
	    ;;

	*)
	    usage_exit
	    ;;
    esac
done

exit $EXIT_STATUS