summaryrefslogtreecommitdiff
path: root/scripts/functions
blob: 697ce89b7b959c0d788d934bd3fa8d768b3267ea (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
# -*- shell-script -*-

_log_msg()
{
	if [ "$quiet" = "y" ]; then return; fi
	printf "$@"
}

log_success_msg()
{
	_log_msg "Success: $@\n"
}

log_failure_msg()
{
	_log_msg "Failure: $@\n"
}

log_warning_msg()
{
	_log_msg "Warning: $@\n"
}

log_begin_msg()
{
	if [ -x /sbin/usplash_write ]; then
		/sbin/usplash_write "TEXT $@"
	fi
	_log_msg "Begin: $@ ... "
}

log_end_msg()
{
	if [ -x /sbin/usplash_write ]; then
		/sbin/usplash_write "SUCCESS ok"
	fi
	_log_msg "done.\n"
}

panic()
{
	if [ -x /sbin/usplash_write ]; then
		/sbin/usplash_write "QUIT"
	fi
	# Disallow console access
	if [ -n "${panic}" ]; then
		sleep ${panic}
		reboot
	fi
	modprobe i8042
	modprobe atkbd
	echo $@
	PS1='(initramfs) ' /bin/sh -i </dev/console >/dev/console 2>&1
}

maybe_break()
{
	if [ "${break:-}" = "$1" ]; then
		panic "Spawning shell within the initramfs"
	fi
}

render()
{
	eval "echo -n \${$@}"
}

set_initlist()
{
	unset initlist
	for si_x in ${initdir}/*; do
		# skip empty dirs without warning
		[ "${si_x}" = "${initdir}/*" ] && return

		# only allow variable name chars
		case ${si_x#${initdir}/} in
		*[![:alnum:]_]*)
			[ "${verbose}" = "y" ] \
			&& echo "$si_x ignored: not alphanumeric or '_' file"
			continue
			;;
		esac

		# skip non executable scripts
		if [ ! -x ${si_x} ]; then
			[ "${verbose}" = "y" ] \
			&& echo "$si_x ignored: not executable"
			continue
		fi

		# skip directories
		if [ -d ${si_x} ]; then
			[ "${verbose}" = "y" ] \
			&& echo "$si_x ignored: a directory"
			continue
		fi

		initlist="${initlist} ${si_x#${initdir}/}"
	done
}

reduce_satisfied()
{
	deplist="$(render array_${1})"
	unset tmpdeplist
	for rs_y in ${deplist}; do
		# only allow variable name chars
		case ${rs_y} in
		*[![:alnum:]_]*)
			continue
			;;
		esac
		# skip non executable scripts
		if [ ! -x ${initdir}/${rs_y} ]; then
			continue
		fi
		# skip directories
		if [ -d ${initdir}/${rs_y} ]; then
			continue
		fi
		tmpdeplist="${tmpdeplist} ${rs_y}"
	done
	deplist=${tmpdeplist}
	for rs_x in ${runlist}; do
		pop_list_item ${rs_x} ${deplist}
		deplist=${tmppop}
	done
	eval array_${1}=\"${deplist}\"
}

get_prereqs()
{
	set_initlist
	for gp_x in ${initlist}; do
		tmp=$(${initdir}/${gp_x} prereqs)
		eval array_${gp_x}=\"${tmp}\"
	done
}

count_unsatisfied()
{
	set -- ${@}
	return ${#}
}

# Removes $1 from initlist
pop_list_item()
{
	item=${1}
	shift
	set -- ${@}
	unset tmppop
	# Iterate
	for pop in ${@}; do
		if [ ${pop} = ${item} ]; then
			continue
		fi
		tmppop="${tmppop} ${pop}"
	done

}

# This function generates the runlist, so we clear it first.
reduce_prereqs()
{
	unset runlist
	set -- ${initlist}
	i=$#
	# Loop until there's no more in the queue to loop through
	while [ ${i} -ne 0 ]; do
		oldi=${i}
		for rp_x in ${initlist}; do
			reduce_satisfied ${rp_x}
			count_unsatisfied $(render array_${rp_x})
			cnt=${?}
			if [ ${cnt} -eq 0 ]; then
				runlist="${runlist} ${rp_x}"
				pop_list_item ${rp_x} ${initlist}
				initlist=${tmppop}
				i=$((${i} - 1))
			fi
		done
		if [ ${i} -eq ${oldi} ]; then
			panic "PANIC: Circular dependancy.  Exiting."
		fi
	done
}

call_scripts()
{
	for cs_x in ${runlist}; do
		# mkinitramfs verbose output
		if [ "${verbose}" = "y" ]; then
			echo "Calling hook ${cs_x}"
		fi
		${initdir}/${cs_x}
		# allow boot scripts to modify exported boot paramaters
		if [ -e /conf/param.conf ]; then
			. /conf/param.conf
		fi
	done
}

run_scripts()
{
	initdir=${1}
	[ ! -d ${initdir} ] && return
	get_prereqs
	reduce_prereqs
	call_scripts
}

# Load custom modules first
load_modules()
{
	if [ -e /conf/modules ]; then
		cat /conf/modules | while read m; do
			# Skip empty lines
			if [ -z "$m" ];  then
				continue
			fi
			# Skip comments - d?ash removes whitespace prefix
			com=$(printf "%.1s" "${m}")
			if [ "$com" = "#" ]; then
				continue
			fi
			modprobe $m
		done
	fi
}

# lilo compatibility
parse_numeric() {
	case $1 in
	"")
		return
		;;
	/*)
		return
		;;
	*:*)
		minor=${1#*:}
		major=${1%:*}
		;;
	[A-Fa-f0-9]*)
		value=$(( 0x${1} ))
		minor=$(( ${value} % 256 ))
		major=$(( ${value} / 256 ))
		;;
	*)
		return
		;;
	esac

	mknod -m 600 /dev/root b ${major} ${minor}
	ROOT=/dev/root
}

configure_networking()
{
	# networking already configured thus bail out
	[ -n "${DEVICE}" ] && [ -e /tmp/net-"${DEVICE}".conf ] && return 0

	# support ip options see linux sources
	# Documentation/filesystems/nfsroot.txt
	case ${IPOPTS} in
	none|off)
		# Do nothing
		;;
	""|on|any)
		# Bring up device
		ipconfig -t 60 ${DEVICE}
		;;
	dhcp|bootp|rarp|both)
		ipconfig -t 60 -c ${IPOPTS} -d ${DEVICE}
		;;
	*)
		ipconfig -t 60 -d $IPOPTS

		# grab device entry from ip option
		NEW_DEVICE=${IPOPTS#*:*:*:*:*:*}
		if [ "${NEW_DEVICE}" != "${IPOPTS}" ]; then
			NEW_DEVICE=${NEW_DEVICE%:*}
		else
			# wrong parse, possibly only a partial string
			NEW_DEVICE=
		fi
		if [ -n "${NEW_DEVICE}" ]; then
			DEVICE="${NEW_DEVICE}"
		fi
		;;
	esac

	# source ipconfig output
	if [ -n "${DEVICE}" ]; then
		# source specific bootdevice
		. /tmp/net-${DEVICE}.conf
	else
		# source any interface as not exaclty specified
		. /tmp/net-*.conf
	fi
}

# Wait for queued kernel/udev events
wait_for_udev()
{
	[ -x "$(command -v udevadm)" ] || return 0
	udevadm settle ${1:+--timeout=$1}
}