add module i18n
[dracut.git] / dracut
blob3be498997a2748a8ccc42ba6a32f9b71f1782239
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
50 --nostrip Do not strip binaries in the initramfs (default)
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 optins add to the stuff in the config file
142 [[ $add_dracutmodules_l ]] && add_dracutmodules+=" $add_dracutmodules_l"
143 [[ $add_drivers_l ]] && add_drivers+=" $add_drivers_l"
145 # these options override the stuff in the config file
146 [[ $dracutmodules_l ]] && dracutmodules=$dracutmodules_l
147 [[ $omit_dracutmodules_l ]] && omit_dracutmodules=$omit_dracutmodules_l
148 [[ $drivers_l ]] && drivers=$drivers_l
149 [[ $filesystems_l ]] && filesystems=$filesystems_l
150 [[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
151 [[ $fw_dir_l ]] && fw_dir=$fw_dir_l
152 [[ $do_strip_l ]] && do_strip=$do_strip_l
153 [[ $hostonly_l ]] && hostonly=$hostonly_l
154 [[ $mdadmconf_l ]] && mdadmconf=$mdadmconf_l
155 [[ $lvmconf_l ]] && lvmconf=$lvmconf_l
156 [[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut
157 [[ $fw_dir ]] || fw_dir=/lib/firmware
158 [[ $do_strip ]] || do_strip=no
159 # eliminate IFS hackery when messing with fw_dir
160 fw_dir=${fw_dir//:/ }
162 [[ $hostonly = yes ]] && hostonly="-h"
163 [[ $hostonly != "-h" ]] && unset hostonly
165 if [[ -f $dracutbasedir/dracut-functions ]]; then
166 . $dracutbasedir/dracut-functions
167 else
168 echo "Cannot find $dracutbasedir/dracut-functions. Are you running from a git checkout?"
169 echo "Try passing -l as an argument to $0"
170 exit 1
173 dracutfunctions=$dracutbasedir/dracut-functions
174 export dracutfunctions
176 # This is kinda legacy -- eventually it should go away.
177 case $dracutmodules in
178 ""|auto) dracutmodules="all" ;;
179 esac
181 [[ $2 ]] && kernel=$2 || kernel=$(uname -r)
182 [[ $1 ]] && outfile=$1 || outfile="/boot/initramfs-$kernel.img"
183 abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
185 srcmods="/lib/modules/$kernel/"
186 [[ $drivers_dir ]] && {
187 if vercmp $(modprobe --version | cut -d' ' -f3) lt 3.7; then
188 derror 'To use --kmoddir option module-init-tools >= 3.7 is required.'
189 exit 1
191 srcmods="$drivers_dir"
193 export srcmods
195 if [[ -f $outfile && ! $force ]]; then
196 echo "Will not override existing initramfs ($outfile) without --force"
197 exit 1
200 outdir=$(dirname "$outfile")
201 if ! [[ -d "$outdir" ]]; then
202 echo "Can't write $outfile: Directory $outdir does not exist."
203 exit 1
206 if ! [[ -w "$outdir" ]]; then
207 echo "No permission to write $outdir."
208 exit 1
211 if [[ -f "$outfile" ]] && ! [[ -w "$outfile" ]]; then
212 echo "No permission to write $outfile."
213 exit 1
216 hookdirs="cmdline pre-udev pre-trigger netroot pre-mount pre-pivot mount emergency"
218 [[ -n "$TMPDIR" ]] && ! [[ -w "$TMPDIR" ]] && unset TMPDIR
219 readonly initdir=$(mktemp -d -t initramfs.XXXXXX)
221 trap 'ret=$?;rm -rf "$initdir";exit $ret;' EXIT # clean up after ourselves no matter how we die.
222 trap 'exit 1;' SIGINT # clean up after ourselves no matter how we die.
224 # Need to be able to have non-root users read stuff (rpcbind etc)
225 chmod 755 "$initdir"
227 export initdir hookdirs dracutbasedir dracutmodules drivers \
228 fw_dir drivers_dir debug beverbose no_kernel kernel_only \
229 add_drivers mdadmconf lvmconf filesystems
231 if [[ $kernel_only != yes ]]; then
232 # Create some directory structure first
233 for d in bin sbin usr/bin usr/sbin usr/lib etc proc sys sysroot tmp dev/pts var/run; do
234 inst_dir "/$d";
235 done
238 # check all our modules to see if they should be sourced.
239 # This builds a list of modules that we will install next.
240 check_modules
242 # source our modules.
243 for moddir in "$dracutbasedir/modules.d"/[0-9][0-9]*; do
244 mod=${moddir##*/}; mod=${mod#[0-9][0-9]}
245 if strstr "$mods_to_load" " $mod "; then
246 if [[ $kernel_only = yes ]]; then
247 [[ -x $moddir/installkernel ]] && . "$moddir/installkernel"
248 else
249 . "$moddir/install"
250 if [[ $no_kernel != yes && -x $moddir/installkernel ]]; then
251 . "$moddir/installkernel"
254 mods_to_load=${mods_to_load// $mod /}
256 done
257 unset moddir
259 ## final stuff that has to happen
261 # generate module dependencies for the initrd
262 if [[ -d $initdir/lib/modules/$kernel ]]; then
263 if ! depmod -a -b "$initdir" $kernel; then
264 derror "\"depmod -a $kernel\" failed."
265 exit 1
269 if [[ $include_src && $include_target ]]; then
270 mkdir -p "$initdir$include_target"
271 cp -a -t "$initdir$include_target" "$include_src"/*
274 for item in $install_items; do
275 dracut_install "$item"
276 done
277 unset item
279 # make sure that library links are correct and up to date
280 cp -ar /etc/ld.so.conf* "$initdir"/etc
281 ldconfig -r "$initdir" || [[ $(id -u) != "0" ]] && dinfo "ldconfig might need uid=0 (root) for chroot()"
283 [[ $beverbose = yes ]] && (du -c "$initdir" | sort -n)
285 # strip binaries
286 if [[ $do_strip = yes ]] ; then
287 for p in strip grep find; do
288 if ! which $p >/dev/null 2>&1; then
289 derror "Could not find '$p'. You should run $0 with '--nostrip'."
290 do_strip=no
292 done
295 if [[ $do_strip = yes ]] ; then
296 for f in $(find "$initdir" -type f \( -perm -0100 -or -perm -0010 -or -perm -0001 -or -path '/lib/modules/*.ko' \) ); do
297 dinfo "Stripping $f"
298 strip -g "$f" 2>/dev/null|| :
300 # FIXME: only strip -g for now
302 #strip -g --strip-unneeded "$f" || :
303 #note="-R .note"
304 #if objdump -h $f | grep '^[ ]*[0-9]*[ ]*.note[ ]' -A 1 | \
305 # grep -q ALLOC; then
306 # note=
308 #strip -R .comment $note "$f" || :
309 done
312 type pigz &>/dev/null && gzip=pigz || gzip=gzip
313 ( cd "$initdir"; find . |cpio -R 0:0 -H newc -o --quiet |$gzip -9 > "$outfile"; )
314 if [ $? -ne 0 ]; then
315 derror "dracut: creation of $outfile failed"
316 exit 1
319 [[ $beverbose = yes ]] && ls -lh "$outfile"
321 exit 0