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