]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut
dracut: add --kmoddir directory, where to look for kernel modules
[thirdparty/dracut.git] / dracut
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
6 #
7
8 # Copyright 2008, Red Hat, Inc. Jeremy Katz <katzj@redhat.com>
9 # GPLv2 header here
10
11
12 usage() {
13 # 80x25 linebreak here ^
14 echo "Usage: $0 [OPTION]... <initramfs> <kernel-version>
15 Creates initial ramdisk images for preloading modules
16
17 -f, --force Overwrite existing initramfs file.
18 -m, --modules [LIST] Specify a space-separated list of dracut modules to
19 call when building the initramfs. Modules are located
20 in /usr/share/dracut/modules.d.
21 -o, --omit [LIST] Omit a space-separated list of dracut modules.
22 -a, --add [LIST] Add a space-separated list of dracut modules.
23 -d, --drivers [LIST] Specify a space-separated list of kernel modules to
24 include in the initramfs.
25 -k, --kmoddir [DIR] Specify the directory, where to look for kernel modules
26 -h, --help This message
27 --debug Output debug information of the build process
28 -v, --verbose Verbose output during the build process
29 -c, --conf [FILE] Specify configuration file to use.
30 Default: /etc/dracut.conf
31 -l, --local Local mode. Use modules from the current working
32 directory instead of the system-wide installed in
33 /usr/share/dracut/modules.d.
34 Useful when running dracut from a git checkout.
35 -H, --hostonly Host-Only mode: Install only what is needed for
36 booting the local host instead of a generic host.
37 -i, --include [SOURCE] [TARGET]
38 Include the files in the SOURCE directory into the
39 Target directory in the final initramfs.
40 -I, --install [LIST] Install the space separated list of files into the
41 initramfs.
42 "
43 }
44
45 while (($# > 0)); do
46 case $1 in
47 -f|--force) force=yes;;
48 -m|--modules) dracutmodules_l="$2"; shift;;
49 -o|--omit) omit_dracutmodules_l="$2"; shift;;
50 -a|--add) add_dracutmodules_l="$2"; shift;;
51 -d|--drivers) drivers_l="$2"; shift;;
52 -k|--kmoddir) drivers_dir_l="$2"; shift;;
53 -h|--help) usage; exit 1 ;;
54 --debug) debug="yes";;
55 -v|--verbose) beverbose="yes";;
56 -c|--conf) conffile="$2"; shift;;
57 -l|--local) allowlocal="yes" ;;
58 -H|--hostonly) hostonly="-h" ;;
59 -i|--include) include_src="$2"; include_target="$3"; shift 2;;
60 -I|--install) install_items="$2"; shift;;
61 *) break ;;
62 esac
63 shift
64 done
65
66 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH
67
68 [[ $debug ]] && {
69 export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): ';
70 set -x
71 }
72
73 # if we were not passed a config file, try the default one
74 [[ ! -f $conffile ]] && conffile="/etc/dracut.conf"
75
76 # source our config file
77 [[ -f $conffile ]] && . "$conffile"
78
79 # these options override the stuff in the config file
80 [[ $dracutmodules_l ]] && dracutmodules=$dracutmodules_l
81 [[ $omit_dracutmodules_l ]] && omit_dracutmodules=$omit_dracutmodules_l
82 [[ $add_dracutmodules_l ]] && add_dracutmodules="$add_dracutmodules $add_dracutmodules_l"
83 [[ $drivers_l ]] && drivers=$drivers_l
84 [[ $drivers_dir_l ]] && drivers_dir=$drivers_l
85 [[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut
86
87 [[ $allowlocal && -f "$(dirname $0)/dracut-functions" ]] && dsrc="$(dirname $0)" || dsrc=$dracutbasedir
88
89 if [[ -f $dsrc/dracut-functions ]]; then
90 . $dsrc/dracut-functions
91 else
92 echo "Cannot find $dsrc/dracut-functions. Are you running from a git checkout?"
93 echo "Try passing -l as an argument to $0"
94 exit 1
95 fi
96
97 dracutfunctions=$dsrc/dracut-functions
98 export dracutfunctions
99
100 # This is kinda legacy -- eventually it should go away.
101 case $dracutmodules in
102 ""|auto) dracutmodules="all" ;;
103 esac
104
105 [[ $2 ]] && kernel=$2 || kernel=$(uname -r)
106 [[ $1 ]] && outfile=$(readlink -f $1) || outfile="/boot/initrd-$kernel.img"
107
108 if [[ -f $outfile && ! $force ]]; then
109 echo "Will not override existing initramfs ($outfile) without --force"
110 exit 1
111 fi
112
113 hookdirs="cmdline pre-udev pre-trigger netroot pre-mount pre-pivot mount emergency"
114
115 readonly initdir=$(mktemp -d -t initramfs.XXXXXX)
116 trap 'rm -rf "$initdir"' 0 # clean up after ourselves no matter how we die.
117
118 # Need to be able to have non-root users read stuff (rpcbind etc)
119 chmod 755 "$initdir"
120
121 export initdir hookdirs dsrc dracutmodules drivers drivers_dir debug beverbose
122
123 # Create some directory structure first
124 for d in bin sbin usr/bin usr/sbin usr/lib etc proc sys sysroot tmp dev/pts var/run; do
125 mkdir -p "$initdir/$d";
126 done
127
128 # check all our modules to see if they should be sourced.
129 # This builds a list of modules that we will install next.
130 check_modules
131
132 #source our modules.
133 for moddir in "$dsrc/modules.d"/[0-9][0-9]*; do
134 mod=${moddir##*/}; mod=${mod#[0-9][0-9]}
135 if strstr "$mods_to_load" " $mod "; then
136 . "$moddir/install"
137 mods_to_load=${mods_to_load// $mod /}
138 fi
139 done
140 unset moddir
141 echo $mods_to_load
142
143 ## final stuff that has to happen
144
145 # generate module dependencies for the initrd
146 if [ -d "$initdir/lib/modules/$kernel" ]; then
147 if ! depmod -a -b "$initdir" $kernel; then
148 echo "\"depmod -a $kernel\" failed."
149 exit 1
150 fi
151 fi
152
153 # make sure that library links are correct and up to date
154 ldconfig -n -r "$initdir" /lib* /usr/lib*
155
156 if [[ $include_src && $include_target ]]; then
157 mkdir -p "$initdir$include_target"
158 cp -a -t "$initdir$include_target" "$include_src"/*
159 fi
160
161 for item in $install_items; do
162 dracut_install "$item"
163 done
164 unset item
165
166 [[ "$beverbose" = "yes" ]] && (du -c "$initdir" | sort -n)
167
168 ( cd "$initdir"; find . |cpio -H newc -o |gzip -9 > "$outfile"; )
169
170 [[ "$beverbose" = "yes" ]] && ls -lh "$outfile"
171
172 exit 0
173