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