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