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