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