depmod: use real regex matching on depmod "overrides" entries
[mit.git] / generate-modprobe.conf
blob7d0d8956ee373c82e55d285d65f82246a9bdfd65
1 #! /bin/sh -e
3 # Naive shell script to translate modules.conf + defaults to modprobe.conf
4 # Normal caveats apply, as with any machine translation.
5 usage() {
6 cat <<-EOF
7 Usage: $0 [--assume-kernel=n.n.n] [--stdin] [modprobe.conf]
8 Converts your current modules setup to modprobe.conf.
9 Don't trust it too much.
10 Version 0.1
11 EOF
12 exit ${1:-1}
15 KV=$(uname -r)
16 STDIN=
17 while [ -n "$1" ] ; do
18 case $1 in
19 --assume-kernel) KV=$2; shift;;
20 --assume-kernel=*) KV=${1#*=};;
21 --stdin) STDIN=1;;
22 -h|--help) usage 0;;
23 *) break;;
24 esac
25 shift
26 done
28 # Set up output if specified.
29 if [ $# -eq 1 ] ; then
30 exec > $1
31 else
32 usage 1
35 MODPROBECONF=`mktemp /tmp/modprobe.XXXXXX || tempfile || echo /tmp/modprobe.$$`
36 trap "rm -f $MODPROBECONF" 0
38 modprobe_abort()
40 echo Failed to to run modprobe. Giving up. 1>&2
41 exit 1
44 if [ x"$TESTING_MODPROBE_CONF" != x ]; then
45 cp $TESTING_MODPROBE_CONF $MODPROBECONF
46 elif [ "$STDIN" = "1" ]; then
47 cat > $MODPROBECONF
48 elif [ -x /sbin/modprobe.old ]; then
49 # In sbin.
50 /sbin/modprobe.old -c > $MODPROBECONF || modprobe_abort
51 elif modprobe.old -c >/dev/null 2>&1; then
52 # Somewhere in path.
53 modprobe.old -c > $MODPROBECONF || modprobe_abort
54 elif /sbin/modprobe -V 2>/dev/null | grep -q 'modprobe version'; then
55 # Running /sbin/modprobe gives old version.
56 /sbin/modprobe -c > $MODPROBECONF || modprobe_abort
57 elif modprobe -V 2>/dev/null | grep -q 'modprobe version'; then
58 # Running modprobe gives old version.
59 modprobe -c > $MODPROBECONF || modprobe_abort
60 else
61 echo Cannot find old version of modprobe. Giving up. 2>&1
62 exit 1
65 # Resolve (possibly recursive) alias: args filename alias
66 resolve_alias()
68 RA_RESOLVE=`grep "^alias[ ][ ]*$2[ ]" -- $1 | awk '{ print $3 }'`
69 if [ x"$RA_RESOLVE" = x ]; then
70 echo $2
71 return
73 # Recurse.
74 (resolve_alias $1 "$RA_RESOLVE")
77 # Parse alias: args filename modulename aliasto.
78 parse_alias()
80 PA_ALIAS=`resolve_alias $1 $3`
81 NAME=`echo $2|sed -e 's/\(block\|char\)-major-\([0-9]\+\)$/\1-major-\2-*/'`
83 echo "alias $NAME $PA_ALIAS"
86 # Parse options: args modulename aliasto.
87 parse_options()
89 # Second arg can be null if grab_noninstall options returns empty.
90 [ x"$2" = x ] || echo "options $1 $2"
93 # Separate out the modprobe options from the module options
94 grab_install_options()
96 GRAB_OPTS=""
97 GRAB_NEXT=0
98 for grab_i; do
99 case "$grab_i" in
100 -o) GRAB_OPTS="$GRAB_OPTS -o"; GRAB_NEXT=1;;
101 -*) echo Warning: Ignoring unknown alias option "$grab_i" >&2;;
103 [ $GRAB_NEXT = 0 ] || GRAB_OPTS="$GRAB_OPTS $grab_i"
104 GRAB_NEXT=0
106 esac
107 done
108 echo "$GRAB_OPTS"
111 grab_noninstall_options()
113 GRAB_OPTS=""
114 GRAB_NEXT=0
115 for grab_i; do
116 case "$grab_i" in
117 -o) GRAB_NEXT=1;;
118 -*) ;;
120 [ $GRAB_NEXT = 1 ] || GRAB_OPTS="$GRAB_OPTS $grab_i"
121 GRAB_NEXT=0
123 esac
124 done
125 echo "$GRAB_OPTS"
128 # Produce a multiple install/remove commands. Args: list, command, separator.
129 multi()
131 mp_res=""
132 for mp_arg in $1; do
133 if [ x"$mp_res" = x ]; then mp_res="/sbin/$2 $mp_arg";
134 else mp_res="$mp_res${3}/sbin/$2 $mp_arg";
136 done
137 echo "$mp_res"
140 # Install and remove commands frequently refer to insmod and rmmod.
141 # Use "modprobe --ignore-install " and "modprobe -r --ignore-remove" since
142 # our versions of rmmod and insmod are deliberately stupid.
143 convert_command()
145 echo "$@" | sed -e 's,insmod,/sbin/modprobe --ignore-install,' -e 's,rmmod,/sbin/modprobe -r --ignore-remove,'
148 while read ADD NAME MODULE REST; do
149 # Ignore a spurious add, otherwise move them down.
150 if [ x"$ADD" = x"add" ]; then :; else
151 REST="`echo $MODULE $REST | sed 's/ *$//'`"
152 MODULE="$NAME"
153 NAME="$ADD";
155 #echo "NAME=$NAME MODULE=$MODULE REST=$REST" >&2
157 # MODNAME is the converted name for use in variables.
158 MODNAME=`echo $MODULE | tr -c A-Za-z_0-9 _`
159 case "$NAME" in
160 '#'*) : ;; # Comment
161 '') : ;; # Blank line
163 keep) # Do nothing.
166 alias)
167 if [ x"$REST" = xoff ] || [ x"$REST" = xnull ]; then
168 INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
169 eval install_$MODNAME=\"/bin/true\"
170 else
171 parse_alias $MODPROBECONF "$MODULE" $REST
175 options)
176 # Options used to be able to contain extra args to insmod
177 # (eg. -o foo). Not any more.
178 case " $REST" in
179 *' '-[-a-z]*)
180 parse_options $MODULE `grab_noninstall_options $REST`
181 INSTALL_OPTIONS=`grab_install_options $REST`
182 INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
183 eval install_$MODNAME=\"/sbin/modprobe $INSTALL_OPTIONS --ignore-install `resolve_alias $MODPROBECONF $MODULE`\"
186 parse_options $MODULE "$REST"
188 esac
191 above)
192 # Pull in these modules after me.
193 INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
194 eval post_install_$MODNAME=\"`multi "$REST" 'modprobe' '; '`\; /bin/true\"
195 REMOVE_COMMANDS="$REMOVE_COMMANDS $MODULE"
196 eval pre_remove_$MODNAME=\"`multi "$REST" 'modprobe -r' '; '`\"
198 below)
199 # Pull in these modules before me.
200 INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
201 eval pre_install_$MODNAME=\"`multi "$REST" 'modprobe' '; '`\"
202 REMOVE_COMMANDS="$REMOVE_COMMANDS $MODULE"
203 eval post_remove_$MODNAME=\"`multi "$REST" 'modprobe -r' '; '`\; /bin/true\"
205 probe)
206 INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
207 eval install_$MODNAME=\"`multi "$REST" 'modprobe' ' || '`\"
209 probeall)
210 # Not quite correct. If they all fail, we should fail.
211 INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
212 eval install_$MODNAME=\"`multi "$REST" 'modprobe' '; '`\; /bin/true\"
214 post-install)
215 INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
216 eval post_install_$MODNAME=\"`convert_command "$REST"`\"
218 pre-install)
219 INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
220 eval pre_install_$MODNAME=\"`convert_command "$REST"`\"
222 install)
223 INSTALL_COMMANDS="$INSTALL_COMMANDS $MODULE"
224 eval install_$MODNAME=\"`convert_command "$REST"`\"
226 post-remove)
227 REMOVE_COMMANDS="$REMOVE_COMMANDS $MODULE"
228 eval post_remove_$MODNAME=\"`convert_command "$REST"`\"
230 pre-remove)
231 REMOVE_COMMANDS="$REMOVE_COMMANDS $MODULE"
232 eval pre_remove_$MODNAME=\"`convert_command "$REST"`\"
234 remove)
235 REMOVE_COMMANDS="$REMOVE_COMMANDS $MODULE"
236 eval remove_$MODNAME=\"`convert_command "$REST"`\"
238 include)
239 # Recurse.
240 ($0 $MODULE)
242 # Ignore default lines which are not required any more.
243 "path[boot]=/lib/modules/boot"|"path[toplevel]=/lib/modules/$KV"|"path[toplevel]=/lib/modules/2.4"|"path[kernel]=/lib/modules/kernel"|"path[fs]=/lib/modules/fs"|"path[net]=/lib/modules/net"|"path[scsi]=/lib/modules/scsi"|"path[block]=/lib/modules/block"|"path[cdrom]=/lib/modules/cdrom"|"path[ipv4]=/lib/modules/ipv4"|"path[ipv6]=/lib/modules/ipv6"|"path[sound]=/lib/modules/sound"|"path[fc4]=/lib/modules/fc4"|"path[video]=/lib/modules/video"|"path[misc]=/lib/modules/misc"|"path[pcmcia]=/lib/modules/pcmcia"|"path[atm]=/lib/modules/atm"|"path[usb]=/lib/modules/usb"|"path[ide]=/lib/modules/ide"|"path[ieee1394]=/lib/modules/ieee1394"|"path[mtd]=/lib/modules/mtd"|"generic_stringfile=/lib/modules/$KV/modules.generic_string"|"pcimapfile=/lib/modules/$KV/modules.pcimap"|"isapnpmapfile=/lib/modules/$KV/modules.isapnpmap"|"usbmapfile=/lib/modules/$KV/modules.usbmap"|"parportmapfile=/lib/modules/$KV/modules.parportmap"|"ieee1394mapfile=/lib/modules/$KV/modules.ieee1394map"|"pnpbiosmapfile=/lib/modules/$KV/modules.pnpbiosmap"|"depfile=/lib/modules/$KV/modules.dep"|"persistdir=/var/lib/modules/persist")
246 # Ignore prune lines unless they end in .o or .ko, which would
247 # have an effect.
248 prune)
249 case "$MODULE" in
250 *.ko|*.o)
251 echo "Warning: not translating $NAME $MODULE $REST" >&2
253 esac
256 echo "Warning: not translating $NAME $MODULE $REST" >&2
258 esac
259 done < $MODPROBECONF
261 for mod in `echo $INSTALL_COMMANDS | tr ' ' '\n' | sort -u`; do
262 # MODNAME is the converted name for use in variables.
263 MODNAME=`echo $mod | tr -c A-Za-z_0-9 _`
264 eval PREINSTALL=\"\$pre_install_$MODNAME\"
265 eval POSTINSTALL=\"\$post_install_$MODNAME\"
266 eval INSTALL=\"\$\{install_$MODNAME:-/sbin/modprobe --first-time --ignore-install $mod}\"
267 if [ -n "$PREINSTALL" ]; then PREINSTALL="{ $PREINSTALL; } ; "; fi
268 if [ -n "$POSTINSTALL" ]; then POSTINSTALL="&& { $POSTINSTALL; }"; fi
269 echo install $mod $PREINSTALL $INSTALL $POSTINSTALL
270 done
272 for mod in `echo $REMOVE_COMMANDS | tr ' ' '\n' | sort -u`; do
273 # MODNAME is the converted name for use in variables.
274 MODNAME=`echo $mod | tr -c A-Za-z_0-9 _`
275 eval PREREMOVE=\"\$pre_remove_$MODNAME\"
276 eval POSTREMOVE=\"\$post_remove_$MODNAME\"
277 eval REMOVE=\"\$\{remove_$MODNAME:-/sbin/modprobe -r --first-time --ignore-remove $mod}\"
278 if [ -n "$PREREMOVE" ]; then PREREMOVE="{ $PREREMOVE; } ; "; fi
279 if [ -n "$POSTREMOVE" ]; then POSTREMOVE="&& { $POSTREMOVE; }"; fi
280 echo remove $mod $PREREMOVE $REMOVE $POSTREMOVE
281 done