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
9
# Check the existence of a kernel named as $1
10
_is_kernel_name_correct() {
14
for kernel in /boot/config-*; do
16
if [ "${KERNEL}" = "${KERNEL_NAME}" ]; then
26
# Get the most recent kernel on Debian based systems. This keeps
27
# into account both the version and the ABI. If the current kernel
28
# is the most recent kernel then the function will print a null string.
29
_get_newest_kernel_debian() {
34
for kernel in /boot/config-*; do
36
KERNEL_VERSION=${KERNEL%%-*}
40
if [ -z "$NEWEST_KERNEL" ]; then
41
# The 1st time get a version which is bigger than $1
44
# Get the biggest version
45
COMPARE_TO="$NEWEST_VERSION-$NEWEST_ABI"
48
# if $kernel is greater than $COMPARE_TO
49
if [ `dpkg --compare-versions "$KERNEL_VERSION-$ABI" gt "$COMPARE_TO" && echo "yes" || \
50
echo "no"` = "yes" ]; then
52
NEWEST_VERSION=$KERNEL_VERSION
60
# Get the most recent kernel in Rhel based systems. If the current kernel
61
# is the most recent kernel then the function will print a null string.
62
_get_newest_kernel_rhel() {
65
LAST_INSTALLED_KERNEL=$(rpm -q --whatprovides kernel --last | grep kernel -m1 | cut -f1 -d' ')
67
LIK_FORMATTED_NAME=$(rpm -q $LAST_INSTALLED_KERNEL --queryformat="%{VERSION}-%{RELEASE}.%{ARCH}\n")
69
if [ `echo $LIK_FORMATTED_NAME | grep 2.6 >/dev/null` ]; then
71
NEWEST_KERNEL=$LIK_FORMATTED_NAME
73
# Hack for Mandriva where $LIK_FORMATTED_NAME is broken
74
LIK_NAME=$(rpm -q $LAST_INSTALLED_KERNEL --queryformat="%{NAME}\n")
75
LIK_TYPE=${LIK_NAME#kernel-}
76
LIK_TYPE=${LIK_TYPE%%-*}
77
LIK_STRIPPED=${LIK_NAME#kernel-}
78
LIK_STRIPPED=${LIK_STRIPPED#$LIK_TYPE-}
79
LIK_STRIPPED_BASE=${LIK_STRIPPED%%-*}
80
LIK_STRIPPED_END=${LIK_STRIPPED#$LIK_STRIPPED_BASE-}
81
LIK_FINAL=$LIK_STRIPPED_BASE-$LIK_TYPE-$LIK_STRIPPED_END
83
NEWEST_KERNEL=$LIK_FINAL
89
# Get the newest kernel on Debian and Rhel based systems.
92
# Try Debian first as rpm can be installed in Debian based distros
93
if [ -e /usr/bin/dpkg ]; then
96
CURRENT_VERSION=${CURRENT_KERNEL%%-*}
97
CURRENT_ABI=${CURRENT_KERNEL#*-}
98
CURRENT_FLAVOUR=${CURRENT_ABI#*-}
99
CURRENT_ABI=${CURRENT_ABI%%-*}
100
NEWEST_KERNEL=$(_get_newest_kernel_debian "$CURRENT_VERSION-$CURRENT_ABI")
102
elif [ `which rpm &>/dev/null` ]; then
104
NEWEST_KERNEL=$(_get_newest_kernel_rhel)
107
# Make sure that kernel name that we extracted corresponds to an installed
109
if [ -n "$NEWEST_KERNEL" ] && [ `_is_kernel_name_correct $NEWEST_KERNEL` = "no" ]; then
122
if [ -z "$NAME" ] || [ -z "$VERSION" ]; then
123
echo "Need NAME, and VERSION defined"
124
echo "ARCH is optional"
128
KERNELS=$(ls /lib/modules/)
129
CURRENT_KERNEL=$(uname -r)
131
#We never want to keep an older version side by side to prevent conflicts
132
if [ -e "/var/lib/dkms/$NAME/$VERSION" ]; then
133
echo "Removing old $NAME-$VERSION DKMS files..."
134
dkms remove -m $NAME -v $VERSION --all
137
#Load new files, by source package and by tarball
138
echo "Loading new $NAME-$VERSION DKMS files..."
139
if [ -d "/usr/src/$NAME-$VERSION" ]; then
140
dkms add -m $NAME -v $VERSION > /dev/null
143
if [ -f "$TARBALL_ROOT/$NAME-$VERSION.dkms.tar.gz" ]; then
144
if ! dkms ldtarball --archive "$TARBALL_ROOT/$NAME-$VERSION.dkms.tar.gz"; then
147
echo "Unable to load DKMS tarball $TARBALL_ROOT/$NAME-$VERSION.dkms.tar.gz."
148
echo "Common causes include: "
149
echo " - You must be using DKMS 2.1.0.0 or later to support binaries only"
150
echo " distribution specific archives."
151
echo " - Corrupt distribution specific archive"
158
# On 1st installation, let us look for a directory
159
# in /lib/modules which matches `uname -r`. If none
160
# is found it is possible that buildd is being used
161
# and that uname -r is giving us the name of the
162
# kernel used by the buildd machine.
164
# If this is the case we try to build the kernel
165
# module for each kernel which has a directory in
166
# /lib/modules. Furthermore we will have to tell
167
# DKMS which architecture it should build the module
168
# for (e.g. if the buildd machine is using a
169
# 2.6.24-23-xen 64bit kernel).
171
# NOTE: if the headers are not installed then the
172
# module won't be built, as usual
173
if [ -z "$UPGRADE" ]; then
174
echo "First Installation: checking all kernels..."
175
for KERNEL in $KERNELS; do
176
if [ ${KERNEL} = ${CURRENT_KERNEL} ]; then
178
KERNELS=$CURRENT_KERNEL
183
KERNELS=$CURRENT_KERNEL
186
# Here we look for the most recent kernel so that we can
187
# build the module for it (in addition to doing it for the
189
NEWEST_KERNEL=$(get_newest_kernel "$KERNELS")
191
if [ -n "$NEWEST_KERNEL" ] && [ ${CURRENT_KERNEL} != ${NEWEST_KERNEL} ]; then
192
echo "Building for $CURRENT_KERNEL and $NEWEST_KERNEL"
193
KERNELS="$KERNELS $NEWEST_KERNEL"
195
echo "Building only for $CURRENT_KERNEL"
199
if [ -n "$ARCH" ]; then
200
if which lsb_release >/dev/null && [ $(lsb_release -s -i) = "Ubuntu" ]; then
210
echo "Building for architecture $ARCH"
214
for KERNEL in $KERNELS; do
215
dkms_status=`dkms status -m $NAME -v $VERSION -k $KERNEL $ARCH`
216
if [ `echo $KERNEL | grep -c "BOOT"` -gt 0 ]; then
218
echo "Module build and install for $KERNEL was skipped as "
219
echo "it is a BOOT variant"
224
#if the module isn't yet built, try to build it
225
if [ `echo $dkms_status | grep -c ": built"` -eq 0 ]; then
226
if [ ! -L /var/lib/dkms/$NAME/$VERSION/source ]; then
227
echo "This package appears to be a binaries-only package"
228
echo " you will not be able to build against kernel $KERNEL"
229
echo " since the package source was not provided"
232
if [ -e /lib/modules/$KERNEL/build/include ]; then
233
echo "Building initial module for $KERNEL"
234
dkms build -m $NAME -v $VERSION -k $KERNEL $ARCH > /dev/null
236
dkms_status=`dkms status -m $NAME -v $VERSION -k $KERNEL $ARCH`
238
echo "Module build for the currently running kernel was skipped since the"
239
echo "kernel source for this kernel does not seem to be installed."
243
#if the module is built (either pre-built or just now), install it
244
if [ `echo $dkms_status | grep -c ": built"` -eq 1 ] &&
245
[ `echo $dkms_status | grep -c ": installed"` -eq 0 ]; then
246
dkms install -m $NAME -v $VERSION -k $KERNEL $ARCH