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