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