aboutsummaryrefslogtreecommitdiff
path: root/utils/hd-idle/files/hd-idle.init
blob: bdf9bb8d0ae29bade2ab8aa0e0532554513fa5c8 (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
#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2011 OpenWrt.org

START=50
STOP=10

USE_PROCD=1

validate_section_hdidle()
{
	uci_load_validate hd-idle hd-idle "$1" "$2" \
		'disk:string' \
		'enabled:bool' \
		'idle_time_interval:uinteger:10' \
		'idle_time_unit:string:minutes'
}

compute_seconds() {
	local interval="$1"
	local unit="$2"

	# compute interval in seconds
	case "$unit" in
		"days" )
			interval_seconds=$(($interval*60*60*24))
			;;
		"hours" )
			interval_seconds=$(($interval*60*60))
			;;
		"minutes" )
			interval_seconds=$(($interval*60))
			;;
		"seconds" )
			interval_seconds=$interval
			;;
		* )
			# default is minutes
			interval_seconds=$(($interval*60))
			;;
	esac

	echo $interval_seconds
}

hdidle_append() {
	[ "$2" = 0 ] || return

	[ "$enabled" -gt 0 ] || return

	if [ "$numdisks" = "0" ]; then
		procd_open_instance
		procd_set_param command /usr/bin/hd-idle
		procd_append_param command -d -i 0
	fi
	procd_append_param command -a $disk
	procd_append_param command -i "$(compute_seconds $idle_time_interval $idle_time_unit)"
	numdisks=$(( numdisks + 1 ))
}

start_service() {
	config_load "hd-idle"

	numdisks="0"
	config_foreach validate_section_hdidle "hd-idle" hdidle_append

	if [ "$numdisks" -gt 0 ]; then
		procd_set_param respawn
		procd_close_instance
	fi
}

service_triggers() {
	procd_add_reload_trigger "hd-idle"
	procd_add_validation validate_section_hdidle
}