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