blob: c9a7aa869654838968e07fdb34cf5ca685124e4a (
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
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
|
#!/bin/bash
# mkinitcpio - modular tool for building an init ramfs cpio image
#
# IMPORTANT: We need to keep a common base syntax here because
# some of these hooks/scripts need to run under busybox's ash -
# therefore, the following constraints should be enforced:
# variables should be quoted and bracketed "${SOMEVAR}"
# inline execution should be done with $() instead of backticks
# use -z "${var}" to test for nulls/empty strings
# in case of embedded spaces, quote all path names and string comparisons
#
shopt -s extglob
# Settings
TMPDIR="$(mktemp -d /tmp/mkinitcpio.XXXXXX)"
BASEDIR=""
MESSAGEFILE="${TMPDIR}/message"
KERNELVERSION="$(uname -r)"
FUNCTIONS="functions"
CONFIG="mkinitcpio.conf"
HOOKDIR="hooks"
INSTDIR="install"
MODULE_FILE=""
SAVELIST=""
GENIMG=""
PRESET=""
MESSAGE=""
SKIPHOOKS=()
PRESETDIR="mkinitcpio.d"
QUIET="y"
SHOW_AUTOMODS="n"
COMPRESSION="gzip"
COMPRESSION_OPTIONS=""
if [ ! $UID ]; then
UID=$(id -u)
fi
# Add /{,usr}/sbin to path
# works around undetected problems like in #8448
PATH="${PATH}:/sbin:/usr/sbin"
# Sanitize environment further
# GREP_OPTIONS="--color=always" will break everything
unset GREP_OPTIONS
APPNAME=${0##*/}
usage ()
{
echo "${APPNAME}: usage"
echo " -c CONFIG Use CONFIG file. default: /etc/mkinitcpio.conf"
echo " -k KERNELVERSION Use KERNELVERSION. default: $(uname -r)"
echo " -s Save build directory. default: no"
echo " -b BASEDIR Use BASEDIR. default: /"
echo " -g IMAGE Generate a cpio image as IMAGE. default: no"
echo " -p PRESET Build specified preset."
echo " -m MESSAGE Print MESSAGE before passing control to init."
echo " -S SKIPHOOKS Skip SKIPHOOKS (comma-separated) when building the image."
echo " -v Verbose output. Default: no"
echo " -M Display modules found via autodetection."
echo " -L List all available hooks."
echo " -H HOOKNAME Output help for hook 'HOOKNAME'."
echo " -h This message."
cleanup
exit 1
}
cleanup ()
{
if [[ $SAVELIST ]]; then
echo ":: build directory saved in $TMPDIR"
else
rm -rf ${TMPDIR}
fi
}
sighandler() {
cleanup
exit 1
}
get_kernver() {
local kernel=$1
if [[ "${kernel:0:1}" != / ]]; then
echo $kernel
return 0
fi
[[ -r "$kernel" ]] || return 1
read _ kernver < <(file -b "$BASEDIR$kernel" | grep -o 'version [^ ]\+')
if [[ "$kernver" && -e "$BASEDIR/lib/modules/$kernver" ]]; then
echo "$kernver"
return 0
fi
return 1
}
trap sighandler TERM INT
while getopts ':c:k:sb:g:p:m:vH:LMhS:' arg; do
if [ "${OPTARG#-}" != "${OPTARG}" ]; then
echo "error: optional argument to '-${arg}' begins with a '-'"
echo " you probably don't want this....aborting."
usage
fi
case "${arg}" in
c) CONFIG="${OPTARG}" ;;
k) optkver=$OPTARG ;;
s) SAVELIST="y"; ;;
b) BASEDIR="${OPTARG}" ;;
g) GENIMG="${OPTARG}" ;;
p) PRESET="${OPTARG}" ;;
m) MESSAGE="${OPTARG}" ;;
v) QUIET="n" ;;
S) OLDIFS=${IFS}
IFS=,
SKIPHOOKS=(${OPTARG})
IFS=${OLDIFS}
unset OLDIFS
;;
H) . "${INSTDIR}/${OPTARG}";
echo "Help for hook '${OPTARG}':"
help
cleanup
exit 0 ;;
L) echo "Available hooks: "
for h in ${INSTDIR}/*; do
echo " ${h##*/}"
done
cleanup
exit 0 ;;
M) SHOW_AUTOMODS="y" ;;
h|?) usage ;;
:) echo "${OPTARG} requires a value..."; usage ;;
*) echo "invalid argument '${arg}'"; usage ;;
esac
done
shift $((${OPTIND} - 1))
if [[ $optkver ]]; then
if ! KERNELVERSION=$(get_kernver "$optkver"); then
echo "error: '$optkver' is an invalid kernel specifier"
cleanup
exit 1
fi
fi
# use preset $PRESET
if [[ $PRESET ]]; then
if [[ -f "$PRESETDIR/$PRESET.preset" ]]; then
# Use -b, -m and -v options specified earlier
declare -a preset_mkopts preset_cmd
[[ $BASEDIR ]] && preset_mkopts+=(-b "$BASEDIR")
[[ $MESSAGE ]] && preset_mkopts+=(-m "$MESSAGE")
[[ "$QUIET" = n ]] && preset_mkopts+=(-v)
# Build all images
. "$PRESETDIR/$PRESET.preset"
for p in "${PRESETS[@]}"; do
echo "==> Building image \"${p}\""
preset_cmd=("${preset_mkopts[@]}")
preset_kver=${p}_kver
if [[ ${!preset_kver:-$ALL_kver} ]]; then
preset_cmd+=(-k "${!preset_kver:-$ALL_kver}")
else
echo "==> No kernel version specified. Skipping image \"${p}\"."
continue
fi
preset_config=${p}_config
if [[ ${!preset_config:-$ALL_config} ]]; then
preset_cmd+=(-c "${!preset_config:-$ALL_config}")
else
echo "==> No configuration file specified. Skipping image \"${p}\"."
continue
fi
preset_image=${p}_image
if [[ ${!preset_image} ]]; then
preset_cmd+=(-g "${!preset_image}")
else
echo "==> No image file specified. Skipping image \"${p}\"."
continue
fi
preset_options=${p}_options
if [[ ${!preset_options} ]]; then
preset_cmd+=(${!preset_options}) # intentional word splitting
fi
echo "==> Running command: $0 ${preset_cmd[@]}"
if "$0" "${preset_cmd[@]}"; then
echo "==> SUCCESS"
else
echo "==> FAIL"
fi
done
cleanup
exit 0
else
echo "Preset $PRESET does not exist. Exiting."
cleanup
exit 1
fi
fi
# remove trailing / from BASEDIR
BASEDIR="${BASEDIR%/}"
MODULEDIR="${BASEDIR}/lib/modules/${KERNELVERSION}"
if [[ $GENIMG ]]; then
IMGPATH=$(readlink -f "$GENIMG")
if [[ -z $IMGPATH || ! -w ${IMGPATH%/*} ]]; then
echo "error: unable to write to path: '$GENIMG'"
cleanup
exit 1
fi
fi
if [ -n "${BASEDIR}" ]; then
if [ "${BASEDIR}" = "${BASEDIR#/}" ]; then
BASEDIR="$(pwd)/${BASEDIR}"
elif [ ! -d "${BASEDIR}" ]; then
echo "base directory '${BASEDIR}' does not exist or is not a directory"
cleanup
exit 1
fi
fi
if [ ! -f "${CONFIG}" ]; then
echo "config file '${CONFIG}' cannot be found, aborting..."
cleanup
exit 1
fi
. "${CONFIG}"
BASEDIR=${BASEDIR//+(\/)//}
MODULEDIR=${MODULEDIR//+(\/)//}
. "${FUNCTIONS}"
if [ "${SHOW_AUTOMODS}" = "y" ]; then
echo "Modules autodetected:"
. "${INSTDIR}/autodetect"
build
cat "${MODULE_FILE}"
cleanup
exit 0
fi
if [ -z "${GENIMG}" ]; then
echo ":: Begin dry run"
else
echo ":: Begin build"
fi
#parse 'global' hook, as defined in ${CONFIG}
parse_hook
for hook in ${HOOKS}; do
in_array "$hook" "${SKIPHOOKS[@]}" && continue
unset MODULES BINARIES FILES SCRIPT
build () { msg "${hook}: no build function..."; }
# Deprecation check
# A hook is considered deprecated if it is a symlink
# within $INSTDIR.
if [ -h "${INSTDIR}/${hook}" ]; then
newhook="$(readlink -ne "${INSTDIR}/${hook}")"
if [ -n "${newhook}" -a "${INSTDIR}/$(get_basename ${newhook})" -ef "${newhook}" ]; then
newhook="$(get_basename ${newhook})"
echo " -------------------------------------------------------------------"
echo " WARNING: Hook \"${hook}\" is deprecated."
echo " Replace it with \"${newhook}\" in your configuration file."
echo " -------------------------------------------------------------------"
hook="${newhook}"
fi
fi
if [[ -r "${INSTDIR}/${hook}" ]]; then
. "${INSTDIR}/${hook}"
echo ":: Parsing hook [${hook}]"
if [[ $(type -t install) = 'function' ]]; then
echo " -----------------------------------------------------------------------"
echo " WARNING: Hook '$hook' uses a deprecated 'install' function. This "
echo " should be switched to a 'build' function instead."
echo " -----------------------------------------------------------------------"
install
unset install
else
build
fi
parse_hook
else
die "Hook '${hook}' can not be found."
fi
done
if (( ${#ADDED_MODULES[*]} )); then
echo ":: Generating module dependencies"
/sbin/depmod -b "${TMPDIR}/root" "${KERNELVERSION}"
rm "$TMPDIR/root/lib/modules/$KERNELVERSION"/modules.!(dep.bin|alias.bin|symbols.bin)
fi
declare -i status=0
declare -a pipesave
if [ -n "${GENIMG}" ]; then
echo ":: Generating image '${GENIMG}'"
[ ${COMPRESSION} = "xz" ] && COMPRESSION_OPTIONS="${COMPRESSION_OPTIONS} --check=crc32"
pushd "$TMPDIR/root" >/dev/null
find . -print0 | bsdcpio -0oH newc | $COMPRESSION $COMPRESSION_OPTIONS > "$IMGPATH"
pipesave=("${PIPESTATUS[@]}") # save immediately
popd >/dev/null
if (( pipesave[0] )); then
errmsg="find reported an error"
elif (( pipesave[1] )); then
errmsg="bsdcpio reported an error"
elif (( pipesave[2] )); then
errmsg="$COMPRESSION reported an error"
fi
if [[ $errmsg ]]; then
echo ":: Image generation FAILED ($errmsg)"
status=1
else
echo ":: Image generation successful"
fi
else
echo ":: Dry run complete, use -g IMAGE to generate a real image"
fi
cleanup
exit ${status}
# vim: set ft=sh ts=4 sw=4 et:
|