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