Magellan Linux

Diff of /trunk/initscripts/sysvinit/sbin/modules-update

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

trunk/magellan-initscripts/sbin/modules-update revision 276 by niro, Fri Oct 21 15:24:54 2005 UTC trunk/initscripts/sysvinit/sbin/modules-update revision 1356 by niro, Sat Jun 4 21:05:01 2011 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 the modules-update script for Debian GNU/Linux.  # This is all for backwards compatibility.  In the perfect world, we would be
11  # Written by Wichert Akkerman <wakkerma@debian.org>  # running a linux-2.6 kernel and not have any modules.d directory.  Then there
12  # Copyright (C) 1998, 1999 Software in the Public Interest  # 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.
 # Modifications by Daniel Robbins <drobbins@gentoo.org>, Gentoo Foundation  
 # 02 Sep 2001 -- Removed "arch" stuff since I see no reason to have  
 # support for varying CPU architectures on a single system.  
14  #  #
 # Updated by Aron Griffis <agriffis@gentoo.org>  
 # 05 May 2004 -- handle --assume-kernel argument for livecd building  
15    
 if [[ 0 -ne $(id -u) ]] ; then  
  echo "You have to be root to do this."  
  exit 2  
 fi  
16    
17  CFGFILE="/etc/modules.conf"  MROOT="${MROOT%/}/"
18  TMPFILE="${CFGFILE}.$$"  [ "${MROOT}" = "${MROOT#/}" ] && MROOT="${PWD}/${MROOT}"
19  CFGFILE2="/etc/modprobe.conf"  cd "${MROOT}"
20  TMPFILE2="${CFGFILE2}.$$"  
21  TMPFILE2B="${CFGFILE2}B.$$"  argv0=${0##*/}
22  CFGFILE3="/etc/modules.devfs"  . /etc/init.d/functions || {
23  TMPFILE3="${CFGFILE3}.$$"   echo "${argv0}: Could not source /etc/init.d/functions!" 1>&2
24  CFGFILE4="/etc/modprobe.devfs"   exit 1
25  TMPFILE4="${CFGFILE4}.$$"  }
26  MODDIR="/etc/modules.d"  umask 022
27  ARCHDIR="${MODDIR}/arch"  esyslog() { :; }
28  HEADER="### This file is automatically generated by modules-update"  export PATH=/sbin:${PATH}
29  FULLHEADER="${HEADER}  
30  #  #
31  # Please do not edit this file directly. If you want to change or add  # Setup some variables
 # anything please take a look at the files in ${MODDIR} and read  
 # the manpage for modules-update(8).  
32  #  #
 "  
33    
34  source /etc/init.d/functions  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    
38    #
39  # Parse command-line  # Parse command-line
40  FORCE=false  #
41  ASSUME_KV=  
42  while [[ -n $1 ]] ; do  VERBOSE=0
43   case "$1" in  DEBUG=0
44   force)  FORCE="false"
45   FORCE=true ;;  BACKUP="false"
46   --assume-kernel=*)  KV=
47   ASSUME_KV=${1#*=} ;;  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     -V|--version)      exec echo "${argv0}: ${VERSION}";;
55     -h|--help)
56     cat <<-EOF
57     Usage: modules-update [options]
58    
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"   echo "Error: I don't understand $1"
72   exit 1 ;;   exit 1
73     ;;
74   esac   esac
75   shift   shift
76  done  done
77    
78  # Set kernel version, either from --assume-kernel or uname -r  if [ ! -w ./etc ] ; then
79  KV=${ASSUME_KV:-$(uname -r)}   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     _header="$(sed -ne 1p ${x})"
123     if [ "${_header}" != "${HEADER}" ] && [ "${_header}" != "${HEADER_OLD}" ]; then
124     echo "Warning: ${x#.} has not been automatically generated"
125    
126     if ${FORCE} ; then
127     echo "--force specified, (re)generating file anyway"
128     else
129     echo "Use \"modules-update force\" to force (re)generation"
130     exit 1
131     fi
132     fi
133    done
134    
135    
136  # needed for kernel 2.6  #
137  if [[ $(kernel_major_version) = 2.6 ]]  # 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  then
142   KERNEL_2_6=true   GENERATE_OLD="true"
143  else  else
144   KERNEL_2_6=false   GENERATE_OLD="false"
145  fi  fi
146    
 # Check if $CONF is valid  
 [[ ! -r ${CONF} ]] && CONF=  
   
 set -e  
147    
148  # Reset the sorting order since we depend on it  # Reset the sorting order since we depend on it
149  export LC_COLLATE="C"  export LC_COLLATE="C"
150    
151  depdir() {  KV=${KV:-$(uname -r)}
  dep=$(egrep '[[:space:]]*depfile' ${CFGFILE} 2> /dev/null | tail -n 1 | sed -e 's/depfile=//' -e 's,/[^/]*$,,')  
  [[ -z ${dep} ]] && dep="/lib/modules/${KV}"  
  echo "${dep}"  
 }  
152    
153  CFGFILES=${CFGFILE}  
154  if ${KERNEL_2_6} ; then  #
155   CFGFILES="${CFGFILES} ${CFGFILE2}"  #  Desc: backup a config file if need be and replace with new one
156   [[ -d /etc/devfs.d ]] && CFGFILES="${CFGFILES} ${CFGFILE4}"  # Usage: backup <old config file to backup> <new config file to replace with>
157  fi  #    Ex: backup /etc/modules.conf /etc/modules.conf.tempfile
158    #
159  for x in ${CFGFILES} ; do  backup() {
160   if [[ -f ${x} ]] ; then   if ${BACKUP} && [ -e "$1" ] ; then
161   if ! sed -ne 1p "${x}" | egrep -q "^${HEADER}" ; then   mv -f "$1" "$1".old
  # Do not bother if its modutils config file, and we  
  # have a 2.6 kernel without modprobe.old  
  [[ ${x} == "${CFGFILE}" ]] && ${KERNEL_2_6} && \  
  ! type -p modprobe.old > /dev/null && \  
  continue  
   
  echo "Error: the current ${x} is not automatically generated."  
   
  if $FORCE ; then  
  echo "force specified, (re)generating file anyway."  
  else  
  echo "Use \"modules-update force\" to force (re)generation."  
  exit 1  
  fi  
  fi  
162   fi   fi
163  done   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     # the manpage for modules-update(8).
180     #
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() {  generate_config() {
191   local cfg=   local config=$1
192   local conf="$1"   local moddir=$2
193   local moddir="$2"   local refdir=$3
194   local tmpfile="$3"   local silent=$4
195   local do_mprobe="$4"   local tmpfile="${config}.$$"
196    
197   for cfg in "${moddir}"/* "${conf}" ; do   [ -z "${silent}" ] && echo "Updating ${config#./etc/}"
  [[ -d ${cfg} ]] && continue  
198    
199   [[ ! -r ${cfg} ]] && continue   create_header ${refdir:-${moddir}} > "${tmpfile}"
200    
201   # Skip backup and RCS files; fixes bug 20597 (07 May 2004 agriffis)   for cfg in "${moddir}"/* ; do
202   [[ ${cfg} == *~ || ${cfg} == *.bak || ${cfg} == *,v ]] && continue   [ -d "${cfg}" ] && continue
203     [ ! -r "${cfg}" ] && continue
204    
205   [[ ${do_mprobe} -eq 1 && -e "/etc/modprobe.d/${cfg##*/}" ]] && continue   # Skip backup and RCS files #20597
206     case ${cfg} in *~|*.bak|*,v) continue;; esac
207    
208   echo "### modules-update: start processing ${cfg}" >> "${tmpfile}"   # If config file is found in the reference dir, then skip it
209     [ -n "${refdir}" ] && [ -e "${refdir}/${cfg##*/}" ] && continue
210    
211   if [[ -x ${cfg} ]] ; then   (
212     echo "### modules-update: start processing ${cfg#.}"
213     if [ -x "${cfg}" ] ; then
214   # $cfg can be executable; nice touch, Wichert! :)   # $cfg can be executable; nice touch, Wichert! :)
215   "${cfg}" >> "${tmpfile}"   "${cfg}"
216   else   else
217   cat "${cfg}" >> "${tmpfile}"   cat "${cfg}"
218   fi   fi
219     echo
220   echo >> "${tmpfile}"   echo "### modules-update: end processing ${cfg#.}"
221   echo "### modules-update: end processing ${cfg}" >> "${tmpfile}"   echo
222   echo >> "${tmpfile}"   ) >> "${tmpfile}"
223   done   done
224    
225     backup "${config}" "${tmpfile}"
226    
227   return 0   return 0
228  }  }
229    
230  if type -p modprobe.old > /dev/null ; then  
231   if ${FORCE} || is_older_than ${CFGFILE} ${MODDIR} || \  #
232     [[ ! -e ${CFGFILE} ]] ; then  # Generate the old modules.conf file based upon all the snippets in
233   echo "Updating ${CFGFILE}"  # modules.d.  Since modprobe doesnt handle modules.d, we need to gather
234   echo "${FULLHEADER}" > "${TMPFILE}"  # the files together in modules.conf for it.
235   generate_config "${CONF}" "${MODDIR}" "${TMPFILE}" 0  #
236   [[ -e ${CFGFILE} ]] && mv -f "${CFGFILE}" "${CFGFILE}.old"  
237   mv -f "${TMPFILE}" "${CFGFILE}"  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   fi
283  fi  fi
284    
285  if ${FORCE} || is_older_than ${CFGFILE2} ${MODDIR} || [[ ! -e ${CFGFILE2} ]]; then  
286   if [[ -x /sbin/generate-modprobe.conf ]] && ${KERNEL_2_6} ; then  #
287   # Make sure that generate-modprobe.conf can handle --assume-kernel  # Generate the new modprobe.conf file if possible.  What this entails is
288   # if we were called with it.  # grabbing details from the old modprobe via the -c option and sticking
289   if [[ -n ${ASSUME_KV} ]] && \  # it in the newer config file.  This is useful for backwards compat support
290   ! grep -qe --assume-kernel /sbin/generate-modprobe.conf  # and for packages that provide older style /etc/modules.d/ files but not
291   then  # newer style /etc/modprobe.d/ files.
292   echo "Error: modules-update called with --assume-kernel flag, but"  #
293   echo "generate-modprobe.conf doesn't understand it.  You need to"  # First we try to use the script `generate-modprobe.conf` from the
294   echo "install >= module-init-tools-3.0-r5"  # module-init-tools and if that fails us, we try and generate modprobe.conf
295   exit 3  # ourselves from the /etc/modules.d/ files.
296   fi  #
297    if ! type -P generate-modprobe.conf > /dev/null ; then
298   echo "Updating ${CFGFILE2}"   vewarn "Skipping /etc/modprobe.conf generation (generate-modprobe.conf doesn't exist)"
299   echo "${FULLHEADER/modules.d/modprobe.d}" > "${TMPFILE2}"  
300   if /sbin/generate-modprobe.conf ${ASSUME_KV:+--assume-kernel=${KV}} \  elif ! ${FORCE} && ! is_older_than ./etc/modprobe.conf ./etc/modules.d ./etc/modprobe.d ; then
301   >> "${TMPFILE2}" 2>/dev/null   veinfo "modprobe.conf: already up-to-date nutness"
302    
303    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    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   then
334   [[ -e ${CFGFILE2} ]] && mv -f "${CFGFILE2}" "${CFGFILE2}.old"   backup "./etc/modprobe.conf" "${tmpfile}"
335   mv -f "${TMPFILE2}" "${CFGFILE2}"   generated_ok=1
336   else   else
337   #   [[ ${VERBOSE} -gt 0 ]] && cat "${tmpfile}.err"
338   # If we made it here, it means either generate-modprobe.conf   echo "Warning: could not generate /etc/modprobe.conf!"
  # bombed on us, or the user doesn't have modutils installed.  
  # If the latter is true, then we should generate modprobe.conf  
  # ourselves with any old files laying around in /etc/modules.d.  
  #  
  rm -f "${TMPFILE2}"  
  if type -p modprobe.old > /dev/null ; then  
  echo "Warning: could not generate ${CFGFILE2}!"  
  else  
  echo "${FULLHEADER/modules.d/modprobe.d}" > "${TMPFILE2B}"  
  generate_config "${CONF}" "${MODDIR}" "${TMPFILE2}" 1  
  export TESTING_MODPROBE_CONF="${TMPFILE2}"  
  if /sbin/generate-modprobe.conf ${ASSUME_KV:+--assume-kernel=${KV}} \  
  >> "${TMPFILE2B}" 2> /dev/null  
  then  
  [[ -e ${CFGFILE2} ]] && mv -f "${CFGFILE2}" "${CFGFILE2}.old"  
  mv -f "${TMPFILE2B}" "${CFGFILE2}"  
  else  
  echo "Warning: could not generate ${CFGFILE2}!"  
  rm -f "${TMPFILE2B}"  
  fi  
  rm -f "${TMPFILE2}"  
  fi  
  fi  
   
  if [[ -f ${CFGFILE3} ]] ; then  
  echo "Updating ${CFGFILE4}"  
  gawk '$0 !~ /^[[:space:]]*include/ { print $0 }' "${CFGFILE3}" \  
  > "${TMPFILE3}"  
   
  echo "${FULLHEADER/modules.d/modprobe.d}" > "${TMPFILE4}"  
  export TESTING_MODPROBE_CONF="${TMPFILE3}"  
  if /sbin/generate-modprobe.conf ${ASSUME_KV:+--assume-kernel=${KV}} \  
  >> "${TMPFILE4}" 2> /dev/null  
  then  
  [[ -e ${CFGFILE4} ]] && mv -f "${CFGFILE4}" "${CFGFILE4}.old"  
  mv -f "${TMPFILE4}" "${CFGFILE4}"  
   
  echo >> "${CFGFILE4}"  
  echo "include /etc/modprobe.conf" >> "${CFGFILE4}"  
  else  
  echo "Warning: could not generate ${CFGFILE4}!"  
  rm -f "${TMPFILE4}"  
  fi  
  rm -f "${TMPFILE3}"  
339   fi   fi
340   fi   fi
 fi  
341    
342  # We also call depmod here to stop insmod from complaining that modules.conf   #
343  # is more recent then modules.dep   # If the helper script failed, we fall back to doing it by hand
344  if [[ ${CFGFILE2} -nt /lib/modules/${KV}/modules.dep ]] ; then   #
345   if [[ -d $(depdir) && -f /proc/modules ]] ; then   if [[ ${generated_ok} -eq 0 ]] ; then
346   if [[ -f /usr/src/linux/System.map ]] ; then   echo "Updating modprobe.conf by hand"
347   depmod -a -F /usr/src/linux/System.map ${KV}  
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   else
360   depmod -a ${KV}   [[ ${VERBOSE} -gt 0 ]] && cat "${tmpfile}.err"
361     echo "Warning: could not generate /etc/modprobe.conf!"
362   fi   fi
363   fi   fi
 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     echo "### modules-update: start processing ${cfg#.}"
381     cat "${cfg}"
382     echo "### modules-update: end processing ${cfg#.}"
383     ) >> "./etc/modprobe.conf"
384     done
385     fi
386    
387  # vim:ts=4   rm -f "${tmpfile}" "${tmpfile}.err"
388    fi
389    
390    : # make sure we fall through with 0 exit status

Legend:
Removed from v.276  
changed lines
  Added in v.1356