Magellan Linux

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

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

revision 697 by niro, Fri Oct 21 15:24:54 2005 UTC revision 698 by niro, Tue Mar 25 21:41:22 2008 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 update-modules"
35    
36    #
37  # Parse command-line  # Parse command-line
38  FORCE=false  #
39  ASSUME_KV=  
40  while [[ -n $1 ]] ; do  VERBOSE=0
41   case "$1" in  DEBUG=0
42   force)  FORCE="false"
43   FORCE=true ;;  BACKUP="false"
44   --assume-kernel=*)  KV=
45   ASSUME_KV=${1#*=} ;;  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"   echo "Error: I don't understand $1"
70   exit 1 ;;   exit 1
71     ;;
72   esac   esac
73   shift   shift
74  done  done
75    
76  # Set kernel version, either from --assume-kernel or uname -r  if [ ! -w ./etc ] ; then
77  KV=${ASSUME_KV:-$(uname -r)}   echo "You must be root to do this"
78     exit 2
79    fi
80    
81    [ ${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  # needed for kernel 2.6  #
134  if [[ $(kernel_major_version) = 2.6 ]]  # 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   KERNEL_2_6=true   GENERATE_OLD="true"
140  else  else
141   KERNEL_2_6=false   GENERATE_OLD="false"
142  fi  fi
143    
 # Check if $CONF is valid  
 [[ ! -r ${CONF} ]] && CONF=  
   
 set -e  
144    
145  # Reset the sorting order since we depend on it  # Reset the sorting order since we depend on it
146  export LC_COLLATE="C"  export LC_COLLATE="C"
147    
148  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}"  
 }  
149    
150  CFGFILES=${CFGFILE}  
151  if ${KERNEL_2_6} ; then  #
152   CFGFILES="${CFGFILES} ${CFGFILE2}"  #  Desc: backup a config file if need be and replace with new one
153   [[ -d /etc/devfs.d ]] && CFGFILES="${CFGFILES} ${CFGFILE4}"  # Usage: backup <old config file to backup> <new config file to replace with>
154  fi  #    Ex: backup /etc/modules.conf /etc/modules.conf.tempfile
155    #
156  for x in ${CFGFILES} ; do  backup() {
157   if [[ -f ${x} ]] ; then   if ${BACKUP} && [ -e "$1" ] ; then
158   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  
159   fi   fi
160  done   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    
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  generate_config() {  generate_config() {
188   local cfg=   local config=$1
189   local conf="$1"   local moddir=$2
190   local moddir="$2"   local refdir=$3
191   local tmpfile="$3"   local silent=$4
192   local do_mprobe="$4"   local tmpfile="${config}.$$"
193    
194   for cfg in "${moddir}"/* "${conf}" ; do   [ -z "${silent}" ] && echo "Updating ${config#./etc/}"
  [[ -d ${cfg} ]] && continue  
195    
196   [[ ! -r ${cfg} ]] && continue   create_header ${refdir:-${moddir}} > "${tmpfile}"
197    
198   # Skip backup and RCS files; fixes bug 20597 (07 May 2004 agriffis)   for cfg in "${moddir}"/* ; do
199   [[ ${cfg} == *~ || ${cfg} == *.bak || ${cfg} == *,v ]] && continue   [ -d "${cfg}" ] && continue
200     [ ! -r "${cfg}" ] && continue
201    
202   [[ ${do_mprobe} -eq 1 && -e "/etc/modprobe.d/${cfg##*/}" ]] && continue   # Skip backup and RCS files #20597
203     case ${cfg} in *~|*.bak|*,v) continue;; esac
204    
205   echo "### modules-update: start processing ${cfg}" >> "${tmpfile}"   # If config file is found in the reference dir, then skip it
206     [ -n "${refdir}" ] && [ -e "${refdir}/${cfg##*/}" ] && continue
207    
208   if [[ -x ${cfg} ]] ; then   (
209     echo "### update-modules: start processing ${cfg#.}"
210     if [ -x "${cfg}" ] ; then
211   # $cfg can be executable; nice touch, Wichert! :)   # $cfg can be executable; nice touch, Wichert! :)
212   "${cfg}" >> "${tmpfile}"   "${cfg}"
213   else   else
214   cat "${cfg}" >> "${tmpfile}"   cat "${cfg}"
215   fi   fi
216     echo
217   echo >> "${tmpfile}"   echo "### update-modules: end processing ${cfg#.}"
218   echo "### modules-update: end processing ${cfg}" >> "${tmpfile}"   echo
219   echo >> "${tmpfile}"   ) >> "${tmpfile}"
220   done   done
221    
222     backup "${config}" "${tmpfile}"
223    
224   return 0   return 0
225  }  }
226    
227  if type -p modprobe.old > /dev/null ; then  
228   if ${FORCE} || is_older_than ${CFGFILE} ${MODDIR} || \  #
229     [[ ! -e ${CFGFILE} ]] ; then  # Generate the old modules.conf file based upon all the snippets in
230   echo "Updating ${CFGFILE}"  # modules.d.  Since modprobe doesnt handle modules.d, we need to gather
231   echo "${FULLHEADER}" > "${TMPFILE}"  # the files together in modules.conf for it.
232   generate_config "${CONF}" "${MODDIR}" "${TMPFILE}" 0  #
233   [[ -e ${CFGFILE} ]] && mv -f "${CFGFILE}" "${CFGFILE}.old"  
234   mv -f "${TMPFILE}" "${CFGFILE}"  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   fi   fi
280  fi  fi
281    
282  if ${FORCE} || is_older_than ${CFGFILE2} ${MODDIR} || [[ ! -e ${CFGFILE2} ]]; then  
283   if [[ -x /sbin/generate-modprobe.conf ]] && ${KERNEL_2_6} ; then  #
284   # Make sure that generate-modprobe.conf can handle --assume-kernel  # Generate the new modprobe.conf file if possible.  What this entails is
285   # if we were called with it.  # grabbing details from the old modprobe via the -c option and sticking
286   if [[ -n ${ASSUME_KV} ]] && \  # it in the newer config file.  This is useful for backwards compat support
287   ! grep -qe --assume-kernel /sbin/generate-modprobe.conf  # and for packages that provide older style /etc/modules.d/ files but not
288   then  # newer style /etc/modprobe.d/ files.
289   echo "Error: modules-update called with --assume-kernel flag, but"  #
290   echo "generate-modprobe.conf doesn't understand it.  You need to"  # First we try to use the script `generate-modprobe.conf` from the
291   echo "install >= module-init-tools-3.0-r5"  # module-init-tools and if that fails us, we try and generate modprobe.conf
292   exit 3  # ourselves from the /etc/modules.d/ files.
293   fi  #
294    if ! type -P generate-modprobe.conf > /dev/null ; then
295   echo "Updating ${CFGFILE2}"   vewarn "Skipping /etc/modprobe.conf generation (generate-modprobe.conf doesn't exist)"
296   echo "${FULLHEADER/modules.d/modprobe.d}" > "${TMPFILE2}"  
297   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
298   >> "${TMPFILE2}" 2>/dev/null   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     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   then   then
327   [[ -e ${CFGFILE2} ]] && mv -f "${CFGFILE2}" "${CFGFILE2}.old"   backup "./etc/modprobe.conf" "${tmpfile}"
328   mv -f "${TMPFILE2}" "${CFGFILE2}"   generated_ok=1
329   else   else
330   #   [[ ${VERBOSE} -gt 0 ]] && cat "${tmpfile}.err"
331   # 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}"  
332   fi   fi
333   fi   fi
 fi  
334    
335  # We also call depmod here to stop insmod from complaining that modules.conf   #
336  # is more recent then modules.dep   # If the helper script failed, we fall back to doing it by hand
337  if [[ ${CFGFILE2} -nt /lib/modules/${KV}/modules.dep ]] ; then   #
338   if [[ -d $(depdir) && -f /proc/modules ]] ; then   if [[ ${generated_ok} -eq 0 ]] ; then
339   if [[ -f /usr/src/linux/System.map ]] ; then   echo "Updating modprobe.conf by hand"
340   depmod -a -F /usr/src/linux/System.map ${KV}  
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   depmod -a ${KV}   [[ ${VERBOSE} -gt 0 ]] && cat "${tmpfile}.err"
354     echo "Warning: could not generate /etc/modprobe.conf!"
355   fi   fi
356   fi   fi
 fi  
357    
358     #
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     fi
379    
380  # vim:ts=4   rm -f "${tmpfile}" "${tmpfile}.err"
381    fi
382    
383    : # make sure we fall through with 0 exit status

Legend:
Removed from v.697  
changed lines
  Added in v.698