2
# Copyright (C) 2002-2005 Flavio Stanchina
3
# Copyright (C) 2005-2006 Aric Cyr
4
# Copyright (C) 2007 Mario Limonciello
5
# Copyright (C) 2009 Alberto Milone
14
Linux) DIR="/lib/modules/$KVER/build" ;;
15
GNU/kFreeBSD) DIR="/usr/src/kfreebsd-headers-$KVER/sys" ;;
21
DIR=$(_get_kernel_dir $1)
23
Linux) test -e $DIR/include ;;
24
GNU/kFreeBSD) test -e $DIR/kern && test -e $DIR/conf/kmod.mk ;;
30
# Check the existence of a kernel named as $1
31
_is_kernel_name_correct() {
35
for kernel in /boot/config-*; do
37
if [ "${KERNEL}" = "${KERNEL_NAME}" ]; then
47
# Get the most recent kernel on Debian based systems. This keeps
48
# into account both the version and the ABI. If the current kernel
49
# is the most recent kernel then the function will print a null string.
50
_get_newest_kernel_debian() {
55
for kernel in /boot/config-*; do
57
KERNEL_VERSION=${KERNEL%%-*}
61
if [ -z "$NEWEST_KERNEL" ]; then
62
# The 1st time get a version which is bigger than $1
65
# Get the biggest version
66
COMPARE_TO="$NEWEST_VERSION-$NEWEST_ABI"
69
# if $kernel is greater than $COMPARE_TO
70
if [ `dpkg --compare-versions "$KERNEL_VERSION-$ABI" gt "$COMPARE_TO" && echo "yes" || \
71
echo "no"` = "yes" ]; then
73
NEWEST_VERSION=$KERNEL_VERSION
81
# Get the most recent kernel in Rhel based systems. If the current kernel
82
# is the most recent kernel then the function will print a null string.
83
_get_newest_kernel_rhel() {
86
LAST_INSTALLED_KERNEL=$(rpm -q --whatprovides kernel --last | grep kernel -m1 | cut -f1 -d' ')
88
LIK_FORMATTED_NAME=$(rpm -q $LAST_INSTALLED_KERNEL --queryformat="%{VERSION}-%{RELEASE}.%{ARCH}\n")
90
if [ `echo $LIK_FORMATTED_NAME | grep 2.6 >/dev/null` ]; then
92
NEWEST_KERNEL=$LIK_FORMATTED_NAME
94
# Hack for Mandriva where $LIK_FORMATTED_NAME is broken
95
LIK_NAME=$(rpm -q $LAST_INSTALLED_KERNEL --queryformat="%{NAME}\n")
96
LIK_TYPE=${LIK_NAME#kernel-}
97
LIK_TYPE=${LIK_TYPE%%-*}
98
LIK_STRIPPED=${LIK_NAME#kernel-}
99
LIK_STRIPPED=${LIK_STRIPPED#$LIK_TYPE-}
100
LIK_STRIPPED_BASE=${LIK_STRIPPED%%-*}
101
LIK_STRIPPED_END=${LIK_STRIPPED#$LIK_STRIPPED_BASE-}
102
LIK_FINAL=$LIK_STRIPPED_BASE-$LIK_TYPE-$LIK_STRIPPED_END
104
NEWEST_KERNEL=$LIK_FINAL
110
# Get the newest kernel on Debian and Rhel based systems.
111
get_newest_kernel() {
113
# Try Debian first as rpm can be installed in Debian based distros
114
if [ -e /usr/bin/dpkg ]; then
117
CURRENT_VERSION=${CURRENT_KERNEL%%-*}
118
CURRENT_ABI=${CURRENT_KERNEL#*-}
119
CURRENT_FLAVOUR=${CURRENT_ABI#*-}
120
CURRENT_ABI=${CURRENT_ABI%%-*}
121
NEWEST_KERNEL=$(_get_newest_kernel_debian "$CURRENT_VERSION-$CURRENT_ABI")
123
elif [ `which rpm >/dev/null` ]; then
125
NEWEST_KERNEL=$(_get_newest_kernel_rhel)
128
# Make sure that kernel name that we extracted corresponds to an installed
130
if [ -n "$NEWEST_KERNEL" ] && [ `_is_kernel_name_correct $NEWEST_KERNEL` = "no" ]; then
143
if [ -z "$NAME" ] || [ -z "$VERSION" ]; then
144
echo "Need NAME, and VERSION defined"
145
echo "ARCH is optional"
149
KERNELS=$(ls /lib/modules/)
150
CURRENT_KERNEL=$(uname -r)
152
#We never want to keep an older version side by side to prevent conflicts
153
if [ -e "/var/lib/dkms/$NAME/$VERSION" ]; then
154
echo "Removing old $NAME-$VERSION DKMS files..."
155
dkms remove -m $NAME -v $VERSION --all
158
#Load new files, by source package and by tarball
159
if [ -f "$TARBALL_ROOT/$NAME-$VERSION.dkms.tar.gz" ]; then
160
if ! dkms ldtarball --archive "$TARBALL_ROOT/$NAME-$VERSION.dkms.tar.gz"; then
163
echo "Unable to load DKMS tarball $TARBALL_ROOT/$NAME-$VERSION.dkms.tar.gz."
164
echo "Common causes include: "
165
echo " - You must be using DKMS 2.1.0.0 or later to support binaries only"
166
echo " distribution specific archives."
167
echo " - Corrupt distribution specific archive"
172
elif [ -d "/usr/src/$NAME-$VERSION" ]; then
173
echo "Loading new $NAME-$VERSION DKMS files..."
174
dkms add -m $NAME -v $VERSION > /dev/null
177
# On 1st installation, let us look for a directory
178
# in /lib/modules which matches `uname -r`. If none
179
# is found it is possible that buildd is being used
180
# and that uname -r is giving us the name of the
181
# kernel used by the buildd machine.
183
# If this is the case we try to build the kernel
184
# module for each kernel which has a directory in
185
# /lib/modules. Furthermore we will have to tell
186
# DKMS which architecture it should build the module
187
# for (e.g. if the buildd machine is using a
188
# 2.6.24-23-xen 64bit kernel).
190
# NOTE: if the headers are not installed then the
191
# module won't be built, as usual
192
if [ -z "$UPGRADE" ]; then
193
echo "First Installation: checking all kernels..."
194
for KERNEL in $KERNELS; do
195
if [ ${KERNEL} = ${CURRENT_KERNEL} ]; then
197
KERNELS=$CURRENT_KERNEL
202
KERNELS=$CURRENT_KERNEL
205
# Here we look for the most recent kernel so that we can
206
# build the module for it (in addition to doing it for the
208
NEWEST_KERNEL=$(get_newest_kernel "$KERNELS")
210
# If the current kernel doesn't come from the host of a chroot
211
if [ `_is_kernel_name_correct $CURRENT_KERNEL` = "yes" ]; then
212
# See if it's worth building the module for both the newest kernel
213
# and for the current kernel
214
if [ -n "$NEWEST_KERNEL" ] && [ ${CURRENT_KERNEL} != ${NEWEST_KERNEL} ]; then
215
echo "Building for $CURRENT_KERNEL and $NEWEST_KERNEL"
216
KERNELS="$CURRENT_KERNEL $NEWEST_KERNEL"
218
echo "Building only for $CURRENT_KERNEL"
220
# The current kernel is not useful as it's the host's
222
echo "It is likely that $CURRENT_KERNEL belongs to a chroot's host"
224
# Let's use only the newest kernel
225
if [ -n "$NEWEST_KERNEL" ]; then
226
KERNELS="$NEWEST_KERNEL"
227
echo "Building only for $NEWEST_KERNEL"
231
if [ -n "$ARCH" ]; then
232
if which lsb_release >/dev/null && [ $(lsb_release -s -i) = "Ubuntu" ]; then
242
echo "Building for architecture $ARCH"
246
for KERNEL in $KERNELS; do
247
dkms_status=`dkms status -m $NAME -v $VERSION -k $KERNEL $ARCH`
248
if [ `echo $KERNEL | grep -c "BOOT"` -gt 0 ]; then
250
echo "Module build and install for $KERNEL was skipped as "
251
echo "it is a BOOT variant"
256
#if the module isn't yet built, try to build it
257
if [ `echo $dkms_status | grep -c ": built"` -eq 0 ]; then
258
if [ ! -L /var/lib/dkms/$NAME/$VERSION/source ]; then
259
echo "This package appears to be a binaries-only package"
260
echo " you will not be able to build against kernel $KERNEL"
261
echo " since the package source was not provided"
264
if _check_kernel_dir $KERNEL; then
265
echo "Building initial module for $KERNEL"
267
dkms build -m $NAME -v $VERSION -k $KERNEL $ARCH > /dev/null
282
dkms_status=`dkms status -m $NAME -v $VERSION -k $KERNEL $ARCH`
284
echo "Module build for the currently running kernel was skipped since the"
285
echo "kernel source for this kernel does not seem to be installed."
289
#if the module is built (either pre-built or just now), install it
290
if [ `echo $dkms_status | grep -c ": built"` -eq 1 ] &&
291
[ `echo $dkms_status | grep -c ": installed"` -eq 0 ]; then
292
dkms install -m $NAME -v $VERSION -k $KERNEL $ARCH