]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut.sh
test: set QEMU machine for ARM and PowerPC
[thirdparty/dracut.git] / dracut.sh
CommitLineData
c1c78f8a 1#!/bin/bash -p
3b403b32 2#
641cc356 3# Generator script for a dracut initramfs
70c26b7f 4
7d848c55 5# Copyright 2005-2013 Red Hat, Inc. All rights reserved.
4f18fe82
HH
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
ec9315e5 20
58dad702 21# store for logging
ee44f629 22
c1c78f8a 23unset BASH_ENV
d8e47e20 24unset GZIP
c1c78f8a 25
ee44f629 26# Verify bash version, current minimum is 4
9a52c3fd 27if ((BASH_VERSINFO[0] < 4)); then
ae88e029 28 printf "%s\n" "dracut[F]: dracut requires at least Bash 4." >&2
ee44f629
HH
29 exit 1
30fi
31
9a52c3fd 32dracut_args=("$@")
909961d0 33# shellcheck disable=SC2155
7310a848 34readonly dracut_cmd=$(readlink -f "$0")
5616feb0 35
7abd4264
HH
36set -o pipefail
37
5616feb0 38usage() {
497916e1 39 [[ $sysroot_l ]] && dracutsysrootdir="$sysroot_l"
7310a848 40 [[ $dracutbasedir ]] || dracutbasedir="$dracutsysrootdir"/usr/lib/dracut
dbf8f6ba 41 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
7310a848
HH
42 # shellcheck source=./dracut-version.sh
43 . "$dracutbasedir"/dracut-version.sh
dbf8f6ba
HH
44 fi
45
560402c3 46 # 80x25 linebreak here ^
cc02093d 47 cat << EOF
e8f19bcf 48Usage: $dracut_cmd [OPTION]... [<initramfs> [<kernel-version>]]
ffa71b4a
HH
49
50Version: $DRACUT_VERSION
51
52Creates initial ramdisk images for preloading modules
53
54 -h, --help Display all options
55
56If a [LIST] has multiple arguments, then you have to put these in quotes.
57
58For example:
59
60 # dracut --add-drivers "module1 module2" ...
61
62EOF
63}
64
65long_usage() {
7310a848 66 [[ $dracutbasedir ]] || dracutbasedir="$dracutsysrootdir"/usr/lib/dracut
ffa71b4a 67 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
7310a848
HH
68 # shellcheck source=./dracut-version.sh
69 . "$dracutbasedir"/dracut-version.sh
ffa71b4a
HH
70 fi
71
560402c3 72 # 80x25 linebreak here ^
ffa71b4a 73 cat << EOF
e8f19bcf 74Usage: $dracut_cmd [OPTION]... [<initramfs> [<kernel-version>]]
dbf8f6ba
HH
75
76Version: $DRACUT_VERSION
77
5616feb0
AT
78Creates initial ramdisk images for preloading modules
79
e65caf36 80 --kver [VERSION] Set kernel version to [VERSION].
39ff0682 81 -f, --force Overwrite existing initramfs file.
9bef7109
AAF
82 [OUTPUT_FILE] --rebuild
83 Append the current arguments to those with which the
84 input initramfs image was built. This option helps in
85 incrementally building the initramfs for testing.
86 If optional [OUTPUT_FILE] is not provided, the input
87 initramfs provided to rebuild will be used as output
88 file.
54e7d7c3 89 -a, --add [LIST] Add a space-separated list of dracut modules.
9bef7109
AAF
90 --force-add [LIST] Force to add a space-separated list of dracut modules
91 to the default set of modules, when -H is specified.
92 -o, --omit [LIST] Omit a space-separated list of dracut modules.
5616feb0
AT
93 -m, --modules [LIST] Specify a space-separated list of dracut modules to
94 call when building the initramfs. Modules are located
c5a65990 95 in /usr/lib/dracut/modules.d.
9bef7109
AAF
96 This option forces dracut to only include the specified
97 dracut modules.
98 In most cases the --add option is what you want to use.
ea3c4e82
HH
99 --add-drivers [LIST] Specify a space-separated list of kernel
100 modules to add to the initramfs.
9bef7109
AAF
101 --force-drivers [LIST]
102 Specify a space-separated list of kernel
cea907f6
TR
103 modules to add to the initramfs and make sure they
104 are tried to be loaded via modprobe same as passing
105 rd.driver.pre=DRIVER kernel parameter.
fcbcb252 106 --omit-drivers [LIST] Specify a space-separated list of kernel
ea3c4e82 107 modules not to add to the initramfs.
9bef7109
AAF
108 -d, --drivers [LIST] Specify a space-separated list of kernel modules to
109 exclusively include in the initramfs.
8fa510d4 110 --filesystems [LIST] Specify a space-separated list of kernel filesystem
ea3c4e82
HH
111 modules to exclusively include in the generic
112 initramfs.
9bef7109
AAF
113 -k, --kmoddir [DIR] Specify the directory where to look for kernel
114 modules.
115 --fwdir [DIR] Specify additional colon-separated list of directories
116 where to look for firmware files.
352e5917
AAF
117 --libdirs [LIST] Specify a space-separated list of directories
118 where to look for libraries.
9bef7109
AAF
119 --kernel-only Only install kernel drivers and firmware files.
120 --no-kernel Do not install kernel drivers and firmware files.
121 --print-cmdline Print the kernel command line for the given disk layout.
122 --early-microcode Combine early microcode with ramdisk.
123 --no-early-microcode Do not combine early microcode with ramdisk.
124 --kernel-cmdline [PARAMETERS]
125 Specify default kernel command line parameters.
126 --strip Strip binaries in the initramfs.
e4f1dbcc 127 --aggressive-strip Strip more than just debug symbol and sections,
9bef7109
AAF
128 for a smaller initramfs build. The --strip option must
129 also be specified.
130 --nostrip Do not strip binaries in the initramfs.
131 --hardlink Hardlink files in the initramfs.
132 --nohardlink Do not hardlink files in the initramfs.
133 --prefix [DIR] Prefix initramfs files with [DIR].
134 --noprefix Do not prefix initramfs files.
135 --mdadmconf Include local /etc/mdadm.conf file.
136 --nomdadmconf Do not include local /etc/mdadm.conf file.
137 --lvmconf Include local /etc/lvm/lvm.conf file.
138 --nolvmconf Do not include local /etc/lvm/lvm.conf file.
25b45979
MS
139 --fscks [LIST] Add a space-separated list of fsck helpers.
140 --nofscks Inhibit installation of any fsck helpers.
ff3953ef 141 --ro-mnt Mount / and /usr read-only by default.
9bef7109
AAF
142 -h, --help This message.
143 --debug Output debug information of the build process.
144 --profile Output profile information of the build process.
e103615b
145 -L, --stdlog [0-6] Specify logging level (to standard error)
146 0 - suppress any messages
147 1 - only fatal errors
148 2 - all errors
149 3 - warnings
54e7d7c3 150 4 - info
e103615b
151 5 - debug info (here starts lots of output)
152 6 - trace info (and even more)
9bef7109
AAF
153 -v, --verbose Increase verbosity level.
154 -q, --quiet Decrease verbosity level.
5616feb0
AT
155 -c, --conf [FILE] Specify configuration file to use.
156 Default: /etc/dracut.conf
3b403b32 157 --confdir [DIR] Specify configuration directory to use *.conf files
cc02093d 158 from. Default: /etc/dracut.conf.d
882c4c5a 159 --tmpdir [DIR] Temporary directory to be used instead of default
bc1e69b6 160 ${TMPDIR:-/var/tmp}.
a0120420 161 -r, --sysroot [DIR] Specify sysroot directory to collect files from.
5616feb0
AT
162 -l, --local Local mode. Use modules from the current working
163 directory instead of the system-wide installed in
c5a65990 164 /usr/lib/dracut/modules.d.
5616feb0 165 Useful when running dracut from a git checkout.
9bef7109
AAF
166 -H, --hostonly Host-only mode: Install only what is needed for
167 booting the local host instead of a generic host.
168 -N, --no-hostonly Disables host-only mode.
169 --hostonly-mode [MODE]
170 Specify the host-only mode to use. [MODE] could be
171 one of "sloppy" or "strict". "sloppy" mode is used
172 by default.
173 In "sloppy" host-only mode, extra drivers and modules
174 will be installed, so minor hardware change won't make
175 the image unbootable (e.g. changed keyboard), and the
176 image is still portable among similar hosts.
177 With "strict" mode enabled, anything not necessary
178 for booting the local host in its current state will
179 not be included, and modules may do some extra job
180 to save more space. Minor change of hardware or
181 environment could make the image unbootable.
182 DO NOT use "strict" mode unless you know what you
183 are doing.
ab9457ef 184 --hostonly-cmdline Store kernel command line arguments needed
9bef7109 185 in the initramfs.
ab9457ef 186 --no-hostonly-cmdline Do not store kernel command line arguments needed
9bef7109 187 in the initramfs.
37f2fe55
XP
188 --no-hostonly-default-device
189 Do not generate implicit host devices like root,
9bef7109
AAF
190 swap, fstab, etc. Use "--mount" or "--add-device"
191 to explicitly add devices as needed.
f367b738 192 --hostonly-i18n Install only needed keyboard and font files according
9bef7109 193 to the host configuration (default).
f367b738 194 --no-hostonly-i18n Install all keyboard and font files available.
1e92f728 195 --hostonly-nics [LIST]
9bef7109
AAF
196 Only enable listed NICs in the initramfs. The list can
197 be empty, so other modules can install only the
198 necessary network drivers.
324ea606
HH
199 --persistent-policy [POLICY]
200 Use [POLICY] to address disks and partitions.
9cc7ceec
AAF
201 POLICY can be any directory name found in /dev/disk
202 (e.g. "by-uuid", "by-label"), or "mapper" to use
203 /dev/mapper device names (default).
7c179686 204 --fstab Use /etc/fstab to determine the root device.
9bef7109 205 --add-fstab [FILE] Add file to the initramfs fstab.
70cb8a68
HH
206 --mount "[DEV] [MP] [FSTYPE] [FSOPTS]"
207 Mount device [DEV] on mountpoint [MP] with filesystem
9bef7109 208 [FSTYPE] and options [FSOPTS] in the initramfs.
cdc9cd0e 209 --mount "[MP]" Same as above, but [DEV], [FSTYPE] and [FSOPTS] are
9bef7109
AAF
210 determined by looking at the current mounts.
211 --add-device "[DEV]" Bring up [DEV] in initramfs.
5616feb0 212 -i, --include [SOURCE] [TARGET]
39ff0682
AT
213 Include the files in the SOURCE directory into the
214 Target directory in the final initramfs.
3b403b32 215 If SOURCE is a file, it will be installed to TARGET
4fea3ea6 216 in the final initramfs.
39ff0682
AT
217 -I, --install [LIST] Install the space separated list of files into the
218 initramfs.
9bef7109
AAF
219 --install-optional [LIST]
220 Install the space separated list of files into the
54b68829 221 initramfs, if they exist.
5b158ad3 222 --gzip Compress the generated initramfs using gzip.
cc02093d
HH
223 This will be done by default, unless another
224 compression option or --no-compress is passed.
5b158ad3 225 --bzip2 Compress the generated initramfs using bzip2.
cc02093d
HH
226 Make sure your kernel has bzip2 decompression support
227 compiled in, otherwise you will not be able to boot.
228 --lzma Compress the generated initramfs using lzma.
3b403b32 229 Make sure your kernel has lzma support compiled in,
cc02093d 230 otherwise you will not be able to boot.
5e6c3b03
VL
231 --xz Compress the generated initramfs using xz.
232 Make sure that your kernel has xz support compiled
233 in, otherwise you will not be able to boot.
37383f71 234 --lzo Compress the generated initramfs using lzop.
871c8e40 235 Make sure that your kernel has lzo support compiled
236 in, otherwise you will not be able to boot.
37383f71 237 --lz4 Compress the generated initramfs using lz4.
871c8e40 238 Make sure that your kernel has lz4 support compiled
239 in, otherwise you will not be able to boot.
7dbadcc7
TPG
240 --zstd Compress the generated initramfs using Zstandard.
241 Make sure that your kernel has zstd support compiled
242 in, otherwise you will not be able to boot.
9bef7109
AAF
243 --compress [COMPRESSION]
244 Compress the generated initramfs with the
5e6c3b03 245 passed compression program. Make sure your kernel
3b403b32 246 knows how to decompress the generated initramfs,
5e6c3b03 247 otherwise you will not be able to boot.
9bef7109 248 --no-compress Do not compress the generated initramfs. This will
cc02093d 249 override any other compression options.
9bef7109
AAF
250 --squash-compressor [COMPRESSION]
251 Specify the compressor and compressor specific options
252 used by mksquashfs if squash module is called when
253 building the initramfs.
afe4a6db 254 --enhanced-cpio Attempt to reflink cpio file data using dracut-cpio.
5b11bb73 255 --list-modules List all available dracut modules.
956af8f2
256 -M, --show-modules Print included module's name to standard output during
257 build.
9bef7109
AAF
258 --keep Keep the temporary initramfs for debugging purposes.
259 --printsize Print out the module install size.
260 --sshkey [SSHKEY] Add SSH key to initramfs (use with ssh-client module).
261 --logfile [FILE] Logfile to use (overrides configuration setting).
262 --reproducible Create reproducible images.
263 --no-reproducible Do not create reproducible images.
264 --loginstall [DIR] Log all files installed from the host to [DIR].
636d2d46 265 --uefi Create an UEFI executable with the kernel cmdline and
9bef7109
AAF
266 kernel combined.
267 --no-uefi Disables UEFI mode.
268 --no-machineid Affects the default output filename of the UEFI
269 executable, discarding the <MACHINE_ID> part.
270 --uefi-stub [FILE] Use the UEFI stub [FILE] to create an UEFI executable.
4237aeb0
DT
271 --uefi-splash-image [FILE]
272 Use [FILE] as a splash image when creating an UEFI
9bef7109
AAF
273 executable. Requires bitmap (.bmp) image format.
274 --kernel-image [FILE] Location of the kernel image.
fffeaded
EGE
275 --sbat [PARAMETERS] The SBAT parameters to be added to .sbat.
276 The string "sbat,1,SBAT Version,sbat,1,
277 https://github.com/rhboot/shim/blob/main/SBAT.md" is
278 already added by default.
cdc9cd0e 279 --regenerate-all Regenerate all initramfs images at the default location
9bef7109 280 for the kernel versions found on the system.
6d923262
MW
281 -p, --parallel Use parallel processing if possible (currently only
282 supported --regenerate-all)
283 images simultaneously.
9bef7109 284 --version Display version.
ccaa9bee
HH
285
286If [LIST] has multiple arguments, then you have to put these in quotes.
ffa71b4a 287
ccaa9bee 288For example:
ffa71b4a
HH
289
290 # dracut --add-drivers "module1 module2" ...
291
cc02093d 292EOF
5616feb0
AT
293}
294
ff2d8bdf 295long_version() {
7310a848 296 [[ $dracutbasedir ]] || dracutbasedir="$dracutsysrootdir"/usr/lib/dracut
ff2d8bdf 297 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
7310a848
HH
298 # shellcheck source=./dracut-version.sh
299 . "$dracutbasedir"/dracut-version.sh
ff2d8bdf
DM
300 fi
301 echo "dracut $DRACUT_VERSION"
302}
303
118ca9ec
TR
304# Fills up host_devs stack variable and makes sure there are no duplicates
305push_host_devs() {
306 local _dev
694725ab 307 for _dev in "$@"; do
7310a848 308 [[ " ${host_devs[*]} " == *" $_dev "* ]] && return
9a52c3fd 309 host_devs+=("$_dev")
694725ab 310 done
8466db96
HH
311}
312
5bc545ed
VL
313# Little helper function for reading args from the commandline.
314# it automatically handles -a b and -a=b variants, and returns 1 if
315# we need to shift $3.
316read_arg() {
317 # $1 = arg name
318 # $2 = arg value
319 # $3 = arg parameter
320 local rematch='^[^=]*=(.*)$'
321 if [[ $2 =~ $rematch ]]; then
7310a848 322 read -r "$1" <<< "${BASH_REMATCH[1]}"
5bc545ed 323 else
7310a848 324 read -r "$1" <<< "$3"
cc02093d
HH
325 # There is no way to shift our callers args, so
326 # return 1 to indicate they should do it instead.
327 return 1
5bc545ed
VL
328 fi
329}
330
9a52c3fd 331check_conf_file() {
dfe2247a 332 if grep -H -e '^[^#]*[+]=\("[^ ]\|.*[^ ]"\)' "$@"; then
ae88e029
AAF
333 printf '\ndracut[W]: <key>+=" <values> ": <values> should have surrounding white spaces!\n' >&2
334 printf 'dracut[W]: This will lead to unwanted side effects! Please fix the configuration file.\n\n' >&2
dfe2247a
HH
335 fi
336}
337
9a52c3fd
HH
338dropindirs_sort() {
339 local suffix=$1
340 shift
b093aa2d
HH
341 local -a files
342 local f d
343
dba20559
HH
344 for d in "$@"; do
345 for i in "$d/"*"$suffix"; do
75d758e8 346 if [[ -e $i ]]; then
dba20559
HH
347 printf "%s\n" "${i##*/}"
348 fi
349 done
350 done | sort -Vu | {
351 readarray -t files
352
353 for f in "${files[@]}"; do
354 for d in "$@"; do
355 if [[ -e "$d/$f" ]]; then
356 printf "%s\n" "$d/$f"
357 continue 2
b093aa2d 358 fi
a42b2b81 359 done
a42b2b81 360 done
dba20559 361 }
a42b2b81
HH
362}
363
9a52c3fd 364rearrange_params() {
659dc319 365 # Workaround -i, --include taking 2 arguments
dcacd2b0
FV
366 newat=()
367 for i in "$@"; do
9a52c3fd 368 if [[ $i == "-i" ]] || [[ $i == "--include" ]]; then
dcacd2b0
FV
369 newat+=("++include") # Replace --include by ++include
370 else
371 newat+=("$i")
372 fi
373 done
374 set -- "${newat[@]}" # Set new $@
659dc319 375
9a52c3fd
HH
376 TEMP=$(
377 unset POSIXLY_CORRECT
378 getopt \
6d923262 379 -o "a:m:o:d:I:k:c:r:L:fvqlHhMNp" \
9a52c3fd
HH
380 --long kver: \
381 --long add: \
382 --long force-add: \
383 --long add-drivers: \
384 --long force-drivers: \
385 --long omit-drivers: \
386 --long modules: \
387 --long omit: \
388 --long drivers: \
389 --long filesystems: \
390 --long install: \
391 --long install-optional: \
392 --long fwdir: \
393 --long libdirs: \
394 --long fscks: \
395 --long add-fstab: \
396 --long mount: \
397 --long device: \
398 --long add-device: \
399 --long nofscks \
400 --long ro-mnt \
401 --long kmoddir: \
402 --long conf: \
403 --long confdir: \
404 --long tmpdir: \
405 --long sysroot: \
406 --long stdlog: \
407 --long compress: \
90d9ae8c 408 --long squash-compressor: \
9a52c3fd
HH
409 --long prefix: \
410 --long rebuild: \
411 --long force \
412 --long kernel-only \
413 --long no-kernel \
414 --long print-cmdline \
415 --long kernel-cmdline: \
416 --long strip \
e4f1dbcc 417 --long aggressive-strip \
9a52c3fd
HH
418 --long nostrip \
419 --long hardlink \
420 --long nohardlink \
421 --long noprefix \
422 --long mdadmconf \
423 --long nomdadmconf \
424 --long lvmconf \
425 --long nolvmconf \
426 --long debug \
427 --long profile \
428 --long sshkey: \
429 --long logfile: \
430 --long verbose \
431 --long quiet \
432 --long local \
433 --long hostonly \
434 --long host-only \
435 --long no-hostonly \
436 --long no-host-only \
437 --long hostonly-mode: \
438 --long hostonly-cmdline \
439 --long no-hostonly-cmdline \
440 --long no-hostonly-default-device \
441 --long persistent-policy: \
442 --long fstab \
443 --long help \
444 --long bzip2 \
445 --long lzma \
446 --long xz \
447 --long lzo \
448 --long lz4 \
449 --long zstd \
450 --long no-compress \
451 --long gzip \
afe4a6db 452 --long enhanced-cpio \
9a52c3fd
HH
453 --long list-modules \
454 --long show-modules \
455 --long keep \
456 --long printsize \
457 --long regenerate-all \
6d923262 458 --long parallel \
9a52c3fd
HH
459 --long noimageifnotneeded \
460 --long early-microcode \
461 --long no-early-microcode \
462 --long reproducible \
463 --long no-reproducible \
464 --long loginstall: \
465 --long uefi \
466 --long no-uefi \
467 --long uefi-stub: \
468 --long uefi-splash-image: \
469 --long kernel-image: \
fffeaded 470 --long sbat: \
9a52c3fd
HH
471 --long no-hostonly-i18n \
472 --long hostonly-i18n \
473 --long hostonly-nics: \
474 --long no-machineid \
475 --long version \
476 -- "$@"
477 )
659dc319 478
7310a848 479 # shellcheck disable=SC2181
9a52c3fd 480 if (($? != 0)); then
659dc319
HB
481 usage
482 exit 1
483 fi
484}
485
432196ae 486verbosity_mod_l=0
f79e587c
HH
487unset kernel
488unset outfile
486a1b93 489
659dc319
HB
490rearrange_params "$@"
491eval set -- "$TEMP"
492
493# parse command line args to check if '--rebuild' option is present
494unset append_args_l
495unset rebuild_file
9a52c3fd 496while :; do
497916e1 497 if [ "$1" == "--" ]; then
9a52c3fd
HH
498 shift
499 break
497916e1
ÉR
500 fi
501 if [ "$1" == "--rebuild" ]; then
502 append_args_l="yes"
9a52c3fd
HH
503 rebuild_file="$2"
504 if [ ! -e "$rebuild_file" ]; then
505 echo "Image file '$rebuild_file', for rebuild, does not exist!"
506 exit 1
507 fi
508 abs_rebuild_file=$(readlink -f "$rebuild_file") && rebuild_file="$abs_rebuild_file"
509 shift
510 continue
497916e1
ÉR
511 fi
512 shift
659dc319
HB
513done
514
515# get output file name and kernel version from command line arguments
516while (($# > 0)); do
517 case ${1%%=*} in
518 ++include)
9a52c3fd
HH
519 shift 2
520 ;;
659dc319
HB
521 *)
522 if ! [[ ${outfile+x} ]]; then
523 outfile=$1
524 elif ! [[ ${kernel+x} ]]; then
525 kernel=$1
526 else
527 printf "\nUnknown arguments: %s\n\n" "$*" >&2
9a52c3fd
HH
528 usage
529 exit 1
659dc319
HB
530 fi
531 ;;
532 esac
533 shift
534done
535
536# extract input image file provided with rebuild option to get previous parameters, if any
537if [[ $append_args_l == "yes" ]]; then
538 unset rebuild_param
539
540 # determine resultant file
541 if ! [[ $outfile ]]; then
542 outfile=$rebuild_file
543 fi
544
7310a848 545 if ! rebuild_param=$(lsinitrd "$rebuild_file" '*lib/dracut/build-parameter.txt'); then
659dc319
HB
546 echo "Image '$rebuild_file' has no rebuild information stored"
547 exit 1
548 fi
549
550 # prepend previous parameters to current command line args
551 if [[ $rebuild_param ]]; then
552 TEMP="$rebuild_param $TEMP"
553 eval set -- "$TEMP"
554 rearrange_params "$@"
555 fi
ffa71b4a
HH
556fi
557
659dc319
HB
558unset PARMS_TO_STORE
559PARMS_TO_STORE=""
560
ffa71b4a
HH
561eval set -- "$TEMP"
562
563while :; do
7310a848 564 if [[ $1 != "--" ]] && [[ $1 != "--rebuild" ]]; then
9a52c3fd 565 PARMS_TO_STORE+=" $1"
659dc319 566 fi
ffa71b4a 567 case $1 in
9a52c3fd
HH
568 --kver)
569 kernel="$2"
570 PARMS_TO_STORE+=" '$2'"
571 shift
572 ;;
573 -a | --add)
574 add_dracutmodules_l+=("$2")
575 PARMS_TO_STORE+=" '$2'"
576 shift
577 ;;
578 --force-add)
579 force_add_dracutmodules_l+=("$2")
580 PARMS_TO_STORE+=" '$2'"
581 shift
582 ;;
583 --add-drivers)
584 add_drivers_l+=("$2")
585 PARMS_TO_STORE+=" '$2'"
586 shift
587 ;;
588 --force-drivers)
589 force_drivers_l+=("$2")
590 PARMS_TO_STORE+=" '$2'"
591 shift
592 ;;
593 --omit-drivers)
594 omit_drivers_l+=("$2")
595 PARMS_TO_STORE+=" '$2'"
596 shift
597 ;;
598 -m | --modules)
599 dracutmodules_l+=("$2")
600 PARMS_TO_STORE+=" '$2'"
601 shift
602 ;;
603 -o | --omit)
604 omit_dracutmodules_l+=("$2")
605 PARMS_TO_STORE+=" '$2'"
606 shift
607 ;;
608 -d | --drivers)
609 drivers_l+=("$2")
610 PARMS_TO_STORE+=" '$2'"
611 shift
612 ;;
613 --filesystems)
614 filesystems_l+=("$2")
615 PARMS_TO_STORE+=" '$2'"
616 shift
617 ;;
618 -I | --install)
619 install_items_l+=("$2")
620 PARMS_TO_STORE+=" '$2'"
621 shift
622 ;;
623 --install-optional)
624 install_optional_items_l+=("$2")
625 PARMS_TO_STORE+=" '$2'"
626 shift
627 ;;
628 --fwdir)
629 fw_dir_l+=("$2")
630 PARMS_TO_STORE+=" '$2'"
631 shift
632 ;;
633 --libdirs)
634 libdirs_l+=("$2")
635 PARMS_TO_STORE+=" '$2'"
636 shift
637 ;;
638 --fscks)
639 fscks_l+=("$2")
640 PARMS_TO_STORE+=" '$2'"
641 shift
642 ;;
643 --add-fstab)
644 add_fstab_l+=("$2")
645 PARMS_TO_STORE+=" '$2'"
646 shift
647 ;;
648 --mount)
649 fstab_lines+=("$2")
650 PARMS_TO_STORE+=" '$2'"
651 shift
652 ;;
653 --add-device | --device)
654 add_device_l+=("$2")
655 PARMS_TO_STORE+=" '$2'"
656 shift
657 ;;
658 --kernel-cmdline)
659 kernel_cmdline_l+=("$2")
660 PARMS_TO_STORE+=" '$2'"
661 shift
662 ;;
663 --nofscks) nofscks_l="yes" ;;
664 --ro-mnt) ro_mnt_l="yes" ;;
665 -k | --kmoddir)
666 drivers_dir_l="$2"
667 PARMS_TO_STORE+=" '$2'"
668 shift
669 ;;
670 -c | --conf)
671 conffile="$2"
672 PARMS_TO_STORE+=" '$2'"
673 shift
674 ;;
675 --confdir)
676 confdir="$2"
677 PARMS_TO_STORE+=" '$2'"
678 shift
679 ;;
680 --tmpdir)
681 tmpdir_l="$2"
682 PARMS_TO_STORE+=" '$2'"
683 shift
684 ;;
685 -r | --sysroot)
686 sysroot_l="$2"
687 PARMS_TO_STORE+=" '$2'"
688 shift
689 ;;
690 -L | --stdlog)
691 stdloglvl_l="$2"
692 PARMS_TO_STORE+=" '$2'"
693 shift
694 ;;
695 --compress)
696 compress_l="$2"
697 PARMS_TO_STORE+=" '$2'"
698 shift
699 ;;
90d9ae8c
TL
700 --squash-compressor)
701 squash_compress_l="$2"
702 PARMS_TO_STORE+=" '$2'"
703 shift
704 ;;
9a52c3fd
HH
705 --prefix)
706 prefix_l="$2"
707 PARMS_TO_STORE+=" '$2'"
708 shift
709 ;;
710 --loginstall)
711 loginstall_l="$2"
712 PARMS_TO_STORE+=" '$2'"
713 shift
714 ;;
715 --rebuild)
716 if [[ $rebuild_file == "$outfile" ]]; then
717 force=yes
718 fi
719 shift
720 ;;
721 -f | --force) force=yes ;;
722 --kernel-only)
723 kernel_only="yes"
724 no_kernel="no"
725 ;;
726 --no-kernel)
727 kernel_only="no"
728 no_kernel="yes"
729 ;;
ab9457ef 730 --print-cmdline)
9a52c3fd
HH
731 print_cmdline="yes"
732 hostonly_l="yes"
733 kernel_only="yes"
734 no_kernel="yes"
735 ;;
ab9457ef 736 --early-microcode)
9a52c3fd
HH
737 early_microcode_l="yes"
738 ;;
ab9457ef 739 --no-early-microcode)
9a52c3fd
HH
740 early_microcode_l="no"
741 ;;
742 --strip) do_strip_l="yes" ;;
e4f1dbcc 743 --aggressive-strip) aggressive_strip_l="yes" ;;
9a52c3fd
HH
744 --nostrip) do_strip_l="no" ;;
745 --hardlink) do_hardlink_l="yes" ;;
746 --nohardlink) do_hardlink_l="no" ;;
747 --noprefix) prefix_l="/" ;;
748 --mdadmconf) mdadmconf_l="yes" ;;
749 --nomdadmconf) mdadmconf_l="no" ;;
750 --lvmconf) lvmconf_l="yes" ;;
751 --nolvmconf) lvmconf_l="no" ;;
752 --debug) debug="yes" ;;
753 --profile) profile="yes" ;;
754 --sshkey)
755 sshkey="$2"
756 PARMS_TO_STORE+=" '$2'"
757 shift
758 ;;
759 --logfile)
760 logfile_l="$2"
761 shift
762 ;;
763 -v | --verbose) ((verbosity_mod_l++)) ;;
764 -q | --quiet) ((verbosity_mod_l--)) ;;
765 -l | --local)
766 allowlocal="yes"
767 [[ -f "$(readlink -f "${0%/*}")/dracut-init.sh" ]] \
768 && dracutbasedir="$(readlink -f "${0%/*}")"
769 ;;
770 -H | --hostonly | --host-only)
771 hostonly_l="yes"
772 ;;
773 -N | --no-hostonly | --no-host-only)
774 hostonly_l="no"
775 ;;
a695250e 776 --hostonly-mode)
9a52c3fd
HH
777 hostonly_mode_l="$2"
778 PARMS_TO_STORE+=" '$2'"
779 shift
780 ;;
ab9457ef 781 --hostonly-cmdline)
9a52c3fd
HH
782 hostonly_cmdline_l="yes"
783 ;;
b73e00af 784 --hostonly-i18n)
9a52c3fd
HH
785 i18n_install_all_l="no"
786 ;;
1e92f728 787 --hostonly-nics)
9a52c3fd
HH
788 hostonly_nics_l+=("$2")
789 PARMS_TO_STORE+=" '$2'"
790 shift
791 ;;
b73e00af 792 --no-hostonly-i18n)
9a52c3fd
HH
793 i18n_install_all_l="yes"
794 ;;
ab9457ef 795 --no-hostonly-cmdline)
9a52c3fd
HH
796 hostonly_cmdline_l="no"
797 ;;
37f2fe55 798 --no-hostonly-default-device)
9a52c3fd
HH
799 hostonly_default_device="no"
800 ;;
324ea606 801 --persistent-policy)
9a52c3fd
HH
802 persistent_policy_l="$2"
803 PARMS_TO_STORE+=" '$2'"
804 shift
805 ;;
806 --fstab) use_fstab_l="yes" ;;
807 -h | --help)
808 long_usage
cda6b00a 809 exit 0
9a52c3fd
HH
810 ;;
811 --bzip2) compress_l="bzip2" ;;
812 --lzma) compress_l="lzma" ;;
813 --xz) compress_l="xz" ;;
b2d7561b 814 --lzo) compress_l="lzop" ;;
9a52c3fd
HH
815 --lz4) compress_l="lz4" ;;
816 --zstd) compress_l="zstd" ;;
817 --no-compress) _no_compress_l="cat" ;;
818 --gzip) compress_l="gzip" ;;
afe4a6db 819 --enhanced-cpio) enhanced_cpio_l="yes" ;;
9a52c3fd
HH
820 --list-modules) do_list="yes" ;;
821 -M | --show-modules)
822 show_modules_l="yes"
823 ;;
824 --keep) keep="yes" ;;
825 --printsize) printsize="yes" ;;
6d923262
MW
826 --regenerate-all) regenerate_all_l="yes" ;;
827 -p | --parallel) parallel_l="yes" ;;
9a52c3fd
HH
828 --noimageifnotneeded) noimageifnotneeded="yes" ;;
829 --reproducible) reproducible_l="yes" ;;
830 --no-reproducible) reproducible_l="no" ;;
831 --uefi) uefi_l="yes" ;;
832 --no-uefi) uefi_l="no" ;;
636d2d46 833 --uefi-stub)
9a52c3fd
HH
834 uefi_stub_l="$2"
835 PARMS_TO_STORE+=" '$2'"
836 shift
837 ;;
4237aeb0 838 --uefi-splash-image)
9a52c3fd
HH
839 uefi_splash_image_l="$2"
840 PARMS_TO_STORE+=" '$2'"
841 shift
842 ;;
636d2d46 843 --kernel-image)
9a52c3fd
HH
844 kernel_image_l="$2"
845 PARMS_TO_STORE+=" '$2'"
846 shift
847 ;;
fffeaded
EGE
848 --sbat)
849 sbat_l="$2"
850 PARMS_TO_STORE+=" '$2'"
851 shift
852 ;;
5c57209b 853 --no-machineid)
9a52c3fd
HH
854 machine_id_l="no"
855 ;;
856 --version)
857 long_version
cda6b00a 858 exit 0
9a52c3fd
HH
859 ;;
860 --)
861 shift
862 break
863 ;;
ffa71b4a 864
9a52c3fd
HH
865 *) # should not even reach this point
866 printf "\n!Unknown option: '%s'\n\n" "$1" >&2
867 usage
868 exit 1
869 ;;
ffa71b4a
HH
870 esac
871 shift
872done
873
874# getopt cannot handle multiple arguments, so just handle "-I,--include"
875# the old fashioned way
876
877while (($# > 0)); do
4eec1a22 878 if [ "${1%%=*}" == "++include" ]; then
5a66d511
HH
879 include_src+=("$2")
880 include_target+=("$3")
659dc319
HB
881 PARMS_TO_STORE+=" --include '$2' '$3'"
882 shift 2
883 fi
b368a5f3 884 shift
641cc356 885done
ffa71b4a 886
b552d364
BZ
887[[ $sysroot_l ]] && dracutsysrootdir="$sysroot_l"
888
02b5c8e3
HH
889export LC_ALL=C
890export LANG=C
8a32bf61
HH
891unset LC_MESSAGES
892unset LC_CTYPE
d619fb5e 893unset LD_LIBRARY_PATH
45404a2a 894unset LD_PRELOAD
1fcf2d2f 895unset GREP_OPTIONS
a55711cd 896
89d44e72 897export DRACUT_LOG_LEVEL=warning
3b403b32 898[[ $debug ]] && {
89d44e72 899 export DRACUT_LOG_LEVEL=debug
22a80629 900 export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]-}): '
c36ce334
VL
901 set -x
902}
903
57258a2c 904[[ $profile ]] && {
9a52c3fd 905 export PS4='+ $(date "+%s.%N") ${BASH_SOURCE}@${LINENO}: '
57258a2c
HH
906 set -x
907 debug=yes
908}
909
6dc1cd05 910[[ $dracutbasedir ]] || dracutbasedir="$dracutsysrootdir"/usr/lib/dracut
5d791c0e 911
f1336ac7 912# if we were not passed a config file, try the default one
7de9ffc0 913if [[ -z $conffile ]]; then
b093aa2d
HH
914 if [[ $allowlocal ]]; then
915 conffile="$dracutbasedir/dracut.conf"
916 else
a0120420 917 conffile="$dracutsysrootdir/etc/dracut.conf"
b093aa2d 918 fi
b72d0d7f 919elif [[ ! -e $conffile ]]; then
ae88e029 920 printf "%s\n" "dracut[F]: Configuration file '$conffile' not found." >&2
7de9ffc0 921 exit 1
42c71947 922fi
f1336ac7 923
7de9ffc0 924if [[ -z $confdir ]]; then
b093aa2d
HH
925 if [[ $allowlocal ]]; then
926 confdir="$dracutbasedir/dracut.conf.d"
927 else
a0120420 928 confdir="$dracutsysrootdir/etc/dracut.conf.d"
b093aa2d 929 fi
7de9ffc0 930elif [[ ! -d $confdir ]]; then
ae88e029 931 printf "%s\n" "dracut[F]: Configuration directory '$confdir' not found." >&2
7de9ffc0 932 exit 1
2c2c4580
HH
933fi
934
2245f372 935# source our config file
dfe2247a
HH
936if [[ -f $conffile ]]; then
937 check_conf_file "$conffile"
6dc1cd05 938 # shellcheck disable=SC1090
dfe2247a
HH
939 . "$conffile"
940fi
2245f372 941
2c2c4580 942# source our config dir
335bc217 943for f in $(dropindirs_sort ".conf" "$confdir" "$dracutbasedir/dracut.conf.d"); do
dfe2247a 944 check_conf_file "$f"
6dc1cd05 945 # shellcheck disable=SC1090
a42b2b81
HH
946 [[ -e $f ]] && . "$f"
947done
2c2c4580 948
6d923262
MW
949# regenerate_all shouldn't be set in conf files
950regenerate_all=$regenerate_all_l
951if [[ $parallel_l == "yes" ]]; then
952 parallel=yes
953fi
954
955if [[ $regenerate_all == "yes" ]]; then
956 ret=0
957 if [[ $kernel ]]; then
ae88e029 958 printf "%s\n" "dracut[F]: --regenerate-all cannot be called with a kernel version." >&2
6d923262
MW
959 exit 1
960 fi
961
962 if [[ $outfile ]]; then
ae88e029 963 printf "%s\n" "dracut[F]: --regenerate-all cannot be called with an image file." >&2
6d923262
MW
964 exit 1
965 fi
966
967 ((len = ${#dracut_args[@]}))
968 for ((i = 0; i < len; i++)); do
969 case ${dracut_args[$i]} in
970 --regenerate-all | --parallel)
08b63a25 971 # shellcheck disable=SC2184
6d923262
MW
972 unset dracut_args["$i"]
973 ;;
974 esac
975 done
976
977 cd "$dracutsysrootdir"/lib/modules || exit 1
978 if [[ $parallel != "yes" ]]; then
979 for i in *; do
980 [[ -f $i/modules.dep ]] || [[ -f $i/modules.dep.bin ]] || continue
981 "$dracut_cmd" --kver="$i" "${dracut_args[@]}"
982 ((ret += $?))
983 done
984 else
985 for i in *; do
986 [[ -f $i/modules.dep ]] || [[ -f $i/modules.dep.bin ]] || continue
987 "$dracut_cmd" --kver="$i" "${dracut_args[@]}" &
988 done
989 while true; do
990 wait -n
991 wst=$?
992 if [[ $wst == 127 ]]; then
993 break
994 else
995 ((ret += wst))
996 fi
997 done
998 fi
999 exit "$ret"
1000fi
1001
1002if ! [[ $kernel ]]; then
1003 kernel=$(uname -r)
1004fi
1005
5f2baf7d 1006DRACUT_PATH=${DRACUT_PATH:-/sbin /bin /usr/sbin /usr/bin}
eab9b75c
HH
1007
1008for i in $DRACUT_PATH; do
1009 rl=$i
a0120420 1010 if [ -L "$dracutsysrootdir$i" ]; then
6dc1cd05 1011 rl=$(readlink -f "$dracutsysrootdir$i")
eab9b75c 1012 fi
08b63a25 1013 rl="${rl#"$dracutsysrootdir"}"
75d758e8 1014 if [[ $NPATH != *:$rl* ]]; then
eab9b75c
HH
1015 NPATH+=":$rl"
1016 fi
1017done
75d758e8 1018[[ -z $dracutsysrootdir ]] && export PATH="${NPATH#:}"
eab9b75c
HH
1019unset NPATH
1020
18d36fab
ZB
1021export SYSTEMCTL=${SYSTEMCTL:-systemctl}
1022
cb0913db 1023# these options add to the stuff in the config file
9a52c3fd 1024((${#add_dracutmodules_l[@]})) && add_dracutmodules+=" ${add_dracutmodules_l[*]} "
96c31333 1025((${#omit_dracutmodules_l[@]})) && omit_dracutmodules+=" ${omit_dracutmodules_l[*]} "
9a52c3fd
HH
1026((${#force_add_dracutmodules_l[@]})) && force_add_dracutmodules+=" ${force_add_dracutmodules_l[*]} "
1027((${#fscks_l[@]})) && fscks+=" ${fscks_l[*]} "
1028((${#add_fstab_l[@]})) && add_fstab+=" ${add_fstab_l[*]} "
1029((${#install_items_l[@]})) && install_items+=" ${install_items_l[*]} "
1030((${#install_optional_items_l[@]})) && install_optional_items+=" ${install_optional_items_l[*]} "
1031((${#hostonly_nics_l[@]})) && hostonly_nics+=" ${hostonly_nics_l[*]} "
54b68829 1032
f1336ac7 1033# these options override the stuff in the config file
9a52c3fd 1034((${#dracutmodules_l[@]})) && dracutmodules="${dracutmodules_l[*]}"
9a52c3fd
HH
1035((${#filesystems_l[@]})) && filesystems="${filesystems_l[*]}"
1036((${#fw_dir_l[@]})) && fw_dir="${fw_dir_l[*]}"
1037((${#libdirs_l[@]})) && libdirs="${libdirs_l[*]}"
c9143a63 1038
e103615b 1039[[ $stdloglvl_l ]] && stdloglvl=$stdloglvl_l
b991617f 1040[[ ! $stdloglvl ]] && stdloglvl=4
432196ae
1041stdloglvl=$((stdloglvl + verbosity_mod_l))
1042((stdloglvl > 6)) && stdloglvl=6
1043((stdloglvl < 0)) && stdloglvl=0
1044
26537a5b 1045[[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
1ddcb137 1046drivers_dir="${drivers_dir%"${drivers_dir##*[!/]}"}"
31f7db66 1047[[ $do_strip_l ]] && do_strip=$do_strip_l
6c128565 1048[[ $do_strip ]] || do_strip=yes
e4f1dbcc 1049[[ $aggressive_strip_l ]] && aggressive_strip=$aggressive_strip_l
04d18f55
HH
1050[[ $do_hardlink_l ]] && do_hardlink=$do_hardlink_l
1051[[ $do_hardlink ]] || do_hardlink=yes
fd786adc 1052[[ $prefix_l ]] && prefix=$prefix_l
75d758e8 1053[[ $prefix == "/" ]] && unset prefix
ba726310 1054[[ $hostonly_l ]] && hostonly=$hostonly_l
ab9457ef 1055[[ $hostonly_cmdline_l ]] && hostonly_cmdline=$hostonly_cmdline_l
a695250e 1056[[ $hostonly_mode_l ]] && hostonly_mode=$hostonly_mode_l
75d758e8 1057[[ $hostonly == "yes" ]] && ! [[ $hostonly_cmdline ]] && hostonly_cmdline="yes"
6dc1cd05 1058# shellcheck disable=SC2034
b73e00af 1059[[ $i18n_install_all_l ]] && i18n_install_all=$i18n_install_all_l
6dc1cd05 1060# shellcheck disable=SC2034
3411ad66 1061[[ $persistent_policy_l ]] && persistent_policy=$persistent_policy_l
7c179686 1062[[ $use_fstab_l ]] && use_fstab=$use_fstab_l
2f02ae9d 1063[[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
7a34efa5 1064[[ $lvmconf_l ]] && lvmconf=$lvmconf_l
6dc1cd05 1065[[ $dracutbasedir ]] || dracutbasedir="$dracutsysrootdir"/usr/lib/dracut
95aeed89 1066[[ $fw_dir ]] || {
1067 fw_path_para=$(< /sys/module/firmware_class/parameters/path)
1068 fw_dir="${fw_path_para:+$dracutsysrootdir$fw_path_para:}$dracutsysrootdir/lib/firmware/updates/$kernel:$dracutsysrootdir/lib/firmware/updates:$dracutsysrootdir/lib/firmware/$kernel:$dracutsysrootdir/lib/firmware"
1069}
882c4c5a 1070[[ $tmpdir_l ]] && tmpdir="$tmpdir_l"
97a931c7 1071[[ $tmpdir ]] || tmpdir="$TMPDIR"
6dc1cd05 1072[[ $tmpdir ]] || tmpdir="$dracutsysrootdir"/var/tmp
871c8e40 1073[[ $INITRD_COMPRESS ]] && compress=$INITRD_COMPRESS
3a714439 1074[[ $compress_l ]] && compress=$compress_l
90d9ae8c 1075[[ $squash_compress_l ]] && squash_compress=$squash_compress_l
afe4a6db 1076[[ $enhanced_cpio_l ]] && enhanced_cpio=$enhanced_cpio_l
956af8f2 1077[[ $show_modules_l ]] && show_modules=$show_modules_l
25b45979 1078[[ $nofscks_l ]] && nofscks="yes"
ff3953ef 1079[[ $ro_mnt_l ]] && ro_mnt="yes"
b7c770e1 1080[[ $early_microcode_l ]] && early_microcode=$early_microcode_l
b25ef46f 1081[[ $early_microcode ]] || early_microcode=yes
d613d88d 1082[[ $early_microcode_image_dir ]] || early_microcode_image_dir=('/boot')
9a52c3fd
HH
1083[[ $early_microcode_image_name ]] \
1084 || early_microcode_image_name=('intel-uc.img' 'intel-ucode.img' 'amd-uc.img' 'amd-ucode.img' 'early_ucode.cpio' 'microcode.cpio')
ee54b840 1085[[ $logfile_l ]] && logfile="$logfile_l"
80626ded 1086[[ $reproducible_l ]] && reproducible="$reproducible_l"
37383f71 1087[[ $loginstall_l ]] && loginstall="$loginstall_l"
f3cbff11 1088[[ $uefi_l ]] && uefi=$uefi_l
636d2d46 1089[[ $uefi_stub_l ]] && uefi_stub="$uefi_stub_l"
4237aeb0 1090[[ $uefi_splash_image_l ]] && uefi_splash_image="$uefi_splash_image_l"
636d2d46 1091[[ $kernel_image_l ]] && kernel_image="$kernel_image_l"
fffeaded 1092[[ $sbat_l ]] && sbat="$sbat_l"
5c57209b
HH
1093[[ $machine_id_l ]] && machine_id="$machine_id_l"
1094
1095if ! [[ $outfile ]]; then
1096 if [[ $machine_id != "no" ]]; then
28ef3bc6
AAF
1097 if [[ -d "$dracutsysrootdir"/efi/Default ]] \
1098 || [[ -d "$dracutsysrootdir"/boot/Default ]] \
1099 || [[ -d "$dracutsysrootdir"/boot/efi/Default ]]; then
1100 MACHINE_ID="Default"
97fe0976 1101 elif [[ -s "$dracutsysrootdir"/etc/machine-id ]]; then
28ef3bc6 1102 read -r MACHINE_ID < "$dracutsysrootdir"/etc/machine-id
97fe0976 1103 [[ $MACHINE_ID == "uninitialized" ]] && MACHINE_ID="Default"
28ef3bc6
AAF
1104 else
1105 MACHINE_ID="Default"
1106 fi
5c57209b
HH
1107 fi
1108
1109 if [[ $uefi == "yes" ]]; then
6dc1cd05 1110 # shellcheck disable=SC2154
75d758e8 1111 if [[ -n $uefi_secureboot_key && -z $uefi_secureboot_cert ]] || [[ -z $uefi_secureboot_key && -n $uefi_secureboot_cert ]]; then
ae88e029 1112 printf "%s\n" "dracut[F]: Need 'uefi_secureboot_key' and 'uefi_secureboot_cert' both to be set." >&2
5a962b59
MR
1113 exit 1
1114 fi
1115
75d758e8 1116 if [[ -n $uefi_secureboot_key && -n $uefi_secureboot_cert ]] && ! command -v sbsign &> /dev/null; then
ae88e029 1117 printf "%s\n" "dracut[F]: Need 'sbsign' to create a signed UEFI executable." >&2
5a962b59
MR
1118 exit 1
1119 fi
1120
6dc1cd05 1121 BUILD_ID=$(cat "$dracutsysrootdir"/etc/os-release "$dracutsysrootdir"/usr/lib/os-release \
9a52c3fd
HH
1122 | while read -r line || [[ $line ]]; do
1123 [[ $line =~ BUILD_ID\=* ]] && eval "$line" && echo "$BUILD_ID" && break
1124 done)
3a12309c
DM
1125 if [[ -z $dracutsysrootdir ]]; then
1126 if [[ -d /efi ]] && mountpoint -q /efi; then
1127 efidir=/efi/EFI
a0120420
BZ
1128 else
1129 efidir=/boot/EFI
28ef3bc6 1130 if [[ -d /boot/efi/EFI ]]; then
a0120420
BZ
1131 efidir=/boot/efi/EFI
1132 fi
1133 fi
5c57209b 1134 else
3a12309c
DM
1135 efidir=/boot/EFI
1136 if [[ -d $dracutsysrootdir/boot/efi/EFI ]]; then
1137 efidir=/boot/efi/EFI
1138 fi
5c57209b 1139 fi
3a12309c
DM
1140 mkdir -p "$dracutsysrootdir$efidir/Linux"
1141 outfile="$dracutsysrootdir$efidir/Linux/linux-$kernel${MACHINE_ID:+-${MACHINE_ID}}${BUILD_ID:+-${BUILD_ID}}.efi"
5c57209b 1142 else
28ef3bc6
AAF
1143 if [[ -d "$dracutsysrootdir"/efi/loader/entries || -L "$dracutsysrootdir"/efi/loader/entries ]] \
1144 && [[ $MACHINE_ID ]] \
1145 && [[ -d "$dracutsysrootdir"/efi/${MACHINE_ID} || -L "$dracutsysrootdir"/efi/${MACHINE_ID} ]]; then
1146 outfile="$dracutsysrootdir/efi/${MACHINE_ID}/${kernel}/initrd"
1147 elif [[ -d "$dracutsysrootdir"/boot/loader/entries || -L "$dracutsysrootdir"/boot/loader/entries ]] \
1148 && [[ $MACHINE_ID ]] \
1149 && [[ -d "$dracutsysrootdir"/boot/${MACHINE_ID} || -L "$dracutsysrootdir"/boot/${MACHINE_ID} ]]; then
1150 outfile="$dracutsysrootdir/boot/${MACHINE_ID}/${kernel}/initrd"
1151 elif [[ -d "$dracutsysrootdir"/boot/efi/loader/entries || -L "$dracutsysrootdir"/boot/efi/loader/entries ]] \
1152 && [[ $MACHINE_ID ]] \
1153 && [[ -d "$dracutsysrootdir"/boot/efi/${MACHINE_ID} || -L "$dracutsysrootdir"/boot/efi/${MACHINE_ID} ]]; then
1154 outfile="$dracutsysrootdir/boot/efi/${MACHINE_ID}/${kernel}/initrd"
1155 elif [[ -f "$dracutsysrootdir"/lib/modules/${kernel}/initrd ]]; then
1156 outfile="$dracutsysrootdir/lib/modules/${kernel}/initrd"
1157 elif [[ -e $dracutsysrootdir/boot/vmlinuz-${kernel} ]]; then
1158 outfile="$dracutsysrootdir/boot/initramfs-${kernel}.img"
527fdfa1
AAF
1159 elif [[ -z $dracutsysrootdir ]] \
1160 && [[ $MACHINE_ID ]] \
1161 && mountpoint -q /efi; then
28ef3bc6 1162 outfile="/efi/${MACHINE_ID}/${kernel}/initrd"
527fdfa1
AAF
1163 elif [[ -z $dracutsysrootdir ]] \
1164 && [[ $MACHINE_ID ]] \
1165 && mountpoint -q /boot/efi; then
28ef3bc6 1166 outfile="/boot/efi/${MACHINE_ID}/${kernel}/initrd"
5c57209b 1167 else
28ef3bc6 1168 outfile="$dracutsysrootdir/boot/initramfs-${kernel}.img"
5c57209b
HH
1169 fi
1170 fi
1171fi
80626ded 1172
ddfd1d10 1173# eliminate IFS hackery when messing with fw_dir
794b2d2c 1174export DRACUT_FIRMWARE_PATH=${fw_dir// /:}
ddfd1d10 1175fw_dir=${fw_dir//:/ }
9a8a00cf 1176
3838dc47 1177# check for logfile and try to create one if it doesn't exist
75d758e8
HH
1178if [[ -n $logfile ]]; then
1179 if [[ ! -f $logfile ]]; then
6dc1cd05 1180 if touch "$logfile"; then
ae88e029 1181 printf "%s\n" "dracut[W]: touch $logfile failed." >&2
3838dc47 1182 fi
1183 fi
1184fi
1185
3a714439 1186# handle compression options.
a0120420
BZ
1187DRACUT_COMPRESS_BZIP2=${DRACUT_COMPRESS_BZIP2:-bzip2}
1188DRACUT_COMPRESS_LBZIP2=${DRACUT_COMPRESS_LBZIP2:-lbzip2}
1189DRACUT_COMPRESS_LZMA=${DRACUT_COMPRESS_LZMA:-lzma}
1190DRACUT_COMPRESS_XZ=${DRACUT_COMPRESS_XZ:-xz}
1191DRACUT_COMPRESS_GZIP=${DRACUT_COMPRESS_GZIP:-gzip}
1192DRACUT_COMPRESS_PIGZ=${DRACUT_COMPRESS_PIGZ:-pigz}
1193DRACUT_COMPRESS_LZOP=${DRACUT_COMPRESS_LZOP:-lzop}
1194DRACUT_COMPRESS_ZSTD=${DRACUT_COMPRESS_ZSTD:-zstd}
1195DRACUT_COMPRESS_LZ4=${DRACUT_COMPRESS_LZ4:-lz4}
1196DRACUT_COMPRESS_CAT=${DRACUT_COMPRESS_CAT:-cat}
1197
75d758e8 1198if [[ $_no_compress_l == "$DRACUT_COMPRESS_CAT" ]]; then
a0120420 1199 compress="$DRACUT_COMPRESS_CAT"
693b7a32
HH
1200fi
1201
75d758e8 1202[[ $hostonly == yes ]] && hostonly="-h"
ba67b923 1203[[ $hostonly != "-h" ]] && unset hostonly
ba726310 1204
a695250e
KS
1205case $hostonly_mode in
1206 '')
9a52c3fd
HH
1207 [[ $hostonly ]] && hostonly_mode="sloppy"
1208 ;;
1209 sloppy | strict)
a695250e
KS
1210 if [[ ! $hostonly ]]; then
1211 unset hostonly_mode
1212 fi
1213 ;;
1214 *)
ae88e029 1215 printf "%s\n" "dracut[F]: Invalid hostonly mode '$hostonly_mode'." >&2
a695250e 1216 exit 1
9a52c3fd 1217 ;;
a695250e
KS
1218esac
1219
80626ded
HH
1220[[ $reproducible == yes ]] && DRACUT_REPRODUCIBLE=1
1221
d32d221e
AAF
1222if [[ -z $DRACUT_KMODDIR_OVERRIDE && -n $drivers_dir ]]; then
1223 drivers_basename="${drivers_dir##*/}"
1224 if [[ -n $drivers_basename && $drivers_basename != "$kernel" ]]; then
ae88e029
AAF
1225 printf "%s\n" "dracut[F]: The provided directory where to look for kernel modules ($drivers_basename)" >&2
1226 printf "%s\n" "dracut[F]: does not match the kernel version set for the initramfs ($kernel)." >&2
1227 printf "%s\n" "dracut[F]: Set DRACUT_KMODDIR_OVERRIDE=1 to ignore this check." >&2
d32d221e
AAF
1228 exit 1
1229 fi
1230 drivers_dirname="${drivers_dir%/*}/"
1231 if [[ ! $drivers_dirname =~ .*/lib/modules/$ ]]; then
ae88e029
AAF
1232 printf "%s\n" "dracut[F]: drivers_dir path ${drivers_dir_l:+"set via -k/--kmoddir "}must contain \"/lib/modules/\" as a parent of your kernel module directory," >&2
1233 printf "%s\n" "dracut[F]: or modules may not be placed in the correct location inside the initramfs." >&2
1234 printf "%s\n" "dracut[F]: was given: ${drivers_dir}" >&2
1235 printf "%s\n" "dracut[F]: expected: ${drivers_dirname}lib/modules/${kernel}" >&2
1236 printf "%s\n" "dracut[F]: Please move your modules into the correct directory structure and pass the new location," >&2
1237 printf "%s\n" "dracut[F]: or set DRACUT_KMODDIR_OVERRIDE=1 to ignore this check." >&2
d32d221e
AAF
1238 exit 1
1239 fi
1240fi
f668e717 1241
909961d0 1242# shellcheck disable=SC2155
bafc82e7
DD
1243readonly TMPDIR="$(realpath -e "$tmpdir")"
1244[ -d "$TMPDIR" ] || {
ae88e029 1245 printf "%s\n" "dracut[F]: Invalid tmpdir '$tmpdir'." >&2
bafc82e7
DD
1246 exit 1
1247}
ecbdff68
TM
1248
1249if findmnt --raw -n --target "$tmpdir" --output=options | grep -q noexec; then
ae88e029 1250 [[ $debug == yes ]] && printf "%s\n" "dracut[D]: Tmpdir '$tmpdir' is mounted with 'noexec'." >&2
ecbdff68
TM
1251 noexec=1
1252fi
1253
909961d0 1254# shellcheck disable=SC2155
62c00a88
HH
1255readonly DRACUT_TMPDIR="$(mktemp -p "$TMPDIR/" -d -t dracut.XXXXXX)"
1256[ -d "$DRACUT_TMPDIR" ] || {
ae88e029 1257 printf "%s\n" "dracut[F]: mktemp -p '$TMPDIR/' -d -t dracut.XXXXXX failed." >&2
26c231f1
HH
1258 exit 1
1259}
1260
c3bb9d18
RM
1261# Cache file used to optimize get_maj_min()
1262declare -x -r get_maj_min_cache_file="${DRACUT_TMPDIR}/majmin_cache"
1263: > "$get_maj_min_cache_file"
1264
d008b6b8 1265# clean up after ourselves no matter how we die.
bbaa0d59
HH
1266trap '
1267 ret=$?;
62c00a88 1268 [[ $keep ]] && echo "Not removing $DRACUT_TMPDIR." >&2 || { [[ $DRACUT_TMPDIR ]] && rm -rf -- "$DRACUT_TMPDIR"; };
d87ae137
SI
1269 if [[ ${FSFROZEN} ]]; then
1270 fsfreeze -u "${FSFROZEN}"
1271 fi
bbaa0d59
HH
1272 exit $ret;
1273 ' EXIT
1274
d008b6b8
HH
1275# clean up after ourselves no matter how we die.
1276trap 'exit 1;' SIGINT
1277
3889234f 1278readonly initdir="${DRACUT_TMPDIR}/initramfs"
9cf7b1c5 1279mkdir -p "$initdir"
3889234f 1280
dd8ff531 1281# shellcheck disable=SC2154
75d758e8 1282if [[ $early_microcode == yes ]] || { [[ $acpi_override == yes ]] && [[ -d $acpi_table_dir ]]; }; then
3889234f
HH
1283 readonly early_cpio_dir="${DRACUT_TMPDIR}/earlycpio"
1284 mkdir "$early_cpio_dir"
1285fi
1286
dd8ff531 1287[[ "$dracutsysrootdir" ]] || [[ "$noexec" ]] || export DRACUT_RESOLVE_LAZY="1"
89d44e72 1288
535f61ca
HH
1289if [[ $print_cmdline ]]; then
1290 stdloglvl=0
1291 sysloglvl=0
1292 fileloglvl=0
1293 kmsgloglvl=0
1294fi
1295
e27ecc9a 1296if [[ -f $dracutbasedir/dracut-version.sh ]]; then
dd8ff531
HH
1297 # shellcheck source=./dracut-version.sh
1298 . "$dracutbasedir"/dracut-version.sh
e27ecc9a
HH
1299fi
1300
7275c6f6
HH
1301if systemd-detect-virt -c &> /dev/null; then
1302 export DRACUT_NO_MKNOD=1 DRACUT_NO_XATTR=1
1303 if [[ $hostonly ]]; then
ae88e029 1304 printf "%s\n" "dracut[W]: Running in hostonly mode in a container!" >&2
7275c6f6
HH
1305 fi
1306fi
1307
777f2db0 1308if [[ -f $dracutbasedir/dracut-init.sh ]]; then
dd8ff531
HH
1309 # shellcheck source=./dracut-init.sh
1310 . "$dracutbasedir"/dracut-init.sh
adbc8a42 1311else
ae88e029
AAF
1312 printf "%s\n" "dracut[F]: Cannot find $dracutbasedir/dracut-init.sh." >&2
1313 printf "%s\n" "dracut[F]: Are you running from a git checkout?" >&2
1314 printf "%s\n" "dracut[F]: Try passing -l as an argument to $dracut_cmd" >&2
cc02093d 1315 exit 1
adbc8a42 1316fi
22fd1627 1317
9cc7ceec
AAF
1318if [[ $persistent_policy == "mapper" ]]; then
1319 unset persistent_policy
1320elif [[ -n $persistent_policy && ! -d "/dev/disk/${persistent_policy}" ]]; then
1321 dwarn "Invalid persistent policy, your system does not have a /dev/disk/${persistent_policy} directory."
1322 unset persistent_policy
1323fi
1324
afe4a6db
DD
1325if [[ $enhanced_cpio == "yes" ]]; then
1326 enhanced_cpio="$dracutbasedir/dracut-cpio"
1327 if [[ -x $enhanced_cpio ]]; then
1328 # align based on statfs optimal transfer size
1329 cpio_align=$(stat --file-system -c "%s" -- "$initdir")
1330 else
1331 dinfo "--enhanced-cpio ignored due to lack of dracut-cpio"
1332 unset enhanced_cpio
1333 fi
1334else
1335 unset enhanced_cpio
1336fi
1337
dd8ff531 1338# shellcheck disable=SC2154
106255af 1339if [[ $no_kernel != yes ]] && ! [[ -d $srcmods ]]; then
f082dad9
AAF
1340 dfatal "Cannot find module directory $srcmods"
1341 dfatal "and --no-kernel was not specified"
106255af
HH
1342 exit 1
1343fi
1344
1743473b 1345if ! [[ $print_cmdline ]]; then
dd8ff531 1346 inst "$DRACUT_TESTBIN"
9a52c3fd 1347 if ! $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${dracutsysrootdir:+-r "$dracutsysrootdir"} -R "$DRACUT_TESTBIN" &> /dev/null; then
1743473b
HH
1348 unset DRACUT_RESOLVE_LAZY
1349 export DRACUT_RESOLVE_DEPS=1
1350 fi
dd8ff531 1351 rm -fr -- "${initdir:?}"/*
bfd2e8c2 1352fi
bfd2e8c2 1353
552ecca6 1354dracutfunctions=$dracutbasedir/dracut-functions.sh
5cad5bb5 1355export dracutfunctions
9a8a00cf 1356
9a52c3fd 1357((${#drivers_l[@]})) && drivers="${drivers_l[*]}"
fcbcb252
HH
1358drivers=${drivers/-/_}
1359
9a52c3fd 1360((${#add_drivers_l[@]})) && add_drivers+=" ${add_drivers_l[*]} "
fcbcb252
HH
1361add_drivers=${add_drivers/-/_}
1362
9a52c3fd 1363((${#force_drivers_l[@]})) && force_drivers+=" ${force_drivers_l[*]} "
cea907f6
TR
1364force_drivers=${force_drivers/-/_}
1365
9a52c3fd 1366((${#omit_drivers_l[@]})) && omit_drivers+=" ${omit_drivers_l[*]} "
fcbcb252
HH
1367omit_drivers=${omit_drivers/-/_}
1368
9a52c3fd 1369((${#kernel_cmdline_l[@]})) && kernel_cmdline+=" ${kernel_cmdline_l[*]} "
d20fb951 1370
34248c92 1371omit_drivers_corrected=""
fcbcb252 1372for d in $omit_drivers; do
b093aa2d 1373 [[ " $drivers $add_drivers " == *\ $d\ * ]] && continue
cea907f6 1374 [[ " $drivers $force_drivers " == *\ $d\ * ]] && continue
444944ab 1375 omit_drivers_corrected+="^$d$|"
fcbcb252 1376done
34248c92 1377omit_drivers="${omit_drivers_corrected%|}"
fcbcb252
HH
1378unset omit_drivers_corrected
1379
ffa71b4a 1380# prepare args for logging
9a52c3fd 1381for ((i = 0; i < ${#dracut_args[@]}; i++)); do
75d758e8 1382 [[ ${dracut_args[$i]} == *\ * ]] \
9a52c3fd
HH
1383 && dracut_args[$i]="\"${dracut_args[$i]}\""
1384 #" keep vim happy
ffa71b4a 1385done
535f61ca 1386
dd8ff531 1387dinfo "Executing: $dracut_cmd ${dracut_args[*]}"
58dad702 1388
75d758e8 1389[[ $do_list == yes ]] && {
dd8ff531 1390 for mod in "$dracutbasedir"/modules.d/*; do
699d51c5 1391 [[ -e $mod/module-setup.sh ]] || continue
b093aa2d 1392 printf "%s\n" "${mod##*/??}"
5b11bb73
HH
1393 done
1394 exit 0
1395}
1396
66ac3cd1 1397# This is kinda legacy -- eventually it should go away.
f1336ac7 1398case $dracutmodules in
9a52c3fd 1399 "" | auto) dracutmodules="all" ;;
f1336ac7 1400esac
e12aac5e 1401
454771cd 1402abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
ec9315e5 1403
6f4a5c90 1404# Helper function to set global variables
c1588995 1405# set_global_var <pkg_config> <pkg_var[:exported_var]> <value[:check_file]> [<value[:check_file]>] ...
6f4a5c90
AAF
1406set_global_var() {
1407 local _pkgconfig="$1"
c1588995
MG
1408 local _pkgvar="${2%:*}"
1409 local _var="${2#*:}"
6f4a5c90 1410 [[ -z ${!_var} || ! -d ${dracutsysrootdir}${!_var} ]] \
c1588995 1411 && export "$_var"="$(pkg-config "$_pkgconfig" --variable="$_pkgvar" 2> /dev/null)"
6f4a5c90
AAF
1412 if [[ -z ${!_var} || ! -d ${dracutsysrootdir}${!_var} ]]; then
1413 shift 2
1414 if (($# == 1)); then
1415 export "$_var"="$1"
1416 else
1417 local _val
1418 for _val in "$@"; do
1419 _check=${_val##*:}
1420 _val=${_val%:*}
1421 [[ -e ${dracutsysrootdir}${_check} ]] && export "$_var"="$_val"
1422 done
1423 fi
1424 fi
1425}
939b7e11 1426
6f4a5c90
AAF
1427# dbus global variables
1428set_global_var "dbus" "dbus" "/usr/share/dbus-1"
1429set_global_var "dbus" "dbusconfdir" "/etc/dbus-1"
1430set_global_var "dbus" "dbusinterfaces" "${dbus}/interfaces"
1431set_global_var "dbus" "dbusinterfacesconfdir" "${dbusconfdir}/interfaces"
1432set_global_var "dbus" "dbusservices" "${dbus}/services"
1433set_global_var "dbus" "dbusservicesconfdir" "${dbusconfdir}/services"
1434set_global_var "dbus" "dbussession" "${dbus}/session.d"
1435set_global_var "dbus" "dbussessionconfdir" "${dbusconfdir}/session.d"
1436set_global_var "dbus" "dbussystem" "${dbus}/system.d"
1437set_global_var "dbus" "dbussystemconfdir" "${dbusconfdir}/system.d"
1438set_global_var "dbus" "dbussystemservices" "${dbus}/system-services"
1439set_global_var "dbus" "dbussystemservicesconfdir" "${dbusconfdir}/system-services"
1440
1441# udev global variables
1442set_global_var "udev" "udevdir" "/lib/udev:/lib/udev/ata_id" "/usr/lib/udev:/usr/lib/udev/ata_id"
1443set_global_var "udev" "udevconfdir" "/etc/udev"
1444set_global_var "udev" "udevrulesdir" "${udevdir}/rules.d"
1445set_global_var "udev" "udevrulesconfdir" "${udevconfdir}/rules.d"
1446
1447# systemd global variables
c1588995 1448set_global_var "systemd" "prefix:systemdprefix" "/usr"
6f4a5c90
AAF
1449set_global_var "systemd" "systemdutildir" "/lib/systemd:/lib/systemd/systemd-udevd" "/usr/lib/systemd:/usr/lib/systemd/systemd-udevd"
1450set_global_var "systemd" "systemdutilconfdir" "/etc/systemd"
1451set_global_var "systemd" "environment" "/usr/lib/environment.d"
1452set_global_var "systemd" "environmentconfdir" "/etc/environment.d"
1453set_global_var "systemd" "modulesload" "/usr/lib/modules-load.d"
1454set_global_var "systemd" "modulesloadconfdir" "/etc/modules-load.d"
1455set_global_var "systemd" "sysctld" "/usr/lib/sysctl.d"
1456set_global_var "systemd" "sysctlconfdir" "/etc/sysctl.d"
1457set_global_var "systemd" "systemdcatalog" "${systemdutildir}/catalog"
1458set_global_var "systemd" "systemdnetwork" "${systemdutildir}/network"
1459set_global_var "systemd" "systemdnetworkconfdir" "${systemdutilconfdir}/network"
1460set_global_var "systemd" "systemdntpunits" "${systemdutildir}/ntp-units.d"
1461set_global_var "systemd" "systemdntpunitsconfdir" "${systemdutilconfdir}/ntp-units.d"
1462set_global_var "systemd" "systemdportable" "${systemdutildir}/portable"
1463set_global_var "systemd" "systemdportableconfdir" "${systemdutilconfdir}/portable"
1464set_global_var "systemd" "systemdsystemunitdir" "${systemdutildir}/system"
1465set_global_var "systemd" "systemdsystemconfdir" "${systemdutilconfdir}/system"
1466set_global_var "systemd" "systemduser" "${systemdutildir}/user"
1467set_global_var "systemd" "systemduserconfdir" "${systemdutilconfdir}/user"
1468set_global_var "systemd" "sysusers" "/usr/lib/sysusers.d"
1469set_global_var "systemd" "sysusersconfdir" "/etc/sysusers.d"
1470set_global_var "systemd" "tmpfilesdir" "/lib/tmpfiles.d" "/usr/lib/tmpfiles.d"
1471set_global_var "systemd" "tmpfilesconfdir" "/etc/tmpfiles.d"
1472
1473# libkmod global variables
1474set_global_var "libkmod" "depmodd" "/usr/lib/depmod.d"
1475set_global_var "libkmod" "depmodconfdir" "/etc/depmod.d"
c8b35bf9 1476
693b7a32
HH
1477if [[ $no_kernel != yes ]] && [[ -d $srcmods ]]; then
1478 if ! [[ -f $srcmods/modules.dep ]]; then
b019952f
HH
1479 if [[ -n "$(find "$srcmods" -name '*.ko*')" ]]; then
1480 dfatal "$srcmods/modules.dep is missing. Did you run depmod?"
1481 exit 1
1482 else
1483 dwarn "$srcmods/modules.dep is missing. Did you run depmod?"
1484 fi
693b7a32 1485 fi
5db6ca5a 1486fi
fe1484f3 1487
96cf7270
HH
1488if [[ ! $print_cmdline ]]; then
1489 if [[ -f $outfile && ! $force ]]; then
1490 dfatal "Will not override existing initramfs ($outfile) without --force"
1491 exit 1
1492 fi
1493
1494 outdir=${outfile%/*}
1495 [[ $outdir ]] || outdir="/"
1496
75d758e8 1497 if [[ ! -d $outdir ]]; then
96cf7270
HH
1498 dfatal "Can't write to $outdir: Directory $outdir does not exist or is not accessible."
1499 exit 1
75d758e8 1500 elif [[ ! -w $outdir ]]; then
96cf7270
HH
1501 dfatal "No permission to write to $outdir."
1502 exit 1
75d758e8 1503 elif [[ -f $outfile && ! -w $outfile ]]; then
96cf7270
HH
1504 dfatal "No permission to write $outfile."
1505 exit 1
1506 fi
37383f71
HH
1507
1508 if [[ $loginstall ]]; then
1509 if ! mkdir -p "$loginstall"; then
1510 dfatal "Could not create directory to log installed files to '$loginstall'."
1511 exit 1
1512 fi
1513 loginstall=$(readlink -f "$loginstall")
1514 fi
636d2d46 1515
75d758e8 1516 if [[ $uefi == yes ]]; then
9a52c3fd 1517 if ! command -v objcopy &> /dev/null; then
636d2d46
HH
1518 dfatal "Need 'objcopy' to create a UEFI executable"
1519 exit 1
1520 fi
1521 unset EFI_MACHINE_TYPE_NAME
a86aea65 1522 case "${DRACUT_ARCH:-$(uname -m)}" in
636d2d46 1523 x86_64)
9a52c3fd
HH
1524 EFI_MACHINE_TYPE_NAME=x64
1525 ;;
63b05a8e 1526 i?86)
9a52c3fd
HH
1527 EFI_MACHINE_TYPE_NAME=ia32
1528 ;;
8391a993 1529 aarch64)
1530 EFI_MACHINE_TYPE_NAME=aa64
8391a993 1531 ;;
636d2d46 1532 *)
a86aea65 1533 dfatal "Architecture '${DRACUT_ARCH:-$(uname -m)}' not supported to create a UEFI executable"
636d2d46
HH
1534 exit 1
1535 ;;
1536 esac
1537
1538 if ! [[ -s $uefi_stub ]]; then
c1588995 1539 uefi_stub="$dracutsysrootdir${systemdprefix}/lib/systemd/boot/efi/linux${EFI_MACHINE_TYPE_NAME}.efi.stub"
636d2d46 1540 fi
5c94cf41 1541
636d2d46
HH
1542 if ! [[ -s $uefi_stub ]]; then
1543 dfatal "Can't find a uefi stub '$uefi_stub' to create a UEFI executable"
1544 exit 1
1545 fi
1546
1547 if ! [[ $kernel_image ]]; then
a0120420 1548 for kernel_image in "$dracutsysrootdir/lib/modules/$kernel/vmlinuz" "$dracutsysrootdir/boot/vmlinuz-$kernel"; do
75d758e8 1549 [[ -s $kernel_image ]] || continue
636d2d46
HH
1550 break
1551 done
1552 fi
1553 if ! [[ -s $kernel_image ]]; then
1554 dfatal "Can't find a kernel image '$kernel_image' to create a UEFI executable"
1555 exit 1
1556 fi
1557 fi
ec9315e5
JK
1558fi
1559
75d758e8 1560if [[ $acpi_override == yes ]] && ! (check_kernel_config CONFIG_ACPI_TABLE_UPGRADE || check_kernel_config CONFIG_ACPI_INITRD_TABLE_OVERRIDE); then
940169e8 1561 dwarn "Disabling ACPI override, because kernel does not support it. CONFIG_ACPI_INITRD_TABLE_OVERRIDE!=y or CONFIG_ACPI_TABLE_UPGRADE!=y"
8e3f6537
HH
1562 unset acpi_override
1563fi
1564
75d758e8 1565if [[ $early_microcode == yes ]]; then
8e3f6537 1566 if [[ $hostonly ]]; then
d8d5cb83 1567 if [[ $(get_cpu_vendor) == "AMD" ]]; then
1568 check_kernel_config CONFIG_MICROCODE_AMD || unset early_microcode
1569 elif [[ $(get_cpu_vendor) == "Intel" ]]; then
1570 check_kernel_config CONFIG_MICROCODE_INTEL || unset early_microcode
1571 else
1572 unset early_microcode
1573 fi
8e3f6537 1574 else
875426f2
HH
1575 ! check_kernel_config CONFIG_MICROCODE_AMD \
1576 && ! check_kernel_config CONFIG_MICROCODE_INTEL \
8e3f6537
HH
1577 && unset early_microcode
1578 fi
d8d5cb83 1579 # Do not complain on non-x86 architectures as it makes no sense
a86aea65 1580 case "${DRACUT_ARCH:-$(uname -m)}" in
9a52c3fd 1581 x86_64 | i?86)
d8d5cb83 1582 [[ $early_microcode != yes ]] \
1583 && dwarn "Disabling early microcode, because kernel does not support it. CONFIG_MICROCODE_[AMD|INTEL]!=y"
1584 ;;
1585 *) ;;
1586 esac
8e3f6537
HH
1587fi
1588
f6f74096
DD
1589# Need to be able to have non-root users read stuff (rpcbind etc)
1590chmod 755 "$initdir"
1591
5bfa3b36
HH
1592if [[ $hostonly ]]; then
1593 for i in /sys /proc /run /dev; do
9a52c3fd 1594 if ! findmnt --target "$i" &> /dev/null; then
5bfa3b36
HH
1595 dwarning "Turning off host-only mode: '$i' is not mounted!"
1596 unset hostonly
1597 fi
1598 done
1599fi
1600
d351541e
HH
1601declare -A host_fs_types
1602
cd7ff122 1603for line in "${fstab_lines[@]}"; do
7b59d15d 1604 # shellcheck disable=SC2086
cd7ff122 1605 set -- $line
6df52351 1606 dev="$1"
cd7ff122 1607 #dev mp fs fsopts
6df52351
TR
1608 case "$dev" in
1609 UUID=*)
7b59d15d 1610 dev=$(blkid -l -t "UUID=${dev#UUID=}" -o device)
6df52351
TR
1611 ;;
1612 LABEL=*)
7b59d15d 1613 dev=$(blkid -l -t "LABEL=${dev#LABEL=}" -o device)
6df52351
TR
1614 ;;
1615 PARTUUID=*)
7b59d15d 1616 dev=$(blkid -l -t "PARTUUID=${dev#PARTUUID=}" -o device)
6df52351
TR
1617 ;;
1618 PARTLABEL=*)
7b59d15d 1619 dev=$(blkid -l -t "PARTLABEL=${dev#PARTLABEL=}" -o device)
6df52351
TR
1620 ;;
1621 esac
7b59d15d 1622 [ -z "$dev" ] && dwarn "Bad fstab entry $*" && continue
75d758e8 1623 if [[ $3 == btrfs ]]; then
3fdc734a
KS
1624 for mp in $(findmnt --source "$1" -o TARGET -n); do
1625 for i in $(btrfs_devs "$mp"); do
1626 push_host_devs "$i"
1627 done
1cadc26f 1628 done
9582f027
SJ
1629 elif [[ $3 == zfs ]]; then
1630 for mp in $(zfs_devs "$1"); do
1631 push_host_devs "$mp"
1632 done
1cadc26f 1633 fi
6df52351
TR
1634 push_host_devs "$dev"
1635 host_fs_types["$dev"]="$3"
cd7ff122
AW
1636done
1637
1638for f in $add_fstab; do
b093aa2d 1639 [[ -e $f ]] || continue
7b59d15d 1640 while read -r dev rest || [ -n "$dev" ]; do
118ca9ec 1641 push_host_devs "$dev"
b093aa2d
HH
1642 done < "$f"
1643done
1644
1645for dev in $add_device; do
118ca9ec 1646 push_host_devs "$dev"
cd7ff122
AW
1647done
1648
9a52c3fd 1649if ((${#add_device_l[@]})); then
7b59d15d 1650 add_device+=" ${add_device_l[*]} "
694725ab 1651 push_host_devs "${add_device_l[@]}"
c586b033
CG
1652fi
1653
75d758e8 1654if [[ $hostonly ]] && [[ $hostonly_default_device != "no" ]]; then
480d772f
HH
1655 # in hostonly mode, determine all devices, which have to be accessed
1656 # and examine them for filesystem types
1657
bcfbddef 1658 for mp in \
7ae5d9d1
HH
1659 "/" \
1660 "/etc" \
d624f606
HH
1661 "/bin" \
1662 "/sbin" \
1663 "/lib" \
1664 "/lib64" \
af75a298 1665 "/usr" \
9fc14445
HH
1666 "/usr/bin" \
1667 "/usr/sbin" \
1668 "/usr/lib" \
1669 "/usr/lib64" \
a956a464
HH
1670 "/boot" \
1671 "/boot/efi" \
9a52c3fd 1672 "/boot/zipl"; do
a0120420 1673 mp=$(readlink -f "$dracutsysrootdir$mp")
9a52c3fd 1674 mountpoint "$mp" > /dev/null 2>&1 || continue
a4f7b504 1675 _dev=$(find_block_device "$mp")
e95b6723 1676 _bdev=$(readlink -f "/dev/block/$_dev")
a4f7b504 1677 [[ -b $_bdev ]] && _dev=$_bdev
75d758e8 1678 [[ $mp == "/" ]] && root_devs+=("$_dev")
118ca9ec 1679 push_host_devs "$_dev"
1cadc26f
HH
1680 if [[ $(find_mp_fstype "$mp") == btrfs ]]; then
1681 for i in $(btrfs_devs "$mp"); do
75d758e8 1682 [[ $mp == "/" ]] && root_devs+=("$i")
1cadc26f
HH
1683 push_host_devs "$i"
1684 done
9582f027
SJ
1685 elif [[ $(find_mp_fstype "$mp") == zfs ]]; then
1686 for i in $(zfs_devs "$(findmnt -n -o SOURCE "$mp")"); do
1687 [[ $mp == "/" ]] && root_devs+=("$i")
1688 push_host_devs "$i"
1689 done
1cadc26f 1690 fi
9582f027 1691
7ae5d9d1 1692 done
dd587549 1693
a0120420
BZ
1694 # TODO - with sysroot, /proc/swaps is not relevant
1695 if [[ -f /proc/swaps ]] && [[ -f $dracutsysrootdir/etc/fstab ]]; then
7b59d15d 1696 while read -r dev type rest || [ -n "$dev" ]; do
7fd850d5 1697 [[ -b $dev ]] || continue
75d758e8 1698 [[ $type == "partition" ]] || continue
7fd850d5 1699
7b59d15d 1700 while read -r _d _m _t _o _ || [ -n "$_d" ]; do
75d758e8 1701 [[ $_d == \#* ]] && continue
7fd850d5 1702 [[ $_d ]] || continue
546d5dac
HH
1703 [[ $_t != "swap" ]] && continue
1704 [[ $_m != "swap" ]] && [[ $_m != "none" ]] && continue
75d758e8 1705 [[ $_o == *noauto* ]] && continue
c82a1133 1706 _d=$(expand_persistent_dev "$_d")
75d758e8 1707 [[ $_d -ef $dev ]] || continue
7fd850d5 1708
a0120420 1709 if [[ -f $dracutsysrootdir/etc/crypttab ]]; then
7b59d15d 1710 while read -r _mapper _ _p _o || [ -n "$_mapper" ]; do
75d758e8
HH
1711 [[ $_mapper == \#* ]] && continue
1712 [[ $_d -ef /dev/mapper/"$_mapper" ]] || continue
7fd850d5 1713 [[ "$_o" ]] || _o="$_p"
64ae4e07 1714 # skip entries with password files
75d758e8 1715 [[ $_p == /* ]] && [[ -f $_p ]] && continue 2
1743473b 1716 # skip mkswap swap
7fd850d5 1717 [[ $_o == *swap* ]] && continue 2
7b59d15d 1718 done < "$dracutsysrootdir"/etc/crypttab
7fd850d5 1719 fi
bcfbddef 1720
3e3ed34f
CG
1721 _dev="$(readlink -f "$dev")"
1722 push_host_devs "$_dev"
5a66d511 1723 swap_devs+=("$_dev")
7fd850d5 1724 break
7b59d15d 1725 done < "$dracutsysrootdir"/etc/fstab
7fd850d5
HH
1726 done < /proc/swaps
1727 fi
916559e0
HH
1728
1729 # collect all "x-initrd.mount" entries from /etc/fstab
a0120420 1730 if [[ -f $dracutsysrootdir/etc/fstab ]]; then
7b59d15d 1731 while read -r _d _m _t _o _ || [ -n "$_d" ]; do
75d758e8 1732 [[ $_d == \#* ]] && continue
916559e0 1733 [[ $_d ]] || continue
75d758e8 1734 [[ $_o != *x-initrd.mount* ]] && continue
916559e0
HH
1735 _dev=$(expand_persistent_dev "$_d")
1736 _dev="$(readlink -f "$_dev")"
1cadc26f
HH
1737 [[ -b $_dev ]] || continue
1738
1739 push_host_devs "$_dev"
75d758e8 1740 if [[ $_t == btrfs ]]; then
bfa4e45f 1741 for i in $(btrfs_devs "$_m"); do
1cadc26f
HH
1742 push_host_devs "$i"
1743 done
9582f027
SJ
1744 elif [[ $_t == zfs ]]; then
1745 for i in $(zfs_devs "$_d"); do
1746 push_host_devs "$i"
1747 done
1cadc26f 1748 fi
7b59d15d 1749 done < "$dracutsysrootdir"/etc/fstab
916559e0 1750 fi
7ae5d9d1 1751fi
7ae5d9d1 1752
3c4315fa
HH
1753unset m
1754unset rest
1755
dba20559 1756_get_fs_type() {
19bab59c 1757 [[ $1 ]] || return
e41f3f90
HH
1758 if [[ -b /dev/block/$1 ]]; then
1759 ID_FS_TYPE=$(get_fs_env "/dev/block/$1") && host_fs_types["$(readlink -f "/dev/block/$1")"]="$ID_FS_TYPE"
19bab59c 1760 return 1
cd7ff122 1761 fi
e41f3f90
HH
1762 if [[ -b $1 ]]; then
1763 ID_FS_TYPE=$(get_fs_env "$1") && host_fs_types["$(readlink -f "$1")"]="$ID_FS_TYPE"
19bab59c 1764 return 1
4f10ae2b 1765 fi
b093aa2d 1766 if fstype=$(find_dev_fstype "$1"); then
dba20559 1767 host_fs_types["$1"]="$fstype"
19bab59c 1768 return 1
cd7ff122
AW
1769 fi
1770 return 1
dba20559
HH
1771}
1772
3721635b 1773for dev in "${host_devs[@]}"; do
dba20559
HH
1774 _get_fs_type "$dev"
1775 check_block_and_slaves_all _get_fs_type "$(get_maj_min "$dev")"
cd7ff122
AW
1776done
1777
8df0b654 1778for dev in "${!host_fs_types[@]}"; do
75d758e8 1779 [[ ${host_fs_types[$dev]} == "reiserfs" ]] || [[ ${host_fs_types[$dev]} == "xfs" ]] || continue
8df0b654 1780 rootopts=$(find_dev_fsopts "$dev")
75d758e8 1781 if [[ ${host_fs_types[$dev]} == "reiserfs" ]]; then
7b59d15d 1782 journaldev=$(fs_get_option "$rootopts" "jdev")
75d758e8 1783 elif [[ ${host_fs_types[$dev]} == "xfs" ]]; then
7b59d15d 1784 journaldev=$(fs_get_option "$rootopts" "logdev")
8df0b654
HH
1785 fi
1786 if [[ $journaldev ]]; then
1787 dev="$(readlink -f "$dev")"
118ca9ec 1788 push_host_devs "$dev"
8df0b654
HH
1789 _get_fs_type "$dev"
1790 check_block_and_slaves_all _get_fs_type "$(get_maj_min "$dev")"
1791 fi
1792done
1793
ab2f95e4
HH
1794export initdir dracutbasedir \
1795 dracutmodules force_add_dracutmodules add_dracutmodules omit_dracutmodules \
1796 mods_to_load \
e103615b 1797 fw_dir drivers_dir debug no_kernel kernel_only \
5634a311 1798 omit_drivers mdadmconf lvmconf root_devs \
ff3953ef 1799 use_fstab fstab_lines libdirs fscks nofscks ro_mnt \
1e64e493 1800 stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \
6f4a5c90
AAF
1801 host_fs_types host_devs swap_devs sshkey add_fstab \
1802 DRACUT_VERSION \
1803 prefix filesystems drivers \
1804 hostonly_cmdline loginstall
f4fff04e 1805
1743473b
HH
1806mods_to_load=""
1807# check all our modules to see if they should be sourced.
1808# This builds a list of modules that we will install next.
1809for_each_module_dir check_module
1810for_each_module_dir check_mount
1811
88c86ac7 1812dracut_module_included "fips" && export DRACUT_FIPS_MODE=1
1743473b 1813
9a52c3fd 1814do_print_cmdline() {
21a20973
HH
1815 local -A _mods_to_print
1816 for i in $modules_loaded $mods_to_load; do
1817 _mods_to_print[$i]=1
1818 done
1819
1743473b
HH
1820 # source our modules.
1821 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
9a52c3fd
HH
1822 _d_mod=${moddir##*/}
1823 _d_mod=${_d_mod#[0-9][0-9]}
21a20973 1824 [[ ${_mods_to_print[$_d_mod]} ]] || continue
5916d31b 1825 module_cmdline "$_d_mod" "$moddir"
1743473b
HH
1826 done
1827 unset moddir
91190597
HH
1828}
1829
1830if [[ $print_cmdline ]]; then
1831 do_print_cmdline
a17d5cf4 1832 printf "\n"
1743473b
HH
1833 exit 0
1834fi
1835
dbad9f46 1836# Create some directory structure first
2c10b299 1837# shellcheck disable=SC2174
fd786adc 1838[[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
dbad9f46 1839
2c10b299 1840# shellcheck disable=SC2174
9a52c3fd 1841[[ -L $dracutsysrootdir/lib ]] || mkdir -m 0755 -p "${initdir}${prefix}/lib"
fd786adc 1842[[ $prefix ]] && ln -sfn "${prefix#/}/lib" "$initdir/lib"
dbad9f46 1843
785a6cd2 1844if [[ $prefix ]]; then
d2a9c4a8 1845 for d in bin etc lib sbin tmp usr var $libdirs; do
de0c0872 1846 d=${d#/}
75d758e8 1847 [[ $d == */* ]] && continue
785a6cd2
HH
1848 ln -sfn "${prefix#/}/${d#/}" "$initdir/$d"
1849 done
1850fi
1851
1852if [[ $kernel_only != yes ]]; then
2c10b299 1853 for d in usr usr/bin usr/sbin bin etc lib sbin tmp var var/tmp $libdirs; do
de0c0872 1854 d=${d#/}
fd786adc 1855 [[ -e "${initdir}${prefix}/$d" ]] && continue
d433da44
HH
1856 if [ -L "/$d" ]; then
1857 inst_symlink "/$d" "${prefix}/$d"
785a6cd2 1858 else
2c10b299 1859 # shellcheck disable=SC2174
785a6cd2
HH
1860 mkdir -m 0755 -p "${initdir}${prefix}/$d"
1861 fi
3b403b32 1862 done
dbad9f46 1863
8d89fc6b 1864 for d in dev proc sys sysroot root run; do
d433da44
HH
1865 if [ -L "/$d" ]; then
1866 inst_symlink "/$d"
785a6cd2 1867 else
2c10b299 1868 # shellcheck disable=SC2174
785a6cd2
HH
1869 mkdir -m 0755 -p "$initdir/$d"
1870 fi
33ee031c 1871 done
dbad9f46 1872
506b2192
HH
1873 ln -sfn ../run "$initdir/var/run"
1874 ln -sfn ../run/lock "$initdir/var/lock"
6f09acf8 1875else
2c10b299 1876 for d in lib "$libdirs"; do
6f09acf8
HH
1877 [[ -e "${initdir}${prefix}/$d" ]] && continue
1878 if [ -h "/$d" ]; then
1879 inst "/$d" "${prefix}/$d"
1880 else
2c10b299 1881 # shellcheck disable=SC2174
6f09acf8
HH
1882 mkdir -m 0755 -p "${initdir}${prefix}/$d"
1883 fi
1884 done
33ee031c 1885fi
0f86847d 1886
8d46cc01
AAF
1887mkdir -p "${initdir}"/lib/dracut
1888
14c47b0e
HH
1889if [[ $kernel_only != yes ]]; then
1890 mkdir -p "${initdir}/etc/cmdline.d"
8d46cc01 1891 mkdir -m 0755 "${initdir}"/lib/dracut/hooks
2c10b299 1892 # shellcheck disable=SC2154
4fed3ddf 1893 for _d in $hookdirs; do
2c10b299
HH
1894 # shellcheck disable=SC2174
1895 mkdir -m 0755 -p "${initdir}/lib/dracut/hooks/$_d"
4fed3ddf 1896 done
7275c6f6 1897 if [[ $EUID == "0" ]] && ! [[ $DRACUT_NO_MKNOD ]]; then
2c10b299
HH
1898 [[ -c ${initdir}/dev/null ]] || mknod "${initdir}"/dev/null c 1 3
1899 [[ -c ${initdir}/dev/kmsg ]] || mknod "${initdir}"/dev/kmsg c 1 11
1900 [[ -c ${initdir}/dev/console ]] || mknod "${initdir}"/dev/console c 5 1
1901 [[ -c ${initdir}/dev/random ]] || mknod "${initdir}"/dev/random c 1 8
1902 [[ -c ${initdir}/dev/urandom ]] || mknod "${initdir}"/dev/urandom c 1 9
26144216 1903 fi
14c47b0e 1904fi
96b8d60a 1905
ec3c5951 1906_isize=0 #initramfs size
979c4a93 1907modules_loaded=" "
95bde758 1908# source our modules.
5d791c0e 1909for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
9a52c3fd
HH
1910 _d_mod=${moddir##*/}
1911 _d_mod=${_d_mod#[0-9][0-9]}
75d758e8
HH
1912 [[ $mods_to_load == *\ $_d_mod\ * ]] || continue
1913 if [[ $show_modules == yes ]]; then
bee08653
HH
1914 printf "%s\n" "$_d_mod"
1915 else
1916 dinfo "*** Including module: $_d_mod ***"
1917 fi
1918 if [[ $kernel_only == yes ]]; then
5916d31b 1919 module_installkernel "$_d_mod" "$moddir" || {
bee08653
HH
1920 dfatal "installkernel failed in module $_d_mod"
1921 exit 1
1922 }
1923 else
5916d31b 1924 module_install "$_d_mod" "$moddir"
bee08653 1925 if [[ $no_kernel != yes ]]; then
5916d31b 1926 module_installkernel "$_d_mod" "$moddir" || {
a6d3be9d
AW
1927 dfatal "installkernel failed in module $_d_mod"
1928 exit 1
1929 }
ec3c5951 1930 fi
66ac3cd1 1931 fi
bee08653
HH
1932 mods_to_load=${mods_to_load// $_d_mod /}
1933 modules_loaded+="$_d_mod "
1934
1935 #print the module install size
1936 if [ -n "$printsize" ]; then
9a52c3fd
HH
1937 _isize_new=$(du -sk "${initdir}" | {
1938 read -r a _
1939 echo -n "$a"
1940 })
bee08653
HH
1941 _isize_delta=$((_isize_new - _isize))
1942 printf "%s\n" "$_d_mod install size: ${_isize_delta}k"
1943 _isize=$_isize_new
1944 fi
15136762 1945done
20abd914 1946unset moddir
0a35a80b
HH
1947
1948for i in $modules_loaded; do
2c10b299 1949 printf "%s\n" "$i" >> "$initdir"/lib/dracut/modules.txt
0a35a80b
HH
1950done
1951
ea216a65 1952dinfo "*** Including modules done ***"
aabc0553 1953
0f86847d 1954## final stuff that has to happen
89d44e72 1955if [[ $no_kernel != yes ]]; then
75d758e8 1956 if [[ $hostonly_mode == "strict" ]]; then
2c10b299 1957 cp "$DRACUT_KERNEL_MODALIASES" "$initdir"/lib/dracut/hostonly-kernel-modules.txt
70472946 1958 fi
aec9f902
HH
1959
1960 if [[ $drivers ]]; then
2c10b299 1961 # shellcheck disable=SC2086
aec9f902
HH
1962 hostonly='' instmods $drivers
1963 fi
1964
75d758e8 1965 if [[ -n ${add_drivers// /} ]]; then
2c10b299 1966 # shellcheck disable=SC2086
aec9f902
HH
1967 hostonly='' instmods -c $add_drivers
1968 fi
cea907f6 1969 if [[ $force_drivers ]]; then
2c10b299 1970 # shellcheck disable=SC2086
cea907f6 1971 hostonly='' instmods -c $force_drivers
2c10b299 1972 rm -f "$initdir"/etc/cmdline.d/20-force_driver.conf
cea907f6 1973 for mod in $force_drivers; do
9a52c3fd 1974 echo "rd.driver.pre=$mod" >> "$initdir"/etc/cmdline.d/20-force_drivers.conf
cea907f6
TR
1975 done
1976 fi
aec9f902 1977 if [[ $filesystems ]]; then
2c10b299 1978 # shellcheck disable=SC2086
aec9f902
HH
1979 hostonly='' instmods -c $filesystems
1980 fi
1981
794b2d2c 1982 dinfo "*** Installing kernel module dependencies ***"
89d44e72 1983 dracut_kernel_post
794b2d2c 1984 dinfo "*** Installing kernel module dependencies done ***"
83bb0893
HH
1985
1986 if [[ $noimageifnotneeded == yes ]] && [[ $hostonly ]]; then
9a52c3fd
HH
1987 if [[ ! -f "$initdir/lib/dracut/need-initqueue" ]] \
1988 && [[ -f ${initdir}/lib/modules/$kernel/modules.dep && ! -s ${initdir}/lib/modules/$kernel/modules.dep ]]; then
510a2211 1989 for i in "${initdir}"/etc/cmdline.d/*.conf; do
83bb0893
HH
1990 # We need no initramfs image and do not generate one.
1991 [[ $i == "${initdir}/etc/cmdline.d/*.conf" ]] && exit 0
1992 done
1993 fi
1994 fi
89d44e72 1995fi
d8aeb3a7 1996
998bf6e0 1997if [[ $kernel_only != yes ]]; then
510a2211
HH
1998 # FIXME: handle legacy item split
1999 # shellcheck disable=SC2068
9a52c3fd 2000 ((${#install_items[@]} > 0)) && inst_multiple ${install_items[@]}
510a2211 2001 # shellcheck disable=SC2068
9a52c3fd 2002 ((${#install_optional_items[@]} > 0)) && inst_multiple -o ${install_optional_items[@]}
998bf6e0 2003
fea53784 2004 if [[ $kernel_cmdline ]] && [[ $uefi != yes ]]; then
2005 printf "%s\n" "$kernel_cmdline" >> "${initdir}/etc/cmdline.d/01-default.conf"
2006 fi
d20fb951 2007
694725ab 2008 for line in "${fstab_lines[@]}"; do
510a2211
HH
2009 read -r -a fstab_field <<< "$line"
2010 if [ -z "${fstab_field[1]}" ]; then
edbd9ca0 2011 # Determine device and mount options from current system
510a2211 2012 mountpoint -q "${fstab_field[0]}" || derror "${fstab_field[0]} is not a mount point!"
2b5ddc77 2013 read -r -a fstab_field < <(findmnt --raw -n --target "${fstab_field[0]}" --output=source,target,fstype,options)
510a2211 2014 dinfo "Line for ${fstab_field[1]}: ${fstab_field[*]}"
edbd9ca0
FV
2015 else
2016 # Use default options
510a2211 2017 [ -z "${fstab_field[3]}" ] && fstab_field[3]="defaults"
edbd9ca0
FV
2018 fi
2019
2020 # Default options for freq and passno
510a2211
HH
2021 [ -z "${fstab_field[4]}" ] && fstab_field[4]="0"
2022 [ -z "${fstab_field[5]}" ] && fstab_field[5]="2"
edbd9ca0 2023
510a2211
HH
2024 strstr "${fstab_field[2]}" "nfs" && fstab_field[5]="0"
2025 echo "${fstab_field[@]}" >> "${initdir}/etc/fstab"
998bf6e0
HH
2026 done
2027
2028 for f in $add_fstab; do
b093aa2d 2029 cat "$f" >> "${initdir}/etc/fstab"
998bf6e0
HH
2030 done
2031
a0120420 2032 if [[ $dracutsysrootdir$systemdutildir ]]; then
510a2211
HH
2033 if [[ -d ${initdir}/$systemdutildir ]]; then
2034 mkdir -p "${initdir}"/etc/conf.d
2da041d6
HH
2035 {
2036 printf "%s\n" "systemdutildir=\"$systemdutildir\""
2037 printf "%s\n" "systemdsystemunitdir=\"$systemdsystemunitdir\""
2038 printf "%s\n" "systemdsystemconfdir=\"$systemdsystemconfdir\""
510a2211 2039 } > "${initdir}"/etc/conf.d/systemd.conf
2da041d6 2040 fi
6c6d8057
HH
2041 fi
2042
998bf6e0
HH
2043 if [[ $DRACUT_RESOLVE_LAZY ]] && [[ $DRACUT_INSTALL ]]; then
2044 dinfo "*** Resolving executable dependencies ***"
097dd367 2045 # shellcheck disable=SC2086
f182cb95 2046 find "$initdir" -type f -perm /0111 -not -path '*.ko' -print0 \
097dd367 2047 | xargs -r -0 $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${dracutsysrootdir:+-r "$dracutsysrootdir"} -R ${DRACUT_FIPS_MODE:+-f} --
b2c6b584
AAF
2048 # shellcheck disable=SC2181
2049 if (($? == 0)); then
2050 dinfo "*** Resolving executable dependencies done ***"
2051 else
2052 dfatal "Resolving executable dependencies failed"
2053 exit 1
2054 fi
998bf6e0 2055 fi
8dcc4830 2056
ed9281d6
HH
2057 # Now we are done with lazy resolving, always install dependencies
2058 unset DRACUT_RESOLVE_LAZY
2059 export DRACUT_RESOLVE_DEPS=1
998bf6e0
HH
2060fi
2061
9a52c3fd 2062for ((i = 0; i < ${#include_src[@]}; i++)); do
694725ab
HH
2063 src="${include_src[$i]}"
2064 target="${include_target[$i]}"
c9364f6e 2065 if [[ $src && $target ]]; then
661f9a34 2066 if [[ -f $src ]]; then
510a2211 2067 inst "$src" "$target"
7a3d6619 2068 elif [[ -d $src ]]; then
3b403b32 2069 ddebug "Including directory: $src"
c9364f6e 2070 destdir="${initdir}/${target}"
332ecaa9 2071 mkdir -p "$destdir"
dbad9f46 2072 # check for preexisting symlinks, so we can cope with the
fd786adc 2073 # symlinks to $prefix
c9364f6e 2074 # Objectname is a file or a directory
f1138012
HB
2075 reset_dotglob="$(shopt -p dotglob)"
2076 shopt -q -s dotglob
c9364f6e 2077 for objectname in "$src"/*; do
75d758e8 2078 [[ -e $objectname || -L $objectname ]] || continue
c7fbc0c8 2079 if [[ -d $objectname ]] && [[ ! -L $objectname ]]; then
c9364f6e 2080 # objectname is a directory, let's compute the final directory name
08b63a25 2081 object_destdir=${destdir}/${objectname#"$src"/}
75d758e8 2082 if ! [[ -e $object_destdir ]]; then
510a2211 2083 # shellcheck disable=SC2174
c9364f6e
EV
2084 mkdir -m 0755 -p "$object_destdir"
2085 chmod --reference="$objectname" "$object_destdir"
dbad9f46 2086 fi
f1138012 2087 $DRACUT_CP -t "$object_destdir" "$dracutsysrootdir$objectname"/.
dbad9f46 2088 else
a0120420 2089 $DRACUT_CP -t "$destdir" "$dracutsysrootdir$objectname"
3b403b32 2090 fi
dbad9f46 2091 done
f1138012 2092 eval "$reset_dotglob"
7a3d6619
RM
2093 elif [[ -e $src ]]; then
2094 derror "$src is neither a directory nor a regular file"
2095 else
2096 derror "$src doesn't exist"
661f9a34 2097 fi
4fea3ea6 2098 fi
661f9a34 2099done
88ffd2df 2100
75d758e8 2101if [[ $do_hardlink == yes ]] && command -v hardlink > /dev/null; then
404815ea 2102 dinfo "*** Hardlinking files ***"
2a26eec5 2103 hardlink "$initdir" 2>&1 | ddebug
404815ea
HH
2104 dinfo "*** Hardlinking files done ***"
2105fi
2106
3b403b32 2107# strip binaries
75d758e8 2108if [[ $do_strip == yes ]]; then
8b6d136e 2109 # Prefer strip from elfutils for package size
510a2211
HH
2110 declare strip_cmd
2111 strip_cmd=$(command -v eu-strip)
3b92d8bf 2112 [ -z "$strip_cmd" ] && strip_cmd="strip"
8b6d136e
DM
2113
2114 for p in $strip_cmd xargs find; do
9a52c3fd 2115 if ! type -P $p > /dev/null; then
fefd3f66 2116 dinfo "Could not find '$p'. Not stripping the initramfs."
cc02093d
HH
2117 do_strip=no
2118 fi
31f7db66 2119 done
67fc670a 2120
e4f1dbcc 2121 if [[ $aggressive_strip == yes ]]; then
67fc670a
KS
2122 # `eu-strip` and `strip` both strips all unneeded parts by default
2123 strip_args=(-p)
2124 else
2125 strip_args=(-g -p)
2126 fi
31f7db66
HH
2127fi
2128
f6e777ec
HH
2129# cleanup empty ldconfig_paths directories
2130for d in $(ldconfig_paths); do
9a52c3fd 2131 rmdir -p --ignore-fail-on-non-empty "$initdir/$d" > /dev/null 2>&1
f6e777ec
HH
2132done
2133
75d758e8 2134if [[ $early_microcode == yes ]]; then
5f2c30d9
KRW
2135 dinfo "*** Generating early-microcode cpio image ***"
2136 ucode_dir=(amd-ucode intel-ucode)
2137 ucode_dest=(AuthenticAMD.bin GenuineIntel.bin)
b5b608e4 2138 _dest_dir="$early_cpio_dir/d/kernel/x86/microcode"
5f2c30d9 2139 _dest_idx="0 1"
510a2211 2140 mkdir -p "$_dest_dir"
5f2c30d9
KRW
2141 if [[ $hostonly ]]; then
2142 [[ $(get_cpu_vendor) == "AMD" ]] && _dest_idx="0"
2143 [[ $(get_cpu_vendor) == "Intel" ]] && _dest_idx="1"
2144 fi
2145 for idx in $_dest_idx; do
2146 _fw=${ucode_dir[$idx]}
2147 for _fwdir in $fw_dir; do
2148 if [[ -d $_fwdir && -d $_fwdir/$_fw ]]; then
2149 _src="*"
f18d069d 2150 dinfo "*** Constructing ${ucode_dest[$idx]} ***"
5f2c30d9
KRW
2151 if [[ $hostonly ]]; then
2152 _src=$(get_ucode_file)
b44156c6 2153 [[ $_src ]] || break
feebf171 2154 [[ -r $_fwdir/$_fw/$_src ]] || _src="${_src}.early"
efba549a 2155 [[ -r $_fwdir/$_fw/$_src ]] || break
5f2c30d9 2156 fi
efba549a 2157
583fb231 2158 for i in $_fwdir/$_fw/$_src; do
efba549a 2159 [ -e "$i" ] && break
583fb231
HH
2160 break 2
2161 done
c44d2252 2162 for i in $_fwdir/$_fw/$_src; do
75d758e8 2163 [[ -e $i ]] || continue
c44d2252
HH
2164 # skip gpg files
2165 str_ends "$i" ".asc" && continue
510a2211 2166 cat "$i" >> "$_dest_dir/${ucode_dest[$idx]}"
c44d2252 2167 done
d8b04dc1 2168 create_early_cpio="yes"
5f2c30d9
KRW
2169 fi
2170 done
d613d88d 2171 if [[ ! -e "$_dest_dir/${ucode_dest[$idx]}" ]]; then
510a2211
HH
2172 if cd "$early_cpio_dir/d"; then
2173 for _ucodedir in "${early_microcode_image_dir[@]}"; do
2174 for _ucodename in "${early_microcode_image_name[@]}"; do
9a52c3fd
HH
2175 [[ -e "$_ucodedir/$_ucodename" ]] \
2176 && cpio --extract --file "$_ucodedir/$_ucodename" --quiet \
2177 "kernel/x86/microcode/${ucode_dest[$idx]}"
510a2211
HH
2178 if [[ -e "$_dest_dir/${ucode_dest[$idx]}" ]]; then
2179 dinfo "*** Using microcode found in '$_ucodedir/$_ucodename' ***"
2180 create_early_cpio="yes"
2181 break 2
2182 fi
2183 done
d613d88d 2184 done
510a2211 2185 fi
d613d88d 2186 fi
5f2c30d9 2187 done
5f2c30d9 2188fi
bbaa0d59 2189
75d758e8 2190if [[ $acpi_override == yes ]] && [[ -d $acpi_table_dir ]]; then
f3b14c2b
TR
2191 dinfo "*** Packaging ACPI tables to override BIOS provided ones ***"
2192 _dest_dir="$early_cpio_dir/d/kernel/firmware/acpi"
510a2211
HH
2193 mkdir -p "$_dest_dir"
2194 for table in "$acpi_table_dir"/*.aml; do
f3b14c2b 2195 dinfo " Adding ACPI table: $table"
510a2211 2196 $DRACUT_CP "$table" "$_dest_dir"
f3b14c2b
TR
2197 create_early_cpio="yes"
2198 done
2199fi
2200
659dc319 2201dinfo "*** Store current command line parameters ***"
9a52c3fd 2202if ! (echo "$PARMS_TO_STORE" > "$initdir"/lib/dracut/build-parameter.txt); then
659dc319
HB
2203 dfatal "Could not store the current command line parameters"
2204 exit 1
2205fi
2206
9a52c3fd 2207if [[ $hostonly_cmdline == "yes" ]]; then
496e3d96 2208 unset _stored_cmdline
9a52c3fd 2209 if [[ -d $initdir/etc/cmdline.d ]]; then
fe273e39 2210 dinfo "Stored kernel commandline:"
9a52c3fd 2211 for conf in "$initdir"/etc/cmdline.d/*.conf; do
496e3d96 2212 [ -e "$conf" ] || continue
3b92d8bf 2213 dinfo "$(< "$conf")"
496e3d96 2214 _stored_cmdline=1
fe273e39 2215 done
496e3d96
HH
2216 fi
2217 if ! [[ $_stored_cmdline ]]; then
62c00a88 2218 dinfo "No dracut internal kernel commandline stored in the initramfs"
fe273e39
HR
2219 fi
2220fi
62c00a88 2221
de3cb0e3
KS
2222if [[ $kernel_only != yes ]]; then
2223 # libpthread workaround: pthread_cancel wants to dlopen libgcc_s.so
2224 for _dir in $libdirs; do
2225 for _f in "$dracutsysrootdir$_dir/libpthread.so"*; do
75d758e8 2226 [[ -e $_f ]] || continue
de3cb0e3
KS
2227 inst_libdir_file "libgcc_s.so*"
2228 break 2
2229 done
2230 done
5a4c3469
KS
2231
2232 # FIPS workaround for Fedora/RHEL: libcrypto needs libssl when FIPS is enabled
2233 if [[ $DRACUT_FIPS_MODE ]]; then
9a52c3fd
HH
2234 for _dir in $libdirs; do
2235 for _f in "$dracutsysrootdir$_dir/libcrypto.so"*; do
75d758e8 2236 [[ -e $_f ]] || continue
9a52c3fd
HH
2237 inst_libdir_file -o "libssl.so*"
2238 break 2
2239 done
2240 done
5a4c3469 2241 fi
de3cb0e3
KS
2242fi
2243
6eff0ebc
AT
2244if [[ $kernel_only != yes ]]; then
2245 # make sure that library links are correct and up to date
bc1b23c2 2246 build_ld_cache
6eff0ebc
AT
2247fi
2248
8c8aecdc
KS
2249if dracut_module_included "squash"; then
2250 readonly squash_dir="$initdir/squash/root"
8f7c332e
KS
2251 readonly squash_img="$initdir/squash-root.img"
2252 mkdir -p "$squash_dir"
8c8aecdc
KS
2253 dinfo "*** Install squash loader ***"
2254 DRACUT_SQUASH_POST_INST=1 module_install "squash"
2255fi
2256
75d758e8 2257if [[ $do_strip == yes ]] && ! [[ $DRACUT_FIPS_MODE ]]; then
afe4a6db
DD
2258 # stripping files negates (dedup) benefits of using reflink
2259 [[ -n $enhanced_cpio ]] && ddebug "strip is enabled alongside cpio reflink"
6a74c03b
KS
2260 dinfo "*** Stripping files ***"
2261 find "$initdir" -type f \
2262 -executable -not -path '*/lib/modules/*.ko' -print0 \
67fc670a 2263 | xargs -r -0 $strip_cmd "${strip_args[@]}" 2> /dev/null
6a74c03b
KS
2264
2265 # strip kernel modules, but do not touch signed modules
2266 find "$initdir" -type f -path '*/lib/modules/*.ko' -print0 \
2267 | while read -r -d $'\0' f || [ -n "$f" ]; do
9a52c3fd
HH
2268 SIG=$(tail -c 28 "$f" | tr -d '\000')
2269 [[ $SIG == '~Module signature appended~' ]] || { printf "%s\000" "$f"; }
67fc670a 2270 done | xargs -r -0 $strip_cmd "${strip_args[@]}"
6a74c03b
KS
2271 dinfo "*** Stripping files done ***"
2272fi
2273
2274if dracut_module_included "squash"; then
2275 dinfo "*** Squashing the files inside the initramfs ***"
5d05ffbd
KS
2276 declare squash_compress_arg
2277 # shellcheck disable=SC2086
90d9ae8c
TL
2278 if [[ $squash_compress ]]; then
2279 if ! mksquashfs /dev/null "$DRACUT_TMPDIR"/.squash-test.img -no-progress -comp $squash_compress &> /dev/null; then
2280 dwarn "mksquashfs doesn't support compressor '$squash_compress', failing back to default compressor."
5d05ffbd 2281 else
90d9ae8c 2282 squash_compress_arg="$squash_compress"
5d05ffbd
KS
2283 fi
2284 fi
2285
2286 # shellcheck disable=SC2086
41cfdfc4 2287 if ! mksquashfs "$squash_dir" "$squash_img" \
5d05ffbd
KS
2288 -no-xattrs -no-exports -noappend -no-recovery -always-use-fragments \
2289 -no-progress ${squash_compress_arg:+-comp $squash_compress_arg} 1> /dev/null; then
8410ee22 2290 dfatal "Failed making squash image"
9a52c3fd 2291 exit 1
095e1f37 2292 fi
6a74c03b 2293
41cfdfc4 2294 rm -rf "$squash_dir"
6a74c03b 2295 dinfo "*** Squashing the files inside the initramfs done ***"
7c0bc0b2
KS
2296
2297 # Skip initramfs compress
2298 compress="cat"
095e1f37
KS
2299fi
2300
6a74c03b
KS
2301dinfo "*** Creating image file '$outfile' ***"
2302
75d758e8 2303if [[ $uefi == yes ]]; then
62c00a88 2304 readonly uefi_outdir="$DRACUT_TMPDIR/uefi"
9cf7b1c5 2305 mkdir -p "$uefi_outdir"
636d2d46 2306fi
c8a9a6b4 2307
80626ded
HH
2308if [[ $DRACUT_REPRODUCIBLE ]]; then
2309 find "$initdir" -newer "$dracutbasedir/dracut-functions.sh" -print0 \
2310 | xargs -r -0 touch -h -m -c -r "$dracutbasedir/dracut-functions.sh"
2311
10f8df1b
HH
2312 if [[ "$(cpio --help)" == *--reproducible* ]]; then
2313 CPIO_REPRODUCIBLE=1
2314 else
2315 dinfo "cpio does not support '--reproducible'. Resulting image will not be reproducible."
2316 fi
80626ded
HH
2317fi
2318
75d758e8 2319[[ $EUID != 0 ]] && cpio_owner="0:0"
c8a9a6b4 2320
75d758e8 2321if [[ $create_early_cpio == yes ]]; then
b9909e33 2322 echo 1 > "$early_cpio_dir/d/early_cpio"
80626ded
HH
2323
2324 if [[ $DRACUT_REPRODUCIBLE ]]; then
2325 find "$early_cpio_dir/d" -newer "$dracutbasedir/dracut-functions.sh" -print0 \
2326 | xargs -r -0 touch -h -m -c -r "$dracutbasedir/dracut-functions.sh"
2327 fi
2328
5f2c30d9 2329 # The microcode blob is _before_ the initramfs blob, not after
afe4a6db
DD
2330 if [[ -n $enhanced_cpio ]]; then
2331 if ! (
2332 umask 077
2333 cd "$early_cpio_dir/d"
2334 find . -print0 | sort -z \
2335 | $enhanced_cpio --null ${cpio_owner:+--owner "$cpio_owner"} \
2336 --mtime 0 --data-align "$cpio_align" --truncate-existing \
2337 "${DRACUT_TMPDIR}/initramfs.img"
2338 ); then
2339 dfatal "dracut-cpio: creation of $outfile failed"
2340 exit 1
2341 fi
2342 else
2343 if ! (
2344 umask 077
2345 cd "$early_cpio_dir/d"
2346 find . -print0 | sort -z \
2347 | cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null \
2348 ${cpio_owner:+-R "$cpio_owner"} -H newc -o --quiet > "${DRACUT_TMPDIR}/initramfs.img"
2349 ); then
8410ee22 2350 dfatal "Creation of $outfile failed"
afe4a6db
DD
2351 exit 1
2352 fi
62c00a88 2353 fi
5f2c30d9 2354fi
80626ded 2355
475497b1
MW
2356if check_kernel_config CONFIG_RD_ZSTD; then
2357 DRACUT_KERNEL_RD_ZSTD=yes
2358else
2359 DRACUT_KERNEL_RD_ZSTD=
2360fi
2361
2362if [[ $compress == $DRACUT_COMPRESS_ZSTD* && ! $DRACUT_KERNEL_RD_ZSTD ]]; then
8410ee22 2363 dwarn "Kernel has no zstd support compiled in."
475497b1
MW
2364 compress=
2365fi
2366
bdac657b
MW
2367if [[ $compress && $compress != cat ]]; then
2368 if ! command -v "${compress%% *}" &> /dev/null; then
8410ee22 2369 derror "Cannot execute compression command '$compress', falling back to default"
bdac657b
MW
2370 compress=
2371 fi
2372fi
2373
a9345f68
KS
2374if ! [[ $compress ]]; then
2375 # check all known compressors, if none specified
acfd97a9 2376 for i in $DRACUT_COMPRESS_PIGZ $DRACUT_COMPRESS_GZIP $DRACUT_COMPRESS_LZ4 $DRACUT_COMPRESS_LZOP $DRACUT_COMPRESS_ZSTD $DRACUT_COMPRESS_LZMA $DRACUT_COMPRESS_XZ $DRACUT_COMPRESS_LBZIP2 $DRACUT_COMPRESS_BZIP2 $DRACUT_COMPRESS_CAT; do
475497b1 2377 [[ $i != "$DRACUT_COMPRESS_ZSTD" || $DRACUT_KERNEL_RD_ZSTD ]] || continue
9a52c3fd 2378 command -v "$i" &> /dev/null || continue
a9345f68
KS
2379 compress="$i"
2380 break
2381 done
75d758e8 2382 if [[ $compress == cat ]]; then
8410ee22 2383 dwarn "No compression tool available. Initramfs image is going to be big."
06d47ded 2384 else
8410ee22 2385 dinfo "Using auto-determined compression method '$compress'"
a9345f68
KS
2386 fi
2387fi
2388
2389# choose the right arguments for the compressor
2390case $compress in
9a52c3fd 2391 bzip2 | lbzip2)
75d758e8 2392 if [[ $compress == lbzip2 ]] || command -v "$DRACUT_COMPRESS_LBZIP2" &> /dev/null; then
a9345f68
KS
2393 compress="$DRACUT_COMPRESS_LBZIP2 -9"
2394 else
2395 compress="$DRACUT_COMPRESS_BZIP2 -9"
2396 fi
2397 ;;
2398 lzma)
2399 compress="$DRACUT_COMPRESS_LZMA -9 -T0"
2400 ;;
2401 xz)
2402 compress="$DRACUT_COMPRESS_XZ --check=crc32 --lzma2=dict=1MiB -T0"
2403 ;;
9a52c3fd 2404 gzip | pigz)
75d758e8 2405 if [[ $compress == pigz ]] || command -v "$DRACUT_COMPRESS_PIGZ" &> /dev/null; then
a9345f68 2406 compress="$DRACUT_COMPRESS_PIGZ -9 -n -T -R"
9a52c3fd 2407 elif command -v gzip &> /dev/null && $DRACUT_COMPRESS_GZIP --help 2>&1 | grep -q rsyncable; then
a9345f68
KS
2408 compress="$DRACUT_COMPRESS_GZIP -n -9 --rsyncable"
2409 else
2410 compress="$DRACUT_COMPRESS_GZIP -n -9"
2411 fi
2412 ;;
9a52c3fd 2413 lzo | lzop)
a9345f68
KS
2414 compress="$DRACUT_COMPRESS_LZOP -9"
2415 ;;
2416 lz4)
2417 compress="$DRACUT_COMPRESS_LZ4 -l -9"
2418 ;;
2419 zstd)
9a52c3fd
HH
2420 compress="$DRACUT_COMPRESS_ZSTD -15 -q -T0"
2421 ;;
a9345f68
KS
2422esac
2423
afe4a6db
DD
2424if [[ -n $enhanced_cpio ]]; then
2425 if [[ $compress == "cat" ]]; then
2426 # dracut-cpio appends by default, so any ucode remains
2427 cpio_outfile="${DRACUT_TMPDIR}/initramfs.img"
2428 else
2429 ddebug "$compress compression enabled alongside cpio reflink"
2430 # dracut-cpio doesn't output to stdout, so stage for compression
2431 cpio_outfile="${DRACUT_TMPDIR}/initramfs.img.uncompressed"
2432 fi
2433
2434 if ! (
2435 umask 077
2436 cd "$initdir"
2437 find . -print0 | sort -z \
2438 | $enhanced_cpio --null ${cpio_owner:+--owner "$cpio_owner"} \
2439 --mtime 0 --data-align "$cpio_align" "$cpio_outfile" || exit 1
2440 [[ $compress == "cat" ]] && exit 0
2441 $compress < "$cpio_outfile" >> "${DRACUT_TMPDIR}/initramfs.img" \
2442 && rm "$cpio_outfile"
2443 ); then
2444 dfatal "dracut-cpio: creation of $outfile failed"
2445 exit 1
2446 fi
2447 unset cpio_outfile
2448else
2449 if ! (
2450 umask 077
2451 cd "$initdir"
2452 find . -print0 | sort -z \
2453 | cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null ${cpio_owner:+-R "$cpio_owner"} -H newc -o --quiet \
2454 | $compress >> "${DRACUT_TMPDIR}/initramfs.img"
2455 ); then
8410ee22 2456 dfatal "Creation of $outfile failed"
afe4a6db
DD
2457 exit 1
2458 fi
3b403b32 2459fi
2cc5e92e 2460
41cfdfc4 2461# shellcheck disable=SC2154
9a52c3fd 2462if ((maxloglvl >= 5)) && ((verbosity_mod_l >= 0)); then
dadd2b69 2463 if [[ $allowlocal ]]; then
9a52c3fd 2464 "$dracutbasedir/lsinitrd.sh" "${DRACUT_TMPDIR}/initramfs.img" | ddebug
dadd2b69 2465 else
9a52c3fd 2466 lsinitrd "${DRACUT_TMPDIR}/initramfs.img" | ddebug
dadd2b69 2467 fi
bbaa0d59 2468fi
1faecdc1 2469
60928f36
HH
2470umask 077
2471
fffeaded
EGE
2472SBAT_DEFAULT="sbat,1,SBAT Version,sbat,1,https://github.com/rhboot/shim/blob/main/SBAT.md"
2473sbat_out=$uefi_outdir/uki.sbat
2474
2475clean_sbat_string() {
2476 local inp=$1
2477 local temp=$uefi_outdir/temp.sbat
2478 sed "/${SBAT_DEFAULT//\//\\/}/d" "$inp" > "$temp"
2479 [[ -s $temp ]] && cat "$temp" >> "$sbat_out"
2480 rm "$temp"
2481}
2482
2483get_sbat_string() {
2484 local inp=$1
2485 local out=$uefi_outdir/$2
2486 objcopy -O binary --only-section=.sbat "$inp" "$out"
2487 clean_sbat_string "$out"
2488}
2489
75d758e8 2490if [[ $uefi == yes ]]; then
636d2d46
HH
2491 if [[ $kernel_cmdline ]]; then
2492 echo -n "$kernel_cmdline" > "$uefi_outdir/cmdline.txt"
1157143d
PV
2493 elif [[ $hostonly_cmdline == yes ]]; then
2494 if [ -d "$initdir/etc/cmdline.d" ]; then
2495 for conf in "$initdir"/etc/cmdline.d/*.conf; do
2496 [ -e "$conf" ] || continue
2497 printf "%s " "$(< "$conf")" >> "$uefi_outdir/cmdline.txt"
2498 done
2499 elif [ -e "/proc/cmdline" ]; then
2500 printf "%s " "$(< "/proc/cmdline")" > "$uefi_outdir/cmdline.txt"
2501 fi
636d2d46 2502 fi
636d2d46 2503
dbdab2d8 2504 offs=$(objdump -h "$uefi_stub" 2> /dev/null | gawk 'NF==7 {size=strtonum("0x"$3);
f32e95bc
VL
2505 offset=strtonum("0x"$4)} END {print size + offset}')
2506 if [[ $offs -eq 0 ]]; then
2507 dfatal "Failed to get the size of $uefi_stub to create UEFI image file"
2508 exit 1
2509 fi
2510 align=$(pe_get_section_align "$uefi_stub")
2511 if [[ $? -eq 1 ]]; then
6178a9d8 2512 dfatal "Failed to get the SectionAlignment of the stub PE header to create the UEFI image file"
f32e95bc
VL
2513 exit 1
2514 fi
2515 offs=$((offs + "$align" - offs % "$align"))
2516 [[ -s $dracutsysrootdir/usr/lib/os-release ]] && uefi_osrelease="$dracutsysrootdir/usr/lib/os-release"
2517 [[ -s $dracutsysrootdir/etc/os-release ]] && uefi_osrelease="$dracutsysrootdir/etc/os-release"
2518 [[ -s $uefi_osrelease ]] \
2519 && uefi_osrelease_offs=${offs} \
2520 && offs=$((offs + $(stat -Lc%s "$uefi_osrelease"))) \
2521 && offs=$((offs + "$align" - offs % "$align"))
2522
1157143d 2523 if [[ $kernel_cmdline ]] || [[ $hostonly_cmdline == yes && -e "${uefi_outdir}/cmdline.txt" ]]; then
8a2edb1e
MT
2524 echo -ne "\x00" >> "$uefi_outdir/cmdline.txt"
2525 dinfo "Using UEFI kernel cmdline:"
41cfdfc4
HH
2526 dinfo "$(tr -d '\000' < "$uefi_outdir/cmdline.txt")"
2527 uefi_cmdline="${uefi_outdir}/cmdline.txt"
f32e95bc
VL
2528 uefi_cmdline_offs=${offs}
2529 offs=$((offs + $(stat -Lc%s "$uefi_cmdline")))
2530 offs=$((offs + "$align" - offs % "$align"))
8a2edb1e 2531 else
41cfdfc4 2532 unset uefi_cmdline
8a2edb1e 2533 fi
636d2d46 2534
41cfdfc4
HH
2535 if [[ -s ${dracutsysrootdir}${uefi_splash_image} ]]; then
2536 uefi_splash_image="${dracutsysrootdir}${uefi_splash_image}"
f32e95bc
VL
2537 uefi_splash_offs=${offs}
2538 offs=$((offs + $(stat -Lc%s "$uefi_splash_image")))
2539 offs=$((offs + "$align" - offs % "$align"))
41cfdfc4
HH
2540 else
2541 unset uefi_splash_image
2542 fi
636d2d46 2543
fffeaded
EGE
2544 echo "$SBAT_DEFAULT" > "$sbat_out"
2545 if [[ -n $sbat ]]; then
2546 echo "$sbat" | sed "/${SBAT_DEFAULT//\//\\/}/d" >> "$sbat_out"
2547 fi
2548 get_sbat_string "$kernel_image" kernel.sbat
2549 get_sbat_string "$uefi_stub" stub.sbat
2550
2551 uefi_sbat_offs="${offs}"
2552 offs=$((offs + $(stat -Lc%s "$sbat_out")))
2553 offs=$((offs + "$align" - offs % "$align"))
f32e95bc
VL
2554 uefi_linux_offs="${offs}"
2555 offs=$((offs + $(stat -Lc%s "$kernel_image")))
2556 offs=$((offs + "$align" - offs % "$align"))
2557 uefi_initrd_offs="${offs}"
2558
6178a9d8 2559 base_image=$(pe_get_image_base "$uefi_stub")
2560 if [[ $? -eq 1 ]]; then
2561 dfatal "Failed to get ImageBase data of $uefi_stub to create UEFI image file"
2562 exit 1
2563 fi
2564
fffeaded
EGE
2565 tmp_uefi_stub=$uefi_outdir/elf.stub
2566 cp "$uefi_stub" "$tmp_uefi_stub"
2567 objcopy --remove-section .sbat "$tmp_uefi_stub" &> /dev/null
2568
62c00a88 2569 if objcopy \
f32e95bc
VL
2570 ${uefi_osrelease:+--add-section .osrel="$uefi_osrelease" --change-section-vma .osrel=$(printf 0x%x "$uefi_osrelease_offs")} \
2571 ${uefi_cmdline:+--add-section .cmdline="$uefi_cmdline" --change-section-vma .cmdline=$(printf 0x%x "$uefi_cmdline_offs")} \
2572 ${uefi_splash_image:+--add-section .splash="$uefi_splash_image" --change-section-vma .splash=$(printf 0x%x "$uefi_splash_offs")} \
fffeaded 2573 --add-section .sbat="$sbat_out" --change-section-vma .sbat="$(printf 0x%x "$uefi_sbat_offs")" \
f32e95bc
VL
2574 --add-section .linux="$kernel_image" --change-section-vma .linux="$(printf 0x%x "$uefi_linux_offs")" \
2575 --add-section .initrd="${DRACUT_TMPDIR}/initramfs.img" --change-section-vma .initrd="$(printf 0x%x "$uefi_initrd_offs")" \
6178a9d8 2576 --image-base="$(printf 0x%x "$base_image")" \
fffeaded 2577 "$tmp_uefi_stub" "${uefi_outdir}/linux.efi"; then
75d758e8 2578 if [[ -n ${uefi_secureboot_key} && -n ${uefi_secureboot_cert} ]]; then
5a962b59 2579 if sbsign \
897e5eff 2580 ${uefi_secureboot_engine:+--engine "$uefi_secureboot_engine"} \
9a52c3fd
HH
2581 --key "${uefi_secureboot_key}" \
2582 --cert "${uefi_secureboot_cert}" \
a6dd5bfb
AAF
2583 --output "$outfile" "${uefi_outdir}/linux.efi" \
2584 && sbverify --cert "${uefi_secureboot_cert}" "$outfile" > /dev/null 2>&1; then
5a962b59
MR
2585 dinfo "*** Creating signed UEFI image file '$outfile' done ***"
2586 else
a6dd5bfb 2587 rm -f -- "$outfile"
5a962b59
MR
2588 dfatal "*** Creating signed UEFI image file '$outfile' failed ***"
2589 exit 1
2590 fi
2591 else
2592 if cp --reflink=auto "${uefi_outdir}/linux.efi" "$outfile"; then
2593 dinfo "*** Creating UEFI image file '$outfile' done ***"
8602df70
AAF
2594 else
2595 rm -f -- "$outfile"
2596 dfatal "Creation of $outfile failed"
2597 exit 1
5a962b59
MR
2598 fi
2599 fi
62c00a88
HH
2600 else
2601 rm -f -- "$outfile"
2602 dfatal "*** Creating UEFI image file '$outfile' failed ***"
2603 exit 1
2604 fi
2605else
60928f36 2606 if cp --reflink=auto "${DRACUT_TMPDIR}/initramfs.img" "$outfile"; then
62c00a88
HH
2607 dinfo "*** Creating initramfs image file '$outfile' done ***"
2608 else
2609 rm -f -- "$outfile"
8410ee22 2610 dfatal "Creation of $outfile failed"
62c00a88
HH
2611 exit 1
2612 fi
636d2d46
HH
2613fi
2614
0386e462
MW
2615btrfs_uuid() {
2616 btrfs filesystem show "$1" | sed -n '1s/^.*uuid: //p'
2617}
2618
2619freeze_ok_for_btrfs() {
2620 local mnt uuid1 uuid2
2621 # If the output file is on btrfs, we need to make sure that it's
2622 # not on a subvolume of the same file system as the root FS.
2623 # Otherwise, fsfreeze() might freeze the entire system.
2624 # This is most conveniently checked by comparing the FS uuid.
2625
2626 [[ "$(stat -f -c %T -- "/")" == "btrfs" ]] || return 0
2627 mnt=$(stat -c %m -- "$1")
2628 uuid1=$(btrfs_uuid "$mnt")
2629 uuid2=$(btrfs_uuid "/")
75d758e8 2630 [[ $uuid1 && $uuid2 && $uuid1 != "$uuid2" ]]
0386e462
MW
2631}
2632
2633freeze_ok_for_fstype() {
2634 local outfile=$1
2635 local fstype
2636
2637 [[ "$(stat -c %m -- "$outfile")" == "/" ]] && return 1
2638 fstype=$(stat -f -c %T -- "$outfile")
2639 case $fstype in
2640 msdos)
9a52c3fd
HH
2641 return 1
2642 ;;
9582f027
SJ
2643 zfs)
2644 return 1
2645 ;;
09d3ec16
AA
2646 tmpfs)
2647 return 1
2648 ;;
0386e462 2649 btrfs)
9a52c3fd
HH
2650 freeze_ok_for_btrfs "$outfile"
2651 ;;
0386e462 2652 *)
9a52c3fd
HH
2653 return 0
2654 ;;
0386e462
MW
2655 esac
2656}
2657
740e41b8
CW
2658# We sync/fsfreeze only if we're operating on a live booted system.
2659# It's possible for e.g. `kernel` to be installed as an RPM BuildRequires or equivalent,
2660# and there's no reason to sync, and *definitely* no reason to fsfreeze.
2661# Another case where this happens is rpm-ostree, which performs its own sync/fsfreeze
2662# globally. See e.g. https://github.com/ostreedev/ostree/commit/8642ef5ab3fec3ac8eb8f193054852f83a8bc4d0
41cfdfc4 2663if [[ -d $dracutsysrootdir/run/systemd/system ]]; then
740e41b8 2664 if ! sync "$outfile" 2> /dev/null; then
8410ee22 2665 dinfo "sync operation on newly created initramfs $outfile failed"
740e41b8
CW
2666 exit 1
2667 fi
7bc681fd 2668
740e41b8 2669 # use fsfreeze only if we're not writing to /
0386e462 2670 if [[ "$(stat -c %m -- "$outfile")" != "/" ]] && freeze_ok_for_fstype "$outfile"; then
d87ae137 2671 FSFROZEN="$(dirname "$outfile")"
9a52c3fd 2672 if ! (fsfreeze -f "${FSFROZEN}" 2> /dev/null && fsfreeze -u "${FSFROZEN}" 2> /dev/null); then
8410ee22 2673 dwarn "Could not fsfreeze $(dirname "$outfile")"
740e41b8 2674 fi
d87ae137 2675 unset FSFROZEN
de576db3 2676 fi
e316ae0e
AK
2677fi
2678
3da58569 2679exit 0