Debian Codegraph @

mkinitramfs

raw
1 #!/bin/sh
2
3 umask 0022
4 export PATH='/usr/bin:/sbin:/bin'
5
6 # Defaults
7 keep="n"
8 CONFDIR="/etc/initramfs-tools"
9 verbose="n"
10 # Will be updated by busybox's conf hook, if present
11 BUSYBOXDIR=
12 export BUSYBOXDIR
13
14 usage()
15 {
16 cat << EOF
17
18 Usage: mkinitramfs [option]... -o outfile [version]
19
20 Options:
21 -c compress Override COMPRESS setting in initramfs.conf.
22 -d confdir Specify an alternative configuration directory.
23 -l level Override COMPRESSLEVEL setting in initramfs.conf.
24 -m modules Override MODULES setting in initramfs.conf.
25 -k Keep temporary directory used to make the image.
26 -o outfile Write to outfile.
27 -r root Override ROOT setting in initramfs.conf.
28
29 See mkinitramfs(8) for further details.
30
31 EOF
32 }
33
34 usage_error()
35 {
36 usage >&2
37 exit 2
38 }
39
40 OPTIONS=$(getopt -o c:d:hl:m:ko:r:v --long help,version -n "$0" -- "$@") || usage_error
41
42 eval set -- "$OPTIONS"
43
44 while true; do
45 case "$1" in
46 -c)
47 compress="$2"
48 shift 2
49 ;;
50 -d)
51 CONFDIR="$2"
52 shift 2
53 if [ ! -d "${CONFDIR}" ]; then
54 echo "${0}: ${CONFDIR}: Not a directory" >&2
55 exit 1
56 fi
57 ;;
58 -h|--help)
59 usage
60 exit 0
61 ;;
62 -l)
63 compresslevel="$2"
64 shift 2
65 ;;
66 -m)
67 modules="$2"
68 shift 2
69 ;;
70 -o)
71 outfile="$2"
72 shift 2
73 ;;
74 -k)
75 keep="y"
76 shift
77 ;;
78 -r)
79 ROOT="$2"
80 shift 2
81 ;;
82 -v)
83 verbose="y"
84 shift
85 ;;
86 --version)
87 echo "initramfs-tools @VERSION@"
88 exit 0
89 ;;
90 --)
91 shift
92 break
93 ;;
94 *)
95 echo "Internal error!" >&2
96 exit 1
97 ;;
98 esac
99 done
100
101 # For dependency ordered mkinitramfs hook scripts.
102 . /usr/share/initramfs-tools/scripts/functions
103 . /usr/share/initramfs-tools/hook-functions
104
105 . "${CONFDIR}/initramfs.conf"
106
107 EXTRA_CONF=''
108 maybe_add_conf() {
109 if [ -e "$1" ] && \
110 basename "$1" \
111 | grep '^[[:alnum:]][[:alnum:]\._-]*$' \
112 | grep -qv '\.dpkg-.*$'; then
113 if [ -d "$1" ]; then
114 echo "W: $1 is a directory instead of file" >&2
115 else
116 EXTRA_CONF="${EXTRA_CONF} $1"
117 . "$1"
118 fi
119 fi
120 }
121 for i in /usr/share/initramfs-tools/conf.d/*; do
122 # Configuration files in /etc mask those in /usr/share
123 if ! [ -e "${CONFDIR}"/conf.d/"$(basename "${i}")" ]; then
124 maybe_add_conf "${i}"
125 fi
126 done
127 for i in "${CONFDIR}"/conf.d/*; do
128 maybe_add_conf "${i}"
129 done
130
131 # source package confs
132 for i in /usr/share/initramfs-tools/conf-hooks.d/*; do
133 if [ -d "${i}" ]; then
134 echo "W: ${i} is a directory instead of file." >&2
135 elif [ -e "${i}" ]; then
136 . "${i}"
137 fi
138 done
139
140 # Check busybox dependency
141 if [ "${BUSYBOX}" = "y" ] && [ -z "${BUSYBOXDIR}" ]; then
142 echo >&2 "E: @BUSYBOX_PACKAGES@ is required but not installed"
143 exit 1
144 fi
145
146 if [ -n "${UMASK:-}" ]; then
147 umask "${UMASK}"
148 fi
149
150 if [ -z "${outfile}" ]; then
151 usage_error
152 fi
153
154 touch "$outfile"
155 outfile="$(readlink -f "$outfile")"
156
157 # And by "version" we really mean path to kernel modules
158 # This is braindead, and exists to preserve the interface with mkinitrd
159 if [ ${#} -ne 1 ]; then
160 version="$(uname -r)"
161 else
162 version="${1}"
163 fi
164
165 case "${version}" in
166 /lib/modules/*/[!/]*)
167 ;;
168 /lib/modules/[!/]*)
169 version="${version#/lib/modules/}"
170 version="${version%%/*}"
171 ;;
172 esac
173
174 case "${version}" in
175 */*)
176 echo "$PROG: ${version} is not a valid kernel version" >&2
177 exit 2
178 ;;
179 esac
180
181 if [ -z "${compress:-}" ]; then
182 compress=${COMPRESS?}
183 fi
184 unset COMPRESS
185
186 if ! command -v "${compress}" >/dev/null 2>&1; then
187 echo "W: No ${compress} in ${PATH}, using gzip" >&2
188 compress=gzip
189 fi
190
191 # Support packages installing in /lib/ rather than /boot/
192 kconfig="/lib/modules/${version}/config-${version}"
193 if [ ! -f "$kconfig" ]; then
194 kconfig="/boot/config-${version}"
195 fi
196
197 # Check that kernel supports selected compressor, and fall back to gzip.
198 # Exit if even gzip is not supported.
199 case "${compress}" in
200 gzip) kconfig_sym=CONFIG_RD_GZIP ;;
201 bzip2) kconfig_sym=CONFIG_RD_BZIP2 ;;
202 lzma) kconfig_sym=CONFIG_RD_LZMA ;;
203 xz) kconfig_sym=CONFIG_RD_XZ ;;
204 lzop) kconfig_sym=CONFIG_RD_LZO ;;
205 lz4) kconfig_sym=CONFIG_RD_LZ4 ;;
206 zstd) kconfig_sym=CONFIG_RD_ZSTD ;;
207 *) echo "W: Unknown compression ${compress}" >&2 ;;
208 esac
209 if [ -e "${kconfig}" ]; then
210 while ! grep -q "^$kconfig_sym=y" "${kconfig}"; do
211 if [ "${compress}" = gzip ]; then
212 echo "E: gzip compression ($kconfig_sym) not supported by kernel" >&2
213 exit 1
214 fi
215 echo "W: ${compress} compression ($kconfig_sym) not supported by kernel, using gzip" >&2
216 compress=gzip
217 kconfig_sym=CONFIG_RD_GZIP
218 done
219 else
220 echo "W: Kernel configuration ${kconfig} is missing, ${compress} compression" \
221 "support ($kconfig_sym) cannot be checked and is assumed to be available" >&2
222 fi
223
224 if [ -z "${compresslevel:-}" ]; then
225 compresslevel=${COMPRESSLEVEL:-}
226 fi
227 case "${compress}" in
228 lz4) compresslevel="-${compresslevel:-9}" ;;
229 zstd) compresslevel="-${compresslevel:-9}" ;;
230 #gzip|xz|bzip2|lzma|lzop included
231 *)
232 # We're not using a compression level by default
233 compresslevel="${compresslevel:+-${compresslevel}}"
234 ;;
235 esac
236 unset COMPRESSLEVEL
237
238 if [ -d "${outfile}" ]; then
239 echo "${outfile} is a directory" >&2
240 exit 1
241 fi
242
243 MODULES="${modules:-$MODULES}"
244 MODULESDIR="/lib/modules/${version}"
245
246 if [ ! -e "${MODULESDIR}" ]; then
247 echo "W: missing ${MODULESDIR}" >&2
248 echo "W: Ensure all necessary drivers are built into the linux image!" >&2
249 fi
250 if [ ! -e "${MODULESDIR}/modules.dep" ]; then
251 depmod "${version}"
252 fi
253
254 # Prepare to clean up temporary files on exit
255 DESTDIR=
256 __TMPMANIFEST=
257 __TMPMAINFILES=
258 __TMPUNCOMPRESSEDFILES=
259 __TMPCPIOGZ=
260 __TMPEARLYCPIO=
261 __MODULES_TO_ADD=
262 # shellcheck disable=SC2317,SC2329
263 clean_on_exit() {
264 if [ "${keep}" = "y" ]; then
265 echo "Working files in ${DESTDIR:-<not yet created>}," \
266 "manifest in ${__TMPMANIFEST:-<not yet created>}," \
267 "list of files for main initramfs in ${__TMPMAINFILES:-<not yet created>}," \
268 "list of files for uncompressed initramfs in ${__TMPUNCOMPRESSEDFILES:-<not yet created>}," \
269 "early initramfs in ${__TMPEARLYCPIO:-<not yet created>} and" \
270 "overlay in ${__TMPCPIOGZ:-<not yet created>}"
271 else
272 for path in "${DESTDIR}" "${__TMPMANIFEST}" "${__TMPMAINFILES}" "${__TMPUNCOMPRESSEDFILES}" "${__TMPCPIOGZ}" "${__TMPEARLYCPIO}" "${__MODULES_TO_ADD}"; do
273 test -z "${path}" || rm -rf "${path}"
274 done
275 fi
276 }
277 trap clean_on_exit EXIT
278 trap "exit 1" INT TERM # makes the EXIT trap effective even when killed
279
280 # Create temporary directory and files for initramfs contents
281 [ -n "${TMPDIR}" ] && [ ! -w "${TMPDIR}" ] && unset TMPDIR
282 DESTDIR="$(mktemp -d "${TMPDIR:-/var/tmp}/mkinitramfs_XXXXXX")" || exit 1
283 chmod 755 "${DESTDIR}"
284 __TMPMANIFEST="$(mktemp "${TMPDIR:-/var/tmp}/mkinitramfs-manifest_XXXXXX")" || exit 1
285 __TMPMAINFILES="$(mktemp "${TMPDIR:-/var/tmp}/mkinitramfs-MAIN_files_XXXXXX")" || exit 1
286 __TMPUNCOMPRESSEDFILES="$(mktemp "${TMPDIR:-/var/tmp}/mkinitramfs-UNCOMPRESSED_files_XXXXXX")" || exit 1
287 __TMPCPIOGZ="$(mktemp "${TMPDIR:-/var/tmp}/mkinitramfs-OL_XXXXXX")" || exit 1
288 __TMPEARLYCPIO="$(mktemp "${TMPDIR:-/var/tmp}/mkinitramfs-FW_XXXXXX")" || exit 1
289 __MODULES_TO_ADD="$(mktemp "${TMPDIR:-/var/tmp}/modules_XXXXXX")" || exit 1
290
291 DPKG_ARCH=$(dpkg --print-architecture)
292
293 # Export environment for hook scripts.
294 #
295 export MODULESDIR
296 export version
297 export CONFDIR
298 export DESTDIR
299 export DPKG_ARCH
300 export verbose
301 export KEYMAP
302 export MODULES
303 export BUSYBOX
304 export RESUME
305 export FSTYPE
306
307 # Private, used by 'catenate_cpiogz'.
308 export __TMPCPIOGZ
309
310 # Private, used by 'prepend_earlyinitramfs'.
311 export __TMPEARLYCPIO
312
313 # Private, used by 'manual_add_modules'.
314 export __MODULES_TO_ADD
315
316 # Create usr-merged filesystem layout, to avoid duplicates if the host
317 # filesystem is usr-merged.
318 for d in /bin /lib* /sbin; do
319 mkdir -p "${DESTDIR}/usr${d}"
320 ln -s "usr${d}" "${DESTDIR}${d}"
321 done
322 for d in conf/conf.d etc run scripts ${MODULESDIR}; do
323 mkdir -p "${DESTDIR}/${d}"
324 done
325
326 # Copy in modules.builtin, modules.builtin.modinfo and modules.order (not generated by depmod)
327 # and modules.builtin.bin (generated by depmod, but too late to avoid
328 # error messages as in #948257)
329 for x in modules.builtin modules.builtin.bin modules.builtin.modinfo modules.order; do
330 if [ -f "${MODULESDIR}/${x}" ]; then
331 cp -p "${MODULESDIR}/${x}" "${DESTDIR}${MODULESDIR}/${x}"
332 fi
333 done
334
335 # MODULES=list case. Always honour.
336 for x in "${CONFDIR}/modules" /usr/share/initramfs-tools/modules.d/*; do
337 if [ -f "${x}" ]; then
338 add_modules_from_file "${x}"
339 fi
340 done
341
342 # MODULES=most is default
343 case "${MODULES}" in
344 dep)
345 dep_add_modules
346 ;;
347 most)
348 auto_add_modules
349 ;;
350 netboot)
351 auto_add_modules base
352 auto_add_modules net
353 ;;
354 list)
355 # nothing to add
356 ;;
357 *)
358 echo "W: mkinitramfs: unsupported MODULES setting: ${MODULES}." >&2
359 echo "W: mkinitramfs: Falling back to MODULES=most." >&2
360 auto_add_modules
361 ;;
362 esac
363
364 # Add firmware for built-in code
365 add_builtin_firmware
366
367 # First file executed by linux
368 cp -p /usr/share/initramfs-tools/init "${DESTDIR}/init"
369
370 # add existant boot scripts
371 for b in $(cd /usr/share/initramfs-tools/scripts/ && find . \
372 -regextype posix-extended -regex '.*/[[:alnum:]\._-]+$' -type f); do
373 [ -d "${DESTDIR}/scripts/$(dirname "${b}")" ] \
374 || mkdir -p "${DESTDIR}/scripts/$(dirname "${b}")"
375 cp -p "/usr/share/initramfs-tools/scripts/${b}" \
376 "${DESTDIR}/scripts/$(dirname "${b}")/"
377 done
378 # Prune dot-files/directories and limit depth to exclude VCS files
379 for b in $(cd "${CONFDIR}/scripts" && find . -maxdepth 2 -name '.?*' -prune -o \
380 -regextype posix-extended -regex '.*/[[:alnum:]\._-]+$' -type f -print); do
381 [ -d "${DESTDIR}/scripts/$(dirname "${b}")" ] \
382 || mkdir -p "${DESTDIR}/scripts/$(dirname "${b}")"
383 cp -p "${CONFDIR}/scripts/${b}" "${DESTDIR}/scripts/$(dirname "${b}")/"
384 done
385
386 echo "DPKG_ARCH=${DPKG_ARCH}" > "${DESTDIR}/conf/arch.conf"
387 cp -p "${CONFDIR}/initramfs.conf" "${DESTDIR}/conf"
388 for i in ${EXTRA_CONF}; do
389 copy_file config "${i}" /conf/conf.d
390 done
391
392 # ROOT hardcoding
393 if [ -n "${ROOT:-}" ]; then
394 echo "ROOT=${ROOT}" > "${DESTDIR}/conf/conf.d/root"
395 fi
396
397 if ! command -v ldd >/dev/null 2>&1 ; then
398 echo "E: no ldd around - install libc-bin" >&2
399 exit 1
400 fi
401
402 # fstab and mtab
403 touch "${DESTDIR}/etc/fstab"
404 ln -s /proc/mounts "${DESTDIR}/etc/mtab"
405
406 # module-init-tools
407 copy_exec /sbin/modprobe /sbin
408 copy_exec /sbin/rmmod /sbin
409 mkdir -p "${DESTDIR}/etc/modprobe.d" "${DESTDIR}/lib/modprobe.d"
410 for file in /etc/modprobe.d/*.conf /lib/modprobe.d/*.conf ; do
411 if test -e "$file" || test -L "$file" ; then
412 copy_file config "$file"
413 fi
414 done
415
416 # Copy kernel modules here, because some hooks rely on them being copied
417 apply_add_modules || exit $?
418 run_scripts /usr/share/initramfs-tools/hooks
419 run_scripts "${CONFDIR}"/hooks
420
421 # cache boot run order
422 for b in $(cd "${DESTDIR}/scripts" && find . -mindepth 1 -type d); do
423 cache_run_scripts "${DESTDIR}" "/scripts/${b#./}"
424 done
425
426 apply_add_modules || exit $?
427 # Resolve hidden dependencies
428 hidden_dep_add_modules
429
430 # generate module deps
431 depmod -a -b "${DESTDIR}" "${version}"
432 rm -f "${DESTDIR}/lib/modules/${version}"/modules.*map
433
434 # make sure that library search path is up to date
435 cp -pPr /etc/ld.so.conf* "$DESTDIR"/etc/
436 if ! ldconfig -r "$DESTDIR" ; then
437 [ "$(id -u)" != "0" ] \
438 && echo "ldconfig might need uid=0 (root) for chroot()" >&2
439 fi
440 # The auxiliary cache is not reproducible and is always invalid at boot
441 # (see #845034)
442 if [ -d "${DESTDIR}"/var/cache/ldconfig ]; then
443 rm -f "${DESTDIR}"/var/cache/ldconfig/aux-cache
444 rmdir --ignore-fail-on-non-empty "${DESTDIR}"/var/cache/ldconfig
445 fi
446
447 # Apply DSDT to initramfs
448 if [ -e "${CONFDIR}/DSDT.aml" ]; then
449 copy_file DSDT "${CONFDIR}/DSDT.aml"
450 fi
451
452 [ "${verbose}" = y ] && echo "Building cpio ${outfile} initramfs"
453
454 # Read list of files and echo them plus all leading directories.
455 # The same directories might be printed multiple times (even with sorted input)!
456 add_directories() {
457 local last_dir path dir
458 while read -r path; do
459 dir="${path%/*}"
460 parent="${dir}"
461 while [ "$parent" != "$last_dir" ] && [ "$parent" != "." ]; do
462 echo "$parent"
463 parent="${parent%/*}"
464 done
465 last_dir="$dir"
466 echo "$path"
467 done
468 if [ -n "$last_dir" ]; then
469 echo "."
470 fi
471 }
472
473 cd "${DESTDIR}" || exit 1
474
475 # work around lack of "set -o pipefail" for pipes.
476 # Write exit code to FD 3 in case of a failure to pass it through the pipes.
477 {
478 {
479 find . \( ! -type d ! -name '*.zst' ! -name '*.xz' \) -o \( -type d -empty \) ||
480 { echo "E: mkinitramfs failure main find $?" >&2; echo 1 >&3; exit; }
481 } | add_directories | {
482 LC_ALL=C sort || { echo "E: mkinitramfs failure sort $?" >&2; echo 1 >&3; exit; }
483 } | uniq > "${__TMPMAINFILES}" || { echo "E: mkinitramfs failure uniq $?" >&2; echo 1 >&3; exit; }
484 } 3>&1 | { read -r exit_code; exit "${exit_code:-0}"; } || exit $?
485
486 {
487 {
488 find . -name '*.zst' -o -name '*.xz' ||
489 { echo "E: mkinitramfs failure uncompressed find $?" >&2; echo 1 >&3; exit; }
490 } | add_directories | {
491 LC_ALL=C sort || { echo "E: mkinitramfs failure sort $?" >&2; echo 1 >&3; exit; }
492 } | uniq > "${__TMPUNCOMPRESSEDFILES}" || { echo "E: mkinitramfs failure uniq $?" >&2; echo 1 >&3; exit; }
493 } 3>&1 | { read -r exit_code; exit "${exit_code:-0}"; } || exit $?
494
495 build_initrd_with_cpio() {
496 local cpio_owner_root
497
498 case "${compress}" in
499 gzip) # If we're doing a reproducible build, use gzip -n
500 if [ -n "${SOURCE_DATE_EPOCH}" ]; then
501 compress="gzip -n"
502 # Otherwise, substitute pigz if it's available
503 elif command -v pigz >/dev/null; then
504 compress=pigz
505 fi
506 if [ -n "${compresslevel}" ]; then
507 compress="${compress} ${compresslevel}"
508 fi
509 ;;
510 lz4) compress="lz4 ${compresslevel} -l" ;;
511 zstd) compress="zstd -q ${compresslevel}"
512 # If we're not doing a reproducible build, enable multithreading
513 test -z "${SOURCE_DATE_EPOCH}" && compress="$compress -T0"
514 ;;
515 xz) compress="xz ${compresslevel} --check=crc32"
516 # If we're not doing a reproducible build, enable multithreading
517 test -z "${SOURCE_DATE_EPOCH}" && compress="$compress --threads=0"
518 ;;
519 bzip2|lzma|lzop)
520 compress="${compress} ${compresslevel}"
521 ;;
522 esac
523
524 # preserve permissions if root builds the image, see #633582
525 [ "$(id -ru)" != 0 ] && cpio_owner_root="-R 0:0"
526
527 # if SOURCE_DATE_EPOCH is set, try and create a reproducible image
528 if [ -n "${SOURCE_DATE_EPOCH}" ]; then
529 # ensure that no timestamps are newer than $SOURCE_DATE_EPOCH
530 find "${DESTDIR}" -newermt "@${SOURCE_DATE_EPOCH}" -print0 | \
531 xargs -0r touch --no-dereference --date="@${SOURCE_DATE_EPOCH}"
532
533 # --reproducible requires cpio >= 2.12
534 cpio_reproducible="--reproducible"
535 fi
536
537 {
538 {
539 if [ -s "${__TMPEARLYCPIO}" ]; then
540 cat "${__TMPEARLYCPIO}" || { echo 1 >&3; exit; }
541 fi
542
543 if [ -s "${__TMPUNCOMPRESSEDFILES}" ]; then
544 # shellcheck disable=SC2086
545 cpio --quiet $cpio_owner_root $cpio_reproducible -o -H newc -D "${DESTDIR}" <"${__TMPUNCOMPRESSEDFILES}" ||
546 { echo "E: mkinitramfs failure uncompressed cpio $?" >&2; echo 1 >&3; exit; }
547 fi
548
549 {
550 # shellcheck disable=SC2086
551 cpio --quiet $cpio_owner_root $cpio_reproducible -o -H newc -D "${DESTDIR}" <"${__TMPMAINFILES}" ||
552 { echo "E: mkinitramfs failure cpio $?" >&2; echo 1 >&3; exit; }
553 } | $compress -c ||
554 { echo "E: mkinitramfs failure $compress $?" >&2; echo 1 >&3; exit; }
555
556 if [ -s "${__TMPCPIOGZ}" ]; then
557 cat "${__TMPCPIOGZ}" || { echo 1 >&3; exit; }
558 fi
559 } >"${outfile}" || echo $? >&3
560 } 3>&1 | { read -r exit_code; exit "${exit_code:-0}"; } || exit $?
561 }
562
563 build_manifest() {
564 if [ "$(id -ru)" = 0 ]; then
565 # preserve permissions if root builds the image, see #633582
566 echo "#cpio"
567 cat "${__TMPUNCOMPRESSEDFILES}"
568 echo "#cpio: ${compress%% *} ${compresslevel}"
569 cat "${__TMPMAINFILES}"
570 else
571 echo "#cpio"
572 sed "s/$/\t\t\t\t0\t0/" "${__TMPUNCOMPRESSEDFILES}"
573 echo "#cpio: ${compress%% *} ${compresslevel}"
574 sed "s/$/\t\t\t\t0\t0/" "${__TMPMAINFILES}"
575 fi
576 }
577
578 build_initrd_with_3cpio() {
579 {
580 {
581 if [ -s "${__TMPEARLYCPIO}" ]; then
582 cat "${__TMPEARLYCPIO}" || { echo 1 >&3; exit; }
583 fi
584
585 3cpio --create -C "${DESTDIR}" <"${__TMPMANIFEST}" ||
586 { echo "E: mkinitramfs failure 3cpio $?" >&2; echo 1 >&3; exit; }
587
588 if [ -s "${__TMPCPIOGZ}" ]; then
589 cat "${__TMPCPIOGZ}" || { echo 1 >&3; exit; }
590 fi
591 } >"${outfile}" || echo $? >&3
592 } 3>&1 | { read -r exit_code; exit "${exit_code:-0}"; } || exit $?
593 }
594
595 if command -v 3cpio >/dev/null; then
596 build_manifest > "${__TMPMANIFEST}"
597 build_initrd_with_3cpio
598 else
599 build_initrd_with_cpio
600 fi
601
602 exit 0