~ubuntu-branches/debian/stretch/dkms/stretch

1.1.11 by Giuseppe Iuculano
Import upstream version 2.1.0.1
1
#!/bin/sh
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
6
7
set -e
8
1.1.12 by Giuseppe Iuculano
Import upstream version 2.1.1.0
9
# Check the existence of a kernel named as $1
10
_is_kernel_name_correct() {
11
    CORRECT="no"
12
    KERNEL_NAME=$1
13
    
14
    for kernel in /boot/config-*; do
15
        KERNEL=${kernel#*-}
1.1.13 by Giuseppe Iuculano
Import upstream version 2.1.1.1
16
        if [ "${KERNEL}" = "${KERNEL_NAME}" ]; then
1.1.12 by Giuseppe Iuculano
Import upstream version 2.1.1.0
17
            CORRECT="yes"
18
            break
19
        fi
20
    done
21
22
    echo $CORRECT
23
}
24
25
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() {
30
    NEWEST_KERNEL=
31
    NEWEST_VERSION=
32
    NEWEST_ABI=
33
    
34
    for kernel in /boot/config-*; do
35
        KERNEL=${kernel#*-}
36
        KERNEL_VERSION=${KERNEL%%-*}
37
        ABI=${KERNEL#*-}
38
        ABI=${ABI%%-*}
39
1.1.13 by Giuseppe Iuculano
Import upstream version 2.1.1.1
40
        if [ -z "$NEWEST_KERNEL" ]; then
1.1.12 by Giuseppe Iuculano
Import upstream version 2.1.1.0
41
            # The 1st time get a version which is bigger than $1
42
            COMPARE_TO=$1
43
        else
44
            # Get the biggest version
45
            COMPARE_TO="$NEWEST_VERSION-$NEWEST_ABI"
46
        fi
47
            
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
51
            NEWEST_KERNEL=$KERNEL
52
            NEWEST_VERSION=$KERNEL_VERSION
53
            NEWEST_ABI=$ABI
54
        fi
55
    done
56
57
    echo "$NEWEST_KERNEL"
58
}
59
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() {
63
    NEWEST_KERNEL=
64
    
65
    LAST_INSTALLED_KERNEL=$(rpm -q --whatprovides kernel  --last | grep kernel -m1 | cut -f1 -d' ')
66
67
    LIK_FORMATTED_NAME=$(rpm -q $LAST_INSTALLED_KERNEL --queryformat="%{VERSION}-%{RELEASE}.%{ARCH}\n")
68
69
    if [ `echo $LIK_FORMATTED_NAME | grep 2.6 >/dev/null` ]; then
70
        # Fedora and Suse
71
        NEWEST_KERNEL=$LIK_FORMATTED_NAME
72
    else
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
82
83
        NEWEST_KERNEL=$LIK_FINAL
84
    fi
85
86
    echo $NEWEST_KERNEL
87
}
88
89
# Get the newest kernel on Debian and Rhel based systems.
90
get_newest_kernel() {
91
    NEWEST_KERNEL=
92
    # Try Debian first as rpm can be installed in Debian based distros
93
    if [ -e /usr/bin/dpkg ]; then
94
        # If DEB based
95
        CURRENT_KERNEL=$1
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")
101
13 by Giuseppe Iuculano, Michael Gilbert, Giuseppe Iuculano
[ Michael Gilbert ]
102
    elif [ `which rpm >/dev/null` ]; then
1.1.12 by Giuseppe Iuculano
Import upstream version 2.1.1.0
103
        # If RPM based
104
        NEWEST_KERNEL=$(_get_newest_kernel_rhel)
105
    fi
106
107
    # Make sure that kernel name that we extracted corresponds to an installed
108
    # kernel
1.1.13 by Giuseppe Iuculano
Import upstream version 2.1.1.1
109
    if [ -n "$NEWEST_KERNEL" ] && [ `_is_kernel_name_correct $NEWEST_KERNEL` = "no" ]; then
1.1.12 by Giuseppe Iuculano
Import upstream version 2.1.1.0
110
        NEWEST_KERNEL=
111
    fi
112
1.1.13 by Giuseppe Iuculano
Import upstream version 2.1.1.1
113
    echo $NEWEST_KERNEL
1.1.12 by Giuseppe Iuculano
Import upstream version 2.1.1.0
114
}
115
1.1.11 by Giuseppe Iuculano
Import upstream version 2.1.0.1
116
NAME=$1
117
VERSION=$2
118
TARBALL_ROOT=$3
119
ARCH=$4
120
UPGRADE=$5
121
122
if [ -z "$NAME" ] || [ -z "$VERSION" ]; then
123
    echo "Need NAME, and VERSION defined"
124
    echo "ARCH is optional"
125
    exit 1
126
fi
127
128
KERNELS=$(ls /lib/modules/)
129
CURRENT_KERNEL=$(uname -r)
130
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
135
fi
136
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
141
fi
142
143
if [ -f "$TARBALL_ROOT/$NAME-$VERSION.dkms.tar.gz" ]; then
144
    if ! dkms ldtarball --archive "$TARBALL_ROOT/$NAME-$VERSION.dkms.tar.gz"; then
145
        echo ""
146
        echo ""
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"
152
        echo ""
153
        echo ""
154
        exit 2 
155
    fi
156
fi
157
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.
163
# 
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).
170
#
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
177
            # Kernel found
178
            KERNELS=$CURRENT_KERNEL
179
            break
180
        fi
181
    done
182
else
183
    KERNELS=$CURRENT_KERNEL
184
fi
185
1.1.12 by Giuseppe Iuculano
Import upstream version 2.1.1.0
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
188
# current kernel.
189
NEWEST_KERNEL=$(get_newest_kernel "$KERNELS")
190
191
if [ -n "$NEWEST_KERNEL" ] && [ ${CURRENT_KERNEL} != ${NEWEST_KERNEL} ]; then
192
    echo "Building for $CURRENT_KERNEL and $NEWEST_KERNEL"
193
    KERNELS="$KERNELS $NEWEST_KERNEL"
194
else
195
    echo "Building only for $CURRENT_KERNEL"
196
fi
197
198
199
if [ -n "$ARCH" ]; then
1.1.13 by Giuseppe Iuculano
Import upstream version 2.1.1.1
200
    if which lsb_release >/dev/null && [ $(lsb_release -s -i) = "Ubuntu" ]; then
201
        case $ARCH in
202
            amd64)
203
                ARCH="x86_64"
204
                ;;
205
            lpia|i?86)
206
                ARCH="i686"
207
                ;;
208
        esac
209
    fi
1.1.11 by Giuseppe Iuculano
Import upstream version 2.1.0.1
210
    echo "Building for architecture $ARCH"
211
    ARCH="-a $ARCH"
212
fi
213
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
217
        echo ""
218
        echo "Module build and install for $KERNEL was skipped as "
219
        echo "it is a BOOT variant"
220
        continue
221
    fi
222
223
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"
230
            continue
231
        fi
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
235
            echo "Done."
236
            dkms_status=`dkms status -m $NAME -v $VERSION -k $KERNEL $ARCH`
237
        else
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."
240
        fi
241
    fi
242
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
247
    fi
248
done
1.1.12 by Giuseppe Iuculano
Import upstream version 2.1.1.0
249