]> git.ipfire.org Git - thirdparty/dracut.git/blame - dracut.sh
rootfs-block: avoid remount when options don't change
[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
cc02093d 10# Copyright 2005-2010 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
HH
26# store for logging
27dracut_args="$@"
5616feb0
AT
28
29usage() {
30# 80x25 linebreak here ^
cc02093d
HH
31 cat << EOF
32Usage: $0 [OPTION]... <initramfs> <kernel-version>
5616feb0
AT
33Creates initial ramdisk images for preloading modules
34
39ff0682 35 -f, --force Overwrite existing initramfs file.
5616feb0
AT
36 -m, --modules [LIST] Specify a space-separated list of dracut modules to
37 call when building the initramfs. Modules are located
c5a65990 38 in /usr/lib/dracut/modules.d.
39ff0682 39 -o, --omit [LIST] Omit a space-separated list of dracut modules.
3e17f33b 40 -a, --add [LIST] Add a space-separated list of dracut modules.
5616feb0 41 -d, --drivers [LIST] Specify a space-separated list of kernel modules to
cb47caf7 42 exclusively include in the initramfs.
fcbcb252 43 --add-drivers [LIST] Specify a space-separated list of kernel
cb47caf7 44 modules to add to the initramfs.
fcbcb252
HH
45 --omit-drivers [LIST] Specify a space-separated list of kernel
46 modules not to add to the initramfs.
8fa510d4
DH
47 --filesystems [LIST] Specify a space-separated list of kernel filesystem
48 modules to exclusively include in the generic
49 initramfs.
3b403b32 50 -k, --kmoddir [DIR] Specify the directory, where to look for kernel
2b9dfbbe 51 modules
3b403b32 52 --fwdir [DIR] Specify additional directories, where to look for
33ee031c
HH
53 firmwares, separated by :
54 --kernel-only Only install kernel drivers and firmware files
55 --no-kernel Do not install kernel drivers and firmware files
5be225d2
HH
56 --strip Strip binaries in the initramfs
57 --nostrip Do not strip binaries in the initramfs (default)
fd786adc
HH
58 --prefix [DIR] Prefix initramfs files with [DIR]
59 --noprefix Do not prefix initramfs files (default)
2f02ae9d
HH
60 --mdadmconf Include local /etc/mdadm.conf
61 --nomdadmconf Do not include local /etc/mdadm.conf
7a34efa5 62 --lvmconf Include local /etc/lvm/lvm.conf
cc02093d 63 --nolvmconf Do not include local /etc/lvm/lvm.conf
25b45979
MS
64 --fscks [LIST] Add a space-separated list of fsck helpers.
65 --nofscks Inhibit installation of any fsck helpers.
5616feb0 66 -h, --help This message
00531568 67 --debug Output debug information of the build process
57258a2c 68 --profile Output profile information of the build process
e103615b
69 -L, --stdlog [0-6] Specify logging level (to standard error)
70 0 - suppress any messages
71 1 - only fatal errors
72 2 - all errors
73 3 - warnings
74 4 - info (default)
75 5 - debug info (here starts lots of output)
76 6 - trace info (and even more)
432196ae
77 -v, --verbose Increase verbosity level (default is info(4))
78 -q, --quiet Decrease verbosity level (default is info(4))
5616feb0
AT
79 -c, --conf [FILE] Specify configuration file to use.
80 Default: /etc/dracut.conf
3b403b32 81 --confdir [DIR] Specify configuration directory to use *.conf files
cc02093d 82 from. Default: /etc/dracut.conf.d
882c4c5a
83 --tmpdir [DIR] Temporary directory to be used instead of default
84 /var/tmp.
5616feb0
AT
85 -l, --local Local mode. Use modules from the current working
86 directory instead of the system-wide installed in
c5a65990 87 /usr/lib/dracut/modules.d.
5616feb0 88 Useful when running dracut from a git checkout.
7c179686 89 -H, --hostonly Host-Only mode: Install only what is needed for
5616feb0 90 booting the local host instead of a generic host.
cb74b382 91 --no-hostonly Disables Host-Only mode
7c179686 92 --fstab Use /etc/fstab to determine the root device.
70cb8a68
HH
93 --add-fstab [FILE] Add file to the initramfs fstab
94 --mount "[DEV] [MP] [FSTYPE] [FSOPTS]"
95 Mount device [DEV] on mountpoint [MP] with filesystem
96 [FSTYPE] and options [FSOPTS] in the initramfs
5616feb0 97 -i, --include [SOURCE] [TARGET]
39ff0682
AT
98 Include the files in the SOURCE directory into the
99 Target directory in the final initramfs.
3b403b32 100 If SOURCE is a file, it will be installed to TARGET
4fea3ea6 101 in the final initramfs.
39ff0682
AT
102 -I, --install [LIST] Install the space separated list of files into the
103 initramfs.
5b158ad3 104 --gzip Compress the generated initramfs using gzip.
cc02093d
HH
105 This will be done by default, unless another
106 compression option or --no-compress is passed.
5b158ad3 107 --bzip2 Compress the generated initramfs using bzip2.
cc02093d
HH
108 Make sure your kernel has bzip2 decompression support
109 compiled in, otherwise you will not be able to boot.
110 --lzma Compress the generated initramfs using lzma.
3b403b32 111 Make sure your kernel has lzma support compiled in,
cc02093d 112 otherwise you will not be able to boot.
5e6c3b03
VL
113 --xz Compress the generated initramfs using xz.
114 Make sure that your kernel has xz support compiled
115 in, otherwise you will not be able to boot.
116 --compress [COMPRESSION] Compress the generated initramfs with the
117 passed compression program. Make sure your kernel
3b403b32 118 knows how to decompress the generated initramfs,
5e6c3b03 119 otherwise you will not be able to boot.
5b158ad3 120 --no-compress Do not compress the generated initramfs. This will
cc02093d 121 override any other compression options.
5b11bb73 122 --list-modules List all available dracut modules.
956af8f2
123 -M, --show-modules Print included module's name to standard output during
124 build.
5fe5c7d0 125 --keep Keep the temporary initramfs for debugging purposes
cdfeb278 126 --sshkey [SSHKEY] Add ssh key to initramfs (use with ssh-client module)
ccaa9bee
HH
127
128If [LIST] has multiple arguments, then you have to put these in quotes.
129For example:
130# dracut --add-drivers "module1 module2" ...
cc02093d 131EOF
5616feb0
AT
132}
133
8466db96
HH
134# function push()
135# push values to a stack
136# $1 = stack variable
137# $2.. values
138# example:
139# push stack 1 2 "3 4"
140push() {
141 local __stack=$1; shift
142 for i in "$@"; do
143 eval ${__stack}'[${#'${__stack}'[@]}]="$i"'
144 done
145}
146
147# function pop()
148# pops the last value from a stack
149# assigns value to second argument variable
150# or echo to stdout, if no second argument
151# $1 = stack variable
152# $2 = optional variable to store the value
153# example:
154# pop stack val
155# val=$(pop stack)
156pop() {
157 local __stack=$1; shift
3b403b32 158 local __resultvar=$1
8466db96
HH
159 local myresult;
160 # check for empty stack
161 eval '[[ ${#'${__stack}'[@]} -eq 0 ]] && return 1'
162
163 eval myresult='${'${__stack}'[${#'${__stack}'[@]}-1]}'
164
165 if [[ "$__resultvar" ]]; then
166 eval $__resultvar="'$myresult'"
167 else
168 echo "$myresult"
169 fi
170 eval unset ${__stack}'[${#'${__stack}'[@]}-1]'
171 return 0
172}
173
5bc545ed
VL
174# Little helper function for reading args from the commandline.
175# it automatically handles -a b and -a=b variants, and returns 1 if
176# we need to shift $3.
177read_arg() {
178 # $1 = arg name
179 # $2 = arg value
180 # $3 = arg parameter
181 local rematch='^[^=]*=(.*)$'
182 if [[ $2 =~ $rematch ]]; then
cc02093d 183 read "$1" <<< "${BASH_REMATCH[1]}"
5bc545ed 184 else
cc02093d
HH
185 read "$1" <<< "$3"
186 # There is no way to shift our callers args, so
187 # return 1 to indicate they should do it instead.
188 return 1
5bc545ed
VL
189 fi
190}
191
661f9a34
HH
192# Little helper function for reading args from the commandline to a stack.
193# it automatically handles -a b and -a=b variants, and returns 1 if
194# we need to shift $3.
195push_arg() {
196 # $1 = arg name
197 # $2 = arg value
198 # $3 = arg parameter
199 local rematch='^[^=]*=(.*)$'
200 if [[ $2 =~ $rematch ]]; then
201 push "$1" "${BASH_REMATCH[1]}"
202 else
203 push "$1" "$3"
204 # There is no way to shift our callers args, so
205 # return 1 to indicate they should do it instead.
206 return 1
207 fi
208}
209
432196ae 210verbosity_mod_l=0
486a1b93 211
b368a5f3 212while (($# > 0)); do
5bc545ed 213 case ${1%%=*} in
661f9a34 214 -a|--add) push_arg add_dracutmodules_l "$@" || shift;;
31f1c02d 215 --force-add) push_arg force_add_dracutmodules_l "$@" || shift;;
661f9a34 216 --add-drivers) push_arg add_drivers_l "$@" || shift;;
fcbcb252 217 --omit-drivers) push_arg omit_drivers_l "$@" || shift;;
661f9a34
HH
218 -m|--modules) push_arg dracutmodules_l "$@" || shift;;
219 -o|--omit) push_arg omit_dracutmodules_l "$@" || shift;;
220 -d|--drivers) push_arg drivers_l "$@" || shift;;
221 --filesystems) push_arg filesystems_l "$@" || shift;;
0ec40e08 222 -I|--install) push_arg install_items_l "$@" || shift;;
661f9a34 223 --fwdir) push_arg fw_dir_l "$@" || shift;;
25b45979 224 --fscks) push_arg fscks_l "$@" || shift;;
70cb8a68
HH
225 --add-fstab) push_arg add_fstab_l "$@" || shift;;
226 --mount) push_arg fstab_lines "$@" || shift;;
25b45979 227 --nofscks) nofscks_l="yes";;
cc02093d
HH
228 -k|--kmoddir) read_arg drivers_dir_l "$@" || shift;;
229 -c|--conf) read_arg conffile "$@" || shift;;
230 --confdir) read_arg confdir "$@" || shift;;
882c4c5a 231 --tmpdir) read_arg tmpdir_l "$@" || shift;;
e103615b 232 -L|--stdlog) read_arg stdloglvl_l "$@" || shift;;
3a714439 233 --compress) read_arg compress_l "$@" || shift;;
fd786adc 234 --prefix) read_arg prefix_l "$@" || shift;;
cc02093d
HH
235 -f|--force) force=yes;;
236 --kernel-only) kernel_only="yes"; no_kernel="no";;
237 --no-kernel) kernel_only="no"; no_kernel="yes";;
238 --strip) do_strip_l="yes";;
239 --nostrip) do_strip_l="no";;
fd786adc 240 --noprefix) prefix_l="/";;
690396a5
VL
241 --mdadmconf) mdadmconf_l="yes";;
242 --nomdadmconf) mdadmconf_l="no";;
243 --lvmconf) lvmconf_l="yes";;
244 --nolvmconf) lvmconf_l="no";;
cc02093d 245 --debug) debug="yes";;
57258a2c 246 --profile) profile="yes";;
cdfeb278 247 --sshkey) read_arg sshkey "$@" || shift;;
432196ae
248 -v|--verbose) ((verbosity_mod_l++));;
249 -q|--quiet) ((verbosity_mod_l--));;
cc02093d
HH
250 -l|--local) allowlocal="yes" ;;
251 -H|--hostonly) hostonly_l="yes" ;;
cb74b382 252 --no-hostonly) hostonly_l="no" ;;
cc02093d
HH
253 --fstab) use_fstab_l="yes" ;;
254 -h|--help) usage; exit 1 ;;
2aad2344
HH
255 -i|--include) push include_src "$2"
256 push include_target "$3"
257 shift 2;;
3a714439
VL
258 --bzip2) compress_l="bzip2";;
259 --lzma) compress_l="lzma";;
260 --xz) compress_l="xz";;
261 --no-compress) _no_compress_l="cat";;
262 --gzip) compress_l="gzip";;
5b11bb73
HH
263 --list-modules)
264 do_list="yes";
265 ;;
956af8f2
266 -M|--show-modules)
267 show_modules_l="yes"
268 ;;
5fe5c7d0 269 --keep) keep="yes";;
cc02093d 270 -*) printf "\nUnknown option: %s\n\n" "$1" >&2; usage; exit 1;;
3b403b32 271 *)
dbf381f7 272 if ! [[ ${outfile+x} ]]; then
486a1b93 273 outfile=$1
8a5354a9 274 elif ! [[ ${kernel+x} ]]; then
486a1b93
HH
275 kernel=$1
276 else
277 usage; exit 1;
278 fi
279 ;;
641cc356 280 esac
b368a5f3 281 shift
641cc356 282done
8a5354a9 283if ! [[ $kernel ]]; then
486a1b93
HH
284 kernel=$(uname -r)
285fi
286[[ $outfile ]] || outfile="/boot/initramfs-$kernel.img"
4cba351e 287
7b2d3d99 288for i in /usr/sbin /sbin /usr/bin /bin; do
65fe8ae8
HH
289 rl=$i
290 if [ -L "$i" ]; then
291 rl=$(readlink -f $i)
292 fi
7b2d3d99 293 NPATH+=":$rl"
65fe8ae8 294done
7b2d3d99 295export PATH="${NPATH#:}"
68910ba4 296unset NPATH
d619fb5e 297unset LD_LIBRARY_PATH
1fcf2d2f 298unset GREP_OPTIONS
a55711cd 299
3b403b32 300[[ $debug ]] && {
c36ce334
VL
301 export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
302 set -x
303}
304
57258a2c
HH
305[[ $profile ]] && {
306 export PS4='+ $(date "+%s.%N") ${BASH_SOURCE}@${LINENO}: ';
307 set -x
308 debug=yes
309}
310
c5a65990 311[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
5d791c0e 312
552ecca6 313[[ $allowlocal && -f "$(readlink -f ${0%/*})/dracut-functions.sh" ]] && \
3b403b32 314 dracutbasedir="$(readlink -f ${0%/*})"
42c71947 315
f1336ac7 316# if we were not passed a config file, try the default one
42c71947 317if [[ ! -f $conffile ]]; then
eebc929a
VL
318 [[ $allowlocal ]] && conffile="$dracutbasedir/dracut.conf" || \
319 conffile="/etc/dracut.conf"
42c71947 320fi
f1336ac7 321
2c2c4580 322if [[ ! -d $confdir ]]; then
ae24b114 323 [[ $allowlocal ]] && confdir="$dracutbasedir/dracut.conf.d" || \
eebc929a 324 confdir="/etc/dracut.conf.d"
2c2c4580
HH
325fi
326
2245f372
AB
327# source our config file
328[[ -f $conffile ]] && . "$conffile"
329
2c2c4580 330# source our config dir
6438b4fe 331if [[ $confdir && -d $confdir ]]; then
3b403b32 332 for f in "$confdir"/*.conf; do
cc02093d 333 [[ -e $f ]] && . "$f"
2c2c4580
HH
334 done
335fi
336
d87c2708 337# these optins add to the stuff in the config file
e5e5c895 338if (( ${#add_dracutmodules_l[@]} )); then
661f9a34
HH
339 while pop add_dracutmodules_l val; do
340 add_dracutmodules+=" $val "
341 done
342fi
343
31f1c02d
AW
344if (( ${#force_add_dracutmodules_l[@]} )); then
345 while pop force_add_dracutmodules_l val; do
346 force_add_dracutmodules+=" $val "
347 done
348fi
349
25b45979
MS
350if (( ${#fscks_l[@]} )); then
351 while pop fscks_l val; do
352 fscks+=" $val "
353 done
354fi
355
70cb8a68
HH
356if (( ${#add_fstab_l[@]} )); then
357 while pop add_fstab_l val; do
358 add_fstab+=" $val "
359 done
360fi
361
362if (( ${#fstab_lines_l[@]} )); then
363 while pop fstab_lines_l val; do
364 push fstab_lines $val
365 done
366fi
367
0ec40e08
HH
368if (( ${#install_items_l[@]} )); then
369 while pop install_items_l val; do
c6c6a088 370 install_items+=" $val "
0ec40e08
HH
371 done
372fi
373
f1336ac7 374# these options override the stuff in the config file
e5e5c895 375if (( ${#dracutmodules_l[@]} )); then
661f9a34
HH
376 dracutmodules=''
377 while pop dracutmodules_l val; do
378 dracutmodules+="$val "
379 done
380fi
381
e5e5c895 382if (( ${#omit_dracutmodules_l[@]} )); then
661f9a34
HH
383 omit_dracutmodules=''
384 while pop omit_dracutmodules_l val; do
385 omit_dracutmodules+="$val "
386 done
387fi
388
e5e5c895 389if (( ${#filesystems_l[@]} )); then
661f9a34
HH
390 filesystems=''
391 while pop filesystems_l val; do
392 filesystems+="$val "
393 done
394fi
395
e5e5c895 396if (( ${#fw_dir_l[@]} )); then
661f9a34
HH
397 fw_dir=''
398 while pop fw_dir_l val; do
399 fw_dir+="$val "
400 done
401fi
402
e103615b 403[[ $stdloglvl_l ]] && stdloglvl=$stdloglvl_l
b991617f 404[[ ! $stdloglvl ]] && stdloglvl=4
432196ae
405stdloglvl=$((stdloglvl + verbosity_mod_l))
406((stdloglvl > 6)) && stdloglvl=6
407((stdloglvl < 0)) && stdloglvl=0
408
26537a5b 409[[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
31f7db66 410[[ $do_strip_l ]] && do_strip=$do_strip_l
fd786adc
HH
411[[ $prefix_l ]] && prefix=$prefix_l
412[[ $prefix = "/" ]] && unset prefix
ba726310 413[[ $hostonly_l ]] && hostonly=$hostonly_l
7c179686 414[[ $use_fstab_l ]] && use_fstab=$use_fstab_l
2f02ae9d 415[[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
7a34efa5 416[[ $lvmconf_l ]] && lvmconf=$lvmconf_l
c5a65990 417[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
8bc650d9 418[[ $fw_dir ]] || fw_dir="/lib/firmware/updates /lib/firmware"
882c4c5a
419[[ $tmpdir_l ]] && tmpdir="$tmpdir_l"
420[[ $tmpdir ]] || tmpdir=/var/tmp
5be225d2 421[[ $do_strip ]] || do_strip=no
3a714439 422[[ $compress_l ]] && compress=$compress_l
956af8f2 423[[ $show_modules_l ]] && show_modules=$show_modules_l
25b45979 424[[ $nofscks_l ]] && nofscks="yes"
ddfd1d10
VL
425# eliminate IFS hackery when messing with fw_dir
426fw_dir=${fw_dir//:/ }
9a8a00cf 427
3a714439 428# handle compression options.
55468fb9 429[[ $compress ]] || compress="gzip"
3a714439 430case $compress in
6f223367 431 bzip2) compress="bzip2 -9";;
3a714439
VL
432 lzma) compress="lzma -9";;
433 xz) compress="xz --check=crc32 --lzma2=dict=1MiB";;
55468fb9 434 gzip) command -v pigz > /dev/null 2>&1 && compress="pigz -9" || \
3a714439
VL
435 compress="gzip -9";;
436esac
437if [[ $_no_compress_l = "cat" ]]; then
438 compress="cat"
439fi
440
ba726310 441[[ $hostonly = yes ]] && hostonly="-h"
ba67b923 442[[ $hostonly != "-h" ]] && unset hostonly
ba726310 443
552ecca6
HH
444if [[ -f $dracutbasedir/dracut-functions.sh ]]; then
445 . $dracutbasedir/dracut-functions.sh
adbc8a42 446else
552ecca6 447 echo "Cannot find $dracutbasedir/dracut-functions.sh." >&2
6f590cd1
HH
448 echo "Are you running from a git checkout?" >&2
449 echo "Try passing -l as an argument to $0" >&2
cc02093d 450 exit 1
adbc8a42 451fi
22fd1627 452
d239b550
MS
453# Verify bash version, curret minimum is 3.1
454if (( ${BASH_VERSINFO[0]} < 3 ||
455 ( ${BASH_VERSINFO[0]} == 3 && ${BASH_VERSINFO[1]} < 1 ) )); then
456 dfatal 'You need at least Bash 3.1 to use dracut, sorry.'
457 exit 1
458fi
459
552ecca6 460dracutfunctions=$dracutbasedir/dracut-functions.sh
5cad5bb5 461export dracutfunctions
9a8a00cf 462
fcbcb252
HH
463if (( ${#drivers_l[@]} )); then
464 drivers=''
465 while pop drivers_l val; do
466 drivers+="$val "
467 done
468fi
469drivers=${drivers/-/_}
470
471if (( ${#add_drivers_l[@]} )); then
472 while pop add_drivers_l val; do
473 add_drivers+=" $val "
474 done
475fi
476add_drivers=${add_drivers/-/_}
477
478if (( ${#omit_drivers_l[@]} )); then
479 while pop omit_drivers_l val; do
480 omit_drivers+=" $val "
481 done
482fi
483omit_drivers=${omit_drivers/-/_}
484
34248c92 485omit_drivers_corrected=""
fcbcb252
HH
486for d in $omit_drivers; do
487 strstr " $drivers $add_drivers " " $d " && continue
34248c92 488 omit_drivers_corrected+="$d|"
fcbcb252 489done
34248c92 490omit_drivers="${omit_drivers_corrected%|}"
fcbcb252
HH
491unset omit_drivers_corrected
492
493
432196ae 494ddebug "Executing $0 $dracut_args"
58dad702 495
5b11bb73
HH
496[[ $do_list = yes ]] && {
497 for mod in $dracutbasedir/modules.d/*; do
498 [[ -d $mod ]] || continue;
d8e8e14e
499 [[ -e $mod/install || -e $mod/installkernel || \
500 -e $mod/module-setup.sh ]] || continue
5b11bb73
HH
501 echo ${mod##*/??}
502 done
503 exit 0
504}
505
fa5cd2bf
506# Detect lib paths
507[[ $libdir ]] || for libdir in /lib64 /lib; do
508 [[ -d $libdir ]] && break
509done || {
432196ae 510 dfatal 'No lib directory?!!!'
fa5cd2bf
511 exit 1
512}
432196ae 513
fa5cd2bf
514[[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
515 [[ -d $usrlibdir ]] && break
432196ae 516done || dwarn 'No usr/lib directory!'
fa5cd2bf 517
66ac3cd1 518# This is kinda legacy -- eventually it should go away.
f1336ac7 519case $dracutmodules in
66ac3cd1 520 ""|auto) dracutmodules="all" ;;
f1336ac7 521esac
e12aac5e 522
454771cd 523abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
ec9315e5 524
f24a2d46 525srcmods="/lib/modules/$kernel/"
ecf42850 526[[ $drivers_dir ]] && {
22ecea45 527 if vercmp $(modprobe --version | cut -d' ' -f3) lt 3.7; then
432196ae 528 dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
ecf42850
529 exit 1
530 fi
531 srcmods="$drivers_dir"
532}
f24a2d46
HH
533export srcmods
534
c8937ec4 535if [[ -f $outfile && ! $force ]]; then
432196ae 536 dfatal "Will not override existing initramfs ($outfile) without --force"
ec9315e5
JK
537 exit 1
538fi
539
ebfdb219 540outdir=${outfile%/*}
52eed268
HH
541[[ $outdir ]] || outdir="/"
542
ebfdb219 543if [[ ! -d "$outdir" ]]; then
432196ae 544 dfatal "Can't write $outfile: Directory $outdir does not exist."
454771cd 545 exit 1
ebfdb219 546elif [[ ! -w "$outdir" ]]; then
432196ae 547 dfatal "No permission to write $outdir."
454771cd 548 exit 1
0a325a91 549elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
432196ae 550 dfatal "No permission to write $outfile."
734a0d9e
HH
551 exit 1
552fi
553
882c4c5a
554readonly TMPDIR="$tmpdir"
555readonly initdir=$(mktemp --tmpdir="$TMPDIR/" -d -t initramfs.XXXXXX)
88b3e005
DY
556[ -d "$initdir" ] || {
557 dfatal "mktemp failed."
558 exit 1
559}
734a0d9e 560
cc02093d 561# clean up after ourselves no matter how we die.
5fe5c7d0 562trap 'ret=$?;[[ $keep ]] && echo "Not removing $initdir." >&2 || rm -rf "$initdir";exit $ret;' EXIT
cc02093d 563# clean up after ourselves no matter how we die.
3b403b32 564trap 'exit 1;' SIGINT
ec9315e5 565
f6f74096
DD
566# Need to be able to have non-root users read stuff (rpcbind etc)
567chmod 755 "$initdir"
568
cd7ff122
AW
569for line in "${fstab_lines[@]}"; do
570 set -- $line
571 #dev mp fs fsopts
d0096de7
AW
572 push host_devs "$1"
573 push host_fs_types "$1|$3"
cd7ff122
AW
574done
575
576for f in $add_fstab; do
577 [ -e $f ] || continue
578 while read dev rest; do
579 push host_devs $dev
580 done < $f
581done
582
7ae5d9d1 583if [[ $hostonly ]]; then
480d772f
HH
584 # in hostonly mode, determine all devices, which have to be accessed
585 # and examine them for filesystem types
586
7ae5d9d1
HH
587 push host_mp \
588 "/" \
589 "/etc" \
590 "/usr" \
591 "/usr/bin" \
592 "/usr/sbin" \
593 "/usr/lib" \
594 "/usr/lib64" \
595 "/boot"
596
7ae5d9d1
HH
597 for mp in "${host_mp[@]}"; do
598 mountpoint "$mp" >/dev/null 2>&1 || continue
d0096de7 599 push host_devs $(readlink -f "/dev/block/$(find_block_device "$mp")")
7ae5d9d1 600 done
7ae5d9d1 601fi
7ae5d9d1 602
cd7ff122
AW
603_get_fs_type() (
604 [[ $1 ]] || return
d0096de7 605 if [[ -b $1 ]] && get_fs_env $1; then
31b6f4a3 606 echo "$(readlink -f $1)|$ID_FS_TYPE"
cd7ff122
AW
607 return 1
608 fi
4f10ae2b 609 if [[ -b /dev/block/$1 ]] && get_fs_env /dev/block/$1; then
31b6f4a3 610 echo "$(readlink -f /dev/block/$1)|$ID_FS_TYPE"
4f10ae2b
HH
611 return 1
612 fi
cd7ff122
AW
613 if fstype=$(find_dev_fstype $1); then
614 echo "$1|$fstype"
615 return 1
616 fi
617 return 1
618)
619
620for dev in "${host_devs[@]}"; do
621 unset fs_type
622 for fstype in $(_get_fs_type $dev) \
d0096de7 623 $(check_block_and_slaves _get_fs_type $(get_maj_min $dev)); do
cd7ff122
AW
624 if ! strstr " ${host_fs_types[*]} " " $fstype ";then
625 push host_fs_types "$fstype"
626 fi
627 done
628done
629
0b53ca70 630export initdir dracutbasedir dracutmodules drivers \
e103615b 631 fw_dir drivers_dir debug no_kernel kernel_only \
fcbcb252 632 add_drivers omit_drivers mdadmconf lvmconf filesystems \
02aa7955 633 use_fstab fstab_lines libdir usrlibdir fscks nofscks \
1e64e493 634 stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \
f1354b8a 635 debug host_fs_types host_devs sshkey add_fstab
f4fff04e 636
dbad9f46 637# Create some directory structure first
fd786adc 638[[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
dbad9f46 639
785a6cd2 640[[ -h /lib ]] || mkdir -m 0755 -p "${initdir}${prefix}/lib"
fd786adc 641[[ $prefix ]] && ln -sfn "${prefix#/}/lib" "$initdir/lib"
dbad9f46 642
785a6cd2 643if [[ $prefix ]]; then
3b403b32 644 for d in bin etc lib "$libdir" sbin tmp usr var; do
785a6cd2
HH
645 ln -sfn "${prefix#/}/${d#/}" "$initdir/$d"
646 done
647fi
648
649if [[ $kernel_only != yes ]]; then
d433da44 650 for d in usr/bin usr/sbin bin etc lib "$libdir" sbin tmp usr var var/log; do
fd786adc 651 [[ -e "${initdir}${prefix}/$d" ]] && continue
d433da44
HH
652 if [ -L "/$d" ]; then
653 inst_symlink "/$d" "${prefix}/$d"
785a6cd2
HH
654 else
655 mkdir -m 0755 -p "${initdir}${prefix}/$d"
656 fi
3b403b32 657 done
dbad9f46 658
7c14b368 659 for d in dev proc sys sysroot root run run/lock run/initramfs; do
d433da44
HH
660 if [ -L "/$d" ]; then
661 inst_symlink "/$d"
785a6cd2
HH
662 else
663 mkdir -m 0755 -p "$initdir/$d"
664 fi
33ee031c 665 done
dbad9f46
HH
666
667 ln -sfn /run "$initdir/var/run"
668 ln -sfn /run/lock "$initdir/var/lock"
6f09acf8
HH
669else
670 for d in lib "$libdir"; do
671 [[ -e "${initdir}${prefix}/$d" ]] && continue
672 if [ -h "/$d" ]; then
673 inst "/$d" "${prefix}/$d"
674 else
675 mkdir -m 0755 -p "${initdir}${prefix}/$d"
676 fi
677 done
33ee031c 678fi
0f86847d 679
14c47b0e
HH
680if [[ $kernel_only != yes ]]; then
681 mkdir -p "${initdir}/etc/cmdline.d"
4fed3ddf
HH
682 for _d in $hookdirs; do
683 mkdir -m 0755 -p ${initdir}/lib/dracut/hooks/$_d
684 done
14c47b0e 685fi
96b8d60a 686
379c34d2
HH
687mkdir -p "$initdir/.kernelmodseen"
688
1b7fd0fa 689mods_to_load=""
66ac3cd1
VL
690# check all our modules to see if they should be sourced.
691# This builds a list of modules that we will install next.
1b7fd0fa
AW
692for_each_module_dir check_module
693for_each_module_dir check_mount
694
979c4a93 695modules_loaded=" "
95bde758 696# source our modules.
5d791c0e 697for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
979c4a93
HH
698 _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]}
699 if strstr "$mods_to_load" " $_d_mod "; then
700 [[ $show_modules = yes ]] && echo "$_d_mod" || \
701 dinfo "*** Including module: $_d_mod ***"
cc02093d 702 if [[ $kernel_only = yes ]]; then
979c4a93 703 module_installkernel $_d_mod
cc02093d 704 else
979c4a93 705 module_install $_d_mod
95d2dabc 706 if [[ $no_kernel != yes ]]; then
979c4a93 707 module_installkernel $_d_mod
cc02093d
HH
708 fi
709 fi
979c4a93
HH
710 mods_to_load=${mods_to_load// $_d_mod /}
711 modules_loaded+="$_d_mod "
66ac3cd1 712 fi
15136762 713done
20abd914 714unset moddir
ea216a65 715dinfo "*** Including modules done ***"
aabc0553 716
0f86847d 717## final stuff that has to happen
bc6b0dec 718
0f86847d 719# generate module dependencies for the initrd
8a474569
VL
720if [[ -d $initdir/lib/modules/$kernel ]] && \
721 ! depmod -a -b "$initdir" $kernel; then
432196ae 722 dfatal "\"depmod -a $kernel\" failed."
8a474569 723 exit 1
f1336ac7 724fi
ec9315e5 725
661f9a34
HH
726while pop include_src src && pop include_target tgt; do
727 if [[ $src && $tgt ]]; then
728 if [[ -f $src ]]; then
729 inst $src $tgt
730 else
3b403b32 731 ddebug "Including directory: $src"
661f9a34 732 mkdir -p "${initdir}/${tgt}"
dbad9f46 733 # check for preexisting symlinks, so we can cope with the
fd786adc 734 # symlinks to $prefix
dbad9f46
HH
735 for i in "$src"/*; do
736 [[ -e "$i" || -h "$i" ]] || continue
737 s=${initdir}/${tgt}/${i#$src/}
738 if [[ -d "$i" ]]; then
739 if ! [[ -e "$s" ]]; then
740 mkdir -m 0755 -p "$s"
741 chmod --reference="$i" "$s"
742 fi
743 cp -a -t "$s" "$i"/*
744 else
745 cp -a -t "$s" "$i"
3b403b32 746 fi
dbad9f46 747 done
661f9a34 748 fi
4fea3ea6 749 fi
661f9a34 750done
88ffd2df 751
14c47b0e
HH
752if [[ $kernel_only != yes ]]; then
753 for item in $install_items; do
754 dracut_install -o "$item"
755 done
756 unset item
39ff0682 757
14c47b0e
HH
758 while pop fstab_lines line; do
759 echo "$line 0 0" >> "${initdir}/etc/fstab"
760 done
70cb8a68 761
14c47b0e
HH
762 for f in $add_fstab; do
763 cat $f >> "${initdir}/etc/fstab"
764 done
1254925f 765
1254925f
HH
766 # make sure that library links are correct and up to date
767 for f in /etc/ld.so.conf /etc/ld.so.conf.d/*; do
482c573d 768 [[ -f $f ]] && inst_simple "$f"
1254925f
HH
769 done
770 if ! ldconfig -r "$initdir"; then
771 if [[ $UID = 0 ]]; then
772 derror "ldconfig exited ungracefully"
773 else
774 derror "ldconfig might need uid=0 (root) for chroot()"
775 fi
432196ae
776 fi
777fi
fdc421db 778
379c34d2
HH
779rm -fr "$initdir/.kernelmodseen"
780
781
432196ae
782if (($maxloglvl >= 5)); then
783 ddebug "Listing sizes of included files:"
784 du -c "$initdir" | sort -n | ddebug
785fi
c4ad7fff 786
3b403b32 787# strip binaries
98adb06e 788if [[ $do_strip = yes ]] ; then
3b403b32 789 for p in strip grep find; do
cc02093d
HH
790 if ! type -P $p >/dev/null; then
791 derror "Could not find '$p'. You should run $0 with '--nostrip'."
792 do_strip=no
793 fi
31f7db66
HH
794 done
795fi
796
98adb06e 797if [[ $do_strip = yes ]] ; then
cc02093d
HH
798 for f in $(find "$initdir" -type f \
799 \( -perm -0100 -or -perm -0010 -or -perm -0001 \
800 -or -path '*/lib/modules/*.ko' \) ); do
801 dinfo "Stripping $f"
802 strip -g "$f" 2>/dev/null|| :
31f7db66
HH
803 done
804fi
805
6292ee9d 806type hardlink &>/dev/null && {
cc02093d 807 hardlink "$initdir" 2>&1
6292ee9d
HH
808}
809
979c4a93 810if strstr "$modules_loaded" " fips " && command -v prelink >/dev/null; then
3335ed0e
HH
811 for dir in "$initdir/bin" \
812 "$initdir/sbin" \
813 "$initdir/usr/bin" \
814 "$initdir/usr/sbin"; do
815 [[ -L "$dir" ]] && continue
816 for i in "$dir"/*; do
817 [[ -x $i ]] && prelink -u $i &>/dev/null
818 done
979c4a93
HH
819 done
820fi
821
59f288ce 822if ! ( cd "$initdir"; find . |cpio -R 0:0 -H newc -o --quiet| \
3b403b32 823 $compress > "$outfile"; ); then
432196ae 824 dfatal "dracut: creation of $outfile failed"
734a0d9e 825 exit 1
3b403b32 826fi
432196ae
827
828dinfo "Wrote $outfile:"
829dinfo "$(ls -l "$outfile")"
1faecdc1 830
3da58569 831exit 0