forked from ngandrass/truenas-spindown-timer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspindown_timer.sh
executable file
·678 lines (606 loc) · 22.3 KB
/
spindown_timer.sh
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
#!/usr/bin/env bash
# ##################################################
# TrueNAS HDD Spindown Timer
# Monitors drive I/O and forces HDD spindown after a given idle period.
#
# Version: 2.3.0
#
# See: https://github.com/ngandrass/truenas-spindown-timer
#
#
# MIT License
#
# Copyright (c) 2023 Niels Gandraß <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ##################################################
VERSION=2.3.0
TIMEOUT=3600 # Default timeout before considering a drive as idle
POLL_TIME=600 # Default time to wait during a single iostat call
IGNORED_DRIVES="" # Default list of drives that are never spun down
MANUAL_MODE=0 # Default manual mode setting
ONESHOT_MODE=0 # Default for one shot mode setting
CHECK_MODE=0 # Default check mode setting
QUIET=0 # Default quiet mode setting
VERBOSE=0 # Default verbosity level
LOG_TO_SYSLOG=0 # Default for logging target (stdout/stderr or syslog)
DRYRUN=0 # Default for dryrun option
SHUTDOWN_TIMEOUT=0 # Default shutdown timeout (0 == no shutdown)
declare -A DRIVES # Associative array for detected drives
declare -A ZFSPOOLS # Array for monitored ZFS pools
declare -A DRIVES_BY_POOLS # Associative array mapping of pool names to list of disk identifiers (e.g. poolname => "ada0 ada1 ada2")
declare -A DRIVEID_TO_DEV # Associative array with the drive id (e.g. GPTID) to a device identifier
DRIVEID_TYPE= # Default for type used for drive IDs ('gptid' (CORE) or 'partuuid' (SCALE))
DISK_PARM_TOOL=camcontrol # Default disk parameter tool to use (camcontrol OR hdparm)
OPERATION_MODE=disk # Default operation mode (disk or zpool)
##
# Prints the help/usage message
##
function print_usage() {
cat << EOF
Usage:
$0 [-h] [-q] [-v] [-l] [-d] [-o] [-c] [-m] [-u <MODE>] [-t <TIMEOUT>] [-p <POLL_TIME>] [-i <DRIVE>] [-s <TIMEOUT>]
Monitors drive I/O and forces HDD spindown after a given idle period.
Resistant to S.M.A.R.T. reads.
Operation is supported on either drive level (MODE = disk) with plain device
identifiers or zpool level (MODE = zpool) with zfs pool names. See -u for more
information. A drive is considered idle and gets spun down if there has been no
I/O operations on it for at least TIMEOUT seconds. I/O requests are detected
within multiple intervals with a length of POLL_TIME seconds. Detected reads or
writes reset the drives timer back to TIMEOUT.
Options:
-t TIMEOUT : Total spindown delay. Number of seconds a drive has to
experience no I/O activity before it is spun down (default: 3600).
-p POLL_TIME : I/O poll interval. Number of seconds to wait for I/O during a
single monitoring period (default: 600).
-s TIMEOUT : Shutdown timeout. If given and no drive is active for TIMEOUT
seconds, the system will be shut down.
-u MODE : Operation mode (default: disk).
If set to 'disk', the script operates with disk identifiers
(e.g. ada0) for all CLI arguments and monitors I/O using
iostat directly.
If set to 'zpool' the script operates with ZFS pool names
(e.g. zfsdata) for all CLI arguments and monitors I/O using
the iostat of zpool.
-i DRIVE : In automatic drive detection mode (default):
Ignores the given drive or zfs pool.
In manual mode [-m]:
Only monitor the specified drives or zfs pools. Multiple
drives or zfs pools can be given by repeating the -i option.
-m : Manual drive detection mode. If set, automatic drive detection
is disabled.
CAUTION: This inverts the -i option, which can then be used to
manually supply drives or zfs pools to monitor. All other drives
or zfs pools will be ignored.
-o : One shot mode. If set, the script performs exactly one I/O poll
interval, then immediately spins down drives that were idle for
the last <POLL_TIME> seconds, and exits. This option ignores
<TIMEOUT>. It can be useful, if you want to invoke to script
via cron.
-c : Check mode. Outputs drive power state after each POLL_TIME
seconds.
-q : Quiet mode. Outputs are suppressed set.
-v : Verbose mode. Prints additional information during execution.
-l : Syslog logging. If set, all output is logged to syslog instead
of stdout/stderr.
-d : Dry run. No actual spindown is performed.
-h : Print this help message.
Example usage:
$0
$0 -q -t 3600 -p 600 -i ada0 -i ada1
$0 -q -m -i ada6 -i ada7 -i da0
$0 -u zpool -i freenas-boot
EOF
}
##
# Writes argument $1 to stdout/syslog if $QUIET is not set
#
# Arguments:
# $1 Message to write to stdout/syslog
##
function log() {
if [[ $QUIET -eq 0 ]]; then
if [[ $LOG_TO_SYSLOG -eq 1 ]]; then
echo "$1" | logger -i -t "spindown_timer"
else
echo "[$(date '+%F %T')] $1"
fi
fi
}
##
# Writes argument $1 to stdout/syslog if $VERBOSE is set and $QUIET is not set
#
# Arguments:
# $1 Message to write to stdout/syslog
##
function log_verbose() {
if [[ $VERBOSE -eq 1 ]]; then
log "$1"
fi
}
##
# Writes argument $1 to stderr/syslog. Ignores $QUIET.
#
# Arguments:
# $1 Message to write to stderr/syslog
##
function log_error() {
if [[ $LOG_TO_SYSLOG -eq 1 ]]; then
echo "[ERROR]: $1" | logger -i -t "spindown_timer"
else
>&2 echo "[$(date '+%F %T')] [ERROR]: $1"
fi
}
##
# Determines which tool to access disk parameters is available. This
# differentiates between TrueNAS Core and TrueNAS SCALE.
#
# Return: Command to use to access disk parameters
#
##
function detect_disk_parm_tool() {
local SUPPORTED_DISK_PARM_TOOLS
SUPPORTED_DISK_PARM_TOOLS="camcontrol hdparm"
for tool in ${SUPPORTED_DISK_PARM_TOOLS}; do
if [[ -n $(which ${tool}) ]]; then
echo "${tool}"
return
fi
done
log_error "No supported disk parameter tool found."
exit 1
}
##
# Detects which type of drive IDs are used.
# CORE uses glabel GPTIDs, SCALE uses partuuids
##
function detect_driveid_type() {
if [[ -n $(which glabel) ]]; then
DRIVEID_TYPE=gptid
elif [[ -d "/dev/disk/by-partuuid/" ]]; then
DRIVEID_TYPE=partuuid
else
log_error "Cannot detect drive id type. Exiting..."
exit 1
fi
log_verbose "Detected drive id type: $DRIVEID_TYPE"
}
##
# Populates the DRIVEID_TO_DEV associative array. Drive IDs are used as keys.
# Must be called after detect_driveid_type()
#
function populate_driveid_to_dev_array() {
# Create mapping
case $DRIVEID_TYPE in
"gptid")
# glabel present. Index by GPTID (CORE)
log_verbose "Creating disk to dev mapping using: glabel"
while read -r row; do
local gptid=$(echo "$row" | cut -d ' ' -f1)
local diskid=$(echo "$row" | cut -d ' ' -f3 | rev | cut -d 'p' -f2 | rev)
if [[ "$gptid" = "gptid"* ]]; then
DRIVEID_TO_DEV[$gptid]=$diskid
fi
done < <(glabel status | tail -n +2 | tr -s ' ')
;;
"partuuid")
# glabel absent. Try to detect by partuuid (SCALE)
log_verbose "Creating disk to dev mapping using: partuuid"
while read -r row; do
local partuuid=$(basename -- "${row}")
local dev=$(basename -- "$(readlink -f "${row}")" | sed "s/[0-9]\+$//")
DRIVEID_TO_DEV[$partuuid]=$dev
done < <(find /dev/disk/by-partuuid/ -type l)
;;
esac
# Verbose logging
if [ $VERBOSE -eq 1 ]; then
log_verbose "Detected disk identifier to dev mappings:"
for deviceid in "${!DRIVEID_TO_DEV[@]}"; do
log_verbose "-> [$deviceid]=${DRIVEID_TO_DEV[$deviceid]}"
done
fi
}
##
# Registers a new drive in $DRIVES array and detects if it is an ATA or SCSI
# drive.
#
# Arguemnts:
# $1 Device identifier (e.g. ada0)
##
function register_drive() {
local drive="$1"
if [ -z "$drive" ]; then
log_error "Failed to register drive. Empty name received."
return 1
fi
local DISK_IS_ATA
case $DISK_PARM_TOOL in
"camcontrol") DISK_IS_ATA=$(camcontrol identify $drive |& grep -E "^protocol(.*)ATA");;
"hdparm") DISK_IS_ATA=$(hdparm -I "/dev/$drive" |& grep -E "^ATA device");;
esac
if [[ -n $DISK_IS_ATA ]]; then
DRIVES[$drive]="ATA"
else
DRIVES[$drive]="SCSI"
fi
}
##
# Detects all connected drives using plain iostat method and whether they are
# ATA or SCSI drives. Drives listed in $IGNORE_DRIVES will be excluded.
#
# Note: This function populates the $DRIVES array directly.
##
function detect_drives_disk() {
local DRIVE_IDS
# Detect relevant drives identifiers
if [[ $MANUAL_MODE -eq 1 ]]; then
# In manual mode the ignored drives become the explicitly monitored drives
DRIVE_IDS=" ${IGNORED_DRIVES} "
else
DRIVE_IDS=`iostat -x | grep -E '^(ada|da|sd)' | awk '{printf $1 " "}'`
DRIVE_IDS=" ${DRIVE_IDS} " # Space padding must be kept for pattern matching
# Remove ignored drives
for drive in ${IGNORED_DRIVES[@]}; do
DRIVE_IDS=`sed "s/ ${drive} / /g" <<< ${DRIVE_IDS}`
done
fi
# Detect protocol type (ATA or SCSI) for each drive and populate $DRIVES array
for drive in ${DRIVE_IDS}; do
register_drive "$drive"
done
}
##
# Detects all connected drives using zpool list method and whether they are
# ATA or SCSI drives. Drives listed in $IGNORE_DRIVES will be excluded.
#
# Note: This function populates the $DRIVES array directly.
##
function detect_drives_zpool() {
local DRIVE_IDS
# Detect zfs pools
if [[ $MANUAL_MODE -eq 1 ]]; then
# Only use explicitly supplied pool names
for poolname in $IGNORED_DRIVES; do
ZFSPOOLS[${#ZFSPOOLS[@]}]="$poolname"
log_verbose "Using zfs pool: $poolname"
done
else
# Auto detect available pools
local poolnames=$(zpool list -H -o name)
# Remove ignored pools
for ignored_pool in $IGNORED_DRIVES; do
poolnames=${poolnames//$ignored_pool/}
log_verbose "Ignoring zfs pool: $ignored_pool"
done
# Store remaining detected pools
for poolname in $poolnames; do
ZFSPOOLS[${#ZFSPOOLS[@]}]="$poolname"
log_verbose "Detected zfs pool: $poolname"
done
fi
# Index disks in detected pools
for poolname in ${ZFSPOOLS[*]}; do
local disks
if ! disks=$(zpool list -H -v "$poolname"); then
log_error "Failed to get information for zfs pool: $poolname. Are you sure it exists?"
continue;
fi
log_verbose "Detecting disks in pool: $poolname"
while read -r driveid; do
# Remove invalid rows (Cannot be statically cut because of different pool geometries)
case $DRIVEID_TYPE in
"gptid")
# log_verbose "Received driveids: $driveid"
driveid=$(echo "$driveid" | grep -E "^gptid/.*$" | sed "s/^\(.*\)\.eli$/\1/") ;;
"partuuid")
# log_verbose "Received driveids: $driveid"
driveid=$(echo "$driveid" | grep -E "^(\w+\-){2,}") ;;
esac
# Skip if current row is invalid after filtering above
if [ -z "$driveid" ]; then
continue
fi
# Skip nvme drives
if [[ "${DRIVEID_TO_DEV[$driveid]}" == "nvme"* ]]; then
log_verbose "-> Skipping NVMe drive: $driveid"
continue
fi
log_verbose "-> Detected disk in pool $poolname: ${DRIVEID_TO_DEV[$driveid]} ($driveid)"
register_drive "${DRIVEID_TO_DEV[$driveid]}"
DRIVES_BY_POOLS[$poolname]="${DRIVES_BY_POOLS[$poolname]} ${DRIVEID_TO_DEV[$driveid]}"
done < <(echo "$disks" | tr -s "\\t" " " | cut -d ' ' -f2)
done
}
##
# Retrieves the list of identifiers (e.g. "ada0") for all monitored drives.
# Drives listed in $IGNORE_DRIVES will be excluded.
#
# Note: Must be run after detect_drives().
##
function get_drives() {
echo "${!DRIVES[@]}"
}
##
# Waits $1 seconds and returns a list of all drives that didn't
# experience I/O operations during that period.
#
# Devices listed in $IGNORED_DRIVES will never get returned.
#
# Arguments:
# $1 Seconds to listen for I/O before drives are considered idle
##
function get_idle_drives() {
# Wait for $1 seconds and get active drives
local IOSTAT_OUTPUT
local ACTIVE_DRIVES
case $OPERATION_MODE in
"disk")
# Operation mode: disk. Detect IO using iostat
IOSTAT_OUTPUT=$(iostat -x -z -d $1 2)
case $DISK_PARM_TOOL in
"camcontrol")
local CUT_OFFSET=$(grep -no "extended device statistics" <<< ${IOSTAT_OUTPUT} | tail -n1 | grep -Eo '^[^:]+') ;;
"hdparm")
local CUT_OFFSET=$(grep -no "Device" <<< ${IOSTAT_OUTPUT} | tail -n1 | grep -Eo '^[^:]+') ;;
esac
ACTIVE_DRIVES=$(tail -n +$((CUT_OFFSET+2)) <<< ${IOSTAT_OUTPUT} | awk '{printf $1}{printf " "}')
;;
"zpool")
# Operation mode: zpool. Detect IO using zpool iostat
IOSTAT_OUTPUT=$(zpool iostat -H ${ZFSPOOLS[*]} $1 2)
while read -r row; do
local poolname=$(echo "$row" | cut -d ' ' -f1)
local reads=$(echo "$row" | cut -d ' ' -f4)
local writes=$(echo "$row" | cut -d ' ' -f5)
if [ "$reads" != "0" ] || [ "$writes" != "0" ]; then
ACTIVE_DRIVES="$ACTIVE_DRIVES ${DRIVES_BY_POOLS[$poolname]}"
fi
done < <(tail -n +$((${#ZFSPOOLS[@]}+1)) <<< "${IOSTAT_OUTPUT}" | tr -s "\\t" " ")
;;
esac
# Remove active drives from list to get idle drives
local IDLE_DRIVES=" $(get_drives) " # Space padding must be kept for pattern matching
for drive in ${ACTIVE_DRIVES}; do
IDLE_DRIVES=`sed "s/ ${drive} / /g" <<< ${IDLE_DRIVES}`
done
echo ${IDLE_DRIVES}
}
##
# Checks if all not ignored drives are idle.
#
# returns 0 if all drives are idle, 1 if at least one drive is spinning
#
# Arguments:
# $1 list of idle drives as returned by get_idle_drives()
##
function all_drives_are_idle() {
local DRIVES=" $(get_drives) "
for drive in ${DRIVES}; do
if [[ ! $1 =~ $drive ]]; then
return 1
fi
done
return 0
}
##
# Determines whether the given drive $1 understands ATA commands
#
# Arguments:
# $1 Device identifier of the drive
##
function is_ata_drive() {
if [[ ${DRIVES[$1]} == "ATA" ]]; then echo 1; else echo 0; fi
}
##
# Determines whether the given drive $1 is spinning
#
# Arguments:
# $1 Device identifier of the drive
##
function drive_is_spinning() {
case $DISK_PARM_TOOL in
"camcontrol")
# camcontrol differentiates between ATA and SCSI drives
if [[ $(is_ata_drive $1) -eq 1 ]]; then
if [[ -z $(camcontrol epc $1 -c status -P | grep 'Standby') ]]; then echo 1; else echo 0; fi
else
# Reads STANDBY values from the power condition mode page (0x1a).
# THIS IS EXPERIMENTAL AND UNTESTED due to the lack of SCSI drives :(
#
# See: /usr/share/misc/scsi_modes and the "SCSI Commands Reference Manual"
if [[ -z $(camcontrol modepage $1 -m 0x1a |& grep -E "^STANDBY(.*)1") ]]; then echo 1; else echo 0; fi
fi
;;
"hdparm")
# It is currently unknown if hdparm also needs to differentiates between ATA and SCSI drives
if [[ -z $(hdparm -C "/dev/$1" | grep 'standby') ]]; then echo 1; else echo 0; fi
;;
esac
}
##
# Prints the power state of all monitored drives
##
function print_drive_power_states() {
local powerstates=""
for drive in $(get_drives); do
powerstates="${powerstates} [$drive] => $(drive_is_spinning "$drive")"
done
log "Drive power states: ${powerstates:1}"
}
##
# Forces the spindown of the drive specified by parameter $1 trough camcontrol
#
# Arguments:
# $1 Device identifier of the drive
##
function spindown_drive() {
if [[ $(drive_is_spinning $1) -eq 1 ]]; then
if [[ $DRYRUN -eq 0 ]]; then
case $DISK_PARM_TOOL in
"camcontrol")
if [[ $(is_ata_drive $1) -eq 1 ]]; then
# Spindown ATA drive
camcontrol standby $1
else
# Spindown SCSI drive
camcontrol stop $1
fi
;;
"hdparm")
hdparm -q -y "/dev/$1"
;;
esac
log "Spun down idle drive: $1"
else
log "Would spin down idle drive: $1. No spindown was performed (dry run)."
fi
else
log_verbose "Drive is already spun down: $1"
fi
}
##
# Generates a list of all active timeouts
##
function get_drive_timeouts() {
echo -n "Drive timeouts: "
for x in "${!DRIVE_TIMEOUTS[@]}"; do printf "[%s]=%s " "$x" "${DRIVE_TIMEOUTS[$x]}" ; done
echo ""
}
##
# Main program loop
##
function main() {
log_verbose "Running HDD Spindown Timer version $VERSION"
if [[ $DRYRUN -eq 1 ]]; then log "Performing a dry run..."; fi
# Setup one shot mode, if selected
if [[ $ONESHOT_MODE -eq 1 ]]; then
TIMEOUT=$POLL_TIME
log "Running in one shot mode... Notice: Timeout (-t) value will be ignored. Using poll time (-p) instead.";
fi
# Verify operation mode
if [ "$OPERATION_MODE" != "disk" ] && [ "$OPERATION_MODE" != "zpool" ]; then
log_error "Invalid operation mode: $OPERATION_MODE. Must be either 'disk' or 'zpool'."
exit 1
fi
log_verbose "Operation mode: $OPERATION_MODE"
# Determine disk parameter tool to use
# (Differentiates between TrueNaS Core and TrueNAs SCALE)
DISK_PARM_TOOL=$(detect_disk_parm_tool)
log_verbose "Using disk parameter tool: ${DISK_PARM_TOOL}"
# Initially identify drives to monitor
detect_driveid_type
populate_driveid_to_dev_array
detect_drives_$OPERATION_MODE
if [[ ${#DRIVES[@]} -eq 0 ]]; then
log_error "No drives to monitor detected. Exiting..."
exit 1
fi
for drive in ${!DRIVES[@]}; do
log_verbose "Detected drive ${drive} as ${DRIVES[$drive]} device"
done
log "Monitoring drives with a timeout of ${TIMEOUT} seconds: $(get_drives)"
log "I/O check sample period: ${POLL_TIME} sec"
if [ ${SHUTDOWN_TIMEOUT} -gt 0 ]; then
log "System will be shut down after ${SHUTDOWN_TIMEOUT} seconds of inactivity"
fi
# Init timeout counters for all monitored drives
declare -A DRIVE_TIMEOUTS
for drive in $(get_drives); do
DRIVE_TIMEOUTS[$drive]=${TIMEOUT}
done
log_verbose "$(get_drive_timeouts)"
# Init shutdown counter
SHUTDOWN_COUNTER=${SHUTDOWN_TIMEOUT}
# Drive I/O monitoring loop
while true; do
if [ $CHECK_MODE -eq 1 ]; then
print_drive_power_states
fi
local IDLE_DRIVES=$(get_idle_drives ${POLL_TIME})
# Update drive timeouts and spin down idle drives
for drive in "${!DRIVE_TIMEOUTS[@]}"; do
if [[ $IDLE_DRIVES =~ $drive ]]; then
DRIVE_TIMEOUTS[$drive]=$((DRIVE_TIMEOUTS[$drive] - POLL_TIME))
if [[ ! ${DRIVE_TIMEOUTS[$drive]} -gt 0 ]]; then
DRIVE_TIMEOUTS[$drive]=${TIMEOUT}
spindown_drive ${drive}
fi
else
DRIVE_TIMEOUTS[$drive]=${TIMEOUT}
fi
done
# Handle shutdown timeout
if [ ${SHUTDOWN_TIMEOUT} -gt 0 ]; then
if all_drives_are_idle "${IDLE_DRIVES}"; then
SHUTDOWN_COUNTER=$((SHUTDOWN_COUNTER - POLL_TIME))
if [[ ! ${SHUTDOWN_COUNTER} -gt 0 ]]; then
log_verbose "Shutting down system"
shutdown -p now
fi
else
SHUTDOWN_COUNTER=${SHUTDOWN_TIMEOUT}
fi
log_verbose "Shutdown timeout: ${SHUTDOWN_COUNTER}"
fi
# Handle one shot mode
if [[ $ONESHOT_MODE -eq 1 ]]; then
log_verbose "One shot mode: Exiting..."
exit 0
fi
# Log updated drive timeouts
log_verbose "$(get_drive_timeouts)"
done
}
# Parse arguments
while getopts ":hqvdlmoct:p:i:s:u:" opt; do
case ${opt} in
t ) TIMEOUT=${OPTARG}
;;
p ) POLL_TIME=${OPTARG}
;;
i ) IGNORED_DRIVES="$IGNORED_DRIVES ${OPTARG}"
;;
s ) SHUTDOWN_TIMEOUT=${OPTARG}
;;
o ) ONESHOT_MODE=1
;;
c ) CHECK_MODE=1
;;
q ) QUIET=1
;;
v ) VERBOSE=1
;;
l ) LOG_TO_SYSLOG=1
;;
d ) DRYRUN=1
;;
m ) MANUAL_MODE=1
;;
u ) OPERATION_MODE=${OPTARG}
;;
h ) print_usage; exit
;;
: ) print_usage; exit
;;
\? ) print_usage; exit
;;
esac
done
main # Start main program