]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut.sh
dracut-functions.sh: only wait for the last pid spawned
[thirdparty/dracut.git] / dracut.sh
CommitLineData
c0815e4e 1#!/bin/bash
cc02093d
HH
2# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3# ex: ts=8 sw=4 sts=4 et filetype=sh
3b403b32 4#
641cc356
JK
5# Generator script for a dracut initramfs
6# Tries to retain some degree of compatibility with the command line
7# of the various mkinitrd implementations out there
8#
70c26b7f 9
7d848c55 10# Copyright 2005-2013 Red Hat, Inc. All rights reserved.
4f18fe82
HH
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program. If not, see <http://www.gnu.org/licenses/>.
24#
ec9315e5 25
58dad702 26# store for logging
ffa71b4a 27dracut_args=( "$@" )
5616feb0 28
7abd4264
HH
29set -o pipefail
30
5616feb0 31usage() {
dbf8f6ba
HH
32 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
33 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
34 . $dracutbasedir/dracut-version.sh
35 fi
36
5616feb0 37# 80x25 linebreak here ^
cc02093d 38 cat << EOF
ffa71b4a
HH
39Usage: $0 [OPTION]... [<initramfs> [<kernel-version>]]
40
41Version: $DRACUT_VERSION
42
43Creates initial ramdisk images for preloading modules
44
45 -h, --help Display all options
46
47If a [LIST] has multiple arguments, then you have to put these in quotes.
48
49For example:
50
51 # dracut --add-drivers "module1 module2" ...
52
53EOF
54}
55
56long_usage() {
57 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
58 if [[ -f $dracutbasedir/dracut-version.sh ]]; then
59 . $dracutbasedir/dracut-version.sh
60 fi
61
62# 80x25 linebreak here ^
63 cat << EOF
64Usage: $0 [OPTION]... [<initramfs> [<kernel-version>]]
dbf8f6ba
HH
65
66Version: $DRACUT_VERSION
67
5616feb0
AT
68Creates initial ramdisk images for preloading modules
69
e65caf36 70 --kver [VERSION] Set kernel version to [VERSION].
39ff0682 71 -f, --force Overwrite existing initramfs file.
54e7d7c3 72 -a, --add [LIST] Add a space-separated list of dracut modules.
5616feb0
AT
73 -m, --modules [LIST] Specify a space-separated list of dracut modules to
74 call when building the initramfs. Modules are located
c5a65990 75 in /usr/lib/dracut/modules.d.
39ff0682 76 -o, --omit [LIST] Omit a space-separated list of dracut modules.
ea3c4e82
HH
77 --force-add [LIST] Force to add a space-separated list of dracut modules
78 to the default set of modules, when -H is specified.
5616feb0 79 -d, --drivers [LIST] Specify a space-separated list of kernel modules to
ea3c4e82
HH
80 exclusively include in the initramfs.
81 --add-drivers [LIST] Specify a space-separated list of kernel
82 modules to add to the initramfs.
fcbcb252 83 --omit-drivers [LIST] Specify a space-separated list of kernel
ea3c4e82 84 modules not to add to the initramfs.
8fa510d4 85 --filesystems [LIST] Specify a space-separated list of kernel filesystem
ea3c4e82
HH
86 modules to exclusively include in the generic
87 initramfs.
3b403b32 88 -k, --kmoddir [DIR] Specify the directory, where to look for kernel
ea3c4e82 89 modules
3b403b32 90 --fwdir [DIR] Specify additional directories, where to look for
ea3c4e82 91 firmwares, separated by :
33ee031c
HH
92 --kernel-only Only install kernel drivers and firmware files
93 --no-kernel Do not install kernel drivers and firmware files
5bbfd484 94 --early-microcode Combine early microcode with ramdisk
5f2c30d9 95 --no-early-microcode Do not combine early microcode with ramdisk
d20fb951 96 --kernel-cmdline [PARAMETERS] Specify default kernel command line parameters
54e7d7c3 97 --strip Strip binaries in the initramfs
6c128565 98 --nostrip Do not strip binaries in the initramfs
54e7d7c3 99 --hardlink Hardlink files in the initramfs
04d18f55 100 --nohardlink Do not hardlink files in the initramfs
fd786adc 101 --prefix [DIR] Prefix initramfs files with [DIR]
54e7d7c3 102 --noprefix Do not prefix initramfs files
2f02ae9d
HH
103 --mdadmconf Include local /etc/mdadm.conf
104 --nomdadmconf Do not include local /etc/mdadm.conf
7a34efa5 105 --lvmconf Include local /etc/lvm/lvm.conf
cc02093d 106 --nolvmconf Do not include local /etc/lvm/lvm.conf
25b45979
MS
107 --fscks [LIST] Add a space-separated list of fsck helpers.
108 --nofscks Inhibit installation of any fsck helpers.
ff3953ef 109 --ro-mnt Mount / and /usr read-only by default.
5616feb0 110 -h, --help This message
00531568 111 --debug Output debug information of the build process
57258a2c 112 --profile Output profile information of the build process
e103615b
113 -L, --stdlog [0-6] Specify logging level (to standard error)
114 0 - suppress any messages
115 1 - only fatal errors
116 2 - all errors
117 3 - warnings
54e7d7c3 118 4 - info
e103615b
119 5 - debug info (here starts lots of output)
120 6 - trace info (and even more)
54e7d7c3
HH
121 -v, --verbose Increase verbosity level
122 -q, --quiet Decrease verbosity level
5616feb0
AT
123 -c, --conf [FILE] Specify configuration file to use.
124 Default: /etc/dracut.conf
3b403b32 125 --confdir [DIR] Specify configuration directory to use *.conf files
cc02093d 126 from. Default: /etc/dracut.conf.d
882c4c5a
127 --tmpdir [DIR] Temporary directory to be used instead of default
128 /var/tmp.
5616feb0
AT
129 -l, --local Local mode. Use modules from the current working
130 directory instead of the system-wide installed in
c5a65990 131 /usr/lib/dracut/modules.d.
5616feb0 132 Useful when running dracut from a git checkout.
7c179686 133 -H, --hostonly Host-Only mode: Install only what is needed for
5616feb0 134 booting the local host instead of a generic host.
472189da 135 -N, --no-hostonly Disables Host-Only mode
7c179686 136 --fstab Use /etc/fstab to determine the root device.
70cb8a68
HH
137 --add-fstab [FILE] Add file to the initramfs fstab
138 --mount "[DEV] [MP] [FSTYPE] [FSOPTS]"
139 Mount device [DEV] on mountpoint [MP] with filesystem
140 [FSTYPE] and options [FSOPTS] in the initramfs
c586b033 141 --add-device "[DEV]" Bring up [DEV] in initramfs
5616feb0 142 -i, --include [SOURCE] [TARGET]
39ff0682
AT
143 Include the files in the SOURCE directory into the
144 Target directory in the final initramfs.
3b403b32 145 If SOURCE is a file, it will be installed to TARGET
4fea3ea6 146 in the final initramfs.
39ff0682
AT
147 -I, --install [LIST] Install the space separated list of files into the
148 initramfs.
5b158ad3 149 --gzip Compress the generated initramfs using gzip.
cc02093d
HH
150 This will be done by default, unless another
151 compression option or --no-compress is passed.
5b158ad3 152 --bzip2 Compress the generated initramfs using bzip2.
cc02093d
HH
153 Make sure your kernel has bzip2 decompression support
154 compiled in, otherwise you will not be able to boot.
155 --lzma Compress the generated initramfs using lzma.
3b403b32 156 Make sure your kernel has lzma support compiled in,
cc02093d 157 otherwise you will not be able to boot.
5e6c3b03
VL
158 --xz Compress the generated initramfs using xz.
159 Make sure that your kernel has xz support compiled
160 in, otherwise you will not be able to boot.
161 --compress [COMPRESSION] Compress the generated initramfs with the
162 passed compression program. Make sure your kernel
3b403b32 163 knows how to decompress the generated initramfs,
5e6c3b03 164 otherwise you will not be able to boot.
5b158ad3 165 --no-compress Do not compress the generated initramfs. This will
cc02093d 166 override any other compression options.
5b11bb73 167 --list-modules List all available dracut modules.
956af8f2
168 -M, --show-modules Print included module's name to standard output during
169 build.
5fe5c7d0 170 --keep Keep the temporary initramfs for debugging purposes
ec3c5951 171 --printsize Print out the module install size
cdfeb278 172 --sshkey [SSHKEY] Add ssh key to initramfs (use with ssh-client module)
ccaa9bee
HH
173
174If [LIST] has multiple arguments, then you have to put these in quotes.
ffa71b4a 175
ccaa9bee 176For example:
ffa71b4a
HH
177
178 # dracut --add-drivers "module1 module2" ...
179
cc02093d 180EOF
5616feb0
AT
181}
182
8466db96
HH
183# function push()
184# push values to a stack
185# $1 = stack variable
186# $2.. values
187# example:
188# push stack 1 2 "3 4"
189push() {
ffa71b4a 190 local _i
8466db96 191 local __stack=$1; shift
ffa71b4a
HH
192 for _i in "$@"; do
193 eval ${__stack}'[${#'${__stack}'[@]}]="$_i"'
8466db96
HH
194 done
195}
196
197# function pop()
198# pops the last value from a stack
199# assigns value to second argument variable
200# or echo to stdout, if no second argument
201# $1 = stack variable
202# $2 = optional variable to store the value
203# example:
204# pop stack val
205# val=$(pop stack)
206pop() {
207 local __stack=$1; shift
3b403b32 208 local __resultvar=$1
ffa71b4a 209 local _value;
8466db96
HH
210 # check for empty stack
211 eval '[[ ${#'${__stack}'[@]} -eq 0 ]] && return 1'
212
ffa71b4a 213 eval _value='${'${__stack}'[${#'${__stack}'[@]}-1]}'
8466db96
HH
214
215 if [[ "$__resultvar" ]]; then
ffa71b4a 216 eval $__resultvar="'$_value'"
8466db96 217 else
b093aa2d 218 printf "%s" "$_value"
8466db96
HH
219 fi
220 eval unset ${__stack}'[${#'${__stack}'[@]}-1]'
221 return 0
222}
223
5bc545ed
VL
224# Little helper function for reading args from the commandline.
225# it automatically handles -a b and -a=b variants, and returns 1 if
226# we need to shift $3.
227read_arg() {
228 # $1 = arg name
229 # $2 = arg value
230 # $3 = arg parameter
231 local rematch='^[^=]*=(.*)$'
232 if [[ $2 =~ $rematch ]]; then
cc02093d 233 read "$1" <<< "${BASH_REMATCH[1]}"
5bc545ed 234 else
cc02093d
HH
235 read "$1" <<< "$3"
236 # There is no way to shift our callers args, so
237 # return 1 to indicate they should do it instead.
238 return 1
5bc545ed
VL
239 fi
240}
241
335bc217 242dropindirs_sort()
a42b2b81 243{
b093aa2d
HH
244 local suffix=$1; shift
245 local -a files
246 local f d
247
248 readarray -t files < <(
249 for d in "$@"; do
250 for i in "$d/"*"$suffix"; do
251 if [[ -e "$i" ]]; then
c00f8e05 252 printf "%s\n" "${i##*/}"
b093aa2d 253 fi
a42b2b81 254 done
a42b2b81
HH
255 done | sort -Vu
256 )
257
b093aa2d
HH
258 for f in "${files[@]}"; do
259 for d in "$@"; do
260 if [[ -e "$d/$f" ]]; then
c00f8e05 261 printf "%s\n" "$d/$f"
a42b2b81
HH
262 continue 2
263 fi
264 done
265 done
266}
267
432196ae 268verbosity_mod_l=0
f79e587c
HH
269unset kernel
270unset outfile
486a1b93 271
ffa71b4a
HH
272# Workaround -i, --include taking 2 arguments
273set -- "${@/--include/++include}"
274
275# This prevents any long argument ending with "-i"
276# -i, like --opt-i but I think we can just prevent that
277set -- "${@/%-i/++include}"
278
279TEMP=$(unset POSIXLY_CORRECT; getopt \
472189da 280 -o "a:m:o:d:I:k:c:L:fvqlHhMN" \
e65caf36 281 --long kver: \
ffa71b4a
HH
282 --long add: \
283 --long force-add: \
284 --long add-drivers: \
285 --long omit-drivers: \
286 --long modules: \
287 --long omit: \
288 --long drivers: \
289 --long filesystems: \
290 --long install: \
291 --long fwdir: \
292 --long libdirs: \
293 --long fscks: \
294 --long add-fstab: \
295 --long mount: \
52f1b041 296 --long device: \
ffa71b4a 297 --long nofscks: \
ff3953ef 298 --long ro-mnt \
ffa71b4a
HH
299 --long kmoddir: \
300 --long conf: \
301 --long confdir: \
302 --long tmpdir: \
303 --long stdlog: \
304 --long compress: \
305 --long prefix: \
306 --long force \
307 --long kernel-only \
308 --long no-kernel \
d20fb951 309 --long kernel-cmdline: \
ffa71b4a
HH
310 --long strip \
311 --long nostrip \
312 --long hardlink \
313 --long nohardlink \
314 --long noprefix \
315 --long mdadmconf \
316 --long nomdadmconf \
317 --long lvmconf \
318 --long nolvmconf \
319 --long debug \
320 --long profile \
321 --long sshkey: \
322 --long verbose \
323 --long quiet \
324 --long local \
325 --long hostonly \
ea3c4e82 326 --long host-only \
ffa71b4a 327 --long no-hostonly \
ea3c4e82 328 --long no-host-only \
ffa71b4a
HH
329 --long fstab \
330 --long help \
331 --long bzip2 \
332 --long lzma \
333 --long xz \
334 --long no-compress \
335 --long gzip \
336 --long list-modules \
337 --long show-modules \
338 --long keep \
339 --long printsize \
7d848c55 340 --long regenerate-all \
83bb0893 341 --long noimageifnotneeded \
ffa71b4a
HH
342 -- "$@")
343
344if (( $? != 0 )); then
345 usage
346 exit 1
347fi
348
349eval set -- "$TEMP"
350
351while :; do
352 case $1 in
e65caf36 353 --kver) kernel="$2"; shift;;
ffa71b4a
HH
354 -a|--add) push add_dracutmodules_l "$2"; shift;;
355 --force-add) push force_add_dracutmodules_l "$2"; shift;;
356 --add-drivers) push add_drivers_l "$2"; shift;;
357 --omit-drivers) push omit_drivers_l "$2"; shift;;
358 -m|--modules) push dracutmodules_l "$2"; shift;;
359 -o|--omit) push omit_dracutmodules_l "$2"; shift;;
360 -d|--drivers) push drivers_l "$2"; shift;;
361 --filesystems) push filesystems_l "$2"; shift;;
362 -I|--install) push install_items_l "$2"; shift;;
363 --fwdir) push fw_dir_l "$2"; shift;;
364 --libdirs) push libdirs_l "$2"; shift;;
365 --fscks) push fscks_l "$2"; shift;;
366 --add-fstab) push add_fstab_l "$2"; shift;;
367 --mount) push fstab_lines "$2"; shift;;
c586b033
CG
368 --add-device|--device)
369 push add_device_l "$2"; shift;;
d20fb951 370 --kernel-cmdline) push kernel_cmdline_l "$2"; shift;;
25b45979 371 --nofscks) nofscks_l="yes";;
ff3953ef 372 --ro-mnt) ro_mnt_l="yes";;
ffa71b4a
HH
373 -k|--kmoddir) drivers_dir_l="$2"; shift;;
374 -c|--conf) conffile="$2"; shift;;
375 --confdir) confdir="$2"; shift;;
376 --tmpdir) tmpdir_l="$2"; shift;;
377 -L|--stdlog) stdloglvl_l="$2"; shift;;
378 --compress) compress_l="$2"; shift;;
379 --prefix) prefix_l="$2"; shift;;
cc02093d
HH
380 -f|--force) force=yes;;
381 --kernel-only) kernel_only="yes"; no_kernel="no";;
382 --no-kernel) kernel_only="no"; no_kernel="yes";;
5bbfd484 383 --early-microcode) early_microcode="yes";;
5f2c30d9 384 --no-early-microcode) early_microcode="no";;
cc02093d
HH
385 --strip) do_strip_l="yes";;
386 --nostrip) do_strip_l="no";;
04d18f55
HH
387 --hardlink) do_hardlink_l="yes";;
388 --nohardlink) do_hardlink_l="no";;
fd786adc 389 --noprefix) prefix_l="/";;
690396a5
VL
390 --mdadmconf) mdadmconf_l="yes";;
391 --nomdadmconf) mdadmconf_l="no";;
392 --lvmconf) lvmconf_l="yes";;
393 --nolvmconf) lvmconf_l="no";;
cc02093d 394 --debug) debug="yes";;
57258a2c 395 --profile) profile="yes";;
ffa71b4a 396 --sshkey) sshkey="$2"; shift;;
432196ae
397 -v|--verbose) ((verbosity_mod_l++));;
398 -q|--quiet) ((verbosity_mod_l--));;
dbf8f6ba
HH
399 -l|--local)
400 allowlocal="yes"
b093aa2d
HH
401 [[ -f "$(readlink -f "${0%/*}")/dracut-functions.sh" ]] \
402 && dracutbasedir="$(readlink -f "${0%/*}")"
dbf8f6ba 403 ;;
ea3c4e82
HH
404 -H|--hostonly|--host-only)
405 hostonly_l="yes" ;;
406 -N|--no-hostonly|--no-host-only)
407 hostonly_l="no" ;;
cc02093d 408 --fstab) use_fstab_l="yes" ;;
ffa71b4a 409 -h|--help) long_usage; exit 1 ;;
2aad2344 410 -i|--include) push include_src "$2"
ffa71b4a 411 shift;;
3a714439
VL
412 --bzip2) compress_l="bzip2";;
413 --lzma) compress_l="lzma";;
414 --xz) compress_l="xz";;
415 --no-compress) _no_compress_l="cat";;
416 --gzip) compress_l="gzip";;
ffa71b4a 417 --list-modules) do_list="yes";;
956af8f2
418 -M|--show-modules)
419 show_modules_l="yes"
420 ;;
5fe5c7d0 421 --keep) keep="yes";;
ec3c5951 422 --printsize) printsize="yes";;
7d848c55 423 --regenerate-all) regenerate_all="yes";;
83bb0893 424 --noimageifnotneeded) noimageifnotneeded="yes";;
ffa71b4a
HH
425
426 --) shift; break;;
427
428 *) # should not even reach this point
429 printf "\n!Unknown option: '%s'\n\n" "$1" >&2; usage; exit 1;;
430 esac
431 shift
432done
433
434# getopt cannot handle multiple arguments, so just handle "-I,--include"
435# the old fashioned way
436
437while (($# > 0)); do
438 case ${1%%=*} in
439 ++include) push include_src "$2"
440 push include_target "$3"
441 shift 2;;
3b403b32 442 *)
dbf381f7 443 if ! [[ ${outfile+x} ]]; then
486a1b93 444 outfile=$1
8a5354a9 445 elif ! [[ ${kernel+x} ]]; then
486a1b93
HH
446 kernel=$1
447 else
ffa71b4a 448 printf "\nUnknown arguments: %s\n\n" "$*" >&2
486a1b93
HH
449 usage; exit 1;
450 fi
451 ;;
641cc356 452 esac
b368a5f3 453 shift
641cc356 454done
ffa71b4a 455
7d848c55
HH
456if [[ $regenerate_all == "yes" ]]; then
457 ret=0
458 if [[ $kernel ]]; then
b093aa2d 459 printf -- "--regenerate-all cannot be called with a kernel version\n" >&2
7d848c55
HH
460 exit 1
461 fi
462
463 if [[ $outfile ]]; then
b093aa2d 464 printf -- "--regenerate-all cannot be called with a image file\n" >&2
7d848c55
HH
465 exit 1
466 fi
467
468 ((len=${#dracut_args[@]}))
469 for ((i=0; i < len; i++)); do
470 [[ ${dracut_args[$i]} == "--regenerate-all" ]] && \
471 unset dracut_args[$i]
472 done
473
474 cd /lib/modules
475 for i in *; do
476 [[ -f $i/modules.builtin ]] || continue
b093aa2d 477 dracut --kver="$i" "${dracut_args[@]}"
7d848c55
HH
478 ((ret+=$?))
479 done
480 exit $ret
481fi
482
8a5354a9 483if ! [[ $kernel ]]; then
486a1b93
HH
484 kernel=$(uname -r)
485fi
ffa71b4a
HH
486
487if ! [[ $outfile ]]; then
cc230300
HH
488 [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
489
490 if [[ $MACHINE_ID ]] && ( [[ -d /boot/${MACHINE_ID} ]] || [[ -L /boot/${MACHINE_ID} ]] ); then
491 outfile="/boot/${MACHINE_ID}/$kernel/initrd"
492 else
493 outfile="/boot/initramfs-$kernel.img"
494 fi
ffa71b4a 495fi
4cba351e 496
7b2d3d99 497for i in /usr/sbin /sbin /usr/bin /bin; do
65fe8ae8
HH
498 rl=$i
499 if [ -L "$i" ]; then
500 rl=$(readlink -f $i)
501 fi
ffa71b4a
HH
502 if [[ "$NPATH" != "*:$rl*" ]] ; then
503 NPATH+=":$rl"
504 fi
65fe8ae8 505done
7b2d3d99 506export PATH="${NPATH#:}"
02b5c8e3
HH
507unset LC_MESSAGES
508unset LC_CTYPE
509export LC_ALL=C
510export LANG=C
68910ba4 511unset NPATH
d619fb5e 512unset LD_LIBRARY_PATH
1fcf2d2f 513unset GREP_OPTIONS
a55711cd 514
89d44e72 515export DRACUT_LOG_LEVEL=warning
3b403b32 516[[ $debug ]] && {
89d44e72 517 export DRACUT_LOG_LEVEL=debug
c36ce334
VL
518 export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
519 set -x
520}
521
57258a2c
HH
522[[ $profile ]] && {
523 export PS4='+ $(date "+%s.%N") ${BASH_SOURCE}@${LINENO}: ';
524 set -x
525 debug=yes
526}
527
c5a65990 528[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
5d791c0e 529
f1336ac7 530# if we were not passed a config file, try the default one
42c71947 531if [[ ! -f $conffile ]]; then
b093aa2d
HH
532 if [[ $allowlocal ]]; then
533 conffile="$dracutbasedir/dracut.conf"
534 else
eebc929a 535 conffile="/etc/dracut.conf"
b093aa2d 536 fi
42c71947 537fi
f1336ac7 538
2c2c4580 539if [[ ! -d $confdir ]]; then
b093aa2d
HH
540 if [[ $allowlocal ]]; then
541 confdir="$dracutbasedir/dracut.conf.d"
542 else
eebc929a 543 confdir="/etc/dracut.conf.d"
b093aa2d 544 fi
2c2c4580
HH
545fi
546
2245f372
AB
547# source our config file
548[[ -f $conffile ]] && . "$conffile"
549
2c2c4580 550# source our config dir
335bc217 551for f in $(dropindirs_sort ".conf" "$confdir" "$dracutbasedir/dracut.conf.d"); do
a42b2b81
HH
552 [[ -e $f ]] && . "$f"
553done
2c2c4580 554
d87c2708 555# these optins add to the stuff in the config file
e5e5c895 556if (( ${#add_dracutmodules_l[@]} )); then
661f9a34
HH
557 while pop add_dracutmodules_l val; do
558 add_dracutmodules+=" $val "
559 done
560fi
561
31f1c02d
AW
562if (( ${#force_add_dracutmodules_l[@]} )); then
563 while pop force_add_dracutmodules_l val; do
564 force_add_dracutmodules+=" $val "
565 done
566fi
567
25b45979
MS
568if (( ${#fscks_l[@]} )); then
569 while pop fscks_l val; do
570 fscks+=" $val "
571 done
572fi
573
70cb8a68
HH
574if (( ${#add_fstab_l[@]} )); then
575 while pop add_fstab_l val; do
576 add_fstab+=" $val "
577 done
578fi
579
580if (( ${#fstab_lines_l[@]} )); then
581 while pop fstab_lines_l val; do
582 push fstab_lines $val
583 done
584fi
585
0ec40e08
HH
586if (( ${#install_items_l[@]} )); then
587 while pop install_items_l val; do
c6c6a088 588 install_items+=" $val "
0ec40e08
HH
589 done
590fi
591
f1336ac7 592# these options override the stuff in the config file
e5e5c895 593if (( ${#dracutmodules_l[@]} )); then
661f9a34
HH
594 dracutmodules=''
595 while pop dracutmodules_l val; do
596 dracutmodules+="$val "
597 done
598fi
599
e5e5c895 600if (( ${#omit_dracutmodules_l[@]} )); then
661f9a34
HH
601 omit_dracutmodules=''
602 while pop omit_dracutmodules_l val; do
603 omit_dracutmodules+="$val "
604 done
605fi
606
e5e5c895 607if (( ${#filesystems_l[@]} )); then
661f9a34
HH
608 filesystems=''
609 while pop filesystems_l val; do
610 filesystems+="$val "
611 done
612fi
613
e5e5c895 614if (( ${#fw_dir_l[@]} )); then
661f9a34
HH
615 fw_dir=''
616 while pop fw_dir_l val; do
617 fw_dir+="$val "
618 done
619fi
620
c9143a63
JAH
621if (( ${#libdirs_l[@]} )); then
622 libdirs=''
623 while pop libdirs_l val; do
624 libdirs+="$val "
625 done
626fi
627
e103615b 628[[ $stdloglvl_l ]] && stdloglvl=$stdloglvl_l
b991617f 629[[ ! $stdloglvl ]] && stdloglvl=4
432196ae
630stdloglvl=$((stdloglvl + verbosity_mod_l))
631((stdloglvl > 6)) && stdloglvl=6
632((stdloglvl < 0)) && stdloglvl=0
633
26537a5b 634[[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
31f7db66 635[[ $do_strip_l ]] && do_strip=$do_strip_l
6c128565 636[[ $do_strip ]] || do_strip=yes
04d18f55
HH
637[[ $do_hardlink_l ]] && do_hardlink=$do_hardlink_l
638[[ $do_hardlink ]] || do_hardlink=yes
fd786adc
HH
639[[ $prefix_l ]] && prefix=$prefix_l
640[[ $prefix = "/" ]] && unset prefix
ba726310 641[[ $hostonly_l ]] && hostonly=$hostonly_l
7c179686 642[[ $use_fstab_l ]] && use_fstab=$use_fstab_l
2f02ae9d 643[[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
7a34efa5 644[[ $lvmconf_l ]] && lvmconf=$lvmconf_l
c5a65990 645[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
8bc650d9 646[[ $fw_dir ]] || fw_dir="/lib/firmware/updates /lib/firmware"
882c4c5a
647[[ $tmpdir_l ]] && tmpdir="$tmpdir_l"
648[[ $tmpdir ]] || tmpdir=/var/tmp
3a714439 649[[ $compress_l ]] && compress=$compress_l
956af8f2 650[[ $show_modules_l ]] && show_modules=$show_modules_l
25b45979 651[[ $nofscks_l ]] && nofscks="yes"
ff3953ef 652[[ $ro_mnt_l ]] && ro_mnt="yes"
ddfd1d10
VL
653# eliminate IFS hackery when messing with fw_dir
654fw_dir=${fw_dir//:/ }
9a8a00cf 655
3a714439 656# handle compression options.
55468fb9 657[[ $compress ]] || compress="gzip"
3a714439 658case $compress in
6f223367 659 bzip2) compress="bzip2 -9";;
3a714439
VL
660 lzma) compress="lzma -9";;
661 xz) compress="xz --check=crc32 --lzma2=dict=1MiB";;
b093aa2d 662 gzip) compress="gzip -9"; command -v pigz > /dev/null 2>&1 && compress="pigz -9";;
3a714439
VL
663esac
664if [[ $_no_compress_l = "cat" ]]; then
665 compress="cat"
666fi
667
ba726310 668[[ $hostonly = yes ]] && hostonly="-h"
ba67b923 669[[ $hostonly != "-h" ]] && unset hostonly
ba726310 670
26c231f1 671readonly TMPDIR="$tmpdir"
b093aa2d 672readonly initdir="$(mktemp --tmpdir="$TMPDIR/" -d -t initramfs.XXXXXX)"
26c231f1 673[ -d "$initdir" ] || {
b093aa2d 674 printf "%s\n" "dracut: mktemp --tmpdir=\"$TMPDIR/\" -d -t initramfs.XXXXXX failed." >&2
26c231f1
HH
675 exit 1
676}
677
5f2c30d9
KRW
678if [[ $early_microcode = yes ]]; then
679 readonly microcode_dir="$(mktemp --tmpdir="$TMPDIR/" -d -t early_microcode.XXXXXX)"
680 [ -d "$microcode_dir" ] || {
681 printf "%s\n" "dracut: mktemp --tmpdir=\"$TMPDIR/\" -d -t early_microcode.XXXXXX failed." >&2
682 exit 1
683 }
684fi
d008b6b8 685# clean up after ourselves no matter how we die.
5f2c30d9 686trap 'ret=$?;[[ $outfile ]] && [[ -f $outfile.$$ ]] && rm -f -- "$outfile.$$";[[ $keep ]] && echo "Not removing $initdir." >&2 || { [[ $initdir ]] && rm -rf -- "$initdir"; [[ $microcode_dir ]] && rm -Rf -- "$microcode_dir"; exit $ret; };' EXIT
d008b6b8
HH
687# clean up after ourselves no matter how we die.
688trap 'exit 1;' SIGINT
689
89d44e72
HH
690export DRACUT_KERNEL_LAZY="1"
691export DRACUT_RESOLVE_LAZY="1"
692
552ecca6
HH
693if [[ -f $dracutbasedir/dracut-functions.sh ]]; then
694 . $dracutbasedir/dracut-functions.sh
adbc8a42 695else
b093aa2d
HH
696 printf "%s\n" "dracut: Cannot find $dracutbasedir/dracut-functions.sh." >&2
697 printf "%s\n" "dracut: Are you running from a git checkout?" >&2
698 printf "%s\n" "dracut: Try passing -l as an argument to $0" >&2
cc02093d 699 exit 1
adbc8a42 700fi
22fd1627 701
bfd2e8c2
HH
702inst /bin/sh
703if ! $DRACUT_INSTALL ${initdir+-D "$initdir"} -R "$initdir/bin/sh" &>/dev/null; then
704 unset DRACUT_RESOLVE_LAZY
705 export DRACUT_RESOLVE_DEPS=1
706fi
32bd2fbb 707rm -fr -- ${initdir}/*
bfd2e8c2 708
dbf8f6ba
HH
709if [[ -f $dracutbasedir/dracut-version.sh ]]; then
710 . $dracutbasedir/dracut-version.sh
711fi
712
bfd2e8c2 713# Verify bash version, current minimum is 3.1
b093aa2d
HH
714if (( BASH_VERSINFO[0] < 4 )); then
715 dfatal 'You need at least Bash 4 to use dracut, sorry.'
d239b550
MS
716 exit 1
717fi
718
552ecca6 719dracutfunctions=$dracutbasedir/dracut-functions.sh
5cad5bb5 720export dracutfunctions
9a8a00cf 721
fcbcb252
HH
722if (( ${#drivers_l[@]} )); then
723 drivers=''
724 while pop drivers_l val; do
725 drivers+="$val "
726 done
727fi
728drivers=${drivers/-/_}
729
730if (( ${#add_drivers_l[@]} )); then
731 while pop add_drivers_l val; do
732 add_drivers+=" $val "
733 done
734fi
735add_drivers=${add_drivers/-/_}
736
737if (( ${#omit_drivers_l[@]} )); then
738 while pop omit_drivers_l val; do
739 omit_drivers+=" $val "
740 done
741fi
742omit_drivers=${omit_drivers/-/_}
743
d20fb951
HH
744if (( ${#kernel_cmdline_l[@]} )); then
745 while pop kernel_cmdline_l val; do
746 kernel_cmdline+=" $val "
747 done
748fi
749
34248c92 750omit_drivers_corrected=""
fcbcb252 751for d in $omit_drivers; do
b093aa2d 752 [[ " $drivers $add_drivers " == *\ $d\ * ]] && continue
34248c92 753 omit_drivers_corrected+="$d|"
fcbcb252 754done
34248c92 755omit_drivers="${omit_drivers_corrected%|}"
fcbcb252
HH
756unset omit_drivers_corrected
757
ffa71b4a
HH
758# prepare args for logging
759for ((i=0; i < ${#dracut_args[@]}; i++)); do
b093aa2d 760 [[ "${dracut_args[$i]}" == *\ * ]] && \
ffa71b4a 761 dracut_args[$i]="\"${dracut_args[$i]}\""
a4210166 762 #" keep vim happy
ffa71b4a
HH
763done
764ddebug "Executing: $0 ${dracut_args[@]}"
58dad702 765
5b11bb73
HH
766[[ $do_list = yes ]] && {
767 for mod in $dracutbasedir/modules.d/*; do
768 [[ -d $mod ]] || continue;
d8e8e14e
769 [[ -e $mod/install || -e $mod/installkernel || \
770 -e $mod/module-setup.sh ]] || continue
b093aa2d 771 printf "%s\n" "${mod##*/??}"
5b11bb73
HH
772 done
773 exit 0
774}
775
66ac3cd1 776# This is kinda legacy -- eventually it should go away.
f1336ac7 777case $dracutmodules in
66ac3cd1 778 ""|auto) dracutmodules="all" ;;
f1336ac7 779esac
e12aac5e 780
454771cd 781abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
ec9315e5 782
5db6ca5a
HH
783if [[ -d $srcmods ]]; then
784 [[ -f $srcmods/modules.dep ]] || {
785 dwarn "$srcmods/modules.dep is missing. Did you run depmod?"
786 }
787fi
fe1484f3 788
c8937ec4 789if [[ -f $outfile && ! $force ]]; then
432196ae 790 dfatal "Will not override existing initramfs ($outfile) without --force"
ec9315e5
JK
791 exit 1
792fi
793
ebfdb219 794outdir=${outfile%/*}
52eed268
HH
795[[ $outdir ]] || outdir="/"
796
ebfdb219 797if [[ ! -d "$outdir" ]]; then
cc230300 798 dfatal "Can't write to $outdir: Directory $outdir does not exist or is not accessible."
454771cd 799 exit 1
ebfdb219 800elif [[ ! -w "$outdir" ]]; then
ffa71b4a 801 dfatal "No permission to write to $outdir."
454771cd 802 exit 1
0a325a91 803elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
432196ae 804 dfatal "No permission to write $outfile."
734a0d9e
HH
805 exit 1
806fi
807
f6f74096
DD
808# Need to be able to have non-root users read stuff (rpcbind etc)
809chmod 755 "$initdir"
810
5bfa3b36
HH
811if [[ $hostonly ]]; then
812 for i in /sys /proc /run /dev; do
5e601454 813 if ! findmnt --target "$i" &>/dev/null; then
5bfa3b36
HH
814 dwarning "Turning off host-only mode: '$i' is not mounted!"
815 unset hostonly
816 fi
817 done
78d1d4f8
HH
818 if ! [[ -d /run/udev/data ]]; then
819 dwarning "Turning off host-only mode: udev database not found!"
820 unset hostonly
821 fi
5bfa3b36
HH
822fi
823
d351541e
HH
824declare -A host_fs_types
825
cd7ff122
AW
826for line in "${fstab_lines[@]}"; do
827 set -- $line
828 #dev mp fs fsopts
d0096de7 829 push host_devs "$1"
d351541e 830 host_fs_types["$1"]="$3"
cd7ff122
AW
831done
832
833for f in $add_fstab; do
b093aa2d 834 [[ -e $f ]] || continue
cd7ff122 835 while read dev rest; do
b093aa2d
HH
836 push host_devs "$dev"
837 done < "$f"
838done
839
840for dev in $add_device; do
841 push host_devs "$dev"
cd7ff122
AW
842done
843
c586b033
CG
844if (( ${#add_device_l[@]} )); then
845 while pop add_device_l val; do
846 add_device+=" $val "
b093aa2d 847 push host_devs "$val"
c586b033
CG
848 done
849fi
850
7ae5d9d1 851if [[ $hostonly ]]; then
480d772f
HH
852 # in hostonly mode, determine all devices, which have to be accessed
853 # and examine them for filesystem types
854
bcfbddef 855 for mp in \
7ae5d9d1
HH
856 "/" \
857 "/etc" \
858 "/usr" \
859 "/usr/bin" \
860 "/usr/sbin" \
861 "/usr/lib" \
862 "/usr/lib64" \
bcfbddef
HH
863 "/boot";
864 do
7ae5d9d1 865 mountpoint "$mp" >/dev/null 2>&1 || continue
5a6a98f8
HH
866 _dev="$(readlink -f "/dev/block/$(find_block_device "$mp")")"
867 [[ "$_mp" == "/" ]] && root_dev="$_dev"
868 push host_devs "$_dev"
7ae5d9d1 869 done
dd587549
HH
870
871 while read dev type rest; do
872 [[ -b $dev ]] || continue
873 [[ "$type" == "partition" ]] || continue
874 while read _d _m _t _o _r; do
875 [[ "$_d" == \#* ]] && continue
876 [[ $_d ]] || continue
877 [[ $_t != "swap" ]] || [[ $_m != "swap" ]] && continue
878 [[ "$_o" == *noauto* ]] && continue
879 [[ "$_d" == UUID\=* ]] && _d="/dev/disk/by-uuid/${_d#UUID=}"
880 [[ "$_d" == LABEL\=* ]] && _d="/dev/disk/by-label/$_d#LABEL=}"
881 [[ "$_d" -ef "$dev" ]] || continue
bcfbddef
HH
882
883 while read _mapper _a _p _o; do
884 [[ $_mapper = \#* ]] && continue
885 [[ "$_d" -ef /dev/mapper/"$_mapper" ]] || continue
886 [[ "$_o" ]] || _o="$_p"
887 # skip mkswap swap
888 [[ $_o == *swap* ]] && continue 2
889 done < /etc/crypttab
890
b093aa2d 891 push host_devs "$(readlink -f "$dev")"
dd587549
HH
892 break
893 done < /etc/fstab
894 done < /proc/swaps
7ae5d9d1 895fi
7ae5d9d1 896
b093aa2d 897_get_fs_type() { (
19bab59c 898 [[ $1 ]] || return
b093aa2d
HH
899 if [[ -b /dev/block/$1 ]] && ID_FS_TYPE=$(get_fs_env "/dev/block/$1"); then
900 printf "%s\n" "$(readlink -f "/dev/block/$1")" "$ID_FS_TYPE"
19bab59c 901 return 1
cd7ff122 902 fi
b093aa2d
HH
903 if [[ -b $1 ]] && ID_FS_TYPE=$(get_fs_env "$1"); then
904 printf "%s\n" "$(readlink -f "$1")" "$ID_FS_TYPE"
19bab59c 905 return 1
4f10ae2b 906 fi
b093aa2d
HH
907 if fstype=$(find_dev_fstype "$1"); then
908 printf "%s\n" "$1" "$fstype"
19bab59c 909 return 1
cd7ff122
AW
910 fi
911 return 1
b093aa2d 912) }
cd7ff122
AW
913
914for dev in "${host_devs[@]}"; do
b093aa2d
HH
915 while read key; do
916 read val
d351541e
HH
917 host_fs_types["$key"]="$val"
918 done < <(
b093aa2d
HH
919 _get_fs_type "$dev"
920 check_block_and_slaves_all _get_fs_type "$(get_maj_min "$dev")"
d351541e 921 )
cd7ff122
AW
922done
923
d1615612 924[[ -d $udevdir ]] \
b093aa2d 925 || udevdir="$(pkg-config udev --variable=udevdir 2>/dev/null)"
d1615612
HH
926if ! [[ -d "$udevdir" ]]; then
927 [[ -d /lib/udev ]] && udevdir=/lib/udev
928 [[ -d /usr/lib/udev ]] && udevdir=/usr/lib/udev
929fi
930
931[[ -d $systemdutildir ]] \
932 || systemdutildir=$(pkg-config systemd --variable=systemdutildir 2>/dev/null)
d1615612
HH
933
934if ! [[ -d "$systemdutildir" ]]; then
935 [[ -d /lib/systemd ]] && systemdutildir=/lib/systemd
936 [[ -d /usr/lib/systemd ]] && systemdutildir=/usr/lib/systemd
937fi
cb8def95
HH
938
939[[ -d $systemdsystemunitdir ]] \
940 || systemdsystemunitdir=$(pkg-config systemd --variable=systemdsystemunitdir 2>/dev/null)
941
d1615612
HH
942[[ -d "$systemdsystemunitdir" ]] || systemdsystemunitdir=${systemdutildir}/system
943
cb8def95
HH
944[[ -d $systemdsystemconfdir ]] \
945 || systemdsystemconfdir=$(pkg-config systemd --variable=systemdsystemconfdir 2>/dev/null)
946
947[[ -d "$systemdsystemconfdir" ]] || systemdsystemconfdir=/etc/systemd/system
948
aec9f902 949export initdir dracutbasedir dracutmodules \
e103615b 950 fw_dir drivers_dir debug no_kernel kernel_only \
5a6a98f8 951 omit_drivers mdadmconf lvmconf root_dev \
ff3953ef 952 use_fstab fstab_lines libdirs fscks nofscks ro_mnt \
1e64e493 953 stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \
dbf8f6ba 954 debug host_fs_types host_devs sshkey add_fstab \
cb8def95
HH
955 DRACUT_VERSION udevdir prefix filesystems drivers \
956 systemdutildir systemdsystemunitdir systemdsystemconfdir
f4fff04e 957
dbad9f46 958# Create some directory structure first
fd786adc 959[[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
dbad9f46 960
785a6cd2 961[[ -h /lib ]] || mkdir -m 0755 -p "${initdir}${prefix}/lib"
fd786adc 962[[ $prefix ]] && ln -sfn "${prefix#/}/lib" "$initdir/lib"
dbad9f46 963
785a6cd2 964if [[ $prefix ]]; then
d2a9c4a8 965 for d in bin etc lib sbin tmp usr var $libdirs; do
b093aa2d 966 [[ "$d" == */* ]] && continue
785a6cd2
HH
967 ln -sfn "${prefix#/}/${d#/}" "$initdir/$d"
968 done
969fi
970
971if [[ $kernel_only != yes ]]; then
3f54a83c 972 for d in usr/bin usr/sbin bin etc lib sbin tmp usr var $libdirs; do
fd786adc 973 [[ -e "${initdir}${prefix}/$d" ]] && continue
d433da44
HH
974 if [ -L "/$d" ]; then
975 inst_symlink "/$d" "${prefix}/$d"
785a6cd2
HH
976 else
977 mkdir -m 0755 -p "${initdir}${prefix}/$d"
978 fi
3b403b32 979 done
dbad9f46 980
7c14b368 981 for d in dev proc sys sysroot root run run/lock run/initramfs; do
d433da44
HH
982 if [ -L "/$d" ]; then
983 inst_symlink "/$d"
785a6cd2
HH
984 else
985 mkdir -m 0755 -p "$initdir/$d"
986 fi
33ee031c 987 done
dbad9f46 988
506b2192
HH
989 ln -sfn ../run "$initdir/var/run"
990 ln -sfn ../run/lock "$initdir/var/lock"
3f54a83c 991 ln -sfn ../run/log "$initdir/var/log"
6f09acf8
HH
992else
993 for d in lib "$libdir"; do
994 [[ -e "${initdir}${prefix}/$d" ]] && continue
995 if [ -h "/$d" ]; then
996 inst "/$d" "${prefix}/$d"
997 else
998 mkdir -m 0755 -p "${initdir}${prefix}/$d"
999 fi
1000 done
33ee031c 1001fi
0f86847d 1002
14c47b0e
HH
1003if [[ $kernel_only != yes ]]; then
1004 mkdir -p "${initdir}/etc/cmdline.d"
4fed3ddf
HH
1005 for _d in $hookdirs; do
1006 mkdir -m 0755 -p ${initdir}/lib/dracut/hooks/$_d
1007 done
26144216 1008 if [[ "$UID" = "0" ]]; then
ad200fbc
HH
1009 [ -c ${initdir}/dev/null ] || mknod ${initdir}/dev/null c 1 3
1010 [ -c ${initdir}/dev/kmsg ] || mknod ${initdir}/dev/kmsg c 1 11
1011 [ -c ${initdir}/dev/console ] || mknod ${initdir}/dev/console c 5 1
26144216 1012 fi
14c47b0e 1013fi
96b8d60a 1014
1b7fd0fa 1015mods_to_load=""
66ac3cd1
VL
1016# check all our modules to see if they should be sourced.
1017# This builds a list of modules that we will install next.
1b7fd0fa
AW
1018for_each_module_dir check_module
1019for_each_module_dir check_mount
1020
b093aa2d 1021[[ "$mods_to_load " == *01fips\ * ]] && export DRACUT_FIPS_MODE=1
89d44e72 1022
ec3c5951 1023_isize=0 #initramfs size
979c4a93 1024modules_loaded=" "
95bde758 1025# source our modules.
5d791c0e 1026for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
979c4a93 1027 _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]}
b093aa2d
HH
1028 if [[ "$mods_to_load" == *\ $_d_mod\ * ]]; then
1029 if [[ $show_modules = yes ]]; then
1030 printf "%s\n" "$_d_mod"
1031 else
979c4a93 1032 dinfo "*** Including module: $_d_mod ***"
b093aa2d
HH
1033 fi
1034 if [[ $kernel_only == yes ]]; then
1035 module_installkernel "$_d_mod" || {
a6d3be9d
AW
1036 dfatal "installkernel failed in module $_d_mod"
1037 exit 1
1038 }
cc02093d 1039 else
b093aa2d 1040 module_install "$_d_mod"
95d2dabc 1041 if [[ $no_kernel != yes ]]; then
b093aa2d 1042 module_installkernel "$_d_mod" || {
a6d3be9d
AW
1043 dfatal "installkernel failed in module $_d_mod"
1044 exit 1
1045 }
cc02093d
HH
1046 fi
1047 fi
979c4a93
HH
1048 mods_to_load=${mods_to_load// $_d_mod /}
1049 modules_loaded+="$_d_mod "
ec3c5951
DY
1050
1051 #print the module install size
1052 if [ -n "$printsize" ]; then
1053 _isize_new=$(du -sk ${initdir}|cut -f1)
b093aa2d
HH
1054 _isize_delta=$((_isize_new - _isize))
1055 printf "%s\n" "$_d_mod install size: ${_isize_delta}k"
ec3c5951
DY
1056 _isize=$_isize_new
1057 fi
66ac3cd1 1058 fi
15136762 1059done
20abd914 1060unset moddir
0a35a80b
HH
1061
1062for i in $modules_loaded; do
43fed151 1063 mkdir -p $initdir/lib/dracut
b093aa2d 1064 printf "%s\n" "$i" >> $initdir/lib/dracut/modules.txt
0a35a80b
HH
1065done
1066
ea216a65 1067dinfo "*** Including modules done ***"
aabc0553 1068
0f86847d 1069## final stuff that has to happen
89d44e72 1070if [[ $no_kernel != yes ]]; then
aec9f902
HH
1071
1072 if [[ $drivers ]]; then
1073 hostonly='' instmods $drivers
1074 fi
1075
1076 if [[ $add_drivers ]]; then
1077 hostonly='' instmods -c $add_drivers
1078 fi
1079 if [[ $filesystems ]]; then
1080 hostonly='' instmods -c $filesystems
1081 fi
1082
89d44e72
HH
1083 dinfo "*** Installing kernel module dependencies and firmware ***"
1084 dracut_kernel_post
1085 dinfo "*** Installing kernel module dependencies and firmware done ***"
83bb0893
HH
1086
1087 if [[ $noimageifnotneeded == yes ]] && [[ $hostonly ]]; then
1088 if [[ ! -f "$initdir/lib/dracut/need-initqueue" ]] && \
1089 [[ -f ${initdir}/lib/modules/$kernel/modules.dep && ! -s ${initdir}/lib/modules/$kernel/modules.dep ]]; then
1090 for i in ${initdir}/etc/cmdline.d/*.conf; do
1091 # We need no initramfs image and do not generate one.
1092 [[ $i == "${initdir}/etc/cmdline.d/*.conf" ]] && exit 0
1093 done
1094 fi
1095 fi
89d44e72 1096fi
d8aeb3a7 1097
998bf6e0 1098if [[ $kernel_only != yes ]]; then
b093aa2d 1099 (( ${#install_items[@]} > 0 )) && dracut_install ${install_items[@]}
998bf6e0 1100
b093aa2d 1101 [[ $kernel_cmdline ]] && printf "%s\n" "$kernel_cmdline" >> "${initdir}/etc/cmdline.d/01-default.conf"
d20fb951 1102
998bf6e0 1103 while pop fstab_lines line; do
b093aa2d 1104 printf "%s\n" "$line 0 0" >> "${initdir}/etc/fstab"
998bf6e0
HH
1105 done
1106
1107 for f in $add_fstab; do
b093aa2d 1108 cat "$f" >> "${initdir}/etc/fstab"
998bf6e0
HH
1109 done
1110
6c6d8057
HH
1111 if [ -d ${initdir}/$systemdutildir ]; then
1112 mkdir -p ${initdir}/etc/conf.d
1113 {
b093aa2d
HH
1114 printf "%s\n" "systemdutildir=\"$systemdutildir\""
1115 printf "%s\n" "systemdsystemunitdir=\"$systemdsystemunitdir\""
1116 printf "%s\n" "systemdsystemconfdir=\"$systemdsystemconfdir\""
6c6d8057
HH
1117 } > ${initdir}/etc/conf.d/systemd.conf
1118 fi
1119
998bf6e0
HH
1120 if [[ $DRACUT_RESOLVE_LAZY ]] && [[ $DRACUT_INSTALL ]]; then
1121 dinfo "*** Resolving executable dependencies ***"
1122 find "$initdir" -type f \
1123 '(' -perm -0100 -or -perm -0010 -or -perm -0001 ')' \
1124 -not -path '*.ko' -print0 \
b093aa2d 1125 | xargs -r -0 $DRACUT_INSTALL ${initdir+-D "$initdir"} -R ${DRACUT_FIPS_MODE+-H} --
998bf6e0
HH
1126 dinfo "*** Resolving executable dependencies done***"
1127 fi
1128fi
1129
661f9a34
HH
1130while pop include_src src && pop include_target tgt; do
1131 if [[ $src && $tgt ]]; then
1132 if [[ -f $src ]]; then
1133 inst $src $tgt
1134 else
3b403b32 1135 ddebug "Including directory: $src"
661f9a34 1136 mkdir -p "${initdir}/${tgt}"
dbad9f46 1137 # check for preexisting symlinks, so we can cope with the
fd786adc 1138 # symlinks to $prefix
dbad9f46
HH
1139 for i in "$src"/*; do
1140 [[ -e "$i" || -h "$i" ]] || continue
1141 s=${initdir}/${tgt}/${i#$src/}
1142 if [[ -d "$i" ]]; then
1143 if ! [[ -e "$s" ]]; then
1144 mkdir -m 0755 -p "$s"
1145 chmod --reference="$i" "$s"
1146 fi
98eb6d57 1147 cp --reflink=auto --sparse=auto -fa -t "$s" "$i"/*
dbad9f46 1148 else
98eb6d57 1149 cp --reflink=auto --sparse=auto -fa -t "$s" "$i"
3b403b32 1150 fi
dbad9f46 1151 done
661f9a34 1152 fi
4fea3ea6 1153 fi
661f9a34 1154done
88ffd2df 1155
14c47b0e 1156if [[ $kernel_only != yes ]]; then
1254925f
HH
1157 # make sure that library links are correct and up to date
1158 for f in /etc/ld.so.conf /etc/ld.so.conf.d/*; do
482c573d 1159 [[ -f $f ]] && inst_simple "$f"
1254925f
HH
1160 done
1161 if ! ldconfig -r "$initdir"; then
1162 if [[ $UID = 0 ]]; then
1163 derror "ldconfig exited ungracefully"
1164 else
1165 derror "ldconfig might need uid=0 (root) for chroot()"
1166 fi
432196ae
1167 fi
1168fi
fdc421db 1169
b093aa2d 1170if (( maxloglvl >= 5 )); then
432196ae
1171 ddebug "Listing sizes of included files:"
1172 du -c "$initdir" | sort -n | ddebug
1173fi
c4ad7fff 1174
b093aa2d 1175PRELINK_BIN="$(command -v prelink)"
404815ea
HH
1176if [[ $UID = 0 ]] && [[ $PRELINK_BIN ]]; then
1177 if [[ $DRACUT_FIPS_MODE ]]; then
8461734e 1178 dinfo "*** Installing prelink files ***"
404815ea 1179 dracut_install -o prelink /etc/prelink.conf /etc/prelink.conf.d/*.conf /etc/prelink.cache
404815ea
HH
1180 else
1181 dinfo "*** Pre-linking files ***"
1182 dracut_install -o prelink /etc/prelink.conf /etc/prelink.conf.d/*.conf
b093aa2d
HH
1183 chroot "$initdir" "$PRELINK_BIN" -a
1184 rm -f -- "$initdir/$PRELINK_BIN"
32bd2fbb 1185 rm -fr -- "$initdir"/etc/prelink.*
404815ea
HH
1186 dinfo "*** Pre-linking files done ***"
1187 fi
1188fi
1189
1190if [[ $do_hardlink = yes ]] && command -v hardlink >/dev/null; then
1191 dinfo "*** Hardlinking files ***"
1192 hardlink "$initdir" 2>&1
1193 dinfo "*** Hardlinking files done ***"
1194fi
1195
3b403b32 1196# strip binaries
98adb06e 1197if [[ $do_strip = yes ]] ; then
69b35075 1198 for p in strip xargs find; do
cc02093d 1199 if ! type -P $p >/dev/null; then
fefd3f66 1200 dinfo "Could not find '$p'. Not stripping the initramfs."
cc02093d
HH
1201 do_strip=no
1202 fi
31f7db66
HH
1203 done
1204fi
1205
89d44e72
HH
1206if [[ $do_strip = yes ]] ; then
1207 dinfo "*** Stripping files ***"
6c128565
HH
1208 if [[ $DRACUT_FIPS_MODE ]]; then
1209 find "$initdir" -type f \
2d9b156e 1210 -executable -not -path '*/lib/modules/*.ko' -print0 \
6c128565
HH
1211 | while read -r -d $'\0' f; do
1212 if ! [[ -e "${f%/*}/.${f##*/}.hmac" ]] \
6f4c2dad
HH
1213 && ! [[ -e "/lib/hmaccalc/${f##*/}.hmac" ]] \
1214 && ! [[ -e "/lib64/hmaccalc/${f##*/}.hmac" ]] \
6c128565
HH
1215 && ! [[ -e "/lib/fipscheck/${f##*/}.hmac" ]] \
1216 && ! [[ -e "/lib64/fipscheck/${f##*/}.hmac" ]]; then
b093aa2d 1217 printf "%s\000" "$f";
6c128565 1218 fi
2d9b156e 1219 done | xargs -r -0 strip -g 2>/dev/null
6c128565
HH
1220 else
1221 find "$initdir" -type f \
2d9b156e 1222 -executable -not -path '*/lib/modules/*.ko' -print0 \
6c128565
HH
1223 | xargs -r -0 strip -g 2>/dev/null
1224 fi
2d9b156e
HH
1225
1226 # strip kernel modules, but do not touch signed modules
1227 find "$initdir" -type f -path '*/lib/modules/*.ko' -print0 \
1228 | while read -r -d $'\0' f; do
1229 SIG=$(tail -c 28 "$f")
b093aa2d 1230 [[ $SIG == '~Module signature appended~' ]] || { printf "%s\000" "$f"; }
2d9b156e
HH
1231 done | xargs -r -0 strip -g
1232
89d44e72 1233 dinfo "*** Stripping files done ***"
979c4a93 1234fi
5f2c30d9
KRW
1235if [[ $early_microcode = yes ]]; then
1236 dinfo "*** Generating early-microcode cpio image ***"
1237 ucode_dir=(amd-ucode intel-ucode)
1238 ucode_dest=(AuthenticAMD.bin GenuineIntel.bin)
1239 _dest_dir="$microcode_dir/d/kernel/x86/microcode"
1240 _dest_idx="0 1"
1241 mkdir -p $_dest_dir
1242 if [[ $hostonly ]]; then
1243 [[ $(get_cpu_vendor) == "AMD" ]] && _dest_idx="0"
1244 [[ $(get_cpu_vendor) == "Intel" ]] && _dest_idx="1"
1245 fi
1246 for idx in $_dest_idx; do
1247 _fw=${ucode_dir[$idx]}
1248 for _fwdir in $fw_dir; do
1249 if [[ -d $_fwdir && -d $_fwdir/$_fw ]]; then
1250 _src="*"
1251 dinfo "*** Constructing ${ucode_dest[$idx]} ****"
1252 if [[ $hostonly ]]; then
1253 _src=$(get_ucode_file)
1254 fi
1255 cat $_fwdir/$_fw/$_src > $_dest_dir/${ucode_dest[$idx]}
1256 fi
1257 done
1258 done
1259 (cd "$microcode_dir/d"; find . | cpio -o -H newc --quiet >../ucode.cpio)
1260fi
32bd2fbb 1261rm -f -- "$outfile"
89d44e72 1262dinfo "*** Creating image file ***"
5f2c30d9
KRW
1263if [[ $early_microcode = yes ]]; then
1264 # The microcode blob is _before_ the initramfs blob, not after
1265 mv $microcode_dir/ucode.cpio $outfile.$$
1266fi
1267if ! ( umask 077; cd "$initdir"; find . |cpio -R 0:0 -H newc -o --quiet| \
1268 $compress >> "$outfile.$$"; ); then
9a9c6e12 1269 dfatal "dracut: creation of $outfile.$$ failed"
734a0d9e 1270 exit 1
3b403b32 1271fi
b093aa2d 1272mv -- "$outfile.$$" "$outfile"
89d44e72 1273dinfo "*** Creating image file done ***"
432196ae
1274
1275dinfo "Wrote $outfile:"
1276dinfo "$(ls -l "$outfile")"
1faecdc1 1277
3da58569 1278exit 0