NEWS: update
[dracut.git] / dracut
blob0ae07f2e5748aeb3804957d4a6cd76fcb6354bd6
1 #!/bin/bash
2 #
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/>.
25 usage() {
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 exclusively include in the initramfs.
38 --add-drivers [LIST] Specify a space-separated list of kernel
39 modules to add to the initramfs.
40 --filesystems [LIST] Specify a space-separated list of kernel filesystem
41 modules to exclusively include in the generic
42 initramfs.
43 -k, --kmoddir [DIR] Specify the directory, where to look for kernel
44 modules
45 --fwdir [DIR] Specify additional directories, where to look for
46 firmwares, separated by :
47 --kernel-only Only install kernel drivers and firmware files
48 --no-kernel Do not install kernel drivers and firmware files
49 --strip Strip binaries in the initramfs (default)
50 --nostrip Do not strip binaries in the initramfs
51 --mdadmconf Include local /etc/mdadm.conf
52 --nomdadmconf Do not include local /etc/mdadm.conf
53 --lvmconf Include local /etc/lvm/lvm.conf
54 --nolvmconf Do not include local /etc/lvm/lvm.conf
55 -h, --help This message
56 --debug Output debug information of the build process
57 -v, --verbose Verbose output during the build process
58 -c, --conf [FILE] Specify configuration file to use.
59 Default: /etc/dracut.conf
60 -l, --local Local mode. Use modules from the current working
61 directory instead of the system-wide installed in
62 /usr/share/dracut/modules.d.
63 Useful when running dracut from a git checkout.
64 -H, --hostonly Host-Only mode: Install only what is needed for
65 booting the local host instead of a generic host.
66 -i, --include [SOURCE] [TARGET]
67 Include the files in the SOURCE directory into the
68 Target directory in the final initramfs.
69 -I, --install [LIST] Install the space separated list of files into the
70 initramfs.
74 while (($# > 0)); do
75 case $1 in
76 -f|--force) force=yes;;
77 -m|--modules) dracutmodules_l="$2"; shift;;
78 -o|--omit) omit_dracutmodules_l="$2"; shift;;
79 -a|--add) add_dracutmodules_l="$2"; shift;;
80 -d|--drivers) drivers_l="$2"; shift;;
81 --add-drivers) add_drivers_l="$2"; shift;;
82 --filesystems) filesystems_l="$2"; shift;;
83 -k|--kmoddir) drivers_dir_l="$2"; shift;;
84 --fwdir) fw_dir_l="$2"; shift;;
85 --kernel-only) kernel_only="yes"; no_kernel="no";;
86 --no-kernel) kernel_only="no"; no_kernel="yes";;
87 --strip) do_strip_l="yes";;
88 --nostrip) do_strip_l="no";;
89 --mdadmconf) mdadmconf_l="yes";;
90 --nomdadmconf) mdadmconf_l="no";;
91 --lvmconf) lvmconf_l="yes";;
92 --nolvmconf) lvmconf_l="no";;
93 -h|--help) usage; exit 1 ;;
94 --debug) debug="yes";;
95 -v|--verbose) beverbose="yes";;
96 -c|--conf) conffile="$2"; shift;;
97 --confdir) confdir="$2"; shift;;
98 -l|--local) allowlocal="yes" ;;
99 -H|--hostonly) hostonly_l="yes" ;;
100 -i|--include) include_src="$2"; include_target="$3"; shift 2;;
101 -I|--install) install_items="$2"; shift;;
102 -*) printf "\nUnknown option: %s\n\n" "$1" >&2; usage; exit 1;;
103 *) break ;;
104 esac
105 shift
106 done
108 PATH=/sbin:/bin:/usr/sbin:/usr/bin
109 export PATH
111 [[ $debug ]] && {
112 export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
113 set -x
116 [[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut
118 [[ $allowlocal && -f "$(readlink -f $(dirname $0))/dracut-functions" ]] && dracutbasedir="$(dirname $0)"
120 # if we were not passed a config file, try the default one
121 if [[ ! -f $conffile ]]; then
122 [[ $allowlocal ]] || conffile="/etc/dracut.conf"
123 [[ $allowlocal ]] && conffile="$dracutbasedir/dracut.conf"
126 if [[ ! -d $confdir ]]; then
127 [[ $allowlocal ]] || confdir="/etc/dracut.conf.d"
128 [[ $allowlocal ]] && confdir="$dracutbasedir/dracut.conf.d"
131 # source our config dir
132 if [ "$confdir" ] && [ -d "$confdir" ]; then
133 for f in "$confdir"/*.conf; do
134 [ -e "$f" ] && . "$f"
135 done
138 # source our config file
139 [[ -f $conffile ]] && . "$conffile"
141 # these options override the stuff in the config file
142 [[ $dracutmodules_l ]] && dracutmodules=$dracutmodules_l
143 [[ $omit_dracutmodules_l ]] && omit_dracutmodules=$omit_dracutmodules_l
144 [[ $add_dracutmodules_l ]] && add_dracutmodules="$add_dracutmodules $add_dracutmodules_l"
145 [[ $drivers_l ]] && drivers=$drivers_l
146 [[ $add_drivers_l ]] && add_drivers="$add_drivers $add_drivers_l"
147 [[ $filesystems_l ]] && filesystems=$filesystems_l
148 [[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
149 [[ $fw_dir_l ]] && fw_dir=$fw_dir_l
150 [[ $do_strip_l ]] && do_strip=$do_strip_l
151 [[ $hostonly_l ]] && hostonly=$hostonly_l
152 [[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
153 [[ $lvmconf_l ]] && lvmconf=$lvmconf_l
154 [[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut
155 [[ $fw_dir ]] || fw_dir=/lib/firmware
156 [[ $do_strip ]] || do_strip=yes
157 # eliminate IFS hackery when messing with fw_dir
158 fw_dir=${fw_dir//:/ }
160 [[ $hostonly = yes ]] && hostonly="-h"
161 [[ $hostonly != "-h" ]] && unset hostonly
163 if [[ -f $dracutbasedir/dracut-functions ]]; then
164 . $dracutbasedir/dracut-functions
165 else
166 echo "Cannot find $dracutbasedir/dracut-functions. Are you running from a git checkout?"
167 echo "Try passing -l as an argument to $0"
168 exit 1
171 dracutfunctions=$dracutbasedir/dracut-functions
172 export dracutfunctions
174 # This is kinda legacy -- eventually it should go away.
175 case $dracutmodules in
176 ""|auto) dracutmodules="all" ;;
177 esac
179 [[ $2 ]] && kernel=$2 || kernel=$(uname -r)
180 [[ $1 ]] && outfile=$1 || outfile="/boot/initramfs-$kernel.img"
181 abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
183 srcmods="/lib/modules/$kernel/"
184 [[ $drivers_dir ]] && srcmods="$drivers_dir"
185 export srcmods
187 if [[ -f $outfile && ! $force ]]; then
188 echo "Will not override existing initramfs ($outfile) without --force"
189 exit 1
192 outdir=$(dirname "$outfile")
193 if ! [[ -d "$outdir" ]]; then
194 echo "Can't write $outfile: Directory $outdir does not exist."
195 exit 1
198 if ! [[ -w "$outdir" ]]; then
199 echo "No permission to write $outdir."
200 exit 1
203 if [[ -f "$outfile" ]] && ! [[ -w "$outfile" ]]; then
204 echo "No permission to write $outfile."
205 exit 1
208 hookdirs="cmdline pre-udev pre-trigger netroot pre-mount pre-pivot mount emergency"
210 [[ -n "$TMPDIR" ]] && ! [[ -w "$TMPDIR" ]] && unset TMPDIR
211 readonly initdir=$(mktemp -d -t initramfs.XXXXXX)
213 trap 'ret=$?;rm -rf "$initdir";exit $ret;' EXIT # clean up after ourselves no matter how we die.
214 trap 'exit 1;' SIGINT # clean up after ourselves no matter how we die.
216 # Need to be able to have non-root users read stuff (rpcbind etc)
217 chmod 755 "$initdir"
219 export initdir hookdirs dracutbasedir dracutmodules drivers \
220 fw_dir drivers_dir debug beverbose no_kernel kernel_only \
221 add_drivers mdadmconf lvmconf filesystems
223 if [[ $kernel_only != yes ]]; then
224 # Create some directory structure first
225 for d in bin sbin usr/bin usr/sbin usr/lib etc proc sys sysroot tmp dev/pts var/run; do
226 inst_dir "/$d";
227 done
230 # check all our modules to see if they should be sourced.
231 # This builds a list of modules that we will install next.
232 check_modules
234 # source our modules.
235 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
236 mod=${moddir##*/}; mod=${mod#[0-9][0-9]}
237 if strstr "$mods_to_load" " $mod "; then
238 if [[ $kernel_only = yes ]]; then
239 [[ -x $moddir/installkernel ]] && . "$moddir/installkernel"
240 else
241 . "$moddir/install"
242 if [[ $no_kernel != yes && -x $moddir/installkernel ]]; then
243 . "$moddir/installkernel"
246 mods_to_load=${mods_to_load// $mod /}
248 done
249 unset moddir
251 ## final stuff that has to happen
253 # generate module dependencies for the initrd
254 if [[ -d $initdir/lib/modules/$kernel ]]; then
255 if ! depmod -a -b "$initdir" $kernel; then
256 derror "\"depmod -a $kernel\" failed."
257 exit 1
261 if [[ $include_src && $include_target ]]; then
262 mkdir -p "$initdir$include_target"
263 cp -a -t "$initdir$include_target" "$include_src"/*
266 for item in $install_items; do
267 dracut_install "$item"
268 done
269 unset item
271 # make sure that library links are correct and up to date
272 cp -ar /etc/ld.so.conf* "$initdir"/etc
273 ldconfig -r "$initdir" || [[ $(id -u) != "0" ]] && dinfo "ldconfig might need uid=0 (root) for chroot()"
275 [[ $beverbose = yes ]] && (du -c "$initdir" | sort -n)
277 # strip binaries
278 if [[ $do_strip = yes ]] ; then
279 for p in strip objdump sed grep find; do
280 if ! which $p >/dev/null 2>&1; then
281 derror "Could not find '$p'. You should run $0 with '--nostrip'."
282 do_strip=no
284 done
287 if [[ $do_strip = yes ]] ; then
288 for f in $(find "$initdir" -type f \( -perm -0100 -or -perm -0010 -or -perm -0001 -or -path '/lib/modules/*.ko' \) -exec file {} \; |
289 grep -v ' shared object,' |
290 sed -n -e 's/^\(.*\):[ ]*ELF.*, not stripped/\1/p'); do
291 dinfo "Stripping $f"
292 strip -g "$f" || :
294 # FIXME: only strip -g for now
296 #strip -g --strip-unneeded "$f" || :
297 #note="-R .note"
298 #if objdump -h $f | grep '^[ ]*[0-9]*[ ]*.note[ ]' -A 1 | \
299 # grep -q ALLOC; then
300 # note=
302 #strip -R .comment $note "$f" || :
303 done
306 type pigz &>/dev/null && gzip=pigz || gzip=gzip
307 ( cd "$initdir"; find . |cpio -R 0:0 -H newc -o --quiet |$gzip -9 > "$outfile"; )
308 if [ $? -ne 0 ]; then
309 derror "dracut: creation of $outfile failed"
310 exit 1
313 [[ $beverbose = yes ]] && ls -lh "$outfile"
315 exit 0