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_l
="yes" ;;
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 [[ $hostonly_l ]] && hostonly
=$hostonly_l
114 [[ $dracutbasedir ]] || dracutbasedir
=/usr
/share
/dracut
115 [[ $fw_dir ]] || fw_dir
=/lib
/firmware
116 [[ $do_strip ]] || do_strip
=yes
117 # eliminate IFS hackery when messing with fw_dir
118 fw_dir
=${fw_dir//:/ }
120 [[ $hostonly = yes ]] && hostonly
="-h"
122 [[ $allowlocal && -f "$(dirname $0)/dracut-functions" ]] && dsrc
="$(dirname $0)" || dsrc
=$dracutbasedir
124 if [[ -f $dsrc/dracut-functions
]]; then
125 .
$dsrc/dracut-functions
127 echo "Cannot find $dsrc/dracut-functions. Are you running from a git checkout?"
128 echo "Try passing -l as an argument to $0"
132 dracutfunctions
=$dsrc/dracut-functions
133 export dracutfunctions
135 # This is kinda legacy -- eventually it should go away.
136 case $dracutmodules in
137 ""|auto
) dracutmodules
="all" ;;
140 [[ $2 ]] && kernel
=$2 || kernel
=$
(uname
-r)
141 [[ $1 ]] && outfile
=$
(readlink
-f $1) || outfile
="/boot/initrd-$kernel.img"
143 srcmods
="/lib/modules/$kernel/"
144 [[ $drivers_dir ]] && srcmods
="$drivers_dir"
147 if [[ -f $outfile && ! $force ]]; then
148 echo "Will not override existing initramfs ($outfile) without --force"
152 hookdirs
="cmdline pre-udev pre-trigger netroot pre-mount pre-pivot mount emergency"
154 readonly initdir
=$
(mktemp
-d -t initramfs.XXXXXX
)
155 trap 'rm -rf "$initdir"' 0 # clean up after ourselves no matter how we die.
157 # Need to be able to have non-root users read stuff (rpcbind etc)
160 export initdir hookdirs dsrc dracutmodules drivers \
161 fw_dir drivers_dir debug beverbose no_kernel kernel_only
163 if [[ $kernel_only != yes ]]; then
164 # Create some directory structure first
165 for d
in bin sbin usr
/bin usr
/sbin usr
/lib etc proc sys sysroot tmp dev
/pts var
/run
; do
166 mkdir
-p "$initdir/$d";
170 # check all our modules to see if they should be sourced.
171 # This builds a list of modules that we will install next.
175 for moddir
in "$dsrc/modules.d"/[0-9][0-9]*; do
176 mod
=${moddir##*/}; mod
=${mod#[0-9][0-9]}
177 if strstr
"$mods_to_load" " $mod "; then
178 if [[ $kernel_only = yes ]]; then
179 [[ -x $moddir/installkernel
]] && .
"$moddir/installkernel"
182 if [[ $no_kernel != yes && -x $moddir/installkernel
]]; then
183 .
"$moddir/installkernel"
186 mods_to_load
=${mods_to_load// $mod /}
192 ## final stuff that has to happen
194 # generate module dependencies for the initrd
195 if [[ -d $initdir/lib
/modules
/$kernel ]]; then
196 if ! depmod
-a -b "$initdir" $kernel; then
197 echo "\"depmod -a $kernel\" failed."
202 # make sure that library links are correct and up to date
203 ldconfig
-n -r "$initdir" /lib
* /usr
/lib
*
205 if [[ $include_src && $include_target ]]; then
206 mkdir
-p "$initdir$include_target"
207 cp -a -t "$initdir$include_target" "$include_src"/*
210 for item
in $install_items; do
211 dracut_install
"$item"
215 [[ $beverbose = yes ]] && (du
-c "$initdir" |
sort -n)
218 if [[ $do_strip = yes ]] ; then
219 for p
in strip objdump
sed grep find; do
220 if ! which $p >/dev
/null
2>&1; then
221 derror
"Could not find '$p'. You should run $0 with '--nostrip'."
227 if [[ $do_strip = yes ]] ; then
228 for f
in $
(find "$initdir" -type f \
( -perm -0100 -or -perm -0010 -or -perm -0001 \
) -exec file {} \
; |
229 grep -v ' shared object,' |
230 sed -n -e 's/^\(.*\):[ ]*ELF.*, not stripped/\1/p'); do
234 # FIXME: only strip -g for now
236 #strip -g --strip-unneeded "$f" || :
238 #if objdump -h $f | grep '^[ ]*[0-9]*[ ]*.note[ ]' -A 1 | \
239 # grep -q ALLOC; then
242 #strip -R .comment $note "$f" || :
246 ( cd "$initdir"; find . |
cpio -R 0:0 -H newc
-o |
gzip -9 > "$outfile"; )
248 [[ $beverbose = yes ]] && ls -lh "$outfile"