Magellan Linux

Annotation of /tags/init-0_5_2/sbin/modules-update

Parent Directory Parent Directory | Revision Log Revision Log


Revision 698 - (hide annotations) (download)
Tue Mar 25 21:41:22 2008 UTC (16 years, 1 month ago) by niro
Original Path: trunk/magellan-initscripts/sbin/modules-update
File size: 10179 byte(s)
-using the complete rewrite from gentoo-upstream to be fully compat with >=module-init-tools-3.4

1 niro 2 #!/bin/bash
2 niro 698 # vim:ts=4
3     # Distributed under the terms of the GNU General Public License v2
4 niro 2 #
5 niro 698 # This script will do:
6     # - create /etc/modules.conf from /etc/modules.d/*
7     # - create /etc/modprobe.conf from /etc/modprobe.d/*
8     # - update modules.dep if modules.conf has been updated so depmod doesnt whine
9 niro 276 #
10 niro 698 # This is all for backwards compatibility. In the perfect world, we would be
11     # running a linux-2.6 kernel and not have any modules.d directory. Then there
12     # would be no work for us as module-init-tools automatically scans modprobe.d.
13     # Until that happens, we'll keep scanning and warning and being a pita.
14 niro 276 #
15 niro 2
16    
17 niro 698 MROOT="${MROOT%/}/"
18     [ "${MROOT}" = "${MROOT#/}" ] && MROOT="${PWD}/${MROOT}"
19     cd "${MROOT}"
20    
21     argv0=${0##*/}
22     . /etc/init.d/functions || {
23     echo "${argv0}: Could not source /etc/init.d/functions!" 1>&2
24     exit 1
25     }
26     umask 022
27     esyslog() { :; }
28     export PATH=/sbin:${PATH}
29    
30 niro 276 #
31 niro 698 # Setup some variables
32 niro 276 #
33 niro 2
34 niro 698 HEADER="### This file is automatically generated by update-modules"
35 niro 2
36 niro 698 #
37 niro 276 # Parse command-line
38 niro 698 #
39    
40     VERBOSE=0
41     DEBUG=0
42     FORCE="false"
43     BACKUP="false"
44     KV=
45     while [ -n "$1" ] ; do
46     case $1 in
47     --assume-kernel=*) KV=${1#*=};;
48     -b|--backup) BACKUP="true";;
49     -f|--force|force) FORCE="true";;
50     -v|--verbose) ((VERBOSE+=1));;
51     -d|--debug) ((DEBUG+=1));;
52     -V|--version) exec echo "${argv0}$Revision: 1.4 $ $Date: 2008-03-25 21:41:22 $";;
53     -h|--help)
54     cat <<-EOF
55     Usage: update-modules [options]
56    
57     Options:
58     --assume-kernel=KV Assume the kernel is at least version KV
59     -b, --backup Backup existing config files (add .old ext)
60     -f, --force Force execution in face of bad things
61     -v, --verbose Be a bit more verbose in what we do
62     -d, --debug Helpful debug output
63     -V, --version Dump version info
64     -h, --help This help screen, duh
65     EOF
66     exit 0
67     ;;
68 niro 276 *)
69     echo "Error: I don't understand $1"
70 niro 698 exit 1
71     ;;
72 niro 276 esac
73     shift
74     done
75 niro 2
76 niro 698 if [ ! -w ./etc ] ; then
77     echo "You must be root to do this"
78     exit 2
79     fi
80 niro 276
81 niro 698 [ ${DEBUG} -gt 0 ] && set -x
82    
83     veinfo() { [ ${VERBOSE} -gt 0 ] && echo "$*" ; return 0 ; }
84     vewarn() { [ ${VERBOSE} -gt 0 ] && echo "$*" ; return 0 ; }
85    
86     [ "${MROOT}" != "/" ] && veinfo "Operating on MROOT = '${MROOT}'"
87    
88     #
89     # Let's check the optimal case first: nothing to do
90     #
91     if ! ${FORCE} ; then
92     if [ ! -d "./etc/modules.d" ] ; then
93     if [ ! -d "./etc/modprobe.d" ] ; then
94     veinfo "No /etc/modules.d or /etc/modprobe.d dir; Nothing to do!"
95     exit 0
96    
97     elif [ -e "./etc/modprobe.conf" ] ; then
98     vewarn "You should put settings in /etc/modprobe.d/ rather than modprobe.conf"
99    
100     elif [ -e "./etc/modules.conf" ] ; then
101     vewarn "If you only run linux-2.4, you should delete /etc/modules.conf"
102    
103     else
104     veinfo "We have just /etc/modprobe.d; Nothing to do!"
105     exit 0
106     fi
107     else
108     vewarn "You have /etc/modules.d, so things need to get coalesced"
109     fi
110     fi
111    
112     #
113     # Build list of config files to generate and verify none
114     # have been modified in any way
115     #
116     for x in modprobe.conf modules.conf ; do
117     x="./etc/${x}"
118     [ -r ${x} ] || continue
119    
120     if [ "$(sed -ne 1p ${x})" != "${HEADER}" ] ; then
121     echo "Warning: ${x#.} has not been automatically generated"
122    
123     if ${FORCE} ; then
124     echo "--force specified, (re)generating file anyway"
125     else
126     echo "Use \"update-modules force\" to force (re)generation"
127     exit 1
128     fi
129     fi
130     done
131    
132    
133     #
134     # If the system doesnt have old modutils, then this is prob linux-2.6 only
135     #
136     if type -P modprobe.old > /dev/null || \
137     LC_ALL=C modprobe -V 2>/dev/null | grep -qs "modprobe version"
138 niro 2 then
139 niro 698 GENERATE_OLD="true"
140 niro 276 else
141 niro 698 GENERATE_OLD="false"
142 niro 2 fi
143    
144    
145     # Reset the sorting order since we depend on it
146     export LC_COLLATE="C"
147    
148 niro 698 KV=${KV:-$(uname -r)}
149    
150    
151     #
152     # Desc: backup a config file if need be and replace with new one
153     # Usage: backup <old config file to backup> <new config file to replace with>
154     # Ex: backup /etc/modules.conf /etc/modules.conf.tempfile
155     #
156     backup() {
157     if ${BACKUP} && [ -e "$1" ] ; then
158     mv -f "$1" "$1".old
159     fi
160     mv -f "$2" "$1"
161 niro 2 }
162    
163    
164 niro 698 #
165     # Desc: Create module header
166     # Usage: create_header <config dir>
167     # Ex: create_header /etc/modules.d
168     create_header() {
169     local moddir=$1
170 niro 105
171 niro 698 cat <<-EOF
172     ${HEADER}
173     #
174     # Please do not edit this file directly. If you want to change or add
175     # anything please take a look at the files in ${moddir} and read
176     # the manpage for update-modules(8).
177     #
178     EOF
179     }
180 niro 2
181 niro 698
182     #
183     # Desc: Combine all config files in a dir and place output in a file
184     # Usage: generate_config <output config file> <config dir> <reference config dir> <silent>
185     # Ex: generate_config /etc/modules.conf /etc/modules.d
186     #
187 niro 276 generate_config() {
188 niro 698 local config=$1
189     local moddir=$2
190     local refdir=$3
191     local silent=$4
192     local tmpfile="${config}.$$"
193 niro 2
194 niro 698 [ -z "${silent}" ] && echo "Updating ${config#./etc/}"
195 niro 2
196 niro 698 create_header ${refdir:-${moddir}} > "${tmpfile}"
197 niro 2
198 niro 698 for cfg in "${moddir}"/* ; do
199     [ -d "${cfg}" ] && continue
200     [ ! -r "${cfg}" ] && continue
201 niro 2
202 niro 698 # Skip backup and RCS files #20597
203     case ${cfg} in *~|*.bak|*,v) continue;; esac
204 niro 2
205 niro 698 # If config file is found in the reference dir, then skip it
206     [ -n "${refdir}" ] && [ -e "${refdir}/${cfg##*/}" ] && continue
207    
208     (
209     echo "### update-modules: start processing ${cfg#.}"
210     if [ -x "${cfg}" ] ; then
211 niro 276 # $cfg can be executable; nice touch, Wichert! :)
212 niro 698 "${cfg}"
213 niro 276 else
214 niro 698 cat "${cfg}"
215 niro 276 fi
216 niro 698 echo
217     echo "### update-modules: end processing ${cfg#.}"
218     echo
219     ) >> "${tmpfile}"
220 niro 276 done
221 niro 2
222 niro 698 backup "${config}" "${tmpfile}"
223    
224 niro 276 return 0
225     }
226 niro 105
227 niro 698
228     #
229     # Generate the old modules.conf file based upon all the snippets in
230     # modules.d. Since modprobe doesnt handle modules.d, we need to gather
231     # the files together in modules.conf for it.
232     #
233    
234     if [ ! -d "./etc/modules.d" ] ; then
235     veinfo "No need to generate modules.conf :)"
236    
237     elif ${FORCE} || is_older_than ./etc/modules.conf ./etc/modules.d ; then
238     generate_config ./etc/modules.conf ./etc/modules.d
239    
240     else
241     veinfo "modules.conf: already up-to-date wheatness"
242     fi
243    
244     #
245     # Call depmod to keep insmod from complaining that modules.conf is more
246     # recent then the modules.dep file.
247     #
248     if [ -e "./etc/modules.conf" ] ; then
249     depfile=$(
250     # the modules.conf file has optional syntax:
251     # depfile=/path/to/modules.dep
252     ret=$(sed -n -e '/^[[:space:]]*depfile=/s:.*=::p' ./etc/modules.conf)
253     eval echo "${ret:-/lib/modules/${KV}/modules.dep}"
254     )
255    
256     if [ -d "${depfile%/*}" ] ; then
257     if [ ./etc/modules.conf -nt "${depfile}" ] ; then
258     arch=$(uname -m)
259     echo "Updating modules.dep"
260     for cfg in /lib/modules/${KV}/build /usr/src/linux-${KV} \
261     /lib/modules/${KV} /boot /usr/src/linux ""
262     do
263     cfg=".${cfg}/System.map"
264     for suffix in -genkernel-${arch}-${KV} -genkernel-'*'-${KV} -${KV} "" ; do
265     scfg=$(echo ${cfg}${suffix})
266     scfg=${scfg%% *}
267     [ -f "${scfg}" ] && cfg=${scfg} && break 2
268     done
269     cfg=""
270     done
271     [ -n "${cfg}" ] && cfg="-F ${cfg}"
272     depmod -b "${ROOT}" -a ${cfg} ${KV}
273     veinfo "Ran: depmod -b '${ROOT}' -a ${cfg} ${KV}"
274     else
275     veinfo "modules.dep: already up-to-date goodness"
276     fi
277     else
278     vewarn "The dir '${depfile}' does not exist, skipping call to depmod"
279 niro 2 fi
280 niro 276 fi
281 niro 105
282 niro 698
283     #
284     # Generate the new modprobe.conf file if possible. What this entails is
285     # grabbing details from the old modprobe via the -c option and sticking
286     # it in the newer config file. This is useful for backwards compat support
287     # and for packages that provide older style /etc/modules.d/ files but not
288     # newer style /etc/modprobe.d/ files.
289     #
290     # First we try to use the script `generate-modprobe.conf` from the
291     # module-init-tools and if that fails us, we try and generate modprobe.conf
292     # ourselves from the /etc/modules.d/ files.
293     #
294     if ! type -P generate-modprobe.conf > /dev/null ; then
295     vewarn "Skipping /etc/modprobe.conf generation (generate-modprobe.conf doesn't exist)"
296    
297     elif ! ${FORCE} && ! is_older_than ./etc/modprobe.conf ./etc/modules.d ./etc/modprobe.d ; then
298     veinfo "modprobe.conf: already up-to-date nutness"
299    
300     else
301     #
302     # First, bitch like crazy
303     #
304     for f in ./etc/modules.d/* ; do
305     # hack: ignore baselayout ;x
306     case ${f##*/} in
307     aliases|i386) continue;;
308     esac
309     [ -e "${f}" ] || continue
310     if [ ! -e "./etc/modprobe.d/${f##*/}" ] ; then
311     echo "Please file a bug about ${f#.}: it needs an /etc/modprobe.d/${f##*/}"
312 niro 276 fi
313 niro 698 done
314    
315     generated_ok=0
316     tmpfile="./etc/modprobe.conf.$$"
317    
318     #
319     # First we try to use regular generate-modprobe.conf
320     #
321     if ${GENERATE_OLD} ; then
322     echo "Updating modprobe.conf"
323     create_header /etc/modprobe.d > "${tmpfile}"
324     if generate-modprobe.conf ${ASSUME_KV:+--assume-kernel=${KV}} \
325     >> "${tmpfile}" 2> "${tmpfile}.err"
326 niro 2 then
327 niro 698 backup "./etc/modprobe.conf" "${tmpfile}"
328     generated_ok=1
329 niro 2 else
330 niro 698 [[ ${VERBOSE} -gt 0 ]] && cat "${tmpfile}.err"
331     echo "Warning: could not generate /etc/modprobe.conf!"
332 niro 2 fi
333 niro 698 fi
334 niro 276
335 niro 698 #
336     # If the helper script failed, we fall back to doing it by hand
337     #
338     if [[ ${generated_ok} -eq 0 ]] ; then
339     echo "Updating modprobe.conf by hand"
340 niro 276
341 niro 698 generate_config ./etc/modprobe.conf ./etc/modules.d ./etc/modprobe.d 0
342     create_header /etc/modprobe.d > "${tmpfile}"
343 niro 276
344 niro 698 # Just use generate-modprobe.conf to filter compatible syntax
345     if TESTING_MODPROBE_CONF=./etc/modprobe.conf \
346     generate-modprobe.conf ${ASSUME_KV:+--assume-kernel=${KV}} \
347     >> "${tmpfile}" 2> "${tmpfile}.err"
348     then
349     # we use mv here instead of backup_config() as the call to
350     # generate_config() above already took care of the backup
351     mv -f "${tmpfile}" "./etc/modprobe.conf"
352     else
353     [[ ${VERBOSE} -gt 0 ]] && cat "${tmpfile}.err"
354     echo "Warning: could not generate /etc/modprobe.conf!"
355 niro 276 fi
356 niro 2 fi
357    
358 niro 698 #
359     # Now append all the new files ... modprobe will not scan /etc/modprobe.d/
360     # if /etc/modprobe.conf exists, so we need to append /etc/modprobe.conf with
361     # /etc/modprobe.d/* ... http://bugs.gentoo.org/145962
362     #
363     if [[ -e ./etc/modprobe.conf ]] ; then
364     for cfg in ./etc/modprobe.d/* ; do
365     [ -d "${cfg}" ] && continue
366     [ ! -r "${cfg}" ] && continue
367    
368     # Skip backup and RCS files #20597
369     case ${cfg} in *~|*.bak|*,v) continue;; esac
370    
371     (
372     echo
373     echo "### update-modules: start processing ${cfg#.}"
374     cat "${cfg}"
375     echo "### update-modules: end processing ${cfg#.}"
376     ) >> "./etc/modprobe.conf"
377     done
378 niro 276 fi
379 niro 698
380     rm -f "${tmpfile}" "${tmpfile}.err"
381 niro 2 fi
382    
383 niro 698 : # make sure we fall through with 0 exit status

Properties

Name Value
svn:executable *