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