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