aboutsummaryrefslogtreecommitdiff
blob: 039681e7a6c025656a522acefdd01d195f71c873 (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
#!/sbin/openrc-run
# Copyright 1999-2015 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

depend() {
	before checkfs fsck
	after modules
}

start() {
	local mdadm_conf mdmod_conf mods mod dev timeout device node output # devs
	declare -i timeout

	mdadm_conf="/etc/mdadm/mdadm.conf"
	mdmod_conf="/etc/mdadm/mdmod.conf"
	[ -e /etc/mdadm.conf ] && [ ! -e "$mdadm_conf" ] && mdadm_conf="/etc/mdadm.conf"
	[ -e /etc/mdmod.conf ] && [ ! -e "$mdmod_conf" ] && mdmod_conf="/etc/mdmod.conf"

	if [ -x /sbin/mdadm ] && [ -f "${mdadm_conf}" ]; then
		mods="$( awk '/^\s*MODULE/ { print $2 }' "${mdadm_conf}" )"
		#devs="$( awk '/^\s*ARRAY/ { print $2 }' "${mdadm_conf}" )"

		if [ -n "${mods}" ]; then
			for mod in ${mods}; do
				if lsmod 2>&1 | grep -q "${mod//-/_}"; then
					ebegin "Loading RAID driver ${mod}"
					modprobe "${mod}"
					eend ${?} "Module load failed - storage array may be unavailable" || continue
				fi

				dev=$( grep "^\s*MODULE" "${mdmod_conf}" |
					tr -s "[:space:]" | sed 's/^ //' |
					cut -d" " -f 3-
				)

				#
				# We can't wait until all devices are present, as some of the
				# physical disks they represent may be unplugged...
				#
				timeout=50

				while [ ${timeout} -gt 0 ]; do
					for device in ${dev}; do
						[ -e "${device}" ] && break 2
					done
					[ ${timeout} -eq 50 ] && ewarn "Waiting for devices to become available ..."
					timeout=$(( timeout - 1 ))
					sleep 0.1 2>/dev/null || sleep 1
				done

				#
				# ... but we still need to give things time to settle
				#
				sleep 1
			done
		fi

		#
		# Before we start using the RAID array, we should really set the
		# IO scheduler to use on the constituent disks...
		#
		# This might better be done alongside hdparm, later in the boot
		# sequence.
		#
		local ios_name ios_module ios_queue
		case "${SCHEDULER}" in
			as|anticipatory)
				ios_name="Anticipatory"
				ios_module="as"
				ios_queue="anticipatory"
				;;
			bfq|budget)
				ios_name="Budget Fair"
				ios_module="bfq"
				ios_queue="bfq"
				;;
			cfq|fair)
				ios_name="Completely Fair"
				ios_module="cfq"
				ios_queue="cfq"
				;;
			deadline)
				ios_name="Deadline"
				ios_module="deadline"
				ios_queue="deadline"
				;;
			*)
				ios_name="Completely Fair"
				ios_module="cfq"
				ios_queue="cfq"
				;;
		esac

		ebegin "Loading ${ios_name} I/O scheduler"
		modprobe "${ios_module}-iosched"
		eend $?

		set -- discard $( grep "^\s*DEVICE\s*" "${mdadm_conf}" )
		shift
		while [ -n "${1}" ]; do
			device="${1}"
			shift

			[ "${device}" = "DEVICE" ] && continue

			for node in /sys/block/"${device}"/queue/scheduler; do
				if [ -e "${node}" ] && grep -q "${ios_queue}" "${node}"; then
					ebegin "Setting I/O scheduler for device '${device}'"
					printf "${ios_queue}" > "${node}"
					eend $? "Cannot install ${ios_queue} scheduler"
				fi
			done
		done

		if yesno "${ENABLENCQ}"; then
			#
			# Additionally, try to enble NCQ for increased performance.
			# The libATA documentation says this should work...
			#
			set -- discard $( grep "^\s*DEVICE\s*" "${mdadm_conf}" )
			shift
			while [ -n "${1}" ]; do
				device="${1}"
				shift

				[ "${device}" = "DEVICE" ] && continue

				for node in /sys/block/"${device}"/device/queue_depth; do
					if [ -w "${node}" ]; then
						# Maximum depth should be 32, but 0xffffffff
						# is the same value returned when a device is
						# hot-unplugged, so that particular value is
						# masked.
						ebegin "Setting maximum NCQ depth for device '${device}'"
						( printf "31" > "${node}" ) >/dev/null 2>&1
						eend $? "NCQ unavailable"
					fi
				done
			done
		fi
	fi

	ebegin "Starting up RAID devices"
	output="$( mdadm -As ${MDADM_ASSEMBLE_OPTS} 2>&1 )"
	eend $? "${output}"

	local pat="/dev/md_d*"
	set -- ${pat}
	if [ "$*" != "${pat}" ] ; then
		ebegin "Creating RAID device partitions"
		blockdev "$@"
		eend $?
		# wait because vgscan runs next, and we want udev to fire
		sleep 1
	fi

	return 0
}

stop() {
	local output

	# XXX: Maybe drop this check ?
	[ ! -e /etc/mdadm/mdadm.conf ] && [ ! -e /etc/mdadm.conf ] && return 0

	ebegin "Shutting down RAID devices"
	output="$( mdadm -Ss 2>&1 )"
	eend $? "${output}"
}