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