]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut.sh
fix(dracut.sh): add global vars for modules-load
[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$modulesload ]] \
1669 || modulesload=$(pkg-config systemd --variable=modulesload 2> /dev/null)
1670
1671 [[ -d $dracutsysrootdir$modulesload ]] || modulesload=/usr/lib/modules-load.d
1672
1673 [[ -d $dracutsysrootdir$modulesloadconfdir ]] \
1674 || modulesloadconfdir=$(pkg-config systemd --variable=modulesloadconfdir 2> /dev/null)
1675
1676 [[ -d $dracutsysrootdir$modulesloadconfdir ]] || modulesloadconfdir=/etc/modules-load.d
1677
1678 [[ -d $dracutsysrootdir$systemdnetwork ]] \
1679 || systemdnetwork=$(pkg-config systemd --variable=systemdnetwork 2> /dev/null)
1680
1681 [[ -d $dracutsysrootdir$systemdnetwork ]] || systemdnetwork=${systemdutildir}/network
1682
1683 [[ -d $dracutsysrootdir$systemdnetworkconfdir ]] \
1684 || systemdnetworkconfdir=$(pkg-config systemd --variable=systemdnetworkconfdir 2> /dev/null)
1685
1686 [[ -d $dracutsysrootdir$systemdnetworkconfdir ]] || systemdnetworkconfdir=${systemdsystemconfdir}/network
1687
1688 [[ -d $dracutsysrootdir$systemdntpunits ]] \
1689 || systemdntpunits=$(pkg-config systemd --variable=systemdntpunits 2> /dev/null)
1690
1691 [[ -d $dracutsysrootdir$systemdntpunits ]] || systemdntpunits=${systemdutildir}/ntp-units.d
1692
1693 [[ -d $dracutsysrootdir$systemdntpunitsconfdir ]] \
1694 || systemdntpunitsconfdir=$(pkg-config systemd --variable=systemdntpunitsconfdir 2> /dev/null)
1695
1696 [[ -d $dracutsysrootdir$systemdntpunitsconfdir ]] || systemdntpunitsconfdir=${systemdsystemconfdir}/ntp-units.d
1697
1698 [[ -d $dracutsysrootdir$systemdportable ]] \
1699 || systemdportable=$(pkg-config systemd --variable=systemdportable 2> /dev/null)
1700
1701 [[ -d $dracutsysrootdir$systemdportable ]] || systemdportable=${systemdutildir}/portable
1702
1703 [[ -d $dracutsysrootdir$systemdportableconfdir ]] \
1704 || systemdportableconfdir=$(pkg-config systemd --variable=systemdportableconfdir 2> /dev/null)
1705
1706 [[ -d "$dracutsysrootdir$systemdportableconfdir" ]] || systemdportableconfdir=${systemdsystemconfdir}/portable
1707
1708 [[ -d $dracutsysrootdir$systemdsystemunitdir ]] \
1709 || systemdsystemunitdir=$(pkg-config systemd --variable=systemdsystemunitdir 2> /dev/null)
1710
1711 [[ -d "$dracutsysrootdir$systemdsystemunitdir" ]] || systemdsystemunitdir=${systemdutildir}/system
1712
1713 [[ -d $dracutsysrootdir$systemduser ]] \
1714 || systemduser=$(pkg-config systemd --variable=systemduser 2> /dev/null)
1715
1716 [[ -d $dracutsysrootdir$systemduser ]] || systemduser=${systemdutildir}/user
1717
1718 [[ -d $dracutsysrootdir$systemduserconfdir ]] \
1719 || systemduserconfdir=$(pkg-config systemd --variable=systemduserconfdir 2> /dev/null)
1720
1721 [[ -d $dracutsysrootdir$systemduserconfdir ]] || systemduserconfdir=${systemdsystemconfdir}/user
1722
1723 [[ -d $dracutsysrootdir$systemdsystemconfdir ]] \
1724 || systemdsystemconfdir=$(pkg-config systemd --variable=systemdsystemconfdir 2> /dev/null)
1725
1726 [[ -d $dracutsysrootdir$systemdsystemconfdir ]] || systemdsystemconfdir=/etc/systemd/system
1727
1728 [[ -d $dracutsysrootdir$sysusers ]] \
1729 || sysusers=$(pkg-config systemd --variable=sysusers 2> /dev/null)
1730
1731 [[ -d $dracutsysrootdir$sysusers ]] || sysusers=/usr/lib/sysusers.d
1732
1733 [[ -d $dracutsysrootdir$sysusersconfdir ]] \
1734 || sysusersconfdir=$(pkg-config systemd --variable=sysusersconfdir 2> /dev/null)
1735
1736 [[ -d $dracutsysrootdir$sysusersconfdir ]] || sysusersconfdir=/etc/sysusers.d
1737
1738 [[ -d $dracutsysrootdir$tmpfilesdir ]] \
1739 || tmpfilesdir=$(pkg-config systemd --variable=tmpfilesdir 2> /dev/null)
1740
1741 if ! [[ -d $dracutsysrootdir$tmpfilesdir ]]; then
1742 [[ -d $dracutsysrootdir/lib/tmpfiles.d ]] && tmpfilesdir=/lib/tmpfiles.d
1743 [[ -d $dracutsysrootdir/usr/lib/tmpfiles.d ]] && tmpfilesdir=/usr/lib/tmpfiles.d
1744 fi
1745
1746 export initdir dracutbasedir \
1747 dracutmodules force_add_dracutmodules add_dracutmodules omit_dracutmodules \
1748 mods_to_load \
1749 fw_dir drivers_dir debug no_kernel kernel_only \
1750 omit_drivers mdadmconf lvmconf root_devs \
1751 use_fstab fstab_lines libdirs fscks nofscks ro_mnt \
1752 stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \
1753 debug host_fs_types host_devs swap_devs sshkey add_fstab \
1754 DRACUT_VERSION udevdir prefix filesystems drivers \
1755 dbus dbusconfdir dbusinterfaces dbusinterfacesconfdir \
1756 dbusservices dbusservicesconfdir dbussession dbussessionconfdir \
1757 dbussystem dbussystemconfdir dbussystemservices dbussystemservicesconfdir \
1758 environment environmentconfdir modulesload modulesloadconfdir sysctl \
1759 sysctlconfdir sysusers sysusersconfdir systemdutildir systemdutilconfdir \
1760 systemdcatalog systemdntpunits systemdntpunitsconfdir systemdsystemunitdir \
1761 systemdsystemconfdir hostonly_cmdline loginstall \
1762 tmpfilesdir
1763
1764 mods_to_load=""
1765 # check all our modules to see if they should be sourced.
1766 # This builds a list of modules that we will install next.
1767 for_each_module_dir check_module
1768 for_each_module_dir check_mount
1769
1770 dracut_module_included "fips" && export DRACUT_FIPS_MODE=1
1771
1772 do_print_cmdline() {
1773 local -A _mods_to_print
1774 for i in $modules_loaded $mods_to_load; do
1775 _mods_to_print[$i]=1
1776 done
1777
1778 # source our modules.
1779 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1780 _d_mod=${moddir##*/}
1781 _d_mod=${_d_mod#[0-9][0-9]}
1782 [[ ${_mods_to_print[$_d_mod]} ]] || continue
1783 module_cmdline "$_d_mod" "$moddir"
1784 done
1785 unset moddir
1786 }
1787
1788 if [[ $print_cmdline ]]; then
1789 do_print_cmdline
1790 printf "\n"
1791 exit 0
1792 fi
1793
1794 # Create some directory structure first
1795 # shellcheck disable=SC2174
1796 [[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
1797
1798 # shellcheck disable=SC2174
1799 [[ -L $dracutsysrootdir/lib ]] || mkdir -m 0755 -p "${initdir}${prefix}/lib"
1800 [[ $prefix ]] && ln -sfn "${prefix#/}/lib" "$initdir/lib"
1801
1802 if [[ $prefix ]]; then
1803 for d in bin etc lib sbin tmp usr var $libdirs; do
1804 [[ $d == */* ]] && continue
1805 ln -sfn "${prefix#/}/${d#/}" "$initdir/$d"
1806 done
1807 fi
1808
1809 if [[ $kernel_only != yes ]]; then
1810 for d in usr usr/bin usr/sbin bin etc lib sbin tmp var var/tmp $libdirs; do
1811 [[ -e "${initdir}${prefix}/$d" ]] && continue
1812 if [ -L "/$d" ]; then
1813 inst_symlink "/$d" "${prefix}/$d"
1814 else
1815 # shellcheck disable=SC2174
1816 mkdir -m 0755 -p "${initdir}${prefix}/$d"
1817 fi
1818 done
1819
1820 for d in dev proc sys sysroot root run; do
1821 if [ -L "/$d" ]; then
1822 inst_symlink "/$d"
1823 else
1824 # shellcheck disable=SC2174
1825 mkdir -m 0755 -p "$initdir/$d"
1826 fi
1827 done
1828
1829 ln -sfn ../run "$initdir/var/run"
1830 ln -sfn ../run/lock "$initdir/var/lock"
1831 else
1832 for d in lib "$libdirs"; do
1833 [[ -e "${initdir}${prefix}/$d" ]] && continue
1834 if [ -h "/$d" ]; then
1835 inst "/$d" "${prefix}/$d"
1836 else
1837 # shellcheck disable=SC2174
1838 mkdir -m 0755 -p "${initdir}${prefix}/$d"
1839 fi
1840 done
1841 fi
1842
1843 if [[ $kernel_only != yes ]]; then
1844 mkdir -p "${initdir}/etc/cmdline.d"
1845 # shellcheck disable=SC2174
1846 mkdir -m 0755 -p "${initdir}"/lib "${initdir}"/lib/dracut "${initdir}"/lib/dracut/hooks
1847 # shellcheck disable=SC2154
1848 for _d in $hookdirs; do
1849 # shellcheck disable=SC2174
1850 mkdir -m 0755 -p "${initdir}/lib/dracut/hooks/$_d"
1851 done
1852 if [[ $EUID == "0" ]]; then
1853 [[ -c ${initdir}/dev/null ]] || mknod "${initdir}"/dev/null c 1 3
1854 [[ -c ${initdir}/dev/kmsg ]] || mknod "${initdir}"/dev/kmsg c 1 11
1855 [[ -c ${initdir}/dev/console ]] || mknod "${initdir}"/dev/console c 5 1
1856 [[ -c ${initdir}/dev/random ]] || mknod "${initdir}"/dev/random c 1 8
1857 [[ -c ${initdir}/dev/urandom ]] || mknod "${initdir}"/dev/urandom c 1 9
1858 fi
1859 fi
1860
1861 _isize=0 #initramfs size
1862 modules_loaded=" "
1863 # source our modules.
1864 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
1865 _d_mod=${moddir##*/}
1866 _d_mod=${_d_mod#[0-9][0-9]}
1867 [[ $mods_to_load == *\ $_d_mod\ * ]] || continue
1868 if [[ $show_modules == yes ]]; then
1869 printf "%s\n" "$_d_mod"
1870 else
1871 dinfo "*** Including module: $_d_mod ***"
1872 fi
1873 if [[ $kernel_only == yes ]]; then
1874 module_installkernel "$_d_mod" "$moddir" || {
1875 dfatal "installkernel failed in module $_d_mod"
1876 exit 1
1877 }
1878 else
1879 module_install "$_d_mod" "$moddir"
1880 if [[ $no_kernel != yes ]]; then
1881 module_installkernel "$_d_mod" "$moddir" || {
1882 dfatal "installkernel failed in module $_d_mod"
1883 exit 1
1884 }
1885 fi
1886 fi
1887 mods_to_load=${mods_to_load// $_d_mod /}
1888 modules_loaded+="$_d_mod "
1889
1890 #print the module install size
1891 if [ -n "$printsize" ]; then
1892 _isize_new=$(du -sk "${initdir}" | {
1893 read -r a _
1894 echo -n "$a"
1895 })
1896 _isize_delta=$((_isize_new - _isize))
1897 printf "%s\n" "$_d_mod install size: ${_isize_delta}k"
1898 _isize=$_isize_new
1899 fi
1900 done
1901 unset moddir
1902
1903 for i in $modules_loaded; do
1904 mkdir -p "$initdir"/lib/dracut
1905 printf "%s\n" "$i" >> "$initdir"/lib/dracut/modules.txt
1906 done
1907
1908 dinfo "*** Including modules done ***"
1909
1910 ## final stuff that has to happen
1911 if [[ $no_kernel != yes ]]; then
1912 if [[ $hostonly_mode == "strict" ]]; then
1913 cp "$DRACUT_KERNEL_MODALIASES" "$initdir"/lib/dracut/hostonly-kernel-modules.txt
1914 fi
1915
1916 if [[ $drivers ]]; then
1917 # shellcheck disable=SC2086
1918 hostonly='' instmods $drivers
1919 fi
1920
1921 if [[ -n ${add_drivers// /} ]]; then
1922 # shellcheck disable=SC2086
1923 hostonly='' instmods -c $add_drivers
1924 fi
1925 if [[ $force_drivers ]]; then
1926 # shellcheck disable=SC2086
1927 hostonly='' instmods -c $force_drivers
1928 rm -f "$initdir"/etc/cmdline.d/20-force_driver.conf
1929 for mod in $force_drivers; do
1930 echo "rd.driver.pre=$mod" >> "$initdir"/etc/cmdline.d/20-force_drivers.conf
1931 done
1932 fi
1933 if [[ $filesystems ]]; then
1934 # shellcheck disable=SC2086
1935 hostonly='' instmods -c $filesystems
1936 fi
1937
1938 dinfo "*** Installing kernel module dependencies ***"
1939 dracut_kernel_post
1940 dinfo "*** Installing kernel module dependencies done ***"
1941
1942 if [[ $noimageifnotneeded == yes ]] && [[ $hostonly ]]; then
1943 if [[ ! -f "$initdir/lib/dracut/need-initqueue" ]] \
1944 && [[ -f ${initdir}/lib/modules/$kernel/modules.dep && ! -s ${initdir}/lib/modules/$kernel/modules.dep ]]; then
1945 for i in "${initdir}"/etc/cmdline.d/*.conf; do
1946 # We need no initramfs image and do not generate one.
1947 [[ $i == "${initdir}/etc/cmdline.d/*.conf" ]] && exit 0
1948 done
1949 fi
1950 fi
1951 fi
1952
1953 if [[ $kernel_only != yes ]]; then
1954 # FIXME: handle legacy item split
1955 # shellcheck disable=SC2068
1956 ((${#install_items[@]} > 0)) && inst_multiple ${install_items[@]}
1957 # shellcheck disable=SC2068
1958 ((${#install_optional_items[@]} > 0)) && inst_multiple -o ${install_optional_items[@]}
1959
1960 if [[ $kernel_cmdline ]] && [[ $uefi != yes ]]; then
1961 printf "%s\n" "$kernel_cmdline" >> "${initdir}/etc/cmdline.d/01-default.conf"
1962 fi
1963
1964 for line in "${fstab_lines[@]}"; do
1965 read -r -a fstab_field <<< "$line"
1966 if [ -z "${fstab_field[1]}" ]; then
1967 # Determine device and mount options from current system
1968 mountpoint -q "${fstab_field[0]}" || derror "${fstab_field[0]} is not a mount point!"
1969 read -r -a fstab_field < <(findmnt --raw -n --target "${fstab_field[0]}" --output=source,target,fstype,options)
1970 dinfo "Line for ${fstab_field[1]}: ${fstab_field[*]}"
1971 else
1972 # Use default options
1973 [ -z "${fstab_field[3]}" ] && fstab_field[3]="defaults"
1974 fi
1975
1976 # Default options for freq and passno
1977 [ -z "${fstab_field[4]}" ] && fstab_field[4]="0"
1978 [ -z "${fstab_field[5]}" ] && fstab_field[5]="2"
1979
1980 strstr "${fstab_field[2]}" "nfs" && fstab_field[5]="0"
1981 echo "${fstab_field[@]}" >> "${initdir}/etc/fstab"
1982 done
1983
1984 for f in $add_fstab; do
1985 cat "$f" >> "${initdir}/etc/fstab"
1986 done
1987
1988 if [[ $dracutsysrootdir$systemdutildir ]]; then
1989 if [[ -d ${initdir}/$systemdutildir ]]; then
1990 mkdir -p "${initdir}"/etc/conf.d
1991 {
1992 printf "%s\n" "systemdutildir=\"$systemdutildir\""
1993 printf "%s\n" "systemdsystemunitdir=\"$systemdsystemunitdir\""
1994 printf "%s\n" "systemdsystemconfdir=\"$systemdsystemconfdir\""
1995 } > "${initdir}"/etc/conf.d/systemd.conf
1996 fi
1997 fi
1998
1999 if [[ $DRACUT_RESOLVE_LAZY ]] && [[ $DRACUT_INSTALL ]]; then
2000 dinfo "*** Resolving executable dependencies ***"
2001 find "$initdir" -type f -perm /0111 -not -path '*.ko' -print0 \
2002 | xargs -r -0 "$DRACUT_INSTALL" ${initdir:+-D "$initdir"} ${dracutsysrootdir:+-r "$dracutsysrootdir"} -R ${DRACUT_FIPS_MODE:+-f} --
2003 dinfo "*** Resolving executable dependencies done ***"
2004 fi
2005
2006 # Now we are done with lazy resolving, always install dependencies
2007 unset DRACUT_RESOLVE_LAZY
2008 export DRACUT_RESOLVE_DEPS=1
2009 fi
2010
2011 for ((i = 0; i < ${#include_src[@]}; i++)); do
2012 src="${include_src[$i]}"
2013 target="${include_target[$i]}"
2014 if [[ $src && $target ]]; then
2015 if [[ -f $src ]]; then
2016 inst "$src" "$target"
2017 elif [[ -d $src ]]; then
2018 ddebug "Including directory: $src"
2019 destdir="${initdir}/${target}"
2020 mkdir -p "$destdir"
2021 # check for preexisting symlinks, so we can cope with the
2022 # symlinks to $prefix
2023 # Objectname is a file or a directory
2024 for objectname in "$src"/*; do
2025 [[ -e $objectname || -L $objectname ]] || continue
2026 if [[ -d $objectname ]]; then
2027 # objectname is a directory, let's compute the final directory name
2028 object_destdir=${destdir}/${objectname#$src/}
2029 if ! [[ -e $object_destdir ]]; then
2030 # shellcheck disable=SC2174
2031 mkdir -m 0755 -p "$object_destdir"
2032 chmod --reference="$objectname" "$object_destdir"
2033 fi
2034 $DRACUT_CP -t "$object_destdir" "$dracutsysrootdir$objectname"/*
2035 else
2036 $DRACUT_CP -t "$destdir" "$dracutsysrootdir$objectname"
2037 fi
2038 done
2039 elif [[ -e $src ]]; then
2040 derror "$src is neither a directory nor a regular file"
2041 else
2042 derror "$src doesn't exist"
2043 fi
2044 fi
2045 done
2046
2047 if [[ $do_hardlink == yes ]] && command -v hardlink > /dev/null; then
2048 dinfo "*** Hardlinking files ***"
2049 hardlink "$initdir" 2>&1
2050 dinfo "*** Hardlinking files done ***"
2051 fi
2052
2053 # strip binaries
2054 if [[ $do_strip == yes ]]; then
2055 # Prefer strip from elfutils for package size
2056 declare strip_cmd
2057 strip_cmd=$(command -v eu-strip)
2058 [ -z "$strip_cmd" ] && strip_cmd="strip"
2059
2060 for p in $strip_cmd xargs find; do
2061 if ! type -P $p > /dev/null; then
2062 dinfo "Could not find '$p'. Not stripping the initramfs."
2063 do_strip=no
2064 fi
2065 done
2066 fi
2067
2068 # cleanup empty ldconfig_paths directories
2069 for d in $(ldconfig_paths); do
2070 rmdir -p --ignore-fail-on-non-empty "$initdir/$d" > /dev/null 2>&1
2071 done
2072
2073 if [[ $early_microcode == yes ]]; then
2074 dinfo "*** Generating early-microcode cpio image ***"
2075 ucode_dir=(amd-ucode intel-ucode)
2076 ucode_dest=(AuthenticAMD.bin GenuineIntel.bin)
2077 _dest_dir="$early_cpio_dir/d/kernel/x86/microcode"
2078 _dest_idx="0 1"
2079 mkdir -p "$_dest_dir"
2080 if [[ $hostonly ]]; then
2081 [[ $(get_cpu_vendor) == "AMD" ]] && _dest_idx="0"
2082 [[ $(get_cpu_vendor) == "Intel" ]] && _dest_idx="1"
2083 fi
2084 for idx in $_dest_idx; do
2085 _fw=${ucode_dir[$idx]}
2086 for _fwdir in $fw_dir; do
2087 if [[ -d $_fwdir && -d $_fwdir/$_fw ]]; then
2088 _src="*"
2089 dinfo "*** Constructing ${ucode_dest[$idx]} ***"
2090 if [[ $hostonly ]]; then
2091 _src=$(get_ucode_file)
2092 [[ $_src ]] || break
2093 [[ -r $_fwdir/$_fw/$_src ]] || _src="${_src}.early"
2094 [[ -r $_fwdir/$_fw/$_src ]] || break
2095 fi
2096
2097 for i in $_fwdir/$_fw/$_src; do
2098 [ -e "$i" ] && break
2099 break 2
2100 done
2101 for i in $_fwdir/$_fw/$_src; do
2102 [[ -e $i ]] || continue
2103 # skip gpg files
2104 str_ends "$i" ".asc" && continue
2105 cat "$i" >> "$_dest_dir/${ucode_dest[$idx]}"
2106 done
2107 create_early_cpio="yes"
2108 fi
2109 done
2110 if [[ ! -e "$_dest_dir/${ucode_dest[$idx]}" ]]; then
2111 if cd "$early_cpio_dir/d"; then
2112 for _ucodedir in "${early_microcode_image_dir[@]}"; do
2113 for _ucodename in "${early_microcode_image_name[@]}"; do
2114 [[ -e "$_ucodedir/$_ucodename" ]] \
2115 && cpio --extract --file "$_ucodedir/$_ucodename" --quiet \
2116 "kernel/x86/microcode/${ucode_dest[$idx]}"
2117 if [[ -e "$_dest_dir/${ucode_dest[$idx]}" ]]; then
2118 dinfo "*** Using microcode found in '$_ucodedir/$_ucodename' ***"
2119 create_early_cpio="yes"
2120 break 2
2121 fi
2122 done
2123 done
2124 fi
2125 fi
2126 done
2127 fi
2128
2129 if [[ $acpi_override == yes ]] && [[ -d $acpi_table_dir ]]; then
2130 dinfo "*** Packaging ACPI tables to override BIOS provided ones ***"
2131 _dest_dir="$early_cpio_dir/d/kernel/firmware/acpi"
2132 mkdir -p "$_dest_dir"
2133 for table in "$acpi_table_dir"/*.aml; do
2134 dinfo " Adding ACPI table: $table"
2135 $DRACUT_CP "$table" "$_dest_dir"
2136 create_early_cpio="yes"
2137 done
2138 fi
2139
2140 dinfo "*** Store current command line parameters ***"
2141 if ! (echo "$PARMS_TO_STORE" > "$initdir"/lib/dracut/build-parameter.txt); then
2142 dfatal "Could not store the current command line parameters"
2143 exit 1
2144 fi
2145
2146 if [[ $hostonly_cmdline == "yes" ]]; then
2147 unset _stored_cmdline
2148 if [[ -d $initdir/etc/cmdline.d ]]; then
2149 dinfo "Stored kernel commandline:"
2150 for conf in "$initdir"/etc/cmdline.d/*.conf; do
2151 [ -e "$conf" ] || continue
2152 dinfo "$(< "$conf")"
2153 _stored_cmdline=1
2154 done
2155 fi
2156 if ! [[ $_stored_cmdline ]]; then
2157 dinfo "No dracut internal kernel commandline stored in the initramfs"
2158 fi
2159 fi
2160
2161 if [[ $kernel_only != yes ]]; then
2162 # libpthread workaround: pthread_cancel wants to dlopen libgcc_s.so
2163 for _dir in $libdirs; do
2164 for _f in "$dracutsysrootdir$_dir/libpthread.so"*; do
2165 [[ -e $_f ]] || continue
2166 inst_libdir_file "libgcc_s.so*"
2167 break 2
2168 done
2169 done
2170
2171 # FIPS workaround for Fedora/RHEL: libcrypto needs libssl when FIPS is enabled
2172 if [[ $DRACUT_FIPS_MODE ]]; then
2173 for _dir in $libdirs; do
2174 for _f in "$dracutsysrootdir$_dir/libcrypto.so"*; do
2175 [[ -e $_f ]] || continue
2176 inst_libdir_file -o "libssl.so*"
2177 break 2
2178 done
2179 done
2180 fi
2181 fi
2182
2183 if [[ $kernel_only != yes ]]; then
2184 # make sure that library links are correct and up to date
2185 for f in "$dracutsysrootdir"/etc/ld.so.conf "$dracutsysrootdir"/etc/ld.so.conf.d/*; do
2186 [[ -f $f ]] && inst_simple "${f#$dracutsysrootdir}"
2187 done
2188 if ! $DRACUT_LDCONFIG -r "$initdir" -f /etc/ld.so.conf; then
2189 if [[ $EUID == 0 ]]; then
2190 derror "ldconfig exited ungracefully"
2191 else
2192 derror "ldconfig might need uid=0 (root) for chroot()"
2193 fi
2194 fi
2195 fi
2196
2197 if dracut_module_included "squash"; then
2198 readonly squash_dir="$initdir/squash/root"
2199 readonly squash_img="$initdir/squash-root.img"
2200 mkdir -p "$squash_dir"
2201 dinfo "*** Install squash loader ***"
2202 DRACUT_SQUASH_POST_INST=1 module_install "squash"
2203 fi
2204
2205 if [[ $do_strip == yes ]] && ! [[ $DRACUT_FIPS_MODE ]]; then
2206 dinfo "*** Stripping files ***"
2207 find "$initdir" -type f \
2208 -executable -not -path '*/lib/modules/*.ko' -print0 \
2209 | xargs -r -0 $strip_cmd -g -p 2> /dev/null
2210
2211 # strip kernel modules, but do not touch signed modules
2212 find "$initdir" -type f -path '*/lib/modules/*.ko' -print0 \
2213 | while read -r -d $'\0' f || [ -n "$f" ]; do
2214 SIG=$(tail -c 28 "$f" | tr -d '\000')
2215 [[ $SIG == '~Module signature appended~' ]] || { printf "%s\000" "$f"; }
2216 done | xargs -r -0 $strip_cmd -g -p
2217 dinfo "*** Stripping files done ***"
2218 fi
2219
2220 if dracut_module_included "squash"; then
2221 dinfo "*** Squashing the files inside the initramfs ***"
2222 declare squash_compress_arg
2223 # shellcheck disable=SC2086
2224 if [[ $compress ]]; then
2225 if ! mksquashfs /dev/null "$DRACUT_TMPDIR"/.squash-test.img -no-progress -comp $compress &> /dev/null; then
2226 dwarn "mksquashfs doesn't support compressor '$compress', failing back to default compressor."
2227 else
2228 squash_compress_arg="$compress"
2229 fi
2230 fi
2231
2232 # shellcheck disable=SC2086
2233 if ! mksquashfs "$squash_dir" "$squash_img" \
2234 -no-xattrs -no-exports -noappend -no-recovery -always-use-fragments \
2235 -no-progress ${squash_compress_arg:+-comp $squash_compress_arg} 1> /dev/null; then
2236 dfatal "dracut: Failed making squash image"
2237 exit 1
2238 fi
2239
2240 rm -rf "$squash_dir"
2241 dinfo "*** Squashing the files inside the initramfs done ***"
2242
2243 # Skip initramfs compress
2244 compress="cat"
2245 fi
2246
2247 dinfo "*** Creating image file '$outfile' ***"
2248
2249 if [[ $uefi == yes ]]; then
2250 readonly uefi_outdir="$DRACUT_TMPDIR/uefi"
2251 mkdir -p "$uefi_outdir"
2252 fi
2253
2254 if [[ $DRACUT_REPRODUCIBLE ]]; then
2255 find "$initdir" -newer "$dracutbasedir/dracut-functions.sh" -print0 \
2256 | xargs -r -0 touch -h -m -c -r "$dracutbasedir/dracut-functions.sh"
2257
2258 if [[ "$(cpio --help)" == *--reproducible* ]]; then
2259 CPIO_REPRODUCIBLE=1
2260 else
2261 dinfo "cpio does not support '--reproducible'. Resulting image will not be reproducible."
2262 fi
2263 fi
2264
2265 [[ $EUID != 0 ]] && cpio_owner="0:0"
2266
2267 if [[ $create_early_cpio == yes ]]; then
2268 echo 1 > "$early_cpio_dir/d/early_cpio"
2269
2270 if [[ $DRACUT_REPRODUCIBLE ]]; then
2271 find "$early_cpio_dir/d" -newer "$dracutbasedir/dracut-functions.sh" -print0 \
2272 | xargs -r -0 touch -h -m -c -r "$dracutbasedir/dracut-functions.sh"
2273 fi
2274
2275 # The microcode blob is _before_ the initramfs blob, not after
2276 if ! (
2277 umask 077
2278 cd "$early_cpio_dir/d"
2279 find . -print0 | sort -z \
2280 | cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null \
2281 ${cpio_owner:+-R "$cpio_owner"} -H newc -o --quiet > "${DRACUT_TMPDIR}/initramfs.img"
2282 ); then
2283 dfatal "dracut: creation of $outfile failed"
2284 exit 1
2285 fi
2286 fi
2287
2288 if ! [[ $compress ]]; then
2289 # check all known compressors, if none specified
2290 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
2291 command -v "$i" &> /dev/null || continue
2292 compress="$i"
2293 break
2294 done
2295 if [[ $compress == cat ]]; then
2296 printf "%s\n" "dracut: no compression tool available. Initramfs image is going to be big." >&2
2297 fi
2298 fi
2299
2300 # choose the right arguments for the compressor
2301 case $compress in
2302 bzip2 | lbzip2)
2303 if [[ $compress == lbzip2 ]] || command -v "$DRACUT_COMPRESS_LBZIP2" &> /dev/null; then
2304 compress="$DRACUT_COMPRESS_LBZIP2 -9"
2305 else
2306 compress="$DRACUT_COMPRESS_BZIP2 -9"
2307 fi
2308 ;;
2309 lzma)
2310 compress="$DRACUT_COMPRESS_LZMA -9 -T0"
2311 ;;
2312 xz)
2313 compress="$DRACUT_COMPRESS_XZ --check=crc32 --lzma2=dict=1MiB -T0"
2314 ;;
2315 gzip | pigz)
2316 if [[ $compress == pigz ]] || command -v "$DRACUT_COMPRESS_PIGZ" &> /dev/null; then
2317 compress="$DRACUT_COMPRESS_PIGZ -9 -n -T -R"
2318 elif command -v gzip &> /dev/null && $DRACUT_COMPRESS_GZIP --help 2>&1 | grep -q rsyncable; then
2319 compress="$DRACUT_COMPRESS_GZIP -n -9 --rsyncable"
2320 else
2321 compress="$DRACUT_COMPRESS_GZIP -n -9"
2322 fi
2323 ;;
2324 lzo | lzop)
2325 compress="$DRACUT_COMPRESS_LZOP -9"
2326 ;;
2327 lz4)
2328 compress="$DRACUT_COMPRESS_LZ4 -l -9"
2329 ;;
2330 zstd)
2331 compress="$DRACUT_COMPRESS_ZSTD -15 -q -T0"
2332 ;;
2333 esac
2334
2335 if ! (
2336 umask 077
2337 cd "$initdir"
2338 find . -print0 | sort -z \
2339 | cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null ${cpio_owner:+-R "$cpio_owner"} -H newc -o --quiet \
2340 | $compress >> "${DRACUT_TMPDIR}/initramfs.img"
2341 ); then
2342 dfatal "dracut: creation of $outfile failed"
2343 exit 1
2344 fi
2345
2346 # shellcheck disable=SC2154
2347 if ((maxloglvl >= 5)) && ((verbosity_mod_l >= 0)); then
2348 if [[ $allowlocal ]]; then
2349 "$dracutbasedir/lsinitrd.sh" "${DRACUT_TMPDIR}/initramfs.img" | ddebug
2350 else
2351 lsinitrd "${DRACUT_TMPDIR}/initramfs.img" | ddebug
2352 fi
2353 fi
2354
2355 umask 077
2356
2357 if [[ $uefi == yes ]]; then
2358 if [[ $kernel_cmdline ]]; then
2359 echo -n "$kernel_cmdline" > "$uefi_outdir/cmdline.txt"
2360 elif [[ $hostonly_cmdline == yes ]] && [ -d "$initdir/etc/cmdline.d" ]; then
2361 for conf in "$initdir"/etc/cmdline.d/*.conf; do
2362 [ -e "$conf" ] || continue
2363 printf "%s " "$(< "$conf")" >> "$uefi_outdir/cmdline.txt"
2364 done
2365 fi
2366
2367 if [[ $kernel_cmdline ]] || [[ $hostonly_cmdline == yes && -d "$initdir/etc/cmdline.d" ]]; then
2368 echo -ne "\x00" >> "$uefi_outdir/cmdline.txt"
2369 dinfo "Using UEFI kernel cmdline:"
2370 dinfo "$(tr -d '\000' < "$uefi_outdir/cmdline.txt")"
2371 uefi_cmdline="${uefi_outdir}/cmdline.txt"
2372 else
2373 unset uefi_cmdline
2374 fi
2375
2376 [[ -s $dracutsysrootdir/usr/lib/os-release ]] && uefi_osrelease="$dracutsysrootdir/usr/lib/os-release"
2377 [[ -s $dracutsysrootdir/etc/os-release ]] && uefi_osrelease="$dracutsysrootdir/etc/os-release"
2378 if [[ -s ${dracutsysrootdir}${uefi_splash_image} ]]; then
2379 uefi_splash_image="${dracutsysrootdir}${uefi_splash_image}"
2380 else
2381 unset uefi_splash_image
2382 fi
2383
2384 if objcopy \
2385 ${uefi_osrelease:+--add-section .osrel="$uefi_osrelease" --change-section-vma .osrel=0x20000} \
2386 ${uefi_cmdline:+--add-section .cmdline="$uefi_cmdline" --change-section-vma .cmdline=0x30000} \
2387 ${uefi_splash_image:+--add-section .splash="$uefi_splash_image" --change-section-vma .splash=0x40000} \
2388 --add-section .linux="$kernel_image" --change-section-vma .linux=0x2000000 \
2389 --add-section .initrd="${DRACUT_TMPDIR}/initramfs.img" --change-section-vma .initrd=0x3000000 \
2390 "$uefi_stub" "${uefi_outdir}/linux.efi"; then
2391 if [[ -n ${uefi_secureboot_key} && -n ${uefi_secureboot_cert} ]]; then
2392 if sbsign \
2393 --key "${uefi_secureboot_key}" \
2394 --cert "${uefi_secureboot_cert}" \
2395 --output "$outfile" "${uefi_outdir}/linux.efi"; then
2396 dinfo "*** Creating signed UEFI image file '$outfile' done ***"
2397 else
2398 dfatal "*** Creating signed UEFI image file '$outfile' failed ***"
2399 exit 1
2400 fi
2401 else
2402 if cp --reflink=auto "${uefi_outdir}/linux.efi" "$outfile"; then
2403 dinfo "*** Creating UEFI image file '$outfile' done ***"
2404 fi
2405 fi
2406 else
2407 rm -f -- "$outfile"
2408 dfatal "*** Creating UEFI image file '$outfile' failed ***"
2409 exit 1
2410 fi
2411 else
2412 if cp --reflink=auto "${DRACUT_TMPDIR}/initramfs.img" "$outfile"; then
2413 dinfo "*** Creating initramfs image file '$outfile' done ***"
2414 else
2415 rm -f -- "$outfile"
2416 dfatal "dracut: creation of $outfile failed"
2417 exit 1
2418 fi
2419 fi
2420
2421 command -v restorecon &> /dev/null && restorecon -- "$outfile"
2422
2423 btrfs_uuid() {
2424 btrfs filesystem show "$1" | sed -n '1s/^.*uuid: //p'
2425 }
2426
2427 freeze_ok_for_btrfs() {
2428 local mnt uuid1 uuid2
2429 # If the output file is on btrfs, we need to make sure that it's
2430 # not on a subvolume of the same file system as the root FS.
2431 # Otherwise, fsfreeze() might freeze the entire system.
2432 # This is most conveniently checked by comparing the FS uuid.
2433
2434 [[ "$(stat -f -c %T -- "/")" == "btrfs" ]] || return 0
2435 mnt=$(stat -c %m -- "$1")
2436 uuid1=$(btrfs_uuid "$mnt")
2437 uuid2=$(btrfs_uuid "/")
2438 [[ $uuid1 && $uuid2 && $uuid1 != "$uuid2" ]]
2439 }
2440
2441 freeze_ok_for_fstype() {
2442 local outfile=$1
2443 local fstype
2444
2445 [[ "$(stat -c %m -- "$outfile")" == "/" ]] && return 1
2446 fstype=$(stat -f -c %T -- "$outfile")
2447 case $fstype in
2448 msdos)
2449 return 1
2450 ;;
2451 btrfs)
2452 freeze_ok_for_btrfs "$outfile"
2453 ;;
2454 *)
2455 return 0
2456 ;;
2457 esac
2458 }
2459
2460 # We sync/fsfreeze only if we're operating on a live booted system.
2461 # It's possible for e.g. `kernel` to be installed as an RPM BuildRequires or equivalent,
2462 # and there's no reason to sync, and *definitely* no reason to fsfreeze.
2463 # Another case where this happens is rpm-ostree, which performs its own sync/fsfreeze
2464 # globally. See e.g. https://github.com/ostreedev/ostree/commit/8642ef5ab3fec3ac8eb8f193054852f83a8bc4d0
2465 if [[ -d $dracutsysrootdir/run/systemd/system ]]; then
2466 if ! sync "$outfile" 2> /dev/null; then
2467 dinfo "dracut: sync operation on newly created initramfs $outfile failed"
2468 exit 1
2469 fi
2470
2471 # use fsfreeze only if we're not writing to /
2472 if [[ "$(stat -c %m -- "$outfile")" != "/" ]] && freeze_ok_for_fstype "$outfile"; then
2473 FSFROZEN="$(dirname "$outfile")"
2474 if ! (fsfreeze -f "${FSFROZEN}" 2> /dev/null && fsfreeze -u "${FSFROZEN}" 2> /dev/null); then
2475 dinfo "dracut: warning: could not fsfreeze $(dirname "$outfile")"
2476 fi
2477 unset FSFROZEN
2478 fi
2479 fi
2480
2481 exit 0