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