3 # Generator script for a dracut initramfs
4 # Tries to retain some degree of compatibility with the command line
5 # of the various mkinitrd implementations out there
8 # Copyright 2005-2009 Red Hat, Inc. All rights reserved.
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 # 80x25 linebreak here ^
27 echo "Usage: $0 [OPTION]... <initramfs> <kernel-version>
28 Creates initial ramdisk images for preloading modules
30 -f, --force Overwrite existing initramfs file.
31 -m, --modules [LIST] Specify a space-separated list of dracut modules to
32 call when building the initramfs. Modules are located
33 in /usr/share/dracut/modules.d.
34 -o, --omit [LIST] Omit a space-separated list of dracut modules.
35 -a, --add [LIST] Add a space-separated list of dracut modules.
36 -d, --drivers [LIST] Specify a space-separated list of kernel modules to
37 include in the initramfs.
38 -k, --kmoddir [DIR] Specify the directory, where to look for kernel
40 --fwdir [DIR] Specify additional directories, where to look for
41 firmwares, separated by :
42 --kernel-only Only install kernel drivers and firmware files
43 --no-kernel Do not install kernel drivers and firmware files
44 --strip Strip binaries in the initramfs (default)
45 --nostrip Do not strip binaries in the initramfs
46 -h, --help This message
47 --debug Output debug information of the build process
48 -v, --verbose Verbose output during the build process
49 -c, --conf [FILE] Specify configuration file to use.
50 Default: /etc/dracut.conf
51 -l, --local Local mode. Use modules from the current working
52 directory instead of the system-wide installed in
53 /usr/share/dracut/modules.d.
54 Useful when running dracut from a git checkout.
55 -H, --hostonly Host-Only mode: Install only what is needed for
56 booting the local host instead of a generic host.
57 -i, --include [SOURCE] [TARGET]
58 Include the files in the SOURCE directory into the
59 Target directory in the final initramfs.
60 -I, --install [LIST] Install the space separated list of files into the
67 -f|
--force) force
=yes;;
68 -m|
--modules) dracutmodules_l
="$2"; shift;;
69 -o|
--omit) omit_dracutmodules_l
="$2"; shift;;
70 -a|
--add) add_dracutmodules_l
="$2"; shift;;
71 -d|
--drivers) drivers_l
="$2"; shift;;
72 -k|
--kmoddir) drivers_dir_l
="$2"; shift;;
73 --fwdir) fw_dir_l
="$2"; shift;;
74 --kernel-only) kernel_only
="yes"; nokernel
="no";;
75 --no-kernel) kernel_only
="no"; no_kernel
="yes";;
76 --strip) do_strip_l
="yes";;
77 --nostrip) do_strip_l
="no";;
78 -h|
--help) usage
; exit 1 ;;
79 --debug) debug
="yes";;
80 -v|
--verbose) beverbose
="yes";;
81 -c|
--conf) conffile
="$2"; shift;;
82 -l|
--local) allowlocal
="yes" ;;
83 -H|
--hostonly) hostonly
="-h" ;;
84 -i|
--include) include_src
="$2"; include_target
="$3"; shift 2;;
85 -I|
--install) install_items
="$2"; shift;;
86 -*) printf "\nUnknown option: %s\n\n" "$1" >&2; usage
; exit 1;;
92 PATH
=/usr
/local
/sbin
:/usr
/local
/bin
:/usr
/sbin
:/usr
/bin
:/sbin
:/bin
:$PATH
95 export PS4
='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
99 # if we were not passed a config file, try the default one
100 [[ ! -f $conffile ]] && conffile
="/etc/dracut.conf"
102 # source our config file
103 [[ -f $conffile ]] && .
"$conffile"
105 # these options override the stuff in the config file
106 [[ $dracutmodules_l ]] && dracutmodules
=$dracutmodules_l
107 [[ $omit_dracutmodules_l ]] && omit_dracutmodules
=$omit_dracutmodules_l
108 [[ $add_dracutmodules_l ]] && add_dracutmodules
="$add_dracutmodules $add_dracutmodules_l"
109 [[ $drivers_l ]] && drivers
=$drivers_l
110 [[ $drivers_dir_l ]] && drivers_dir
=$drivers_dir_l
111 [[ $fw_dir_l ]] && fw_dir
=$fw_dir_l
112 [[ $do_strip_l ]] && do_strip
=$do_strip_l
113 [[ $dracutbasedir ]] || dracutbasedir
=/usr
/share
/dracut
114 [[ $fw_dir ]] || fw_dir
=/lib
/firmware
115 [[ $do_strip ]] || do_strip
=yes
117 [[ $allowlocal && -f "$(dirname $0)/dracut-functions" ]] && dsrc
="$(dirname $0)" || dsrc
=$dracutbasedir
119 if [[ -f $dsrc/dracut-functions
]]; then
120 .
$dsrc/dracut-functions
122 echo "Cannot find $dsrc/dracut-functions. Are you running from a git checkout?"
123 echo "Try passing -l as an argument to $0"
127 dracutfunctions
=$dsrc/dracut-functions
128 export dracutfunctions
130 # This is kinda legacy -- eventually it should go away.
131 case $dracutmodules in
132 ""|auto
) dracutmodules
="all" ;;
135 [[ $2 ]] && kernel
=$2 || kernel
=$
(uname
-r)
136 [[ $1 ]] && outfile
=$
(readlink
-f $1) || outfile
="/boot/initrd-$kernel.img"
138 srcmods
="/lib/modules/$kernel/"
139 [ -n "$drivers_dir" ] && srcmods
="$drivers_dir"
142 if [[ -f $outfile && ! $force ]]; then
143 echo "Will not override existing initramfs ($outfile) without --force"
147 hookdirs
="cmdline pre-udev pre-trigger netroot pre-mount pre-pivot mount emergency"
149 readonly initdir
=$
(mktemp
-d -t initramfs.XXXXXX
)
150 trap 'rm -rf "$initdir"' 0 # clean up after ourselves no matter how we die.
152 # Need to be able to have non-root users read stuff (rpcbind etc)
155 export initdir hookdirs dsrc dracutmodules drivers \
156 fw_dir drivers_dir debug beverbose no_kernel kernel_only
158 if [[ "$kernel_only" != "yes" ]]; then
159 # Create some directory structure first
160 for d
in bin sbin usr
/bin usr
/sbin usr
/lib etc proc sys sysroot tmp dev
/pts var
/run
; do
161 mkdir
-p "$initdir/$d";
165 # check all our modules to see if they should be sourced.
166 # This builds a list of modules that we will install next.
170 for moddir
in "$dsrc/modules.d"/[0-9][0-9]*; do
171 mod
=${moddir##*/}; mod
=${mod#[0-9][0-9]}
172 if strstr
"$mods_to_load" " $mod "; then
173 if [[ "$kernel_only" = "yes" ]]; then
174 [[ -x "$moddir/installkernel" ]] && .
"$moddir/installkernel"
177 if [[ "$no_kernel" != "yes" && -x "$moddir/installkernel" ]]; then
178 .
"$moddir/installkernel"
181 mods_to_load
=${mods_to_load// $mod /}
187 ## final stuff that has to happen
189 # generate module dependencies for the initrd
190 if [ -d "$initdir/lib/modules/$kernel" ]; then
191 if ! depmod
-a -b "$initdir" $kernel; then
192 echo "\"depmod -a $kernel\" failed."
197 # make sure that library links are correct and up to date
198 ldconfig
-n -r "$initdir" /lib
* /usr
/lib
*
200 if [[ $include_src && $include_target ]]; then
201 mkdir
-p "$initdir$include_target"
202 cp -a -t "$initdir$include_target" "$include_src"/*
205 for item
in $install_items; do
206 dracut_install
"$item"
210 [[ "$beverbose" = "yes" ]] && (du
-c "$initdir" |
sort -n)
213 if [ "$do_strip" = "yes" ] ; then
214 for p
in strip objdump
sed grep find; do
215 if ! which $p >/dev
/null
2>&1; then
216 derror
"Could not find '$p'. You should run $0 with '--nostrip'."
222 if [ "$do_strip" = "yes" ] ; then
223 for f
in $
(find "$initdir" -type f \
( -perm -0100 -or -perm -0010 -or -perm -0001 \
) -exec file {} \
; |
224 grep -v ' shared object,' |
225 sed -n -e 's/^\(.*\):[ ]*ELF.*, not stripped/\1/p'); do
229 # FIXME: only strip -g for now
231 #strip -g --strip-unneeded "$f" || :
233 #if objdump -h $f | grep '^[ ]*[0-9]*[ ]*.note[ ]' -A 1 | \
234 # grep -q ALLOC; then
237 #strip -R .comment $note "$f" || :
241 ( cd "$initdir"; find . |
cpio -H newc
-o |
gzip -9 > "$outfile"; )
243 [[ "$beverbose" = "yes" ]] && ls -lh "$outfile"