2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
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
10 # Copyright 2005-2010 Red Hat, Inc. All rights reserved.
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.
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.
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/>.
30 # 80x25 linebreak here ^
32 Usage: $0 [OPTION]... <initramfs> <kernel-version>
33 Creates initial ramdisk images for preloading modules
35 -f, --force Overwrite existing initramfs file.
36 -m, --modules [LIST] Specify a space-separated list of dracut modules to
37 call when building the initramfs. Modules are located
38 in /usr/share/dracut/modules.d.
39 -o, --omit [LIST] Omit a space-separated list of dracut modules.
40 -a, --add [LIST] Add a space-separated list of dracut modules.
41 -d, --drivers [LIST] Specify a space-separated list of kernel modules to
42 exclusively include in the initramfs.
43 --add-drivers [LIST] Specify a space-separated list of kernel
44 modules to add to the initramfs.
45 --filesystems [LIST] Specify a space-separated list of kernel filesystem
46 modules to exclusively include in the generic
48 -k, --kmoddir [DIR] Specify the directory, where to look for kernel
50 --fwdir [DIR] Specify additional directories, where to look for
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
54 --strip Strip binaries in the initramfs
55 --nostrip Do not strip binaries in the initramfs (default)
56 --mdadmconf Include local /etc/mdadm.conf
57 --nomdadmconf Do not include local /etc/mdadm.conf
58 --lvmconf Include local /etc/lvm/lvm.conf
59 --nolvmconf Do not include local /etc/lvm/lvm.conf
60 -h, --help This message
61 --debug Output debug information of the build process
62 -v, --verbose Verbose output during the build process
63 -c, --conf [FILE] Specify configuration file to use.
64 Default: /etc/dracut.conf
65 --confdir [DIR] Specify configuration directory to use *.conf files
66 from. Default: /etc/dracut.conf.d
67 -l, --local Local mode. Use modules from the current working
68 directory instead of the system-wide installed in
69 /usr/share/dracut/modules.d.
70 Useful when running dracut from a git checkout.
71 -H, --hostonly Host-Only mode: Install only what is needed for
72 booting the local host instead of a generic host.
73 --fstab Use /etc/fstab to determine the root device.
74 -i, --include [SOURCE] [TARGET]
75 Include the files in the SOURCE directory into the
76 Target directory in the final initramfs.
77 -I, --install [LIST] Install the space separated list of files into the
79 --gzip Compress the generated initramfs using gzip.
80 This will be done by default, unless another
81 compression option or --no-compress is passed.
82 --bzip2 Compress the generated initramfs using bzip2.
83 Make sure your kernel has bzip2 decompression support
84 compiled in, otherwise you will not be able to boot.
85 --lzma Compress the generated initramfs using lzma.
86 Make sure your kernel has lzma support compiled in,
87 otherwise you will not be able to boot.
88 --no-compress Do not compress the generated initramfs. This will
89 override any other compression options.
90 --list-modules List all available dracut modules.
94 # Little helper function for reading args from the commandline.
95 # it automatically handles -a b and -a=b variants, and returns 1 if
96 # we need to shift $3.
101 local rematch
='^[^=]*=(.*)$'
102 if [[ $2 =~
$rematch ]]; then
103 read "$1" <<< "${BASH_REMATCH[1]}"
106 # There is no way to shift our callers args, so
107 # return 1 to indicate they should do it instead.
114 -m|
--modules) read_arg dracutmodules_l
"$@" ||
shift;;
115 -o|
--omit) read_arg omit_dracutmodules_l
"$@" ||
shift;;
116 -a|
--add) read_arg add_dracutmodules_l
"$@" ||
shift;;
117 -d|
--drivers) read_arg drivers_l
"$@" ||
shift;;
118 --add-drivers) read_arg add_drivers_l
"$@" ||
shift;;
119 --filesystems) read_arg filesystems_l
"$@" ||
shift;;
120 -k|
--kmoddir) read_arg drivers_dir_l
"$@" ||
shift;;
121 -c|
--conf) read_arg conffile
"$@" ||
shift;;
122 --confdir) read_arg confdir
"$@" ||
shift;;
123 -I|
--install) read_arg install_items
"$@" ||
shift;;
124 --fwdir) read_arg fw_dir_l
"$@" ||
shift;;
125 -f|
--force) force
=yes;;
126 --kernel-only) kernel_only
="yes"; no_kernel
="no";;
127 --no-kernel) kernel_only
="no"; no_kernel
="yes";;
128 --strip) do_strip_l
="yes";;
129 --nostrip) do_strip_l
="no";;
130 --mdadmconf) mdadmconf_l
="yes";;
131 --nomdadmconf) mdadmconf_l
="no";;
132 --lvmconf) lvmconf_l
="yes";;
133 --nolvmconf) lvmconf_l
="no";;
134 --debug) debug
="yes";;
135 -v|
--verbose) beverbose
="yes";;
136 -l|
--local) allowlocal
="yes" ;;
137 -H|
--hostonly) hostonly_l
="yes" ;;
138 --fstab) use_fstab_l
="yes" ;;
139 -h|
--help) usage
; exit 1 ;;
140 -i|
--include) include_src
="$2"; include_target
="$3"; shift 2;;
141 --bzip2) [[ $compress != cat ]] && compress="bzip2 -9";;
142 --lzma) [[ $compress != cat ]] && compress="lzma -9";;
143 --no-compress) compress="cat";;
144 --gzip) if [[ $compress != cat ]]; then
145 type pigz
> /dev
/null
2>&1 && compress="pigz -9" || \
151 -*) printf "\nUnknown option: %s\n\n" "$1" >&2; usage
; exit 1;;
157 PATH
=/sbin
:/bin
:/usr
/sbin
:/usr
/bin
161 export PS4
='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
165 [[ $dracutbasedir ]] || dracutbasedir
=/usr
/share
/dracut
167 [[ $allowlocal && -f "$(readlink -f ${0%/*})/dracut-functions" ]] && \
168 dracutbasedir
="${0%/*}"
170 # if we were not passed a config file, try the default one
171 if [[ ! -f $conffile ]]; then
172 [[ $allowlocal ]] && conffile
="$dracutbasedir/dracut.conf" || \
173 conffile
="/etc/dracut.conf"
176 if [[ ! -d $confdir ]]; then
177 [[ $allowlocal ]] && confdir
="$dracutbasedir/dracut.conf.d" || \
178 confdir
="/etc/dracut.conf.d"
181 # source our config file
182 [[ -f $conffile ]] && .
"$conffile"
184 # source our config dir
185 if [[ $confdir && -d $confdir ]]; then
186 for f
in "$confdir"/*.conf
; do
187 [[ -e $f ]] && .
"$f"
191 # these optins add to the stuff in the config file
192 [[ $add_dracutmodules_l ]] && add_dracutmodules
+=" $add_dracutmodules_l"
193 [[ $add_drivers_l ]] && add_drivers
+=" $add_drivers_l"
195 # these options override the stuff in the config file
196 [[ $dracutmodules_l ]] && dracutmodules
=$dracutmodules_l
197 [[ $omit_dracutmodules_l ]] && omit_dracutmodules
=$omit_dracutmodules_l
198 [[ $drivers_l ]] && drivers
=$drivers_l
199 [[ $filesystems_l ]] && filesystems
=$filesystems_l
200 [[ $drivers_dir_l ]] && drivers_dir
=$drivers_dir_l
201 [[ $fw_dir_l ]] && fw_dir
=$fw_dir_l
202 [[ $do_strip_l ]] && do_strip
=$do_strip_l
203 [[ $hostonly_l ]] && hostonly
=$hostonly_l
204 [[ $use_fstab_l ]] && use_fstab
=$use_fstab_l
205 [[ $mdadmconf_l ]] && mdadmconf
=$mdadmconf_l
206 [[ $lvmconf_l ]] && lvmconf
=$lvmconf_l
207 [[ $dracutbasedir ]] || dracutbasedir
=/usr
/share
/dracut
208 [[ $fw_dir ]] || fw_dir
=/lib
/firmware
209 [[ $do_strip ]] || do_strip
=no
210 # eliminate IFS hackery when messing with fw_dir
211 fw_dir
=${fw_dir//:/ }
213 [[ $hostonly = yes ]] && hostonly
="-h"
214 [[ $hostonly != "-h" ]] && unset hostonly
215 [[ $compress ]] ||
compress="gzip -9"
217 if [[ -f $dracutbasedir/dracut-functions
]]; then
218 .
$dracutbasedir/dracut-functions
220 echo "Cannot find $dracutbasedir/dracut-functions." >&2
221 echo "Are you running from a git checkout?" >&2
222 echo "Try passing -l as an argument to $0" >&2
226 dracutfunctions
=$dracutbasedir/dracut-functions
227 export dracutfunctions
229 dinfo
"Executing $0 $dracut_args"
231 [[ $do_list = yes ]] && {
232 for mod
in $dracutbasedir/modules.d
/*; do
233 [[ -d $mod ]] ||
continue;
234 [[ -e $mod/install ||
-e $mod/installkernel
]] ||
continue;
241 [[ $libdir ]] ||
for libdir
in /lib64
/lib
; do
242 [[ -d $libdir ]] && break
244 derror
'No lib directory?!!!'
247 [[ $usrlibdir ]] ||
for usrlibdir
in /usr
/lib64
/usr
/lib
; do
248 [[ -d $usrlibdir ]] && break
249 done || dwarning
'No usr/lib directory!'
251 # This is kinda legacy -- eventually it should go away.
252 case $dracutmodules in
253 ""|auto
) dracutmodules
="all" ;;
256 [[ $2 ]] && kernel
=$2 || kernel
=$
(uname
-r)
257 [[ $1 ]] && outfile
=$1 || outfile
="/boot/initramfs-$kernel.img"
258 abs_outfile
=$
(readlink
-f "$outfile") && outfile
="$abs_outfile"
260 srcmods
="/lib/modules/$kernel/"
261 [[ $drivers_dir ]] && {
262 if vercmp $
(modprobe
--version | cut
-d' ' -f3) lt
3.7; then
263 derror
'To use --kmoddir option module-init-tools >= 3.7 is required.'
266 srcmods
="$drivers_dir"
270 if [[ -f $outfile && ! $force ]]; then
271 derror
"Will not override existing initramfs ($outfile) without --force"
276 if [[ ! -d "$outdir" ]]; then
277 derror
"Can't write $outfile: Directory $outdir does not exist."
279 elif [[ ! -w "$outdir" ]]; then
280 derror
"No permission to write $outdir."
282 elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
283 derror
"No permission to write $outfile."
287 hookdirs
="cmdline pre-udev pre-trigger netroot pre-mount"
288 hookdirs
+=" pre-pivot mount emergency"
290 [[ $TMPDIR && ! -w $TMPDIR ]] && unset TMPDIR
291 readonly initdir
=$
(mktemp
-d -t initramfs.XXXXXX
)
293 # clean up after ourselves no matter how we die.
294 trap 'ret=$?;rm -rf "$initdir";exit $ret;' EXIT
295 # clean up after ourselves no matter how we die.
296 trap 'exit 1;' SIGINT
298 # Need to be able to have non-root users read stuff (rpcbind etc)
301 export initdir hookdirs dracutbasedir dracutmodules drivers \
302 fw_dir drivers_dir debug beverbose no_kernel kernel_only \
303 add_drivers mdadmconf lvmconf filesystems \
304 use_fstab libdir usrlibdir
306 if [[ $kernel_only != yes ]]; then
307 # Create some directory structure first
308 for d
in bin sbin usr
/bin usr
/sbin usr
/lib etc \
309 proc sys sysroot tmp dev
/pts var
/run
; do
314 # check all our modules to see if they should be sourced.
315 # This builds a list of modules that we will install next.
318 # source our modules.
319 for moddir
in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
320 mod
=${moddir##*/}; mod
=${mod#[0-9][0-9]}
321 if strstr
"$mods_to_load" " $mod "; then
322 dinfo
"*** Sourcing module $mod"
323 if [[ $kernel_only = yes ]]; then
324 module_installkernel
$mod
327 if [[ $no_kernel != yes ]]; then
328 module_installkernel
$mod
331 mods_to_load
=${mods_to_load// $mod /}
336 ## final stuff that has to happen
338 # generate module dependencies for the initrd
339 if [[ -d $initdir/lib
/modules
/$kernel ]] && \
340 ! depmod
-a -b "$initdir" $kernel; then
341 derror
"\"depmod -a $kernel\" failed."
345 if [[ $include_src && $include_target ]]; then
346 mkdir
-p "$initdir$include_target"
347 cp -a -t "$initdir$include_target" "$include_src"/*
350 for item
in $install_items; do
351 dracut_install
"$item"
355 # make sure that library links are correct and up to date
356 cp -ar /etc
/ld.so.conf
* "$initdir"/etc
357 ldconfig
-r "$initdir" ||
[[ $UID != "0" ]] && \
358 dinfo
"ldconfig might need uid=0 (root) for chroot()"
360 [[ $beverbose = yes ]] && (du
-c "$initdir" |
sort -n)
363 if [[ $do_strip = yes ]] ; then
364 for p
in strip
grep find; do
365 if ! type -P $p >/dev
/null
; then
366 derror
"Could not find '$p'. You should run $0 with '--nostrip'."
372 if [[ $do_strip = yes ]] ; then
373 for f
in $
(find "$initdir" -type f \
374 \
( -perm -0100 -or -perm -0010 -or -perm -0001 \
375 -or -path '*/lib/modules/*.ko' \
) ); do
377 strip
-g "$f" 2>/dev
/null||
:
381 type hardlink
&>/dev
/null
&& {
382 hardlink
"$initdir" 2>&1
385 if ! ( cd "$initdir"; find . |
cpio -R 0:0 -H newc
-o --quiet | \
386 $compress > "$outfile"; ); then
387 derror
"dracut: creation of $outfile failed"
390 dinfo
"Wrote $outfile"
391 dinfo $
(ls -l "$outfile")