-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-dektec-dkms
executable file
·430 lines (369 loc) · 13.5 KB
/
build-dektec-dkms
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/bin/bash
#
# Build script for the Linux DKMS for Dektec device drivers.
# Copyright (c) 2017, Thierry Lelegard
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
#
SCRIPT=$(basename $BASH_SOURCE)
SCRIPTDIR=$(cd $(dirname $BASH_SOURCE); pwd)
#-----------------------------------------------------------------------------
# Default command line parameter values.
#-----------------------------------------------------------------------------
CLEAN=false
DOWNLOAD_ONLY=false
FORCE=false
INSTALL=false
export KEEP=false
PREPARE_ONLY=false
UNINSTALL=false
VERBOSE=false
PATCH_OPTION="--silent"
LOAD_DRIVERS=false
TEST_PACKAGES=false
#-----------------------------------------------------------------------------
# Display help text
#-----------------------------------------------------------------------------
showhelp()
{
cat >&2 <<EOF
Build the Linux DKMS package for Dektec device drivers. The type of package
depends on the underlying operating system.
Usage: $SCRIPT [options]
Options:
-c
--clean
Do not build anything, just cleanup downloaded and temporary files.
-d
--download
Only download and expand the Dektec LinuxSDK. Do not build DKMS packages.
-h
--help
Display this help text.
-f
--force
Force a download of the Dektec LinuxSDK, even if it is already present.
This makes sure that the latest version is used.
--install
Directly install Dektec DKMS on the current system. Do not create a
package. This is useful on unsupported systems (no .rpm, no .deb).
-k
--keep
Keep temporary files. By default, they are deleted.
-l
--load-drivers
Do not build anything, just load all installed Dektec drivers and
test the load.
-p
--prepare
Only prepare the DKMS file structure in the temporary directory.
Do not build DKMS packages, do not clean up files.
-t
--test-package
After building the package, install it, load the drivers, uninstall
the package.
--uninstall
Directly uninstall Dektec DKMS from the current system. Do not properly
uninstall a package. This is useful on unsupported systems (no .rpm,
no .deb).
-v
--verbose
Display verbose information.
EOF
exit 1
}
#-----------------------------------------------------------------------------
# Basic functions
#-----------------------------------------------------------------------------
clean-exit() { $KEEP || [[ -z "$TMPDIR" ]] || rm -rf "$TMPDIR"; exit $1; }
error() { echo >&2 "$SCRIPT: $*"; clean-exit 1; }
info() { echo "$SCRIPT: $*"; }
verbose() { $VERBOSE && echo "$SCRIPT: $*"; }
usage() { echo >&2 "invalid command, try \"$SCRIPT --help\""; exit 1; }
# Cleanup if interrupted.
export TMPDIR=
trap clean-exit SIGINT
#-----------------------------------------------------------------------------
# Decode command line arguments
#-----------------------------------------------------------------------------
while [[ $# -gt 0 ]]; do
case "$1" in
-c|--clean)
CLEAN=true
;;
-d|--download)
DOWNLOAD_ONLY=true
;;
-f|--force)
FORCE=true
;;
-h|--help)
showhelp
;;
--install)
INSTALL=true
;;
-k|--keep)
export KEEP=true
;;
-l|--load-drivers)
LOAD_DRIVERS=true
;;
-p|--prepare)
PREPARE_ONLY=true
export KEEP=true
;;
-t|--test-package)
TEST_PACKAGES=true
;;
--uninstall)
UNINSTALL=true
;;
-v|--verbose)
VERBOSE=true
PATCH_OPTION=
;;
*)
usage
;;
esac
shift
done
CONFDIR="$SCRIPTDIR/config"
SDKDIR="$SCRIPTDIR/LinuxSDK"
DTAPI_H="$SDKDIR/DTAPI/Include/DTAPI.h"
PKGDIR="$SCRIPTDIR/packages"
export TMPDIR="$SCRIPTDIR/tmp"
# Text file containing the name of the LinuxSDK tarball.
VERSFILE="$SCRIPTDIR/VERSION"
#-----------------------------------------------------------------------------
# Special operations.
#-----------------------------------------------------------------------------
clean-files()
{
verbose "cleaning up non-original files"
rm -rf "$SCRIPTDIR"/LinuxSDK* "$SDKDIR" "$TMPDIR" "$VERSFILE"
}
load-drivers()
{
echo "=== Unload current drivers"
for drv in DtPcieNw DtPcie DtaNw Dta Dtu; do
sudo rmmod $drv 2>/dev/null
done
echo "=== Load new drivers"
for drv in Dtu Dta DtaNw DtPcie DtPcieNw; do
sudo modprobe $drv
done
echo "=== List loaded drivers"
lsmod | grep -e '^Module' -e '^Dt'
echo "=== Recent kernel messages"
sudo dmesg | tail -20 | grep Dt
}
if $CLEAN; then
clean-files
clean-exit 0
elif $LOAD_DRIVERS; then
load-drivers
clean-exit 0
fi
#-----------------------------------------------------------------------------
# Main operation.
#-----------------------------------------------------------------------------
# Download the LinuxSDK if not present.
TARNAME=$(cat "$VERSFILE" 2>/dev/null)
TARBALL="$SCRIPTDIR/$TARNAME"
if $FORCE || [[ -z "$TARNAME" ]] || [[ ! -e "$TARBALL" ]]; then
URL=$($SCRIPTDIR/get-dektec-linux-sdk-url.sh)
[[ -n "$URL" ]] || error "got empty URL"
verbose "downloading $URL"
mkdir -p "$TMPDIR"
TARNAME=$(basename "$URL")
TARBALL="$SCRIPTDIR/$TARNAME"
verbose "actual file name: $TARNAME"
echo "$TARNAME" >"$VERSFILE"
curl --location "$URL" -o "$TARBALL" || clean-exit 1
fi
# Get the Linux SDL version from the file name.
VERSION=$(sed <<<$TARNAME -e 's/LinuxSDK_v//' -e 's/\.tar\.gz//g' -e 's/\.tgz//g')
verbose "package version is $VERSION"
# Expand the LinuxSDK if newer than the expanded files.
if $FORCE || [[ ! -e "$DTAPI_H" || "$TARBALL" -nt "$DTAPI_H" ]]; then
verbose "expanding $TARBALL"
rm -rf "$SDKDIR"
tar -x -C $(dirname "$TARBALL") -z -f "$TARBALL" || clean-exit 1
# Make sure expanded files are more recent than the tarball.
find "$SDKDIR" -print0 | xargs -0 touch
fi
SRCDIR="$TMPDIR/dektec-dkms-$VERSION"
DKMS_DIR="$SRCDIR/dektec-$VERSION"
DKMS_INSTALL="/usr/src/dektec-$VERSION"
# In case of "download only", we are done.
$DOWNLOAD_ONLY && clean-exit 0
# Build DKMS directory.
verbose "building DKMS structure in $DKMS_DIR"
rm -rf "$TMPDIR"
mkdir -p "$DKMS_DIR" || clean-exit 1
sed -e "s/{{VERSION}}/$VERSION/g" "$CONFDIR/dkms.conf.template" >"$DKMS_DIR/dkms.conf"
sed -e "s/{{VERSION}}/$VERSION/g" "$CONFDIR/Makefile.template" >"$DKMS_DIR/Makefile"
cp -r "$SDKDIR/Common" "$SDKDIR/Drivers" "$SDKDIR/License" "$DKMS_DIR"
make -C "$DKMS_DIR" --no-print-directory --silent clean
find "$DKMS_DIR" -type f -print0 | xargs -0 chmod 644
find "$DKMS_DIR" -type d -print0 | xargs -0 chmod 755
# Put other files to install in the upper level directory.
cp "$SDKDIR/License" \
"$SDKDIR/Readme" \
"$SDKDIR/Drivers/Dta/Source/Linux/51-dta.rules" \
"$SDKDIR/Drivers/Dtu/Source/Linux/51-dtu.rules" \
"$SDKDIR/Drivers/DtPcie/Source/Linux/51-dtpcie.rules" \
"$SRCDIR"
# In case of "prepare only", we are done.
$PREPARE_ONLY && clean-exit 0
# If there is a patch for this version of the package, apply it.
PATCH="$SCRIPTDIR/patches/dektec-dkms-$VERSION.patch"
if [[ -e "$PATCH" ]]; then
verbose "applying patch $PATCH"
patch -d "$SRCDIR" -p1 $PATCH_OPTION <"$PATCH"
fi
# Build a source tarball for the dektec-dkms package.
mkdir -p "$PKGDIR" || clean-exit 1
SRCTARBALL="$PKGDIR/dektec-dkms-$VERSION.tgz"
verbose "creating $SRCTARBALL"
tar czp -f "$SRCTARBALL" -C "$TMPDIR" --owner=0 --group=0 "dektec-dkms-$VERSION"
# Special install operation.
if $INSTALL; then
verbose "installing dkms dektec/$VERSION"
sudo rm -rf "$DKMS_INSTALL"
sudo cp -r "$DKMS_DIR" "$DKMS_INSTALL" || clean-exit 1
sudo install -m 644 -o root -g root "$SRCDIR/51-dta.rules" "$SRCDIR/51-dtu.rules" "$SRCDIR/51-dtpcie.rules" /etc/udev/rules.d/
sudo dkms install -m dektec -v "$VERSION" --force
clean-exit 0
fi
# Special uninstall operation.
if $UNINSTALL; then
verbose "removing dkms dektec/$VERSION"
sudo rmmod DtaNw Dta Dtu DtPcieNw DtPcie
sudo dkms remove "dektec/$VERSION" --all
sudo rm -rf "$DKMS_INSTALL" /etc/udev/rules.d/51-dta.rules /etc/udev/rules.d/51-dtu.rules /etc/udev/rules.d/51-dtpcie.rules
clean-exit 0
fi
# Determine operating system type and version.
FC_DISTRO=$(grep " release [0-9]" /etc/fedora-release 2>/dev/null | sed -e 's/^.* release \([0-9]*\).*$/\1/')
RH_DISTRO=$(grep " release [0-9]" /etc/redhat-release 2>/dev/null | sed -e 's/^.* release \([0-9]*\).*$/\1/')
UB_DISTRO=$([[ -f /etc/lsb-release ]] && source /etc/lsb-release 2>/dev/null && [[ "`tr A-Z a-z <<<$DISTRIB_ID`" = ubuntu ]] && echo $DISTRIB_RELEASE)
DB_DISTRO=$(head -1 /etc/debian_version 2>/dev/null | sed -e 's/ //g')
DISTRO_PREFIX=
DISTRO_VERSION=
RPM_DISTRO=false
DEB_DISTRO=false
if [[ -n "$FC_DISTRO" ]]; then
DISTRO_PREFIX=".fc"
DISTRO_VERSION="$FC_DISTRO"
RPM_DISTRO=true
elif [[ -n "$RH_DISTRO" ]]; then
DISTRO_PREFIX=".el"
DISTRO_VERSION="$RH_DISTRO"
RPM_DISTRO=true
elif [[ -n "$UB_DISTRO" ]]; then
DISTRO_PREFIX=".ub"
DISTRO_VERSION="$UB_DISTRO"
DEB_DISTRO=true
elif [[ -n "$DB_DISTRO" ]]; then
DISTRO_PREFIX=".deb"
DISTRO_VERSION="$DB_DISTRO"
DEB_DISTRO=true
fi
DISTRO="$DISTRO_PREFIX"$(sed <<<"$DISTRO_VERSION" -e 's/\..*//')
verbose "Linux distro: $DISTRO, full version: $DISTRO_VERSION"
! $RPM_DISTRO && ! $DEB_DISTRO && error "unsupported Linux distro"
# This function copies a file from $PKGDIR to a possibly shared repo on a mounted filesystem.
copy-shared()
{
local file=$1
local dir=
for dir in $(df | sed -e 's/.* //' -e '/^\//!d'); do
if [[ -d $dir/dektec-dkms/.git && -d $dir/dektec-dkms/packages ]]; then
info "copying $file to $dir/dektec-dkms/packages"
cp "$PKGDIR/$file" "$dir/dektec-dkms/packages/$file"
fi
done
}
# Build RPM distro.
if $RPM_DISTRO; then
verbose "building RPM package"
# User's rpmbuild environment:
RPMBUILD="$HOME/rpmbuild"
if [[ ! -d "$RPMBUILD" ]]; then
[[ -z "$(which rpmdev-setuptree 2>/dev/null)" ]] && error "rpmdev-setuptree not found, install package rpmdevtools"
rpmdev-setuptree
[[ ! -d "$RPMBUILD" ]] && error "$RPMBUILD not found"
fi
# Build RPM package.
RELEASE=0
cp -f "$SRCTARBALL" $RPMBUILD/SOURCES/
rpmbuild -ba -D "version $VERSION" -D "release $RELEASE$DISTRO" "$CONFDIR/dektec-dkms.spec"
# Collect package files.
cp -f \
"$RPMBUILD/RPMS/noarch/dektec-dkms-$VERSION-$RELEASE$DISTRO.noarch.rpm" \
"$RPMBUILD/SRPMS/dektec-dkms-$VERSION-$RELEASE$DISTRO.src.rpm" \
"$PKGDIR"
copy-shared dektec-dkms-$VERSION-$RELEASE$DISTRO.noarch.rpm
copy-shared dektec-dkms-$VERSION-$RELEASE$DISTRO.src.rpm
# Test the package if required.
if $TEST_PACKAGES; then
echo "=== Install package dektec-dkms-$VERSION-$RELEASE$DISTRO.noarch.rpm"
sudo rpm -Uvh "$PKGDIR/dektec-dkms-$VERSION-$RELEASE$DISTRO.noarch.rpm"
load-drivers
echo "=== Uninstall package dektec-dkms"
sudo rpm -e dektec-dkms
fi
fi
# Build DEB distro.
if $DEB_DISTRO; then
verbose "building DEB package"
# Root of .deb package build.
DEBDIR="$TMPDIR/deb"
rm -rf "$DEBDIR"
# Build file structure of the package.
mkdir -p -m 0755 "$DEBDIR/DEBIAN" "$DEBDIR/usr/src" "$DEBDIR/etc/udev/rules.d" "$DEBDIR/usr/share/doc/dektec-dkms"
for f in control postinst prerm; do
sed -e "s/{{VERSION}}/$VERSION/g" "$CONFDIR/$f.template" >"$DEBDIR/DEBIAN/$f"
done
chmod 755 "$DEBDIR/DEBIAN/postinst" "$DEBDIR/DEBIAN/prerm"
cp -rp "$SRCDIR/dektec-$VERSION" "$DEBDIR/usr/src"
install -m 644 "$SRCDIR/51-dta.rules" "$SRCDIR/51-dtu.rules" "$SRCDIR/51-dtpcie.rules" "$DEBDIR/etc/udev/rules.d"
install -m 644 "$SRCDIR/Readme" "$SRCDIR/License" "$DEBDIR/usr/share/doc/dektec-dkms"
# Build the binary package
dpkg --build "$DEBDIR" "$PKGDIR"
# Collect package files.
copy-shared dektec-dkms_${VERSION}_all.deb
# Test the package if required.
if $TEST_PACKAGES; then
echo "=== Install package dektec-dkms_${VERSION}_all.deb"
sudo dpkg -i "$PKGDIR/dektec-dkms_${VERSION}_all.deb"
load-drivers
echo "=== Uninstall package dektec-dkms"
sudo dpkg -r dektec-dkms
fi
fi
# Final cleanup.
clean-exit 0