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