blob: 8ce24ea6c620b02a164f74f244264f6f03c2e3a0 (
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
342
343
344
345
|
#!/bin/bash
plain() {
local mesg=$1; shift
printf "$BOLD $mesg$NC\n" "$@" >&1
}
msg() {
local mesg=$1; shift
printf "$GREEN==>$NC$BOLD $mesg$NC\n" "$@" >&1
}
msg2() {
local mesg=$1; shift
printf "$BLUE ->$NC$BOLD $mesg$NC\n" "$@" >&1
}
warning() {
local mesg=$1; shift
printf "$YELLOW==> WARNING:$NC$BOLD $mesg$NC\n" "$@" >&2
}
error() {
local mesg=$1; shift
printf "$RED==> ERROR:$NC$BOLD $mesg$NC\n" "$@" >&2
}
die() {
error "$@"
cleanup 1
}
get_basename() {
local base=${1%/}
base=${base##*/}
printf '%s' "${base:-/}"
}
get_dirname() {
local dir=${1%/}
dir=${dir%/*}
printf '%s' "${dir:-/}"
}
in_array() {
# Search for an element in an array.
# $1: needle
# ${@:2}: haystack
local item= needle=$1; shift
for item in "$@"; do
[[ $item = $needle ]] && return 0 # Found
done
return 1 # Not Found
}
pathlookup() {
# a basedir aware 'type -P' (or which) for executables
# $1: binary to find
local path=
local -a paths=
IFS=: read -r -a paths <<< "$PATH"
for path in "${paths[@]}"; do
[[ ${path:0:1} = [.~] ]] && continue
if [[ -x $BASEDIR$path/$1 ]]; then
printf '%s' "$BASEDIR$path/$1"
return 0
fi
done
return 1
}
_add_file() {
# add a file to $BUILDROOT
# $1: pathname on initcpio
# $2: source on disk
# $3: mode
(( $# == 3 )) || return $EINVAL
[[ -e "$BUILDROOT$1" ]] && return $EEXIST
(( QUIET )) || plain "adding file: %s" "$1"
command install -Dm$3 "$2" "$BUILDROOT$1"
}
_add_dir() {
# add a directory (with parents) to $BUILDROOT
# $1: pathname on initcpio
# $2: mode
(( $# == 2 )) || [[ "$1" == /?* ]] || return 1 # bad args
[[ -e "$BUILDROOT$1" ]] && return 0 # file exists
(( QUIET )) || plain "adding dir: %s" "$1"
command install -dm$2 "$BUILDROOT$1"
}
_add_symlink() {
# add a symlink to $buildroot
# $1: name on initcpio
# $2: target of $1
(( $# == 2 )) || return $EINVAL
[[ -L "$BUILDROOT$1" ]] && return $EEXIST
(( QUIET )) || plain "adding symlink: %s -> %s" "$2" "$1"
ln -s "$2" "$BUILDROOT$1"
}
auto_modules() {
# Perform auto detection of modules via sysfs.
IFS=$'\n' read -rd '' -a mods < \
<(find /sys/devices -name modalias -exec sort -u {} + |
xargs modprobe -d "$BASEDIR" -aRS "$KERNELVERSION" |
sort -u)
printf "%s\n" "${mods[@]//-/_}"
(( ${#mods[*]} ))
}
all_modules() {
# Add modules to the initcpio, filtered by grep.
# $@: filter arguments to grep
local -i count=0
while read -r -d '' mod; do
(( ++count ))
mod=${mod##*/}
mod="${mod%.ko*}"
printf '%s\n' "${mod//-/_}"
done < <(find "$MODULEDIR" -name '*.ko*' -print0 2>/dev/null | grep -Zz "$@")
(( count ))
}
checked_modules() {
# Add modules to the initcpio, filtered by the list of autodetected
# modules.
# $@: arguments to all_modules
if [[ -s "$MODULE_FILE" ]]; then
grep -xFf "$MODULE_FILE" <(all_modules "$@")
return 1
else
all_modules "$@"
fi
}
add_module() {
# Add a kernel module to the initcpio image. Dependencies will be
# discovered and added.
# $1: module name
local module path dep deps field value
local -i ign_errors=0
if [[ $1 = -@(t|-try) ]]; then
ign_errors=1
shift
fi
module=${1%.ko*}
# skip expensive stuff if this module has already been added
in_array "${module//-/_}" "${ADDED_MODULES[@]}" && return
while IFS=':= ' read -r -d '' field value; do
case "$field" in
filename)
path=$value
;;
depends)
IFS=',' read -r -a deps <<< "$value"
for dep in "${deps[@]}"; do
add_module "$dep"
done
;;
firmware)
if [[ -e "$BASEDIR/lib/firmware/$value" ]]; then
_add_file "/lib/firmware/$value" "$BASEDIR/lib/firmware/$value" 644
fi
;;
esac
done < <(modinfo -b "$BASEDIR" -k "$KERNELVERSION" -0 "$module" 2>/dev/null)
if [[ -z $path ]]; then
(( ign_errors )) && return 0
error "module not found: \`%s'" "$module"
return 1
fi
# aggregate modules and add them all at once to save some forks
(( QUIET )) || plain "adding module: %s" "$1"
MODPATHS+=("$path")
ADDED_MODULES+=("${module//-/_}")
# explicit module depends
case "$module" in
fat) add_module --try "nls_cp437" ;;
ocfs2) add_module --try "configfs" ;;
libcrc32c) add_module --try "crc32c"; add_module --try "crc32c_intel" ;;
esac
}
add_full_dir() {
# Add a directory and all its contents, recursively, to the initcpio image.
# No parsing is performed and the contents of the directory is added as is.
# $1: path to directory
if [[ -n $1 && -d $1 ]]; then
for f in "$1"/*; do
if [[ -d "$f" ]]; then
add_full_dir "$f"
else
add_file "$f"
fi
done
fi
}
add_dir() {
# Add a directory to the initcpio image.
# $1: absolute path of directory on image
(( ! $# )) && return 1
local path=$1 mode=${2:-755}
_add_dir "$path" "$mode"
}
add_symlink() {
# Add a symlink to the initcpio image.
# $1: pathname of symlink on image
# $2: absolute path to target of symlink
(( $# == 2 )) || return 1
_add_dir "$(get_dirname "$1")"
_add_symlink "$2" "$1"
}
add_file() {
# Add a plain file to the initcpio image. No parsing is performed and only
# the singular file is added.
# $1: path to file
# $2: destination on initcpio (optional, defaults to same as source)
(( $# )) || return 1
# determine source and destination
local src= dest=${2:-$1} mode=
src=$BASEDIR$1
[[ -f "$src" ]] || { error "file not found: \`%s'" "$src"; return 1; }
mode=$(stat -c %a "$src")
if [[ -z "$mode" ]]; then
error "failed to stat file: \`%s'." "$src"
return 1
fi
_add_file "${dest#$BASEDIR}" "$src" "$mode"
}
add_binary() {
# add a binary file to the initcpio image. library dependencies will
# be discovered and added.
# $1: path to binary
# $2: destination on initcpio (optional, defaults to same as source)
local -a sodeps
local regex binary dest mode sodep resolved dirname
if [[ ${1:0:1} != '/' ]]; then
binary=$(pathlookup "$1")
else
binary=$BASEDIR$1
fi
[[ -f "$binary" ]] || { error "file not found: \`%s'" "$binary"; return 1; }
dest=${2:-$binary}
mode=$(stat -c %a "$binary")
# always add the binary itself
_add_file "${dest#$BASEDIR}" "$binary" "$mode"
lddout=$(ldd "$binary" 2>/dev/null) || return 1 # not a binary!
# resolve sodeps
regex='(/.+) \(0x[a-fA-F0-9]+\)'
while read line; do
[[ "$line" =~ $regex ]] && sodep=${BASH_REMATCH[1]} || continue
if [[ -f "$sodep" ]]; then # -f follows symlinks, don't believe it!
if [[ ! -L $sodep ]]; then
_add_file "$sodep" "$BASEDIR$sodep" "$(stat -c %a "$sodep")"
else
resolved=$(readlink -e "$BASEDIR$sodep")
dirname=${resolved%/*}
_add_dir "${dirname#$BASEDIR}" 755
_add_symlink "$sodep" "${resolved#$BASEDIR}"
_add_file "${resolved#$BASEDIR}" "$resolved" 755
fi
fi
done <<< "$lddout"
return 0
}
parse_hook() {
# parse key global variables set by install hooks. This function is called
# prior to the start of hook processing, and after each hook's build
# function is run.
local item
for item in $MODULES; do
if [[ ${item:(-1)} = '?' ]]; then
try=--try
item=${item%\?}
fi
add_module $try "$item"
done
for item in $BINARIES; do
add_binary "$item"
done
for item in $FILES; do
add_file "$item"
done
[[ $SCRIPT ]] && add_file "$HOOKDIR/$SCRIPT" "/hooks/$SCRIPT"
}
# vim: set ft=sh ts=4 sw=4 et:
|