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