forked from allanliu/smartmontools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
smartd.initd.in
662 lines (603 loc) · 18.2 KB
/
smartd.initd.in
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#! /bin/sh
# smartmontools init file for smartd
# Copyright (C) 2002-8 Bruce Allen
# $Id$
# For RedHat and cousins:
# chkconfig: 2345 40 40
# description: Self Monitoring and Reporting Technology (SMART) Daemon
# processname: smartd
# For SuSE and cousins
### BEGIN INIT INFO
# Provides: smartd
# Required-Start: $syslog $remote_fs
# Should-Start: sendmail
# Required-Stop: $syslog $remote_fs
# Should-Stop: sendmail
# Default-Start: 2 3 5
# Default-Stop:
# Short-Description: Monitors disk and tape health via S.M.A.R.T.
# Description: Start S.M.A.R.T. disk and tape monitor.
### END INIT INFO
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any later
# version.
# You should have received a copy of the GNU General Public License (for
# example COPYING); if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# This code was originally developed as a Senior Thesis by Michael Cornwell
# at the Concurrent Systems Laboratory (now part of the Storage Systems
# Research Center), Jack Baskin School of Engineering, University of
# California, Santa Cruz. http://ssrc.soe.ucsc.edu/.
# Uncomment the line below to pass options to smartd on startup.
# Note that distribution specific configuration files like
# /etc/{default,sysconfig}/smartmontools might override these
#smartd_opts="--interval=1800"
SMARTD_BIN=/usr/local/sbin/smartd
report_unsupported () {
echo "Currently the smartmontools package has no init script for"
echo "the $1 OS/distribution. If you can provide one or this"
echo "one works after removing some ifdefs, please contact"
echo "[email protected]."
exit 1
}
# Red Hat or Yellow Dog or Mandrake
if [ -f /etc/redhat-release -o -f /etc/yellowdog-release -o -f /etc/mandrake-release -o -f /etc/whitebox-release -o -f /etc/trustix-release -o -f /etc/tinysofa-release ] ; then
# Source function library
. /etc/rc.d/init.d/functions
# Source configuration file. This should define the shell variable smartd_opts
[ -r /etc/sysconfig/smartmontools ] && . /etc/sysconfig/smartmontools
RETVAL=0
prog=smartd
pidfile=/var/lock/subsys/smartd
config=/etc/smartd.conf
start()
{
[ $UID -eq 0 ] || exit 4
[ -x $SMARTD_BIN ] || exit 5
[ -f $config ] || exit 6
echo -n $"Starting $prog: "
daemon $SMARTD_BIN $smartd_opts
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch $pidfile
return $RETVAL
}
stop()
{
[ $UID -eq 0 ] || exit 4
echo -n $"Shutting down $prog: "
killproc $SMARTD_BIN
RETVAL=$?
echo
rm -f $pidfile
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog daemon configuration: "
killproc $SMARTD_BIN -HUP
RETVAL=$?
echo
return $RETVAL
}
report()
{
echo -n $"Checking SMART devices now: "
killproc $SMARTD_BIN -USR1
RETVAL=$?
echo
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
report)
report
;;
restart)
stop
start
;;
condrestart|try-restart)
if [ -f $pidfile ]; then
stop
start
fi
;;
force-reload)
reload || (stop; start)
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|status|condrestart|try-restart|reload|force-reload|report}"
RETVAL=2
[ "$1" = 'usage' ] && RETVAL=0
esac
exit $RETVAL
# Slackware
elif [ -f /etc/slackware-version ] ; then
# Source configuration file. This should define the shell variable smartd_opts.
# Email smartmontools-support mailing list if there is a better choice
# of path for Slackware.
[ -r /etc/sysconfig/smartmontools ] && . /etc/sysconfig/smartmontools
RETVAL=0
case "$1" in
start)
echo -n "Starting smartd: "
$SMARTD_BIN $smartd_opts
RETVAL=$?
echo
;;
stop)
echo -n "Shutting down smartd: "
killall $SMARTD_BIN
RETVAL=$?
echo
;;
restart)
$0 stop
sleep 1
$0 start
RETVAL=$?
;;
try-restart)
if pidof $SMARTD_BIN >/dev/null; then
$0 restart
RETVAL=$?
fi
;;
force-reload)
$0 reload || $0 restart
RETVAL=$?
;;
reload)
echo -n "Reloading smartd configuration: "
killall -s HUP $SMARTD_BIN
RETVAL=$?
echo
;;
report)
echo -n "Checking SMART devices now: "
killall -s USR1 $SMARTD_BIN
RETVAL=$?
echo
;;
status)
if pidof $SMARTD_BIN >/dev/null; then
echo "$SMARTD_BIN is running."
else
echo "$SMARTD_BIN is not running."
RETVAL=1
fi
;;
*)
echo "Usage: $0 {start|stop|restart|try-restart|force-reload|reload|report|status}"
RETVAL=1
esac
exit $RETVAL
# SuSE
elif [ -f /etc/SuSE-release ] ; then
test -x $SMARTD_BIN || exit 5
# Existence of config file is optional
SMARTD_CONFIG=/etc/smartd.conf
# source configuration file.
[ -r /etc/sysconfig/smartmontools ] && . /etc/sysconfig/smartmontools
smartd_opts=
if test -n "$SMARTD_CHECK_INTERVAL" -a "$SMARTD_CHECK_INTERVAL" != 1800 ; then
smartd_opts=" -i $SMARTD_CHECK_INTERVAL"
fi
if test -n "$SMARTD_LOG_FACILITY" -a "$SMARTD_LOG_FACILITY" != "daemon" ; then
smartd_opts="$smartd_opts -l $SMARTD_LOG_FACILITY"
fi
if test -n "$SMARTD_DRIVEDB" ; then
smartd_opts="$smartd_opts -B $SMARTD_DRIVEDB"
fi
if test "$SMARTD_SAVESTATES" = "no" ; then
smartd_opts="$smartd_opts -s \"\""
fi
if test "$SMARTD_ATTRLOG" = "no" ; then
smartd_opts="$smartd_opts -A \"\""
fi
if test -n "$SMARTD_EXTRA_OPTS" ; then
smartd_opts="$smartd_opts $SMARTD_EXTRA_OPTS"
fi
# Shell functions sourced from /etc/rc.status:
# rc_check check and set local and overall rc status
# rc_status check and set local and overall rc status
# rc_status -v be verbose in local rc status and clear it afterwards
# rc_status -v -r ditto and clear both the local and overall rc status
# rc_status -s display "skipped" and exit with status 3
# rc_status -u display "unused" and exit with status 3
# rc_failed set local and overall rc status to failed
# rc_failed <num> set local and overall rc status to <num>
# rc_reset clear both the local and overall rc status
# rc_exit exit appropriate to overall rc status
# rc_active checks whether a service is activated by symlinks
. /etc/rc.status
# Reset status of this service
rc_reset
# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - user had insufficient privileges
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
# 8--199 - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.
case "$1" in
start)
echo -n "Starting smartd "
## Start daemon with startproc(8). If this fails
## the return value is set appropriately by startproc.
# We don't use startproc - we need to check for return code 17.
if ! /sbin/checkproc $SMARTD_BIN ; then
eval $SMARTD_BIN$smartd_opts
# Remember status and be verbose
if test $? -ne 17 ; then
rc_status -v
else
rc_status -u
fi
else
rc_reset
rc_status -v
fi
;;
stop)
echo -n "Shutting down smartd "
/sbin/killproc -TERM $SMARTD_BIN
# Remember status and be verbose
rc_status -v
;;
try-restart)
## Do a restart only if the service was active before.
## Note: try-restart is now part of LSB (as of 1.9).
$0 status
if test $? = 0; then
$0 restart
else
rc_reset # Not running is not a failure.
fi
# Remember status and be quiet
rc_status
;;
restart)
$0 stop
$0 start
# Remember status and be quiet
rc_status
;;
force-reload|reload)
echo -n "Reload service smartd "
/sbin/killproc -HUP $SMARTD_BIN
rc_status -v
;;
report)
## Checking SMART devices now (smartd specific function)
echo -n "Checking SMART devices now "
/sbin/killproc -USR1 $SMARTD_BIN
rc_status -v
;;
status)
echo -n "Checking for service smartd "
## Check status with checkproc(8), if process is running
## checkproc will return with exit status 0.
# Return value is slightly different for the status command:
# 0 - service up and running
# 1 - service dead, but /var/run/ pid file exists
# 2 - service dead, but /var/lock/ lock file exists
# 3 - service not running (unused)
# 4 - service status unknown :-(
# 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
# NOTE: checkproc returns LSB compliant status values.
/sbin/checkproc $SMARTD_BIN
rc_status -v
;;
probe)
## Optional: Probe for the necessity of a reload, print out the
## argument to this init script which is required for a reload.
## Note: probe is not (yet) part of LSB (as of 1.9)
test $SMARTD_CONFIG -nt /var/run/smartd.pid && echo reload
;;
*)
echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|report|probe}"
exit 1
esac
rc_exit
# Debian case
elif [ -f /etc/debian_version ] ; then
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
SMARTDPID=/var/run/smartd.pid
[ -x $SMARTD_BIN ] || exit 0
RET=0
# source configuration file
[ -r /etc/default/rcS ] && . /etc/default/rcS
[ -r /etc/default/smartmontools ] && . /etc/default/smartmontools
smartd_opts="--pidfile $SMARTDPID $smartd_opts"
case "$1" in
start)
echo -n "Starting S.M.A.R.T. daemon: smartd"
if start-stop-daemon --start --quiet --pidfile $SMARTDPID \
--exec $SMARTD_BIN -- $smartd_opts; then
echo "."
else
echo " (failed)"
RET=1
fi
;;
stop)
echo -n "Stopping S.M.A.R.T. daemon: smartd"
start-stop-daemon --stop --quiet --oknodo --pidfile $SMARTDPID
echo "."
;;
restart)
$0 stop
$0 start
;;
force-reload)
$0 reload || $0 restart
;;
reload)
echo -n "Reload S.M.A.R.T. daemon: smartd"
if start-stop-daemon --stop --quiet --signal 1 \
--pidfile $SMARTDPID; then
echo "."
else
echo " (failed)"
RET=1
fi
;;
report)
echo -n "Checking SMART devices now"
if start-stop-daemon --stop --quiet --signal 10 \
--pidfile $SMARTDPID; then
echo "."
else
echo " (failed)"
RET=1
fi
;;
status)
if pidof $SMARTD_BIN >/dev/null; then
echo "$SMARTD_BIN is running."
else
echo "$SMARTD_BIN is not running."
RET=1
fi
;;
*)
echo "Usage: $0 {start|stop|restart|force-reload|reload|report|status}"
exit 1
esac
exit $RET
elif [ -f /etc/gentoo-release ] ; then
report_unsupported "Gentoo"
elif [ -f /etc/turbolinux-release ] ; then
report_unsupported "Turbolinux"
elif [ -f /etc/environment.corel ] ; then
report_unsupported "Corel"
# PLEASE ADD OTHER LINUX DISTRIBUTIONS JUST BEFORE THIS LINE, USING elif
elif uname -a | grep FreeBSD > /dev/null 2>&1 ; then
# following is replaced by port install
PREFIX=@@PREFIX@@
# Updated to try both the RCNG version of things from 5.x, or fallback to
# oldfashioned rc.conf
if [ -r /etc/rc.subr ]; then
# This is RC-NG, pick up our values
. /etc/rc.subr
name="smartd"
rcvar="smartd_enable"
command="$SMARTD_BIN"
load_rc_config $name
elif [ -r /etc/defaults/rc.conf ]; then
# Not a 5.x system, try the default location for variables
. /etc/defaults/rc.conf
source_rc_confs
elif [ -r /etc/rc.conf ]; then
# Worst case, fallback to system config file
. /etc/rc.conf
fi
if [ -r /etc/rc.subr ]; then
# Use new functionality from RC-NG
run_rc_command "$1"
else
PID_FILE=/var/run/smartd.pid
case "$1" in
start)
$SMARTD_BIN -p $PID_FILE $smartd_flags
echo -n " smartd"
;;
stop)
kill `cat $PID_FILE`
echo -n " smartd"
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
kill -s HUP `cat $PID_FILE`
;;
report)
kill -s USR1 `cat $PID_FILE`
;;
*)
echo "Usage: $0 {start|stop|restart|reload|report}"
exit 1
esac
exit 0
fi
elif uname -a | grep SunOS > /dev/null 2>&1 ; then
# Source configuration file. This should define the shell variable smartd_opts.
# Email smartmontools-support mailing list if there is a better choice
# of path for Solaris
[ -r /etc/default/smartmontools ] && . /etc/default/smartmontools
PID_FILE=/var/run/smartd.pid
case "$1" in
start)
$SMARTD_BIN -p $PID_FILE $smartd_opts
echo -n "smartd "
;;
stop)
[ -f $PID_FILE ] && kill `cat $PID_FILE`
echo -n "smartd "
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
kill -s HUP `cat $PID_FILE`
;;
report)
kill -s USR1 `cat $PID_FILE`
;;
*)
echo "Usage: $0 {start|stop|restart|reload|report}"
exit 1
esac
exit 0
# Cygwin
elif uname | grep -i CYGWIN > /dev/null 2>&1 ; then
# The following settings may be changed by the configuration file below
# Service Name (must be unique)
smartd_svcname=smartd
# Service display name
smartd_svcdisp="CYGWIN smartd"
# Service description
smartd_svcdesc="\
Controls and monitors storage devices using the Self-Monitoring \
Analysis and Reporting Technology System (S.M.A.R.T.) \
built into ATA and SCSI Hard Drives. \
http://www.smartmontools.org/"
# Source configuration file. This should define the shell variable smartd_opts.
# Email smartmontools-support mailing list if there is a better choice
# of path for Cygwin
[ -r /etc/sysconfig/smartmontools ] && . /etc/sysconfig/smartmontools
PID_FILE=/var/run/smartd.pid
RETVAL=0
# Note: "[ -r $PID_FILE ]" is not used here. On Cygwin, this command may
# return success even if the file is present but cannot be read by current user.
# If smartd is running as service, smartd.pid is owned by local system account
# which is different from any user ever executing this script.
case "$1" in
start)
if cygrunsrv -L 2>/dev/null | grep "^${smartd_svcname}$" >/dev/null 2>&1; then
echo -n "Starting service $smartd_svcname: "
cygrunsrv -S "$smartd_svcname"
else
echo -n "Starting smartd as daemon: "
$SMARTD_BIN -p $PID_FILE $smartd_opts
fi
RETVAL=$?
;;
stop)
echo -n "Shutting down smartd: "
pid="`cat $PID_FILE 2>/dev/null`" && kill "$pid"
RETVAL=$?
;;
reload)
echo -n "Reloading smartd configuration: "
pid="`cat $PID_FILE 2>/dev/null`" && kill -HUP "$pid"
RETVAL=$?
;;
report)
echo -n "Checking SMART devices now: "
pid="`cat $PID_FILE 2>/dev/null`" && kill -USR1 "$pid"
RETVAL=$?
;;
restart)
$0 stop
sleep 1
$0 start
exit $?
;;
install)
shift
[ $# -eq 0 ] || smartd_opts="$*"
dep=; dep2=
if cygrunsrv -L 2>/dev/null | grep "^syslogd$" >/dev/null 2>&1; then
dep="syslogd"
fi
if cygrunsrv -L 2>/dev/null | grep "^syslog-ng" >/dev/null 2>&1; then
dep2="syslog-ng"
fi
if [ -z "$dep" ]; then
if [ -z "$dep2" ]; then
echo "Warning: no syslog service installed, smartd will write to windows event log.";
else
dep="$dep2"
fi
else
if [ -z "$dep2" ]; then
:
else
dep=
echo "Warning: both syslogd and syslog-ng installed, dependency not set."
fi
fi
echo "Installing service ${smartd_svcname}${dep:+ (depending on '$dep')}${smartd_opts:+ with options '$smartd_opts'}:"
cygrunsrv -I "$smartd_svcname" -d "$smartd_svcdisp" -f "$smartd_svcdesc" ${dep:+-y} $dep \
-e CYGWIN="$CYGWIN" -p $SMARTD_BIN -a "-n -p ${PID_FILE}${smartd_opts:+ }$smartd_opts"
RETVAL=$?
;;
remove)
echo "Removing service $smartd_svcname:"
cygrunsrv -R "$smartd_svcname"
RETVAL=$?
;;
status)
echo -n "Checking smartd status: "
if cygrunsrv -L 2>/dev/null | grep "^${smartd_svcname}$" >/dev/null 2>&1; then
if cygrunsrv -Q "$smartd_svcname" 2>/dev/null | grep "State *: Running" >/dev/null 2>&1; then
echo "running as service '$smartd_svcname'."
elif ps -e 2>/dev/null | grep " ${SMARTD_BIN}$" >/dev/null 2>&1; then
echo "installed as service '$smartd_svcname' but running as daemon."
else
echo "installed as service '$smartd_svcname' but not running."
RETVAL=1
fi
elif ps -e 2>/dev/null | grep " ${SMARTD_BIN}$" >/dev/null 2>&1; then
echo "running as daemon."
else
echo "not running."
RETVAL=1
fi
exit $RETVAL
;;
*)
echo "Usage: $0 {start|stop|restart|reload|report|status}"
echo " $0 {install [options]|remove}"
exit 1
esac
if [ "$RETVAL" -eq 0 ]; then echo "done"; else echo "ERROR"; fi
exit $RETVAL
# Add other OSes HERE, using elif...
else
report_unsupported "Unknown"
fi
# One should NEVER arrive here, except for a badly written case above,
# that fails to exit.
echo "SOMETHING IS WRONG WITH THE SMARTD STARTUP SCRIPT"
echo "PLEASE CONTACT [email protected]"
exit 1