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