Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

branches/unlabeled-1.1.1/magellan-initscripts/sbin/modules-update revision 2 by niro, Mon Dec 13 22:52:07 2004 UTC tags/init-0_5_2/sbin/modules-update revision 851 by niro, Mon May 4 19:58:56 2009 UTC
# Line 1  Line 1 
1  #!/bin/bash  #!/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  #  #
 # This is the modules-update script for Debian GNU/Linux.  
 # Written by Wichert Akkerman <wakkerma@debian.org>  
 # Copyright (C) 1998, 1999 Software in the Public Interest  
   
 # Modifications by Daniel Robbins <drobbins@gentoo.org>, Gentoo Technologies, Inc.  
 # 02 Sep 2001 -- Removed "arch" stuff since I see no reason to have support for varying  
 # CPU architectures on a single system.  
   
 # More Modifications by Niels Rogalla <niro@magellan-linux.net> for Megallan Linux  
 # Hacked some entries to fit into Magellan:  
 # functions.sh & kernel-2.5 stuff not needed  
   
 # 06.09.2004 Added kernel 2.6 stuff <niro@magellan-linux.net>  
   
 CFGFILE="/etc/modules.conf"  
 TMPFILE="${CFGFILE}.$$"  
 CFGFILE2="/etc/modprobe.conf"  
 TMPFILE2="${CFGFILE2}.$$"  
 CFGFILE3="/etc/modules.devfs"  
 TMPFILE3="${CFGFILE3}.$$"  
 CFGFILE4="/etc/modprobe.devfs"  
 TMPFILE4="${CFGFILE4}.$$"  
 MODDIR="/etc/modules.d"  
 ARCHDIR="${MODDIR}/arch"  
 HEADER="### This file is automatically generated by modules-update"  
   
 #source /sbin/functions.sh  
 source /etc/init.d/functions  
   
 KERNEL_2_5="no"  
   
 #if [ "$(get_KV)" -ge "$(KV_to_int '2.5.48')" ]  
 #then  
 # KERNEL_2_5="yes"  
 #fi  
15    
16  #needed for kernel 2.6  
17  if [ "$(kernel_major_version)" == "2.6" ]  MROOT="${MROOT%/}/"
18  then  [ "${MROOT}" = "${MROOT#/}" ] && MROOT="${PWD}/${MROOT}"
19   KERNEL_2_5="yes"  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    HEADER="### This file is automatically generated by update-modules"
35    
36    #
37    # Parse command-line
38    #
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     *)
69     echo "Error: I don't understand $1"
70     exit 1
71     ;;
72     esac
73     shift
74    done
75    
76    if [ ! -w ./etc ] ; then
77     echo "You must be root to do this"
78     exit 2
79  fi  fi
80    
81    [ ${DEBUG} -gt 0 ] && set -x
82    
83  set -e  veinfo() { [ ${VERBOSE} -gt 0 ] && echo "$*" ; return 0 ; }
84    vewarn() { [ ${VERBOSE} -gt 0 ] && echo "$*" ; return 0 ; }
85    
86  # Reset the sorting order since we depend on it  [ "${MROOT}" != "/" ] && veinfo "Operating on MROOT = '${MROOT}'"
 export LC_COLLATE="C"  
87    
88  depdir() {  #
89   dep="`egrep '[[:space:]]*depfile' ${CFGFILE} | tail -n 1 | sed -e 's/depfile=//' -e 's,/[^/]*$,,'`"  # Let's check the optimal case first: nothing to do
90   if [ -z "${dep}" ]  #
91   then  if ! ${FORCE} ; then
92      dep="/lib/modules/$(uname -r)"   if [ ! -d "./etc/modules.d" ] ; then
93   fi   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   echo "${dep}"   elif [ -e "./etc/modprobe.conf" ] ; then
98  }   vewarn "You should put settings in /etc/modprobe.d/ rather than modprobe.conf"
99    
100  CFGFILES="${CFGFILE}"   elif [ -e "./etc/modules.conf" ] ; then
101     vewarn "If you only run linux-2.4, you should delete /etc/modules.conf"
102    
103  if [ "${KERNEL_2_5}" = "yes" ]   else
104  then   veinfo "We have just /etc/modprobe.d; Nothing to do!"
105   CFGFILES="${CFGFILES} ${CFGFILE2} ${CFGFILE4}"   exit 0
106     fi
107     else
108     vewarn "You have /etc/modules.d, so things need to get coalesced"
109     fi
110  fi  fi
111    
112  for x in ${CFGFILES}  #
113  do  # Build list of config files to generate and verify none
114   if [ -f "${x}" ]  # have been modified in any way
115   then  #
116   if ! sed -ne 1p "${x}" | egrep -q "^${HEADER}"  for x in modprobe.conf modules.conf ; do
117   then   x="./etc/${x}"
118   echo "Error: the current ${x} is not automatically generated."   [ -r ${x} ] || continue
119    
120   if [ "$1" != "force" ]   if [ "$(sed -ne 1p ${x})" != "${HEADER}" ] ; then
121   then   echo "Warning: ${x#.} has not been automatically generated"
122   echo "Use \"modules-update force\" to force (re)generation."  
123   exit 1   if ${FORCE} ; then
124   else   echo "--force specified, (re)generating file anyway"
125   echo "force specified, (re)generating file anyway."   else
126   fi   echo "Use \"update-modules force\" to force (re)generation"
127     exit 1
128   fi   fi
129   fi   fi
130  done  done
131    
 if [ 0 -ne "`id -u`" ]  
 then  
  echo "You have to be root to do this."  
  exit 2  
 fi  
132    
133  if [ -e "${CFGFILE}" ]  #
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  then  then
139   cp -f "${CFGFILE}" "${CFGFILE}".old   GENERATE_OLD="true"
140    else
141     GENERATE_OLD="false"
142  fi  fi
143  if [ "${KERNEL_2_5}" = "yes" ]  
144  then  
145   if [ -e "${CFGFILE2}" ]  # Reset the sorting order since we depend on it
146   then  export LC_COLLATE="C"
147   cp -f "${CFGFILE2}" "${CFGFILE2}".old  
148   fi  KV=${KV:-$(uname -r)}
149   if [ -e "${CFGFILE4}" ]  
150   then  
151   cp -f "${CFGFILE4}" "${CFGFILE4}".old  #
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   fi
160  fi   mv -f "$2" "$1"
161    }
162    
163    
164    #
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    
171     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    
181    
 echo "${HEADER}" > "${TMPFILE}"  
 cat <<EOF >> "${TMPFILE}"  
182  #  #
183  # Please do not edit this file directly. If you want to change or add  #  Desc: Combine all config files in a dir and place output in a file
184  # anything please take a look at the files in ${MODDIR} and read  # Usage: generate_config <output config file> <config dir> <reference config dir> <silent>
185  # the manpage for modules-update.  #    Ex: generate_config /etc/modules.conf /etc/modules.d
186  #  #
187  EOF  generate_config() {
188  if [ -x /sbin/generate-modprobe.conf -a "${KERNEL_2_5}" = "yes" ]   local config=$1
189  then   local moddir=$2
190   sed -e "s:the files in ${MODDIR}:${CFGFILE}:" \   local refdir=$3
191   "${TMPFILE}" > "${TMPFILE2}"   local silent=$4
192     local tmpfile="${config}.$$"
193    
194     [ -z "${silent}" ] && echo "Updating ${config#./etc/}"
195    
196     create_header ${refdir:-${moddir}} > "${tmpfile}"
197    
198     for cfg in "${moddir}"/* ; do
199     [ -d "${cfg}" ] && continue
200     [ ! -r "${cfg}" ] && continue
201    
202     # Skip backup and RCS files #20597
203     case ${cfg} in *~|*.bak|*,v) continue;; esac
204    
205     # 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     # $cfg can be executable; nice touch, Wichert! :)
212     "${cfg}"
213     else
214     cat "${cfg}"
215     fi
216     echo
217     echo "### update-modules: end processing ${cfg#.}"
218     echo
219     ) >> "${tmpfile}"
220     done
221    
222   if [ -f "${CFGFILE3}" ]   backup "${config}" "${tmpfile}"
223   then  
224   sed -e "s:the files in ${MODDIR}:${CFGFILE3}:" \   return 0
225   "${TMPFILE}" > "${TMPFILE4}"  }
226   fi  
227    
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  fi
243    
244  for cfg in "${MODDIR}"/* "${CONF}"  #
245  do  # Call depmod to keep insmod from complaining that modules.conf is more
246   [ -d "${cfg}" ] && continue  # recent then the modules.dep file.
247    #
248   [ ! -r "${cfg}" ] && continue  if [ -e "./etc/modules.conf" ] ; then
249     depfile=$(
250   [ -n "`echo "${cfg}" | awk '!/~$|\.bak$/ { print $0 }'`" ] || continue   # the modules.conf file has optional syntax:
251     # depfile=/path/to/modules.dep
252   echo "### modules-update: start processing ${cfg}" >> "${TMPFILE}"   ret=$(sed -n -e '/^[[:space:]]*depfile=/s:.*=::p' ./etc/modules.conf)
253     eval echo "${ret:-/lib/modules/${KV}/modules.dep}"
254   if [ -x ${cfg} ]   )
255   then  
256   # $cfg can be executable; nice touch, Wichert! :)   if [ -d "${depfile%/*}" ] ; then
257   "${cfg}" >> "${TMPFILE}"   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   else
278   cat "${cfg}" >> "${TMPFILE}"   vewarn "The dir '${depfile}' does not exist, skipping call to depmod"
279   fi   fi
280    fi
  echo >> "${TMPFILE}"  
  echo "### modules-update: end processing ${cfg}" >> "${TMPFILE}"  
  echo >> "${TMPFILE}"  
 done  
281    
 mv -f "${TMPFILE}" "${CFGFILE}"  
282    
283  if [ -x /sbin/generate-modprobe.conf -a "${KERNEL_2_5}" = "yes" ]  #
284  then  # Generate the new modprobe.conf file if possible.  What this entails is
285   if /sbin/generate-modprobe.conf >> "${TMPFILE2}" 2> /dev/null  # grabbing details from the old modprobe via the -c option and sticking
286   then  # it in the newer config file.  This is useful for backwards compat support
287   mv -f "${TMPFILE2}" "${CFGFILE2}"  # and for packages that provide older style /etc/modules.d/ files but not
288   else  # newer style /etc/modprobe.d/ files.
289   echo "Warning: could not generate ${CFGFILE2}!"  #
290   rm -f "${TMPFILE2}"  # First we try to use the script `generate-modprobe.conf` from the
291   fi  # 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     fi
313     done
314    
315   if [ -f "${CFGFILE3}" ]   generated_ok=0
316   then   tmpfile="./etc/modprobe.conf.$$"
317   gawk '$0 !~ /^[[:space:]]*include/ { print $0 }' "${CFGFILE3}" > "${TMPFILE3}"  
318     #
319   export TESTING_MODPROBE_CONF="${TMPFILE3}"   # First we try to use regular generate-modprobe.conf
320   if /sbin/generate-modprobe.conf >> "${TMPFILE4}" 2> /dev/null   #
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   then   then
327   mv -f "${TMPFILE4}" "${CFGFILE4}"   backup "./etc/modprobe.conf" "${tmpfile}"
328     generated_ok=1
329     else
330     [[ ${VERBOSE} -gt 0 ]] && cat "${tmpfile}.err"
331     echo "Warning: could not generate /etc/modprobe.conf!"
332     fi
333     fi
334    
335   echo >> "${CFGFILE4}"   #
336   echo "include /etc/modprobe.conf" >> "${CFGFILE4}"   # 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    
341     generate_config ./etc/modprobe.conf ./etc/modules.d ./etc/modprobe.d 0
342     create_header /etc/modprobe.d > "${tmpfile}"
343    
344     # 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   else
353   echo "Warning: could not generate ${CFGFILE4}!"   [[ ${VERBOSE} -gt 0 ]] && cat "${tmpfile}.err"
354   rm -f "${TMPFILE4}"   echo "Warning: could not generate /etc/modprobe.conf!"
355   fi   fi
  rm -f "${TMPFILE3}"  
356   fi   fi
 fi  
357    
358  # We also call depmod here to stop insmod from complaining that modules.conf   #
359  # is more recent then modules.dep   # 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  if [ -d "`depdir`" -a -f /proc/modules ]   # /etc/modprobe.d/* ... http://bugs.gentoo.org/145962
362  then   #
363   depmod -a   if [[ -e ./etc/modprobe.conf ]] ; then
364  fi   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     fi
379    
380     rm -f "${tmpfile}" "${tmpfile}.err"
381    fi
382    
383  # vim:ts=4  : # make sure we fall through with 0 exit status

Legend:
Removed from v.2  
changed lines
  Added in v.851