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