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