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