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