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