blob: f6de5255355bbf0e76b4417ea87d6df2b205b939 (
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
|
#!/bin/sh
# 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
# the klibc shell or even 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
# incase of embedded spaces, quote all path names and string comarpisons
#
#TODO trap and remove FILELIST
# Settings
BASEDIR=""
FILELIST="$(mktemp /tmp/.tmpfilelist.XXXX)"
MESSAGEFILE="$(mktemp /tmp/.message.XXXX)"
KERNELVERSION="$(uname -r)"
FUNCTIONS="functions"
CONFIG="mkinitcpio.conf"
HOOKDIR="hooks"
INSTDIR="install"
MODULE_FILE=""
SAVELIST=""
GENIMG=""
APPEND=""
PRESET=""
MESSAGE=""
PRESETDIR="mkinitcpio.d"
QUIET="y"
SHOW_AUTOMODS="n"
# Add /{,usr}/sbin to path
# works around undetected problems like in #8448
PATH="${PATH}:/sbin:/usr/sbin"
APPNAME=$(basename "${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 NAME Save filelist. default: no"
echo " -b BASEDIR Use BASEDIR. default: /"
echo " -g IMAGE Generate a cpio image as IMAGE. default: no"
echo " -a NAME Append to an existing filelist. default: no"
echo " -p PRESET Build specified preset."
echo " -m MESSAGE Print MESSAGE before passing control to kinit."
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."
exit 1
}
while getopts ':c:k:s:b:g:a:p:m:vH:LMh' arg; do
if [ "${OPTARG:0:1}" = "-" ]; 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) KERNELVERSION="$OPTARG" ;;
s) SAVELIST="y"; FILELIST="$OPTARG" ;;
b) BASEDIR="$OPTARG" ;;
g) GENIMG="$OPTARG" ;;
a) APPEND="y"; SAVELIST="y"; FILELIST="$OPTARG" ;;
p) PRESET="$OPTARG" ;;
m) MESSAGE="$OPTARG" ;;
v) QUIET="n" ;;
H) source "${INSTDIR}/${OPTARG}";
echo "Help for hook '${OPTARG}':"
help
exit 0 ;;
L) echo "Available hooks: "
for h in ${INSTDIR}/*; do
echo " $(basename ${h})"
done
exit 0 ;;
M) SHOW_AUTOMODS="y" ;;
h|?) usage ;;
:) echo "${OPTARG} requires a value..."; usage ;;
*) echo "invalid argument '$arg'"; usage ;;
esac
done
shift $(($OPTIND - 1))
# use preset $PRESET
if [ -n "${PRESET}" ]; then
if [ -f "${PRESETDIR}/${PRESET}.preset" ]; then
# Use -b, -m and -v options specified earlier
PRESET_MKOPTS="${0}"
[ -n "${BASEDIR}" ] && PRESET_MKOPTS="${PRESET_MKOPTS} -b ${BASEDIR}"
[ -n "${MESSAGE}" ] && PRESET_MKOPTS="${PRESET_MKOPTS} -m \"${MESSAGE}\""
[ "${QUIET}" = "n" ] && PRESET_MKOPTS="${PRESET_MKOPTS} -v"
# Build all images
source ${PRESETDIR}/${PRESET}.preset
for p in ${PRESETS[@]}; do
echo "==> Building image \"${p}\""
PRESET_CMD="${PRESET_MKOPTS}"
eval "PRESET_KVER=\"\${${p}_kver}\""
eval "PRESET_CONFIG=\"\${${p}_config}\""
eval "PRESET_IMAGE=\"\${${p}_image}\""
if [ -n "${PRESET_KVER}" ]; then
PRESET_CMD="${PRESET_CMD} -k ${PRESET_KVER}"
elif [ -n "${ALL_kver}" ]; then
PRESET_CMD="${PRESET_CMD} -k ${ALL_kver}"
else
echo "==> No kernel version specified. Skipping image \"${p}\"."
continue
fi
if [ -n "${PRESET_CONFIG}" ]; then
PRESET_CMD="${PRESET_CMD} -c ${PRESET_CONFIG}"
else
echo "==> No configuration file specified. Skipping image \"${p}\"."
continue
fi
if [ -n "${PRESET_IMAGE}" ]; then
PRESET_CMD="${PRESET_CMD} -g ${PRESET_IMAGE}"
else
echo "==> No image file specified. Skipping image \"${p}\"."
continue
fi
echo "==> Running command: ${PRESET_CMD}"
if eval ${PRESET_CMD}; then
echo "==> SUCCESS"
else
echo "==> FAIL"
fi
done
exit 0
else
echo "Preset ${PRESET} does not exist. Exiting."
exit 1
fi
fi
# append a trailing / if needed
if [ "${BASEDIR:${#BASEDIR}}" == "/" ]; then
BASEDIR="${BASEDIR:0:${#BASEDIR}-1}"
fi
MODULEDIR="${BASEDIR}/lib/modules/${KERNELVERSION}"
if [ -n "${BASEDIR}" ]; then
if [ "${BASEDIR:0:1}" != "/" ]; then
BASEDIR="$(pwd)/${BASEDIR}"
elif [ ! -d "${BASEDIR}" ]; then
echo "base directory '${BASEDIR}' does not exist or is not a directory"
exit 1
fi
fi
if [ ! -f "${CONFIG}" ]; then
echo "config file '${CONFIG}' cannot be found, aborting..."
exit 1
fi
source "${CONFIG}"
if [ -f "${FILELIST}" -a -z "${APPEND}" ]; then
if [ -z "${SAVELIST}" ]; then
rm ${FILELIST}
touch "${FILELIST}"
else
echo "destination file list '${FILELIST}' exists - remove before running"
exit 1
fi
else
touch "${FILELIST}"
fi
BASEDIR=$(echo ${BASEDIR} | tr -s /)
MODULEDIR=$(echo ${MODULEDIR} | tr -s /)
source "${FUNCTIONS}"
if [ "${SHOW_AUTOMODS}" = "y" ]; then
echo "Modules autodetected:"
source "${INSTDIR}/autodetect"
install
cat "${MODULE_FILE}"
exit 0
fi
echo ":: Begin build"
#parse 'global' hook, as defined in ${CONFIG}
parse_hook
for hook in $HOOKS; do
unset MODULES
unset BINARIES
unset FILES
install () { msg "${hook}: no install function..."; }
# Deprecation check
# A hook is considered deprecated if it is a symlink
# within $INSTDIR.
if [ -L "${INSTDIR}/${hook}" ]; then
newhook="$(readlink -ne "${INSTDIR}/${hook}")"
if [ -n "${newhook}" -a "${INSTDIR}/$(basename ${newhook})" -ef "${newhook}" ]; then
newhook="$(basename ${newhook})"
echo " -------------------------------------------------------------------"
echo " WARNING: Hook \"${hook}\" is deprecated."
echo " Replace it with \"${newhook}\" in your configuration file."
echo " -------------------------------------------------------------------"
hook="${newhook}"
fi
fi
if grep "install" "${INSTDIR}/${hook}" >/dev/null 2>&1; then
source "${INSTDIR}/${hook}"
echo ":: Parsing hook [${hook}]"
install
parse_hook
else
die "Hook '${hook}' can not be found."
fi
done
if [ "${HAS_MODULES}" == "y" ]; then
echo ":: Generating module dependencies"
for mod in $(grep "file /lib/modules/${KERNELVERSION}" ${FILELIST} | cut -d' ' -f2); do
dir=$(dirname "${mod}")
mkdir -p "/tmp/${dir}"
cp "${BASEDIR}${mod}" "/tmp/${dir}/"
done
/sbin/depmod -b /tmp ${KERNELVERSION}
add_file "/tmp/lib/modules/${KERNELVERSION}/modules.dep" "/lib/modules/${KERNELVERSION}/modules.dep"
add_file "/tmp/lib/modules/${KERNELVERSION}/modules.alias" "/lib/modules/${KERNELVERSION}/modules.alias"
add_file "/tmp/lib/modules/${KERNELVERSION}/modules.symbols" "/lib/modules/${KERNELVERSION}/modules.symbols"
fi
status=0
if [ -n "$GENIMG" ]; then
echo -n ":: Generating image '${GENIMG}'..."
if ! /sbin/gen_init_cpio ${FILELIST} | gzip -9 > "${GENIMG}"; then
echo "FAILED"
status=1
else
echo "SUCCESS"
status=0
fi
if [ -z "${SAVELIST}" ]; then
rm ${FILELIST}
fi
rm ${MESSAGEFILE}
else
echo ":: Dry run complete, use -g IMAGE to generate a real image"
fi
#cleanup - we should probably trap this...
isempty () { [ $(ls -1 "${1}" | wc -l) -eq 0 ]; }
[ -e "${MODULE_FILE}" ] && rm "${MODULE_FILE}"
if [ -d /tmp/lib/modules/${KERNELVERSION} ]; then
rm -rf /tmp/lib/modules/${KERNELVERSION}
isempty /tmp/lib/modules && rm -rf /tmp/lib/modules
isempty /tmp/lib && rm -rf /tmp/lib
fi
exit $status
#vim:set ft=sh ts=4 sw=4 noet:
|