summaryrefslogtreecommitdiff
blob: 77c25be4e50f56b3310289b94ca450c1afa643cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/bin/bash
#
# 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 Foundation
# 02 Sep 2001 -- Removed "arch" stuff since I see no reason to have
# support for varying CPU architectures on a single system.
#
# Updated by Aron Griffis <agriffis@gentoo.org>
# 05 May 2004 -- handle --assume-kernel argument for livecd building

if [[ 0 -ne $(id -u) ]] ; then
	echo "You have to be root to do this."
	exit 2
fi

CFGFILE="/etc/modules.conf"
TMPFILE="${CFGFILE}.$$"
CFGFILE2="/etc/modprobe.conf"
TMPFILE2="${CFGFILE2}.$$"
TMPFILE2B="${CFGFILE2}B.$$"
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"
FULLHEADER="${HEADER}
#
# Please do not edit this file directly. If you want to change or add
# anything please take a look at the files in ${MODDIR} and read
# the manpage for modules-update(8).
#
"

source /sbin/functions.sh

# Parse command-line
FORCE=false
ASSUME_KV=
while [[ -n $1 ]] ; do
	case "$1" in
		force)
			FORCE=true ;;
		--assume-kernel=*)
			ASSUME_KV=${1#*=} ;;
		*)
			eerror "Error: I don't understand $1"
			exit 1 ;;
	esac
	shift
done

# Set kernel version, either from --assume-kernel or uname -r
KV=${ASSUME_KV:-$(uname -r)}
if [[ $(KV_to_int ${KV}) -ge $(KV_to_int 2.5.48) ]]; then
	KERNEL_2_6=true
else
	KERNEL_2_6=false
fi

# Check if $CONF is valid
[[ ! -r ${CONF} ]] && CONF=

set -e

# Reset the sorting order since we depend on it
export LC_COLLATE="C"

depdir() {
	local dep=$(sed -n -e '/[ \t]*depfile=/h;${x;s/[ \t]*depfile=//g;s,/[^/]*$,,p}' \
		"${CFGFILE}")
	[[ -z ${dep} ]] && dep="/lib/modules/${KV}"
	echo "${dep}"
}

CFGFILES=${CFGFILE}
if ${KERNEL_2_6} ; then
	CFGFILES="${CFGFILES} ${CFGFILE2}"
	[[ -d /etc/devfs.d ]] && CFGFILES="${CFGFILES} ${CFGFILE4}"
fi

for x in ${CFGFILES} ; do
	if [[ -f ${x} ]] ; then
		if ! sed -ne 1p "${x}" | egrep -q "^${HEADER}" ; then
			# 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
			
			ewarn "Error: the current ${x} is not automatically generated."

			if $FORCE ; then
				ewarn "force specified, (re)generating file anyway."
			else
				eerror "Use \"modules-update force\" to force (re)generation."
				exit 1
			fi
		fi
	fi
done

generate_config() {
	local cfg=
	local conf="$1"
	local moddir="$2"
	local tmpfile="$3"
	local do_mprobe="$4"
	
	for cfg in "${moddir}"/* "${conf}" ; do
		[[ -d ${cfg} ]] && continue

		[[ ! -r ${cfg} ]] && continue

		# Skip backup and RCS files; fixes bug 20597 (07 May 2004 agriffis)
		[[ ${cfg} == *~ || ${cfg} == *.bak || ${cfg} == *,v ]] && continue

		[[ ${do_mprobe} -eq 1 && -e "/etc/modprobe.d/${cfg##*/}" ]] && continue

		echo "### modules-update: start processing ${cfg}" >> "${tmpfile}"

		if [[ -x ${cfg} ]] ; then
			# $cfg can be executable; nice touch, Wichert! :)
			"${cfg}" >> "${tmpfile}"
		else
			cat "${cfg}" >> "${tmpfile}"
		fi

		echo >> "${tmpfile}"
		echo "### modules-update: end processing ${cfg}" >> "${tmpfile}"
		echo >> "${tmpfile}"
	done

	return 0
}

if type -p modprobe.old > /dev/null ; then
	if ${FORCE} || is_older_than ${CFGFILE} ${MODDIR} || \
	   [[ ! -e ${CFGFILE} ]] ; then
		ebegin "Updating ${CFGFILE}"
		echo "${FULLHEADER}" > "${TMPFILE}"
		generate_config "${CONF}" "${MODDIR}" "${TMPFILE}" 0
		[[ -e ${CFGFILE} ]] && mv -f "${CFGFILE}" "${CFGFILE}.old"
		mv -f "${TMPFILE}" "${CFGFILE}"
		eend 0
	fi
fi

if ${FORCE} || is_older_than ${CFGFILE2} ${MODDIR} || [[ ! -e ${CFGFILE2} ]]; then
	if [[ -x /sbin/generate-modprobe.conf ]] && ${KERNEL_2_6} ; then
		# Make sure that generate-modprobe.conf can handle --assume-kernel
		# if we were called with it.
		if [[ -n ${ASSUME_KV} ]] && \
			! grep -qe -- --assume-kernel /sbin/generate-modprobe.conf
		then
			eerror "Error: modules-update called with --assume-kernel flag, but"
			eerror "generate-modprobe.conf doesn't understand it.  You need to"
			eerror "install >=module-init-tools-3.0-r2"
			exit 3
		fi
		
		ebegin "Updating ${CFGFILE2}"
		echo "${FULLHEADER/modules.d/modprobe.d}" > "${TMPFILE2}"
		if /sbin/generate-modprobe.conf ${ASSUME_KV:+--assume-kernel=${KV}} \
			>> "${TMPFILE2}" 2>/dev/null
		then
			[[ -e ${CFGFILE2} ]] && mv -f "${CFGFILE2}" "${CFGFILE2}.old"
			mv -f "${TMPFILE2}" "${CFGFILE2}"
			eend 0
		else
			#
			# If we made it here, it means either generate-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
				eend 1 "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}"
					eend 0
				else
					eend 1 "Warning: could not generate ${CFGFILE2}!"
					rm -f "${TMPFILE2B}"
				fi
				rm -f "${TMPFILE2}"
			fi
		fi

		if [[ -f ${CFGFILE3} ]] ; then
			ebegin "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}"
				eend 0
			else
				eend 1 "Warning: could not generate ${CFGFILE4}!"
				rm -f "${TMPFILE4}"
			fi
			rm -f "${TMPFILE3}"
		fi
	fi
fi

# We also call depmod here to stop insmod from complaining that modules.conf
# is more recent then modules.dep
if [[ ${CFGFILE2} -nt /lib/modules/${KV}/modules.dep ]] ; then
	if [[ -d $(depdir) && -f /proc/modules ]] ; then
		if [[ -f /usr/src/linux/System.map ]] ; then
			depmod -a -F /usr/src/linux/System.map ${KV}
		else
			ewarn "System.map not found - unable to check symbols"
		fi
	fi
fi


# vim:ts=4