-
Notifications
You must be signed in to change notification settings - Fork 9
/
mkinitramfs-ll.sh
executable file
·529 lines (484 loc) · 15 KB
/
mkinitramfs-ll.sh
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#!/bin/sh
#
# $Header: mkinitramfs-ll/mkinitramfs-ll.sh Exp $
# $Author: (c) 2011-6 tokiclover <[email protected]> Exp $
# $License: 2-clause/new/simplified BSD Exp $
# $Version: 0.22.0 2016/10/31 12:33:03 Exp $
#
name=mkinitramfs-ll
package=${name}
shell=sh
version=0.22.0
null=/dev/null
# @FUNCTION: Print help message
usage() {
cat <<-EOH
${package}.${shell} version ${version}
Usage: ${0##*/} [-a|-all] [OPTIONS]
-a, --all Short variant of "-lLgtq -H'btrfs zfs zram'"
-e, --extension=cpio.img Extension to use (default to cpio.\$compressor)
-f, --font=ter-v14n Fonts to include in the initramfs
-F, --firmware=name Firmware file/directory to include
-k, --kernel-version=KV Build an initramfs for kernel version VERSION
-c, --compressor='gzip -9' Use 'gzip -9' compressor instead of default
-L, --luks Enable LUKS support (require cryptsetup binary)
-l, --lvm Enable LVM2 support (require lvm2 binary)
-b, --bin=<bins> Binar-y-ies to include if available
-d, --usrdir=DIRECTORY Use DIRECTORY as USRDIR instead of the default
-g, --gpg Enable GnuPG support (require gnupg-1.4.x)
-p, --prefix=initrd- Use 'initrd-' prefix instead of default ['initramfs-']
-H, --hook=<name> Include hook or script if available
-m, --module=<mod> Include kernel modules if available
--module-tuxonice=mod Append kernel modules to tuxonice group
--module-remdev=mod Append kernel modules to remdev group
--module-squashd=mod Append kernel modules to squashd group
--module-gpg=mod Append kernel modules to gpg group
--module-boot=mod Append kernel modules to boot group
-s, --splash=<theme> Include splash themes if available
-t, --toi Enable TuxOnIce support (require tuxoniceui-userui)
-q, --squashd Enable UnionFS+SquashFS support (AUFS/OverlayFS)
-r, --rebuild Re-Build an initramfs from an old directory
-y, --keymap=fr-latin1 Keymaps to include the initramfs
-K, --keep-tmpdir Keep the temporary build directory
-h, --help, -? Print this help or usage message
EOH
exit $?
}
# @FUNCTION: Print error message to stderr & exit
die()
{
local ret=${?}; error "${@}"; exit ${ret}
}
# @FUNCTION: (static/dynamic) Binary copy helper
# @ARG: <binary>
dobin() {
local bin="${1}" lib
docp ${bin} || return
ldd ${bin} >${null} 2>&1 || return 0
for lib in $(ldd ${bin} | sed -nre 's,.* (/.*lib.*/.*.so.*) .*,\1,p' \
-e 's,.*(/lib.*/ld.*.so.*) .*,\1,p'); do
mkdir -p .${lib%/*} && docp ${lib} || die
done
}
# @FUNCTION: File copy helper (handle symlinks)
# @ARG: <file>
docp() {
local link=${1} prefix
[ -n "${1}" -a -e "${1}" ] || return
mkdir -p .${1%/*}
rm -f .${1} && cp -a ${1} .${1} || die
[ -h ${link} ] &&
while true; do
prefix=${link%/*}
link=$(readlink ${link})
[ ${link%/*} = ${link} ] && link=${prefix}/${link}
rm -f .${link} && cp -a ${link} .${link} || die
[ -h ${link} ] || break
done
return 0
}
# @FUNCTION: CPIO image builder
docpio() {
local ext=cpio file=${1:-/boot/$initramfs}
local cmd="find . -print0 | cpio -0 -ov -Hnewc"
case "${compressor%% *}" in
(bzip2) ext=${ext}.bz2;;
(gzip) ext=${ext}.gz ;;
(xz) ext=${ext}.xz ;;
(lzma) ext=${ext}.lzma;;
(lzip) ext=${ext}.lz ;;
(lzop) ext=${ext}.lzo;;
(lz4) ext=${ext}.lz4;;
(*) compressor=; warn "Initramfs will not be compressed";;
esac
: ${extension:=${ext}}
if [ -f ${file}.${extension} ]; then
mv ${file}.${extension} ${file}.${extension}.old
fi
if [ -n "${ext#.cpio}" ]; then
cmd+=" | ${compressor} -c"
fi
eval ${cmd} >/${file}.${extension} ||
die "Failed to build ${file}.${extension} initramfs"
}
# @FUNCTION: Kernel module copy helper
# @ARG: [-v|--verbose] <module-group>
domod() {
case "${1}" in
(-v|--verbose)
local verbose="${2}"
shift 2;;
esac
local m mod ret n p=/lib/modules/${kv}/
for mod; do
modules="$(sed -nre "s/(${mod}(|[_-]).*$)/\1/p" .${p}modules.dep)"
if [ -n "${modules}" ]; then
for m in ${modules}; do
case "${m}" in
(*:)
m="${m%:}"
if [ "${verbose}" ]; then
n="${m##*/}"
echo "${n/.ko}" >>${verbose} || die
fi
;;
esac
mkdir -p .${p}${m%/*} && cp -ar ${p}${m} .${p}${m} ||
die "Failed to copy ${m} kernel module"
done
else
warn "${mod} kernel module not found"
ret=$((${ret}+1))
fi
done
return ${ret}
}
# @FUNCTION: Device nodes (helper)
donod() {
pushd dev || die
[ -c console ] || mknod -m 600 console c 5 1 || die
[ -c urandom ] || mknod -m 666 urandom c 1 9 || die
[ -c random ] || mknod -m 666 random c 1 8 || die
[ -c mem ] || mknod -m 640 mem c 1 1 && chmod 0:9 mem || die
[ -c null ] || mknod -m 666 null c 1 3 || die
[ -c tty ] || mknod -m 666 tty c 5 0 || die
[ -c zero ] || mknod -m 666 zero c 1 5 || die
local i
for i in 0 1 2 3 4 5 6 7; do
[ -c tty${i} ] || mknod -m 600 tty${i} c 4 ${i} || die
done
popd || die
}
# @FUNCTION: Temporary dir/file helper
mktmp() {
local tmp="${TMPDIR:-/tmp}/${1}-XXXXXX"
mkdir -p "${tmp}" || die "Failed to make ${tmp}"
echo "${tmp}"
}
[ -f ./${package}.conf ] && source ./${package}.conf ||
die "No ${package}.conf configuration file found"
opt="$(getopt \
-o ab:c:e:f:F:gk:lH:KLm:p:qrs:thu:y:\? \
-l all,bin:,compressor:,extension:,firmware:,font:,gpg,help \
-l hook:,luks,lvm,keep-tmpdir,module:,keymap:,kernel-version: \
-l module-boot:,module-gpg:,module-remdev:,module-squashd:,module-tuxonice: \
-l prefix:,rebuild,splash:,squashd,toi,usrdir: \
-n ${name} -s sh -- "${@}" || usage)"
eval set -- ${opt}
while true; do
case "${1}" in
(--) shift; break;;
(-a|--all) option_all=':';;
(-l|--lvm) option_lvm=':';;
(-t|--toi) option_toi=':';;
(-g|--gpg) option_gpg=':';;
(-L|--luks) option_luks=':';;
(-q|--squashd) option_squashd=':';;
(-r|--rebuild) option_rebuild=':';;
(-K|--keep-tmpdir) option_tmpdir=':';;
(-b|--bin) shift; bins="${bins} ${1}";;
(-k|--kernel-version) shift; kv="${1}";;
(-e|--extension) shift; extension="${1}";;
(-f|--font) shift; fonts="${1} ${fonts}";;
(-c|--compressor) shift; compressor="${1}";;
(-y|--keymap) shift; keymaps="${1} ${keymaps}";;
(-m|--module) shift; modules="${modules} ${1}";;
(-f|--firmware) shift; firmwares="${firmware} ${1}";;
(--module-*) eval ${1/-/_}="\$${1/-/_} ${2}"; shift;;
(-s|--splash) shift; splash="${splash} ${1}";;
(-H|--hook) shift; hooks="${hooks} ${1}";;
(-d|--usrdir) shift; usrdir="${1}";;
(-p|--prefix) shift; prefix="${1}";;
(-?|-h|--help|*) usage;;
esac
shift
done
# @VARIABLE: Kernel version
: ${kv:=$(uname -r)}
# @VARIABLE: Initramfs prefx
: ${prefix:=initramfs-}
# @VARIABLE: USRDIR path to use
: ${usrdir:=${PWD}/usr}
# @VARIABLE: Full path to initramfs image
: ${initramfs=${prefix}${kv}}
: ${compressor:=xz -9 --check=crc32}
# @VARIABLE: Kernel ARCHitecture
: ${ARCH:=$(uname -m)}
# @VARIABLE: Kernel bit lenght
: ${LONG_BIT:=$(getconf LONG_BIT)}
# @VARIABLE: (initramfs) Tmporary directory
tmpdir="$(mktmp ${initramfs})"
# @DESCRIPTION: (initramfs) Configuration directory
confdir="etc/${package}"
source "${usrdir}"/lib/${package}/functions || exit 1
eval_colors
if [ -n "${option_all}" ]; then
fonts="${fonts:-:}" keymaps="${keymaps:-:}" hooks="${hooks} btrfs zfs zram"
option_gpg=true option_lvm=true option_squashd=true option_toi=true option_luks=true
fi
if [ -n "${fonts}" -a "${fonts}" = ":" -a -e /etc/conf.d/consolefont ]; then
fonts="${fonts} $(sed -nre 's,^consolefont="([a-zA-Z].*)",\1,p' \
/etc/conf.d/consolefont)"
fi
if [ -n "${keymaps}" -a "${keymaps}" = ":" -a -e /etc/conf.d/keymaps ]; then
keymaps="${keymaps} $(sed -nre 's,^keymap="([a-zA-Z].*)",\1,p' \
/etc/conf.d/keymaps)"
fi
case "${compressor}" in
(none) ;;
([a-z]*)
if [ -e /usr/src/linux-${kv}/.config ]; then
config=/usr/src/linux-${kv}/.config
xgrep=$(type -p grep 2>${null})
elif [ -e /proc/config.gz ]; then
config=/proc/config.gz
xgrep=$(type -p zgrep 2>${null})
else
warn "No kernel config file found"
fi
esac
if [ -f "${config}" ]; then
comp="${compressor%% *}"
CONFIG=CONFIG_RD_$(echo "${comp}" | tr '[a-z]' '[A-Z]')
if ! ${xgrep} -q "^${CONFIG}=y" ${config} >${null} 2>&1; then
warn "${comp} compressor is not supported by kernel-${kv}"
for comp in bzip2 gzip lzip lzop lz4 xz; do
CONFIG=CONFIG_RD_$(echo "${comp}" | tr '[a-z]' '[A-Z]')
if ${xgrep} -q "^${CONFIG}=y" ${config}; then
compressor="${comp} -9"
info "Setting up compressor to ${comp}"
break
elif [ "${comp}" = "xz" ]; then
warn "No suitable compressor support found in kernel-${kv}"
compressor=
fi
done
fi
unset config xgrep CONFIG comp
fi
echo -e "${COLOR_BLU}>>>${COLOR_RST} Building ${initramfs}..."
cd "${tmpdir}" || die "${tmpdir} not found"
if [ -n "${rebuild}" ]; then
cp -af "${usrdir}"/lib/${package}/functions lib/${package} &&
cp -af "${usrdir}"/../init . && chmod 775 init || die
docpio
echo -e "${COLOR_BLU}>>>${COLOR_RST} Regenerated ${initramfs}"
exit
else
rm -fr *
fi
# Set up the initramfs
if [ -d "${usrdir}" ]; then
cp -ar "${usrdir}" . &&
mv -f usr/root usr/etc . &&
mv -f usr/lib lib${LONG_BIT} || die
else
die "${usrdir} usrdir not found"
fi
mkdir -p usr/share/consolefonts usr/share/keymaps usr/lib${LONG_BIT} \
usr/bin usr/sbin sbin bin dev proc sys newroot mnt/tok etc/${package} \
etc/splash run lib${LONG_BIT}/modules/${kv} lib${LONG_BIT}/${package} || die
for dir in lib usr/lib; do
ln -s lib${LONG_BIT} ${dir}
done
{
for key in name shell version; do
eval echo "${key}=\${$key}"
done
echo "build=$(date +%Y-%m-%d-%H-%M-%S)"
} >${confdir}/id
touch etc/fstab etc/mtab
cp -a /dev/console /dev/random /dev/urandom /dev/mem /dev/null /dev/tty \
/dev/tty[0-6] /dev/zero dev/ || donod
major="${kv%%.*}" minor="${kv#*.}"
minor="${minor%%.*}"
if [ "${major}" -eq 3 -a "${minor}" -ge 1 ]; then
cp -a /dev/loop-control dev/ >$null 2>&1 ||
mknod -m 600 dev/loop-control c 10 237 || die
fi
unset major minor
cp -a "${usrdir}"/../init . && chmod 775 init || die
[ -d root ] && chmod 0700 root || mkdir -m 0700 root || die
cp -af /lib/modules/${kv}/modules.dep lib/modules/${kv} ||
die "Failed to copy modules.dep"
# Set up (requested) firmware
if [ -n "${firmwares}" ]; then
case "${firmwares}" in
(:)
warn "Adding the whole firmware directory"
cp -a /lib/firmware lib
;;
(*)
mkdir -p lib/firmware
for fw in ${firmwares}; do
[ -e ${fw} ] && cp -a ${fw} lib/firmware ||
for fw in /lib/firmware/*${fw}*; do
docp ${fw}
done
done
;;
esac
fi
# Set up RAID option
for bin in dmraid mdadm zfs; do
case "${bins}" in
(*${bin}*) module_group="${module_group} ${bin}";;
esac
done
module_group="${module_group/mdadm/raid}"
# Set up (requested) hook
for hook in ${hooks}; do
for file in "${usrdir}"/../hooks/*${hook}*; do
cp -a "${file}" lib/${package}/
done
if [ ${?} != 0 ]; then
warn "No $hook hook/script does not exist"
continue
fi
eval bins="\"${bins} \${bin_$hook}\""
module_group="${module_group} ${hook}"
done
[ -f /etc/issue.logo ] && cp /etc/issue.logo etc/
# Handle & copy BusyBox binary
if [ -x usr/bin/busybox ]; then
mv -f usr/bin/busybox bin/
elif type -p busybox >${null} 2>&1; then
bb=$(type -p busybox 2>${null})
if ldd ${bb} >${null} 2>&1; then
busybox --list-full >${confdir}/busybox.applets
bins="${bb} ${bins}"
warn "busybox is not a static binary"
fi
cp -a ${bb} bin/
unset bb
else
die "No busybox binary found"
fi
if [ ! -f ${confdir}/busybox.applets ]; then
bin/busybox --list-full >${confdir}/busybox.applets || die
fi
while read bin; do
grep -qsw ${bin} ${confdir}/busybox.applets ||
die "${bin} applet not found, no suitable busybox found"
done <"${usrdir}"/../scripts/minimal.applets
for bin in $(grep '^bin/' ${confdir}/busybox.applets); do
ln -s busybox ${bin}
done
for bin in $(grep '^sbin/' ${confdir}/busybox.applets); do
ln -s ../bin/busybox ${bin}
done
# Set up a few options
if [ -n "${option_luks}" ]; then
bins="${bins} cryptsetup" module_group="${module_group} dm-crypt"
fi
if [ -n "${opts_squashd}" ]; then
bins="${bins} umount.aufs mount.aufs" module_group="${module_group} squashd"
fi
if [ -n "${opts_gpg}" ]; then
if [ -x usr/bin/gpg ]; then :;
elif [ "$(gpg --version | sed -nre '/^gpg/s/.* ([0-9]{1})\..*$/\1/p')" -eq 1 ]; then
bins="${bins} $(type -p gpg 2>${null})"
else
die "No usable gpg/gnupg-1.4.x binary found"
fi
fi
if [ -n "${option_lvm}" ]; then
bins="${bins} lvm" module_group="${module_group} device-mapper"
fi
# Handle & copy keymap/consolefont
for keymap in ${keymaps}; do
if [ -f usr/share/keymaps/${keymap}-${ARCH}.bin ]; then
:;
elif [ -f "${keymap}" ]; then
cp -a "${keymap}" usr/share/keymaps/
else
loadkeys -b -u ${keymap} >usr/share/keymaps/${keymap}-${ARCH}.bin ||
die "Failed to build ${keymap} keymap"
fi
[ "${?}" = 0 ] && KEYMAPS="${KEYMAPS} ${keymap}-${ARCH}.bin"
done
for keymap in ${KEYMAPS}; do
echo "${keymap}" >${confdir}/kmap
break
done
for font in ${fonts}; do
if [ -f usr/share/consolefonts/${font} ]; then :;
elif [ -f ${font} ]; then
cp -a ${font} usr/share/consolefonts/
else
for file in /usr/share/consolefonts/${font}*.gz; do
if [ -f ${file} ]; then
cp ${file} .
gzip -d ${file##*/}
fi
done
mv ${font}* usr/share/consolefonts/
fi
[ ${?} = 0 ] && FONTS="${FONTS} ${font}"
done
for font in ${FONTS}; do
echo "${font}" >${confdir}/font
break
done
unset FONTS font KEYMAPS keymap
# Handle & copy splash themes
if [ -n "${splash}" ]; then
bins="${bins} splash_util.static fbcondecor_helper"
if [ -n "${option_toi}" ]; then
bins="${bins} tuxoniceui_text" && module_group="${module_group} tuxonice"
fi
for theme in ${splash}; do
if [ -d etc/splash/${theme} ]; then :;
elif [ -d /etc/splash/${theme} ]; then
cp -r /etc/splash/${theme} etc/splash/
elif [ -d ${theme} ]; then
cp -ar ${theme} etc/splash/
else
warn "Failed to copy ${theme} theme"
fi
done
fi
# Handle & copy binaries
for bin in ${bins}; do
for b in usr/*bin/${bin} *bin/${bin}; do
[ -x ${b} -a ! -h ${b} ] && continue 2
done
[ -x ${bin} ] && binary=${bin} || binary=$(type -p ${bin} 2>${null})
[ -n "${binary}" ] && dobin ${binary} || warn "no ${bin} binary found"
done
unset binary bin b
# Handle & copy kernel module
domod ${modules}
for mod in ${module_boot}; do
if eval [ -n \"\${module_$mod}\" ]; then
echo "${mod}" >> ${confdir}/module-boot
else
mboot="${boot} ${mod}"
fi
done
module_boot="${mboot}"
unset mboot mod
for group in ${module_group}; do
eval domod -v ${confdir}/module-${group/_/-} \${module_${group/-/_}}
done
# Set up user environment if present
for e in ${env}; do
echo "${e}" >>${confdir}/env
done
unset e env
# Handle GCC libraries
for dir in $(sed -nre '/^\/.*gcc.*/p'); do
if [ -e ${dir}/libgcc_s.so ]; then
mkdir -p .${dir}
cp ${dir}/libgcc_s.so* .${dir}
fi
done
cp -ar /etc/ld.so.* etc
echo >etc/ld.so.cache
ldconfig -r "${tmpdir}" -f etc/ld.so.conf -C etc/ld.so.cache
docpio
[ -n "${option_tmpdir}" ] || rm -rf ${tmpdir}
echo -e "${COLOR_GRN}>>>${COLOR_RST} Built ${initramfs} initramfs"
#
# vim:fenc=utf-8:ci:pi:sts=2:sw=2:ts=2:
#