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