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