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
116 # eliminate IFS hackery when messing with fw_dir
117 fw_dir
=${fw_dir//:/ }
119 [[ $allowlocal && -f "$(dirname $0)/dracut-functions" ]] && dsrc
="$(dirname $0)" || dsrc
=$dracutbasedir
121 if [[ -f $dsrc/dracut-functions
]]; then
122 .
$dsrc/dracut-functions
124 echo "Cannot find $dsrc/dracut-functions. Are you running from a git checkout?"
125 echo "Try passing -l as an argument to $0"
129 dracutfunctions
=$dsrc/dracut-functions
130 export dracutfunctions
132 # This is kinda legacy -- eventually it should go away.
133 case $dracutmodules in
134 ""|auto
) dracutmodules
="all" ;;
137 [[ $2 ]] && kernel
=$2 || kernel
=$
(uname
-r)
138 [[ $1 ]] && outfile
=$
(readlink
-f $1) || outfile
="/boot/initrd-$kernel.img"
140 srcmods
="/lib/modules/$kernel/"
141 [[ $drivers_dir ]] && srcmods
="$drivers_dir"
144 if [[ -f $outfile && ! $force ]]; then
145 echo "Will not override existing initramfs ($outfile) without --force"
149 hookdirs
="cmdline pre-udev pre-trigger netroot pre-mount pre-pivot mount emergency"
151 readonly initdir
=$
(mktemp
-d -t initramfs.XXXXXX
)
152 trap 'rm -rf "$initdir"' 0 # clean up after ourselves no matter how we die.
154 # Need to be able to have non-root users read stuff (rpcbind etc)
157 export initdir hookdirs dsrc dracutmodules drivers \
158 fw_dir drivers_dir debug beverbose no_kernel kernel_only
160 if [[ $kernel_only != yes ]]; then
161 # Create some directory structure first
162 for d
in bin sbin usr
/bin usr
/sbin usr
/lib etc proc sys sysroot tmp dev
/pts var
/run
; do
163 mkdir
-p "$initdir/$d";
167 # check all our modules to see if they should be sourced.
168 # This builds a list of modules that we will install next.
172 for moddir
in "$dsrc/modules.d"/[0-9][0-9]*; do
173 mod
=${moddir##*/}; mod
=${mod#[0-9][0-9]}
174 if strstr
"$mods_to_load" " $mod "; then
175 if [[ $kernel_only = yes ]]; then
176 [[ -x $moddir/installkernel
]] && .
"$moddir/installkernel"
179 if [[ $no_kernel != yes && -x $moddir/installkernel
]]; then
180 .
"$moddir/installkernel"
183 mods_to_load
=${mods_to_load// $mod /}
189 ## final stuff that has to happen
191 # generate module dependencies for the initrd
192 if [[ -d $initdir/lib
/modules
/$kernel ]]; then
193 if ! depmod
-a -b "$initdir" $kernel; then
194 echo "\"depmod -a $kernel\" failed."
199 # make sure that library links are correct and up to date
200 ldconfig
-n -r "$initdir" /lib
* /usr
/lib
*
202 if [[ $include_src && $include_target ]]; then
203 mkdir
-p "$initdir$include_target"
204 cp -a -t "$initdir$include_target" "$include_src"/*
207 for item
in $install_items; do
208 dracut_install
"$item"
212 [[ $beverbose = yes ]] && (du
-c "$initdir" |
sort -n)
215 if [[ $do_strip = yes ]] ; then
216 for p
in strip objdump
sed grep find; do
217 if ! which $p >/dev
/null
2>&1; then
218 derror
"Could not find '$p'. You should run $0 with '--nostrip'."
224 if [[ $do_strip = yes ]] ; then
225 for f
in $
(find "$initdir" -type f \
( -perm -0100 -or -perm -0010 -or -perm -0001 \
) -exec file {} \
; |
226 grep -v ' shared object,' |
227 sed -n -e 's/^\(.*\):[ ]*ELF.*, not stripped/\1/p'); do
231 # FIXME: only strip -g for now
233 #strip -g --strip-unneeded "$f" || :
235 #if objdump -h $f | grep '^[ ]*[0-9]*[ ]*.note[ ]' -A 1 | \
236 # grep -q ALLOC; then
239 #strip -R .comment $note "$f" || :
243 ( cd "$initdir"; find . |
cpio -R 0:0 -H newc
-o |
gzip -9 > "$outfile"; )
245 [[ $beverbose = yes ]] && ls -lh "$outfile"