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