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