Magellan Linux

Annotation of /tags/busybox-init-0_1_15/sbin/modules-update

Parent Directory Parent Directory | Revision Log Revision Log


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