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