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