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