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