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