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