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