]>
Commit | Line | Data |
---|---|---|
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 |
27 | dracut_args="$@" | |
5616feb0 AT |
28 | |
29 | usage() { | |
30 | # 80x25 linebreak here ^ | |
cc02093d HH |
31 | cat << EOF |
32 | Usage: $0 [OPTION]... <initramfs> <kernel-version> | |
5616feb0 AT |
33 | Creates 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 AŻ |
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 AŻ |
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 AŻ |
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 | |
124 | If [LIST] has multiple arguments, then you have to put these in quotes. | |
125 | For example: | |
126 | # dracut --add-drivers "module1 module2" ... | |
cc02093d | 127 | EOF |
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" | |
136 | push() { | |
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) | |
152 | pop() { | |
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. | |
173 | read_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. | |
191 | push_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 | 206 | verbosity_mod_l=0 |
486a1b93 | 207 | |
b368a5f3 | 208 | while (($# > 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 AŻ |
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 AŻ |
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 | 276 | done |
8a5354a9 | 277 | if ! [[ $kernel ]]; then |
486a1b93 HH |
278 | kernel=$(uname -r) |
279 | fi | |
280 | [[ $outfile ]] || outfile="/boot/initramfs-$kernel.img" | |
4cba351e | 281 | |
f8545d04 HH |
282 | PATH=/sbin:/bin:/usr/sbin:/usr/bin |
283 | export PATH | |
d619fb5e | 284 | unset LD_LIBRARY_PATH |
1fcf2d2f | 285 | unset GREP_OPTIONS |
a55711cd | 286 | |
3b403b32 | 287 | [[ $debug ]] && { |
c36ce334 VL |
288 | export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): '; |
289 | set -x | |
290 | } | |
291 | ||
57258a2c HH |
292 | [[ $profile ]] && { |
293 | export PS4='+ $(date "+%s.%N") ${BASH_SOURCE}@${LINENO}: '; | |
294 | set -x | |
295 | debug=yes | |
296 | } | |
297 | ||
c5a65990 | 298 | [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut |
5d791c0e | 299 | |
cc02093d | 300 | [[ $allowlocal && -f "$(readlink -f ${0%/*})/dracut-functions" ]] && \ |
3b403b32 | 301 | dracutbasedir="$(readlink -f ${0%/*})" |
42c71947 | 302 | |
f1336ac7 | 303 | # if we were not passed a config file, try the default one |
42c71947 | 304 | if [[ ! -f $conffile ]]; then |
eebc929a VL |
305 | [[ $allowlocal ]] && conffile="$dracutbasedir/dracut.conf" || \ |
306 | conffile="/etc/dracut.conf" | |
42c71947 | 307 | fi |
f1336ac7 | 308 | |
2c2c4580 | 309 | if [[ ! -d $confdir ]]; then |
ae24b114 | 310 | [[ $allowlocal ]] && confdir="$dracutbasedir/dracut.conf.d" || \ |
eebc929a | 311 | confdir="/etc/dracut.conf.d" |
2c2c4580 HH |
312 | fi |
313 | ||
2245f372 AB |
314 | # source our config file |
315 | [[ -f $conffile ]] && . "$conffile" | |
316 | ||
2c2c4580 | 317 | # source our config dir |
6438b4fe | 318 | if [[ $confdir && -d $confdir ]]; then |
3b403b32 | 319 | for f in "$confdir"/*.conf; do |
cc02093d | 320 | [[ -e $f ]] && . "$f" |
2c2c4580 HH |
321 | done |
322 | fi | |
323 | ||
d87c2708 | 324 | # these optins add to the stuff in the config file |
e5e5c895 | 325 | if (( ${#add_dracutmodules_l[@]} )); then |
661f9a34 HH |
326 | while pop add_dracutmodules_l val; do |
327 | add_dracutmodules+=" $val " | |
328 | done | |
329 | fi | |
330 | ||
31f1c02d AW |
331 | if (( ${#force_add_dracutmodules_l[@]} )); then |
332 | while pop force_add_dracutmodules_l val; do | |
333 | force_add_dracutmodules+=" $val " | |
334 | done | |
335 | fi | |
336 | ||
337 | ||
e5e5c895 | 338 | if (( ${#add_drivers_l[@]} )); then |
661f9a34 HH |
339 | while pop add_drivers_l val; do |
340 | add_drivers+=" $val " | |
341 | done | |
342 | fi | |
d87c2708 | 343 | |
25b45979 MS |
344 | if (( ${#fscks_l[@]} )); then |
345 | while pop fscks_l val; do | |
346 | fscks+=" $val " | |
347 | done | |
348 | fi | |
349 | ||
70cb8a68 HH |
350 | if (( ${#add_fstab_l[@]} )); then |
351 | while pop add_fstab_l val; do | |
352 | add_fstab+=" $val " | |
353 | done | |
354 | fi | |
355 | ||
356 | if (( ${#fstab_lines_l[@]} )); then | |
357 | while pop fstab_lines_l val; do | |
358 | push fstab_lines $val | |
359 | done | |
360 | fi | |
361 | ||
0ec40e08 HH |
362 | if (( ${#install_items_l[@]} )); then |
363 | while pop install_items_l val; do | |
364 | push install_items $val | |
365 | done | |
366 | fi | |
367 | ||
f1336ac7 | 368 | # these options override the stuff in the config file |
e5e5c895 | 369 | if (( ${#dracutmodules_l[@]} )); then |
661f9a34 HH |
370 | dracutmodules='' |
371 | while pop dracutmodules_l val; do | |
372 | dracutmodules+="$val " | |
373 | done | |
374 | fi | |
375 | ||
e5e5c895 | 376 | if (( ${#omit_dracutmodules_l[@]} )); then |
661f9a34 HH |
377 | omit_dracutmodules='' |
378 | while pop omit_dracutmodules_l val; do | |
379 | omit_dracutmodules+="$val " | |
380 | done | |
381 | fi | |
382 | ||
e5e5c895 | 383 | if (( ${#drivers_l[@]} )); then |
661f9a34 HH |
384 | drivers='' |
385 | while pop drivers_l val; do | |
386 | drivers+="$val " | |
387 | done | |
388 | fi | |
389 | ||
e5e5c895 | 390 | if (( ${#filesystems_l[@]} )); then |
661f9a34 HH |
391 | filesystems='' |
392 | while pop filesystems_l val; do | |
393 | filesystems+="$val " | |
394 | done | |
395 | fi | |
396 | ||
e5e5c895 | 397 | if (( ${#fw_dir_l[@]} )); then |
661f9a34 HH |
398 | fw_dir='' |
399 | while pop fw_dir_l val; do | |
400 | fw_dir+="$val " | |
401 | done | |
402 | fi | |
403 | ||
e103615b | 404 | [[ $stdloglvl_l ]] && stdloglvl=$stdloglvl_l |
b991617f | 405 | [[ ! $stdloglvl ]] && stdloglvl=4 |
432196ae AŻ |
406 | stdloglvl=$((stdloglvl + verbosity_mod_l)) |
407 | ((stdloglvl > 6)) && stdloglvl=6 | |
408 | ((stdloglvl < 0)) && stdloglvl=0 | |
409 | ||
26537a5b | 410 | [[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l |
31f7db66 | 411 | [[ $do_strip_l ]] && do_strip=$do_strip_l |
fd786adc HH |
412 | [[ $prefix_l ]] && prefix=$prefix_l |
413 | [[ $prefix = "/" ]] && unset prefix | |
ba726310 | 414 | [[ $hostonly_l ]] && hostonly=$hostonly_l |
7c179686 | 415 | [[ $use_fstab_l ]] && use_fstab=$use_fstab_l |
2f02ae9d | 416 | [[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l |
7a34efa5 | 417 | [[ $lvmconf_l ]] && lvmconf=$lvmconf_l |
c5a65990 | 418 | [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut |
8bc650d9 | 419 | [[ $fw_dir ]] || fw_dir="/lib/firmware/updates /lib/firmware" |
5be225d2 | 420 | [[ $do_strip ]] || do_strip=no |
3a714439 | 421 | [[ $compress_l ]] && compress=$compress_l |
956af8f2 | 422 | [[ $show_modules_l ]] && show_modules=$show_modules_l |
25b45979 | 423 | [[ $nofscks_l ]] && nofscks="yes" |
ddfd1d10 VL |
424 | # eliminate IFS hackery when messing with fw_dir |
425 | fw_dir=${fw_dir//:/ } | |
9a8a00cf | 426 | |
3a714439 | 427 | # handle compression options. |
55468fb9 | 428 | [[ $compress ]] || compress="gzip" |
3a714439 | 429 | case $compress in |
6f223367 | 430 | bzip2) compress="bzip2 -9";; |
3a714439 VL |
431 | lzma) compress="lzma -9";; |
432 | xz) compress="xz --check=crc32 --lzma2=dict=1MiB";; | |
55468fb9 | 433 | gzip) command -v pigz > /dev/null 2>&1 && compress="pigz -9" || \ |
3a714439 VL |
434 | compress="gzip -9";; |
435 | esac | |
436 | if [[ $_no_compress_l = "cat" ]]; then | |
437 | compress="cat" | |
438 | fi | |
439 | ||
ba726310 | 440 | [[ $hostonly = yes ]] && hostonly="-h" |
ba67b923 | 441 | [[ $hostonly != "-h" ]] && unset hostonly |
ba726310 | 442 | |
5d791c0e | 443 | if [[ -f $dracutbasedir/dracut-functions ]]; then |
cc02093d | 444 | . $dracutbasedir/dracut-functions |
adbc8a42 | 445 | else |
6f590cd1 HH |
446 | echo "Cannot find $dracutbasedir/dracut-functions." >&2 |
447 | echo "Are you running from a git checkout?" >&2 | |
448 | echo "Try passing -l as an argument to $0" >&2 | |
cc02093d | 449 | exit 1 |
adbc8a42 | 450 | fi |
22fd1627 | 451 | |
d239b550 MS |
452 | # Verify bash version, curret minimum is 3.1 |
453 | if (( ${BASH_VERSINFO[0]} < 3 || | |
454 | ( ${BASH_VERSINFO[0]} == 3 && ${BASH_VERSINFO[1]} < 1 ) )); then | |
455 | dfatal 'You need at least Bash 3.1 to use dracut, sorry.' | |
456 | exit 1 | |
457 | fi | |
458 | ||
5d791c0e | 459 | dracutfunctions=$dracutbasedir/dracut-functions |
5cad5bb5 | 460 | export dracutfunctions |
9a8a00cf | 461 | |
432196ae | 462 | ddebug "Executing $0 $dracut_args" |
58dad702 | 463 | |
5b11bb73 HH |
464 | [[ $do_list = yes ]] && { |
465 | for mod in $dracutbasedir/modules.d/*; do | |
466 | [[ -d $mod ]] || continue; | |
d8e8e14e AŻ |
467 | [[ -e $mod/install || -e $mod/installkernel || \ |
468 | -e $mod/module-setup.sh ]] || continue | |
5b11bb73 HH |
469 | echo ${mod##*/??} |
470 | done | |
471 | exit 0 | |
472 | } | |
473 | ||
fa5cd2bf AŻ |
474 | # Detect lib paths |
475 | [[ $libdir ]] || for libdir in /lib64 /lib; do | |
476 | [[ -d $libdir ]] && break | |
477 | done || { | |
432196ae | 478 | dfatal 'No lib directory?!!!' |
fa5cd2bf AŻ |
479 | exit 1 |
480 | } | |
432196ae | 481 | |
fa5cd2bf AŻ |
482 | [[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do |
483 | [[ -d $usrlibdir ]] && break | |
432196ae | 484 | done || dwarn 'No usr/lib directory!' |
fa5cd2bf | 485 | |
66ac3cd1 | 486 | # This is kinda legacy -- eventually it should go away. |
f1336ac7 | 487 | case $dracutmodules in |
66ac3cd1 | 488 | ""|auto) dracutmodules="all" ;; |
f1336ac7 | 489 | esac |
e12aac5e | 490 | |
454771cd | 491 | abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile" |
ec9315e5 | 492 | |
f24a2d46 | 493 | srcmods="/lib/modules/$kernel/" |
ecf42850 | 494 | [[ $drivers_dir ]] && { |
22ecea45 | 495 | if vercmp $(modprobe --version | cut -d' ' -f3) lt 3.7; then |
432196ae | 496 | dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.' |
ecf42850 AŻ |
497 | exit 1 |
498 | fi | |
499 | srcmods="$drivers_dir" | |
500 | } | |
f24a2d46 HH |
501 | export srcmods |
502 | ||
c8937ec4 | 503 | if [[ -f $outfile && ! $force ]]; then |
432196ae | 504 | dfatal "Will not override existing initramfs ($outfile) without --force" |
ec9315e5 JK |
505 | exit 1 |
506 | fi | |
507 | ||
ebfdb219 | 508 | outdir=${outfile%/*} |
52eed268 HH |
509 | [[ $outdir ]] || outdir="/" |
510 | ||
ebfdb219 | 511 | if [[ ! -d "$outdir" ]]; then |
432196ae | 512 | dfatal "Can't write $outfile: Directory $outdir does not exist." |
454771cd | 513 | exit 1 |
ebfdb219 | 514 | elif [[ ! -w "$outdir" ]]; then |
432196ae | 515 | dfatal "No permission to write $outdir." |
454771cd | 516 | exit 1 |
0a325a91 | 517 | elif [[ -f "$outfile" && ! -w "$outfile" ]]; then |
432196ae | 518 | dfatal "No permission to write $outfile." |
734a0d9e HH |
519 | exit 1 |
520 | fi | |
521 | ||
6787b8cc | 522 | readonly TMPDIR=/var/tmp |
9a5393ac | 523 | readonly initdir=$(mktemp --tmpdir=/var/tmp/ -d -t initramfs.XXXXXX) |
88b3e005 DY |
524 | [ -d "$initdir" ] || { |
525 | dfatal "mktemp failed." | |
526 | exit 1 | |
527 | } | |
734a0d9e | 528 | |
cc02093d | 529 | # clean up after ourselves no matter how we die. |
5fe5c7d0 | 530 | trap 'ret=$?;[[ $keep ]] && echo "Not removing $initdir." >&2 || rm -rf "$initdir";exit $ret;' EXIT |
cc02093d | 531 | # clean up after ourselves no matter how we die. |
3b403b32 | 532 | trap 'exit 1;' SIGINT |
ec9315e5 | 533 | |
f6f74096 DD |
534 | # Need to be able to have non-root users read stuff (rpcbind etc) |
535 | chmod 755 "$initdir" | |
536 | ||
cd7ff122 AW |
537 | for line in "${fstab_lines[@]}"; do |
538 | set -- $line | |
539 | #dev mp fs fsopts | |
d0096de7 AW |
540 | push host_devs "$1" |
541 | push host_fs_types "$1|$3" | |
cd7ff122 AW |
542 | done |
543 | ||
544 | for f in $add_fstab; do | |
545 | [ -e $f ] || continue | |
546 | while read dev rest; do | |
547 | push host_devs $dev | |
548 | done < $f | |
549 | done | |
550 | ||
7ae5d9d1 | 551 | if [[ $hostonly ]]; then |
480d772f HH |
552 | # in hostonly mode, determine all devices, which have to be accessed |
553 | # and examine them for filesystem types | |
554 | ||
7ae5d9d1 HH |
555 | push host_mp \ |
556 | "/" \ | |
557 | "/etc" \ | |
558 | "/usr" \ | |
559 | "/usr/bin" \ | |
560 | "/usr/sbin" \ | |
561 | "/usr/lib" \ | |
562 | "/usr/lib64" \ | |
563 | "/boot" | |
564 | ||
7ae5d9d1 HH |
565 | for mp in "${host_mp[@]}"; do |
566 | mountpoint "$mp" >/dev/null 2>&1 || continue | |
d0096de7 | 567 | push host_devs $(readlink -f "/dev/block/$(find_block_device "$mp")") |
7ae5d9d1 | 568 | done |
7ae5d9d1 | 569 | fi |
7ae5d9d1 | 570 | |
cd7ff122 AW |
571 | _get_fs_type() ( |
572 | [[ $1 ]] || return | |
d0096de7 | 573 | if [[ -b $1 ]] && get_fs_env $1; then |
cd7ff122 AW |
574 | echo "$1|$ID_FS_TYPE" |
575 | return 1 | |
576 | fi | |
4f10ae2b HH |
577 | if [[ -b /dev/block/$1 ]] && get_fs_env /dev/block/$1; then |
578 | echo "/dev/block/$1|$ID_FS_TYPE" | |
579 | return 1 | |
580 | fi | |
cd7ff122 AW |
581 | if fstype=$(find_dev_fstype $1); then |
582 | echo "$1|$fstype" | |
583 | return 1 | |
584 | fi | |
585 | return 1 | |
586 | ) | |
587 | ||
588 | for dev in "${host_devs[@]}"; do | |
589 | unset fs_type | |
590 | for fstype in $(_get_fs_type $dev) \ | |
d0096de7 | 591 | $(check_block_and_slaves _get_fs_type $(get_maj_min $dev)); do |
cd7ff122 AW |
592 | if ! strstr " ${host_fs_types[*]} " " $fstype ";then |
593 | push host_fs_types "$fstype" | |
594 | fi | |
595 | done | |
596 | done | |
597 | ||
0b53ca70 | 598 | export initdir dracutbasedir dracutmodules drivers \ |
e103615b | 599 | fw_dir drivers_dir debug no_kernel kernel_only \ |
9d1015b6 | 600 | add_drivers mdadmconf lvmconf filesystems \ |
4401925c | 601 | use_fstab libdir usrlibdir fscks nofscks cttyhack \ |
1e64e493 | 602 | stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \ |
cdfeb278 | 603 | debug host_fs_types host_devs sshkey |
f4fff04e | 604 | |
dbad9f46 | 605 | # Create some directory structure first |
fd786adc | 606 | [[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}" |
dbad9f46 | 607 | |
785a6cd2 | 608 | [[ -h /lib ]] || mkdir -m 0755 -p "${initdir}${prefix}/lib" |
fd786adc | 609 | [[ $prefix ]] && ln -sfn "${prefix#/}/lib" "$initdir/lib" |
dbad9f46 | 610 | |
785a6cd2 | 611 | if [[ $prefix ]]; then |
3b403b32 | 612 | for d in bin etc lib "$libdir" sbin tmp usr var; do |
785a6cd2 HH |
613 | ln -sfn "${prefix#/}/${d#/}" "$initdir/$d" |
614 | done | |
615 | fi | |
616 | ||
617 | if [[ $kernel_only != yes ]]; then | |
ef6900eb | 618 | for d in bin etc lib "$libdir" sbin tmp usr var var/log usr/bin usr/sbin; do |
fd786adc | 619 | [[ -e "${initdir}${prefix}/$d" ]] && continue |
785a6cd2 HH |
620 | if [ -h "/$d" ]; then |
621 | inst "/$d" "${prefix}/$d" | |
622 | else | |
623 | mkdir -m 0755 -p "${initdir}${prefix}/$d" | |
624 | fi | |
3b403b32 | 625 | done |
dbad9f46 | 626 | |
7c14b368 | 627 | for d in dev proc sys sysroot root run run/lock run/initramfs; do |
785a6cd2 HH |
628 | if [ -h "/$d" ]; then |
629 | inst "/$d" | |
630 | else | |
631 | mkdir -m 0755 -p "$initdir/$d" | |
632 | fi | |
33ee031c | 633 | done |
dbad9f46 HH |
634 | |
635 | ln -sfn /run "$initdir/var/run" | |
636 | ln -sfn /run/lock "$initdir/var/lock" | |
6f09acf8 HH |
637 | else |
638 | for d in lib "$libdir"; do | |
639 | [[ -e "${initdir}${prefix}/$d" ]] && continue | |
640 | if [ -h "/$d" ]; then | |
641 | inst "/$d" "${prefix}/$d" | |
642 | else | |
643 | mkdir -m 0755 -p "${initdir}${prefix}/$d" | |
644 | fi | |
645 | done | |
33ee031c | 646 | fi |
0f86847d | 647 | |
1b7fd0fa | 648 | mods_to_load="" |
66ac3cd1 VL |
649 | # check all our modules to see if they should be sourced. |
650 | # This builds a list of modules that we will install next. | |
1b7fd0fa AW |
651 | for_each_module_dir check_module |
652 | for_each_module_dir check_mount | |
653 | ||
979c4a93 | 654 | modules_loaded=" " |
95bde758 | 655 | # source our modules. |
5d791c0e | 656 | for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do |
979c4a93 HH |
657 | _d_mod=${moddir##*/}; _d_mod=${_d_mod#[0-9][0-9]} |
658 | if strstr "$mods_to_load" " $_d_mod "; then | |
659 | [[ $show_modules = yes ]] && echo "$_d_mod" || \ | |
660 | dinfo "*** Including module: $_d_mod ***" | |
cc02093d | 661 | if [[ $kernel_only = yes ]]; then |
979c4a93 | 662 | module_installkernel $_d_mod |
cc02093d | 663 | else |
979c4a93 | 664 | module_install $_d_mod |
95d2dabc | 665 | if [[ $no_kernel != yes ]]; then |
979c4a93 | 666 | module_installkernel $_d_mod |
cc02093d HH |
667 | fi |
668 | fi | |
979c4a93 HH |
669 | mods_to_load=${mods_to_load// $_d_mod /} |
670 | modules_loaded+="$_d_mod " | |
66ac3cd1 | 671 | fi |
15136762 | 672 | done |
20abd914 | 673 | unset moddir |
ea216a65 | 674 | dinfo "*** Including modules done ***" |
aabc0553 | 675 | |
0f86847d | 676 | ## final stuff that has to happen |
bc6b0dec | 677 | |
0f86847d | 678 | # generate module dependencies for the initrd |
8a474569 VL |
679 | if [[ -d $initdir/lib/modules/$kernel ]] && \ |
680 | ! depmod -a -b "$initdir" $kernel; then | |
432196ae | 681 | dfatal "\"depmod -a $kernel\" failed." |
8a474569 | 682 | exit 1 |
f1336ac7 | 683 | fi |
ec9315e5 | 684 | |
661f9a34 HH |
685 | while pop include_src src && pop include_target tgt; do |
686 | if [[ $src && $tgt ]]; then | |
687 | if [[ -f $src ]]; then | |
688 | inst $src $tgt | |
689 | else | |
3b403b32 | 690 | ddebug "Including directory: $src" |
661f9a34 | 691 | mkdir -p "${initdir}/${tgt}" |
dbad9f46 | 692 | # check for preexisting symlinks, so we can cope with the |
fd786adc | 693 | # symlinks to $prefix |
dbad9f46 HH |
694 | for i in "$src"/*; do |
695 | [[ -e "$i" || -h "$i" ]] || continue | |
696 | s=${initdir}/${tgt}/${i#$src/} | |
697 | if [[ -d "$i" ]]; then | |
698 | if ! [[ -e "$s" ]]; then | |
699 | mkdir -m 0755 -p "$s" | |
700 | chmod --reference="$i" "$s" | |
701 | fi | |
702 | cp -a -t "$s" "$i"/* | |
703 | else | |
704 | cp -a -t "$s" "$i" | |
3b403b32 | 705 | fi |
dbad9f46 | 706 | done |
661f9a34 | 707 | fi |
4fea3ea6 | 708 | fi |
661f9a34 | 709 | done |
88ffd2df | 710 | |
661f9a34 HH |
711 | while pop install_items items; do |
712 | for item in $items; do | |
713 | dracut_install "$item" | |
714 | done | |
39ff0682 AT |
715 | done |
716 | unset item | |
717 | ||
70cb8a68 HH |
718 | while pop fstab_lines line; do |
719 | echo "$line 0 0" >> "${initdir}/etc/fstab" | |
720 | done | |
721 | ||
722 | for f in $add_fstab; do | |
723 | cat $f >> "${initdir}/etc/fstab" | |
724 | done | |
1254925f HH |
725 | |
726 | if [[ $kernel_only != yes ]]; then | |
727 | # make sure that library links are correct and up to date | |
728 | for f in /etc/ld.so.conf /etc/ld.so.conf.d/*; do | |
482c573d | 729 | [[ -f $f ]] && inst_simple "$f" |
1254925f HH |
730 | done |
731 | if ! ldconfig -r "$initdir"; then | |
732 | if [[ $UID = 0 ]]; then | |
733 | derror "ldconfig exited ungracefully" | |
734 | else | |
735 | derror "ldconfig might need uid=0 (root) for chroot()" | |
736 | fi | |
432196ae AŻ |
737 | fi |
738 | fi | |
fdc421db | 739 | |
432196ae AŻ |
740 | if (($maxloglvl >= 5)); then |
741 | ddebug "Listing sizes of included files:" | |
742 | du -c "$initdir" | sort -n | ddebug | |
743 | fi | |
c4ad7fff | 744 | |
3b403b32 | 745 | # strip binaries |
98adb06e | 746 | if [[ $do_strip = yes ]] ; then |
3b403b32 | 747 | for p in strip grep find; do |
cc02093d HH |
748 | if ! type -P $p >/dev/null; then |
749 | derror "Could not find '$p'. You should run $0 with '--nostrip'." | |
750 | do_strip=no | |
751 | fi | |
31f7db66 HH |
752 | done |
753 | fi | |
754 | ||
98adb06e | 755 | if [[ $do_strip = yes ]] ; then |
cc02093d HH |
756 | for f in $(find "$initdir" -type f \ |
757 | \( -perm -0100 -or -perm -0010 -or -perm -0001 \ | |
758 | -or -path '*/lib/modules/*.ko' \) ); do | |
759 | dinfo "Stripping $f" | |
760 | strip -g "$f" 2>/dev/null|| : | |
31f7db66 HH |
761 | done |
762 | fi | |
763 | ||
6292ee9d | 764 | type hardlink &>/dev/null && { |
cc02093d | 765 | hardlink "$initdir" 2>&1 |
6292ee9d HH |
766 | } |
767 | ||
979c4a93 HH |
768 | if strstr "$modules_loaded" " fips " && command -v prelink >/dev/null; then |
769 | for i in $initdir/bin/* \ | |
bc313467 | 770 | $initdir/sbin/* \ |
979c4a93 HH |
771 | $initdir/usr/bin/* \ |
772 | $initdir/usr/sbin/*; do | |
773 | [ -x $i ] && prelink -u $i &>/dev/null | |
774 | done | |
775 | fi | |
776 | ||
59f288ce | 777 | if ! ( cd "$initdir"; find . |cpio -R 0:0 -H newc -o --quiet| \ |
3b403b32 | 778 | $compress > "$outfile"; ); then |
432196ae | 779 | dfatal "dracut: creation of $outfile failed" |
734a0d9e | 780 | exit 1 |
3b403b32 | 781 | fi |
432196ae AŻ |
782 | |
783 | dinfo "Wrote $outfile:" | |
784 | dinfo "$(ls -l "$outfile")" | |
1faecdc1 | 785 | |
3da58569 | 786 | exit 0 |