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