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