-
Notifications
You must be signed in to change notification settings - Fork 0
/
dkms.in
2636 lines (2343 loc) · 93.1 KB
/
dkms.in
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# Dynamic Kernel Module Support (DKMS) <[email protected]>
# Copyright (C) 2003-2008 Dell, Inc.
# by Gary Lerhaupt, Matt Domsch, & Mario Limonciello
# Copyright (C) 2012 by Darik Horn <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
shopt -s extglob
# All of the variables we will accept from dkms.conf.
# Does not include directives
# The last group of variables has been deprecated
readonly dkms_conf_variables="CLEAN PACKAGE_NAME
PACKAGE_VERSION POST_ADD POST_BUILD POST_INSTALL POST_REMOVE PRE_BUILD
PRE_INSTALL BUILD_DEPENDS BUILD_EXCLUSIVE_ARCH BUILD_EXCLUSIVE_CONFIG
BUILD_EXCLUSIVE_KERNEL BUILD_EXCLUSIVE_KERNEL_MIN BUILD_EXCLUSIVE_KERNEL_MAX
build_exclude OBSOLETE_BY MAKE MAKE_MATCH
PATCH PATCH_MATCH patch_array BUILT_MODULE_NAME
built_module_name BUILT_MODULE_LOCATION built_module_location
DEST_MODULE_NAME dest_module_name
DEST_MODULE_LOCATION dest_module_location
STRIP strip AUTOINSTALL NO_WEAK_MODULES
SIGN_FILE MOK_SIGNING_KEY MOK_CERTIFICATE
REMAKE_INITRD MODULES_CONF MODULES_CONF_OBSOLETES
MODULES_CONF_ALIAS_TYPE MODULES_CONF_OBSOLETE_ONLY"
# All of the variables not related to signing we will accept from framework.conf.
readonly dkms_framework_nonsigning_variables="source_tree dkms_tree install_tree tmp_location
verbose symlink_modules autoinstall_all_kernels
modprobe_on_install parallel_jobs"
# All of the signing related variables we will accept from framework.conf.
readonly dkms_framework_signing_variables="sign_file mok_signing_key mok_certificate"
# Some important regular expressions. Requires bash 3 or above.
# Any poor souls still running bash 2 or older really need an upgrade.
readonly mv_re='^([^/]*)/(.*)$'
# Areas that will vary between Linux and other OS's
_get_kernel_dir() {
if [[ -z $ksourcedir_fromcli ]]; then
KVER=$1
case ${current_os} in
Linux) DIR="$install_tree/$KVER/build" ;;
GNU/kFreeBSD) DIR="/usr/src/kfreebsd-headers-$KVER/sys" ;;
esac
echo $DIR
else
echo $kernel_source_dir
fi
}
_check_kernel_dir() {
DIR=$(_get_kernel_dir $1)
case ${current_os} in
Linux) test -e $DIR/include ;;
GNU/kFreeBSD) test -e $DIR/kern && test -e $DIR/conf/kmod.mk ;;
*) return 1 ;;
esac
return $?
}
# Run a command that we may or may not want to be detailed about.
invoke_command()
{
# $1 = command to be executed using eval.
# $2 = Description of command to run
# $3 = Redirect command output to this file
# $4 = 'background' if you want to run the command asynchronously.
local exitval=0
local -r cmd=$([[ $3 ]] && echo "{ $1; } >> $3 2>&1" || echo "$1")
[[ $verbose ]] && echo -e "$cmd" || echo -en "$2..."
if [[ $4 = background && ! $verbose ]]; then
local pid progresspid
(eval "$cmd" >/dev/null 2>&1) & pid=$!
{
on_exit() {
kill $(jobs -p) 2>/dev/null
wait $(jobs -p) 2>/dev/null
}
trap on_exit EXIT
while /bin/kill --signal 0 $pid > /dev/null 2>&1; do
sleep 3 &
wait $!
echo -en "."
done
} & progresspid=$!
wait $pid 2>/dev/null
exitval=$?
kill $progresspid 2>/dev/null
wait $progresspid 2>/dev/null
else
eval "$cmd"; exitval=$?
fi
if (($exitval > 0)); then
echo -en "(bad exit status: $exitval)"
# Print the failing command without the clunky redirection
[[ ! $verbose ]] && echo -en "\nFailed command:\n$1"
else
echo " done."
fi
return $exitval
}
error() (
exec >&2
echo $""
echo -n $"Error! "
for s in "$@"; do echo "$s"; done
)
warn() (
exec >&2
echo -n $"Warning: "
for s in "$@"; do echo "$s"; done
)
deprecated() (
exec >&2
echo -n $"Deprecated feature: "
for s in "$@"; do echo "$s"; done
)
# Print an error message and die with the passed error code.
die() {
# $1 = error code to return with
# rest = strings to print before we exit.
ret=$1
shift
error "$@"
[[ $die_is_fatal = yes ]] && exit $ret || return $ret
}
# Print a warning message and die with the passed error code.
diewarn() {
# $1 = error code to return with
# rest = strings to print before we exit.
ret=$1
shift
warn "$@"
[[ $die_is_fatal = yes ]] && exit $ret || return $ret
}
mktemp_or_die() {
local t
t=$(mktemp "$@") && echo "$t" && return
[[ $* = *-d* ]] && die 1 $"Unable to make temporary directory"
die 1 "Unable to make temporary file."
}
show_usage()
{
echo $"Usage: $0 [action] [options]"
echo $" [action] = { add | remove | build | unbuild | install | uninstall | match |"
echo $" autoinstall | mktarball | ldtarball | status | generate_mok }"
echo $" [options] = [-m module] [-v module-version] [-k kernel-version] [-a arch]"
echo $" [-c dkms.conf-location] [-q] [--force] [--force-version-override] [--all]"
echo $" [--templatekernel=kernel] [--directive='cli-directive=cli-value']"
echo $" [--config=kernel-.config-location] [--archive=tarball-location]"
echo $" [--kernelsourcedir=source-location] [--rpm_safe_upgrade]"
echo $" [--dkmstree path] [--sourcetree path] [--installtree path]"
echo $" [--binaries-only] [--source-only] [--verbose]"
echo $" [--no-depmod] [--modprobe-on-install] [-j number] [--version]"
}
VER()
{
# $1 = kernel version string
# Pad all numbers in $1 so that they have at least three digits, e.g.,
# 2.6.9-1cvs200409091247 => 002.006.009-001cvs200409091247
# The result should compare correctly as a string.
echo $1 | sed -e 's:\([^0-9]\)\([0-9]\):\1 \2:g' \
-e 's:\([0-9]\)\([^0-9]\):\1 \2:g' \
-e 's:\(.*\): \1 :' \
-e 's: \([0-9]\) : 00\1 :g' \
-e 's: \([0-9][0-9]\) : 0\1 :g' \
-e 's: ::g'
}
# Find out how many CPUs there are so that we may pass an appropriate -j
# option to make. Ignore hyperthreading for now.
get_num_cpus()
{
# use nproc(1) from coreutils 8.1-1+ if available, otherwise single job
if [[ -x /usr/bin/nproc ]]; then
nproc
else
echo "1"
fi
}
# Finds a .ko or .ko.xz based on a directory and module name
# must call set_module_suffix first
compressed_or_uncompressed()
{
# module dir = $1
# module = $2
local test1="$1/$2$module_uncompressed_suffix"
local test2="$1/$2$module_uncompressed_suffix$module_compressed_suffix"
if [[ -e "$test1" ]]; then
echo "$test1"
elif [[ -e "$test2" ]]; then
echo "$test2"
fi
}
# Finds .ko or .ko.xz based on a tree and module name
# must call set_module_suffix first
find_module()
{
# tree = $1
# module = $2
find "$1" -name "$2$module_uncompressed_suffix" -o -name "$2$module_suffix" -type f
return $?
}
# Figure out the correct module suffix for the kernel we are currently
# dealing with, which may or may not be the currently installed kernel.
set_module_suffix()
{
# $1 = the kernel to base the module_suffix on
kernel_test="${1:-$(uname -r)}"
module_uncompressed_suffix=".ko"
grep -q '\.gz:' $install_tree/$kernel_test/modules.dep 2>/dev/null && module_compressed_suffix=".gz"
grep -q '\.xz:' $install_tree/$kernel_test/modules.dep 2>/dev/null && module_compressed_suffix=".xz"
grep -q '\.zst:' $install_tree/$kernel_test/modules.dep 2>/dev/null && module_compressed_suffix=".zst"
module_suffix="$module_uncompressed_suffix$module_compressed_suffix"
}
set_kernel_source_dir_and_kconfig()
{
if [[ -z "${ksourcedir_fromcli}" ]]; then
# $1 = the kernel to base the directory on
kernel_source_dir="$(_get_kernel_dir "$1")"
fi
if [[ -z "${kconfig_fromcli}" ]]; then
kernel_config="${kernel_source_dir}/.config"
fi
}
check_all_is_banned()
{
if [[ $all ]]; then
die 5 $"The action $1 does not support the --all parameter."
fi
}
# A little test function for DKMS commands that only work on one kernel.
have_one_kernel() {
if (( ${#kernelver[@]} != 1 )); then
die 4 $"The action $1 does not support multiple kernel version parameters on the command line."
fi
check_all_is_banned $1
}
# Set up the kernelver and arch arrays. You must have a 1:1 correspondence --
# if there is an entry in kernelver[$i], there must also be an entry in arch[$i]
# Note the special casing for the status action -- the status functions just
# report on what we already have, and will break with the preprocessing that
# this function provides.
setup_kernels_arches()
{
# If all is set, use dkms status to fill the arrays
if [[ $all && $1 != status ]]; then
local i=0
while read line; do
line=${line#*/}; line=${line#*/};
# (I would leave out the delimiters in the status output
# in the first place.)
kernelver[$i]=${line%/*}
arch[$i]=${line#*/}
i=$(($i + 1))
done < <(module_status_built "$module" "$module_version" | sort -V)
fi
# Set default kernel version and arch, if none set (but only --all isn't set)
if [[ $1 != status ]]; then
if [[ ! $kernelver && ! $all ]]; then
kernelver[0]=$(uname -r)
fi
if [[ ! $arch ]]; then
kernelver_rpm=$(rpm -qf "$install_tree/$kernelver" 2>/dev/null | \
grep -v "not owned by any package" | grep kernel | head -n 1)
if ! arch[0]=$(rpm -q --queryformat "%{ARCH}" "$kernelver_rpm" 2>/dev/null); then
arch[0]=$(uname -m)
if [[ $arch = x86_64 ]] && grep -q Intel /proc/cpuinfo && ls $install_tree/$kernelver/build/configs 2>/dev/null | grep -q "ia32e"; then
arch[0]="ia32e"
fi
fi
fi
if [[ ! $arch ]]; then
die 12 $"Could not determine architecture."
fi
fi
# If only one arch is specified, make it so for all the kernels
if ((${#arch[@]} == 1 && ${#kernelver[@]} > 1)); then
while ((${#arch[@]} < ${#kernelver[@]})); do
arch[${#arch[@]}]=$arch
done
fi
# Set global multi_arch
multi_arch=""
local i=0
for ((i=0; $i < ${#arch[@]}; i++)); do
[[ $arch != ${arch[$i]} ]] && {
multi_arch="true"
break
}
done
}
do_depmod()
{
if [[ $no_depmod ]]; then
return
fi
# $1 = kernel version
if [[ ${current_os} != Linux ]] ; then
return
fi
if [[ ! -f $install_tree/$1/modules.dep ]]; then
# if the corresponding linux image $1 is not installed
# do not create modules.dep
echo "Skipping depmod because '$install_tree/$1/modules.dep' is missing."
return
fi
if [[ -f /boot/System.map-$1 ]]; then
depmod -a "$1" -F "/boot/System.map-$1"
else
depmod -a "$1"
fi
if [[ -f $install_tree/$1/modules.dep && ! -s $install_tree/$1/modules.dep ]]; then
# if modules.dep is empty, we just removed the last kernel module from
# no longer installed kernel $1, so do not leave stale depmod files around
rm -fv $install_tree/$1/modules.{alias,dep,devname,softdep,symbols,weakdep,*.bin}
rmdir --ignore-fail-on-non-empty $install_tree/$1
[[ -d $install_tree/$1 ]] || echo $"removed directory $install_tree/$1"
fi
}
# Grab distro information from os-release.
distro_version()
{
for f in /etc/os-release /usr/lib/os-release; do
if [[ -e $f ]]; then
(
. "$f"
if [[ "$ID" = "ubuntu" ]]; then
# ID_LIKE=debian in ubuntu
echo $ID
elif [[ ${#ID_LIKE[@]} != 0 ]]; then
echo ${ID_LIKE[0]}
else
echo $ID
fi
)
return
fi
done
die 4 $"System is missing os-release file."
}
override_dest_module_location()
{
local orig_location="$1"
[[ ${addon_modules_dir} ]] && echo "/${addon_modules_dir}" && return
if [[ $current_os = GNU/kFreeBSD ]] ; then
# Does not support subdirs, regardless of distribution
echo "" && return
fi
case "$running_distribution" in
fedora* | rhel* | ovm*)
echo "/extra" && return
;;
sles* | suse* | opensuse*)
echo "/updates" && return
;;
debian* | ubuntu*)
echo "/updates/dkms" && return
;;
arch*)
echo "/updates/dkms" && return
;;
*)
;;
esac
echo "$orig_location"
}
# Source a file safely.
# We want to ensure that the .conf file we source does not stomp all over
# parts of the environment we don't want them to. This makes it so that
# it is harder to accidentally corrupt our environment. conf files can
# still deliberately trash the environment by abusing dkms_directive env
# variables or by crafting special values that will make eval do evil things.
safe_source() {
# $1 = file to source
# $@ = environment variables to echo out
local to_source_file="$1"; shift
declare -a -r export_envs=("$@")
local tmpfile=$(mktemp_or_die)
( exec >"$tmpfile"
. "$to_source_file" >/dev/null
# This is really ugly, but a neat hack
# Remember, in bash 2.0 and greater all variables are really arrays.
for _export_env in "${export_envs[@]}"; do
for _i in $(eval echo \${!$_export_env[@]}); do
eval echo '$_export_env[$_i]=\"${'$_export_env'[$_i]}\"'
done
done
# handle DKMS_DIRECTIVE stuff specially.
for directive in $(set | grep ^DKMS_DIRECTIVE | cut -d = -f 2-3); do
directive_name=${directive%%=*}
directive_value=${directive#*=}
echo "$directive_name=\"$directive_value\""
done
)
. "$tmpfile"
rm "$tmpfile"
(( ${#REMAKE_INITRD[@]} )) && deprecated "REMAKE_INITRD ($to_source_file)"
(( ${#MODULES_CONF[@]} )) && deprecated "MODULES_CONF ($to_source_file)"
(( ${#MODULES_CONF_OBSOLETES[@]} )) && deprecated "MODULES_CONF_OBSOLETES ($to_source_file)"
(( ${#MODULES_CONF_ALIAS_TYPE[@]} )) && deprecated "MODULES_CONF_ALIAS_TYPE ($to_source_file)"
(( ${#MODULES_CONF_OBSOLETE_ONLY[@]} )) && deprecated "MODULES_CONF_OBSOLETE_ONLY ($to_source_file)"
}
# Source a dkms.conf file and perform appropriate postprocessing on it.
# Do our best to not repeatedly source the same .conf file -- this can happen
# when chaining module installation functions or autoinstalling.
read_conf()
{
# $1 kernel version (required)
# $2 arch (required)
# $3 dkms.conf location (optional)
local return_value=0
local read_conf_file="$dkms_tree/$module/$module_version/source/dkms.conf"
# Set variables supported in dkms.conf files (eg. $kernelver)
local kernelver="$1"
local arch="$2"
set_kernel_source_dir_and_kconfig "$1"
# Find which conf file to check
[[ $conf ]] && read_conf_file="$conf"
[[ $3 ]] && read_conf_file="$3"
[[ -r $read_conf_file ]] || die 4 $"Could not locate dkms.conf file." \
$"File: $read_conf_file does not exist."
[[ $last_mvka = $module/$module_version/$1/$2 && \
$last_mvka_conf = $(readlink -f $read_conf_file) ]] && return
# Clear variables and arrays
for var in $dkms_conf_variables; do
unset $var
done
# Source in the dkms.conf.
# Allow for user-specified overrides in order of specificity.
local _conf_file
for _conf_file in "$read_conf_file" "/etc/dkms/$module.conf" \
"/etc/dkms/$module-$module_version.conf" "/etc/dkms/$module-$module_version-$1.conf" \
"/etc/dkms/$module-$module_version-$1-$2.conf"; do
[[ -e $_conf_file ]] && safe_source "$_conf_file" $dkms_conf_variables
done
# Source in the directive_array
for directive in "${directive_array[@]}"; do
directive_name=${directive%%=*}
directive_value=${directive#*=}
export $directive_name="$directive_value"
echo $"DIRECTIVE: $directive_name=\"$directive_value\""
done
# Set variables
clean="$CLEAN"
package_name="$PACKAGE_NAME"
package_version="$PACKAGE_VERSION"
post_add="$POST_ADD"
post_build="$POST_BUILD"
post_install="$POST_INSTALL"
post_remove="$POST_REMOVE"
pre_build="$PRE_BUILD"
pre_install="$PRE_INSTALL"
obsolete_by="$OBSOLETE_BY"
# Fail if no PACKAGE_NAME
if [[ ! $package_name ]]; then
echo $"dkms.conf: Error! No 'PACKAGE_NAME' directive specified.">&2
return_value=1
fi
# Fail if no PACKAGE_VERSION
if [[ ! $package_version ]]; then
echo $"dkms.conf: Error! No 'PACKAGE_VERSION' directive specified.">&2
return_value=1
fi
# Set module naming/location arrays
local index array_size=0 s
for s in ${#BUILT_MODULE_NAME[@]} \
${#BUILT_MODULE_LOCATION[@]} \
${#DEST_MODULE_NAME[@]} \
${#DEST_MODULE_LOCATION[@]}; do
((s > array_size)) && array_size=$s
done
for ((index=0; index < array_size; index++)); do
# Set values
built_module_name[$index]=${BUILT_MODULE_NAME[$index]}
built_module_location[$index]=${BUILT_MODULE_LOCATION[$index]}
dest_module_name[$index]=${DEST_MODULE_NAME[$index]}
dest_module_location[$index]=${DEST_MODULE_LOCATION[$index]}
case ${STRIP[$index]} in
[nN]*)
strip[$index]="no"
;;
[yY]*)
strip[$index]="yes"
;;
'')
strip[$index]=${strip[0]:-yes}
;;
esac
# If unset, set by defaults
[[ ! ${built_module_name[$index]} ]] && \
((array_size == 1)) && \
built_module_name[$index]=$PACKAGE_NAME
[[ ! ${dest_module_name[$index]} ]] && \
dest_module_name[$index]=${built_module_name[$index]}
[[ ${built_module_location[$index]} && \
${built_module_location[$index]:(-1)} != / ]] && \
built_module_location[$index]="${built_module_location[$index]}/"
# FAIL if no built_module_name
if [[ ! ${built_module_name[$index]} ]]; then
echo $"dkms.conf: Error! No 'BUILT_MODULE_NAME' directive specified for record #$index." >&2
return_value=1
fi
# FAIL if built_module_name ends in .o or .ko
case ${built_module_name[$index]} in
*.o|*.ko)
echo $"dkms.conf: Error! 'BUILT_MODULE_NAME' directive ends in '.o' or '.ko' in record #$index." >&2
return_value=1
;;
esac
# FAIL if dest_module_name ends in .o or .ko
case ${dest_module_name[$index]} in
*.o|*.ko)
echo $"dkms.conf: Error! 'DEST_MODULE_NAME' directive ends in '.o' or '.ko' in record #$index." >&2
return_value=1
;;
esac
# Override location for specific distributions
dest_module_location[$index]="$(override_dest_module_location ${dest_module_location[$index]})"
# Fail if no DEST_MODULE_LOCATION
if [[ ! ${DEST_MODULE_LOCATION[$index]} ]]; then
echo $"dkms.conf: Error! No 'DEST_MODULE_LOCATION' directive specified for record #$index.">&2
return_value=1
fi
# Fail if bad DEST_MODULE_LOCATION
case ${DEST_MODULE_LOCATION[$index]} in
/kernel*)
;;
/updates*)
;;
/extra*)
;;
*)
echo $"dkms.conf: Error! Directive 'DEST_MODULE_LOCATION' does not begin with">&2
echo $"'/kernel', '/updates', or '/extra' in record #$index.">&2
return_value=1
;;
esac
done
# Warn if no modules are specified
if ((array_size == 0)); then
echo $"dkms.conf: Warning! Zero modules specified." >&2
fi
# Get the correct make command
[[ ${MAKE_MATCH[0]} ]] || make_command="${MAKE[0]}"
for ((index=0; index < ${#MAKE[@]}; index++)); do
[[ ${MAKE[$index]} && ${MAKE_MATCH[$index]} && \
$1 =~ ${MAKE_MATCH[$index]} ]] && \
make_command="${MAKE[$index]}"
done
# Use the generic make and make clean commands if not specified
[[ ! $make_command ]] && make_command="make -C $kernel_source_dir M=$dkms_tree/$module/$module_version/build"
[[ ! $clean ]] && clean="make -C $kernel_source_dir M=$dkms_tree/$module/$module_version/build clean"
# Check if clang was used to compile or lld was used to link the kernel.
if [[ -e $kernel_source_dir/vmlinux ]]; then
if readelf -p .comment $kernel_source_dir/vmlinux | grep -q clang; then
make_command="${make_command} CC=clang"
fi
if readelf -p .comment $kernel_source_dir/vmlinux | grep -q LLD; then
make_command="${make_command} LD=ld.lld"
fi
elif [[ -e "${kernel_config}" ]]; then
if grep -q CONFIG_CC_IS_CLANG=y "${kernel_config}"; then
make_command="${make_command} CC=clang"
fi
if grep -q CONFIG_LD_IS_LLD=y "${kernel_config}"; then
make_command="${make_command} LD=ld.lld"
fi
fi
# Set patch_array (including kernel specific patches)
count=0
for ((index=0; index < ${#PATCH[@]}; index++)); do
if [[ ${PATCH[$index]} && (! ${PATCH_MATCH[$index]} || $1 =~ ${PATCH_MATCH[$index]}) ]]; then
patch_array[$count]="${PATCH[$index]}"
count=$(($count+1))
fi
done
# Set build_exclude
[[ $BUILD_EXCLUSIVE_KERNEL && ! $1 =~ $BUILD_EXCLUSIVE_KERNEL ]] && build_exclude="yes"
[[ $BUILD_EXCLUSIVE_KERNEL_MIN && "$(VER "$1")" < "$(VER "$BUILD_EXCLUSIVE_KERNEL_MIN")" ]] && build_exclude="yes"
[[ $BUILD_EXCLUSIVE_KERNEL_MAX && "$(VER "$1")" > "$(VER "$BUILD_EXCLUSIVE_KERNEL_MAX")" ]] && build_exclude="yes"
[[ $BUILD_EXCLUSIVE_ARCH && ! $2 =~ $BUILD_EXCLUSIVE_ARCH ]] && build_exclude="yes"
if [[ $BUILD_EXCLUSIVE_CONFIG && -e "${kernel_config}" ]]; then
local kconf
for kconf in $BUILD_EXCLUSIVE_CONFIG ; do
case "$kconf" in
!*) grep -q "^${kconf#!}=[ym]" "${kernel_config}" && build_exclude="yes" ;;
*) grep -q "^${kconf}=[ym]" "${kernel_config}" || build_exclude="yes" ;;
esac
done
fi
# Set clean
[[ $clean ]] || clean="make clean"
((return_value == 0)) && last_mvka="$module/$module_version/$1/$2" && last_mvka_conf="$(readlink -f "$read_conf_file")"
return $return_value
}
# Source specified variables from dkms framework configuration files.
read_framework_conf() {
for i in /etc/dkms/framework.conf /etc/dkms/framework.conf.d/*.conf; do
[[ -e "$i" ]] && safe_source "$i" "$@"
done
}
# Little helper function for parsing the output of modinfo.
get_module_verinfo(){
local ver
local srcver
local checksum
local vals=
while read -a vals; do
case "${vals[0]}" in
version:)
ver="${vals[1]}"
checksum="${vals[2]}"
;;
srcversion:)
srcver="${vals[1]}"
;;
esac
done < <(modinfo "$1")
echo -E "${ver}"
# Use obsolete checksum info if srcversion is not available
echo -E "${srcver:-$checksum}"
}
# Compare two modules' version
# Output:
# "==": They are the same version and the same srcversion
# "=": They are the same version, but not the same srcversion
# ">": 1st one is newer than 2nd one
# "<": 1st one is older than 2nd one
# "?": Cannot determine
# Returns 0 if same version, otherwise 1
compare_module_version()
{
readarray -t ver1 <<< "$(get_module_verinfo "$1")"
readarray -t ver2 <<< "$(get_module_verinfo "$2")"
if [[ "${ver1[0]}" = "${ver2[0]}" ]]; then
if [[ "${ver1[1]}" = "${ver2[1]}" ]]; then
echo "=="
else
echo "="
fi
return 0
elif [[ ! "$ver1" ]] || [[ ! "$ver2" ]]; then
echo "?"
elif [[ "$(VER "${ver1[0]}")" > "$(VER "${ver2[0]}")" ]]; then
echo ">"
else
echo "<"
fi
return 1
}
# Perform some module version sanity checking whenever we are installing
# modules.
check_version_sanity()
{
# $1 = kernel_version
# $2 = arch
# $3 = obs by kernel version
# $4 = dest_module_name
local lib_tree="$install_tree/$1" res=
local i=0
if [[ -n $3 ]]; then
# Magic split into array syntax saves trivial awk and cut calls.
local -a obs=(${3//-/ })
local -a my=(${1//-/ })
local obsolete=0
if [[ ${obs} && ${my} ]]; then
if [[ $(VER ${obs}) == $(VER ${my}) && ! $force ]]; then
# They get obsoleted possibly in this kernel release
if [[ ! ${obs[1]} ]]; then
# They were obsoleted in this upstream kernel
obsolete=1
elif [[ $(VER ${my[1]}) > $(VER ${obs[1]}) ]]; then
# They were obsoleted in an earlier ABI bump of the kernel
obsolete=1
elif [[ $(VER ${my[1]}) = $(VER ${obs[1]}) ]]; then
# They were obsoleted in this ABI bump of the kernel
obsolete=1
fi
elif [[ $(VER ${my}) > $(VER ${obs}) && ! $force ]]; then
# They were obsoleted in an earlier kernel release
obsolete=1
fi
fi
if ((obsolete == 1)); then
echo $"" >&2
echo $"Module has been obsoleted due to being included" >&2
echo $"in kernel $3. We will avoid installing" >&2
echo $"for future kernels above $3." >&2
echo $"You may override by specifying --force." >&2
return 1
fi
fi
set_module_suffix "$1"
read -a kernels_module < <(find_module "$lib_tree" "${4}")
[[ -z $kernels_module ]] && return 0
if [[ "$force_version_override" == "true" ]]; then
# Skip the following version checking code.
return 0
fi
if [[ ${kernels_module[1]} ]]; then
warn $"Warning! Cannot do version sanity checking because multiple ${4}$module_suffix modules were found in kernel $1."
return 0
fi
local dkms_module=$(compressed_or_uncompressed "$dkms_tree/$module/$module_version/$1/$2/module/" "${4}")
local cmp_res="$(compare_module_version "${kernels_module}" "${dkms_module}")"
if [[ "${cmp_res}" = ">" ]]; then
if [[ ! "$force" ]]; then
error $"Module version $(get_module_verinfo "${dkms_module}" | head -n 1) for $4${module_suffix}" \
$"is not newer than what is already found in kernel $1 ($(get_module_verinfo "${kernels_module}" | head -n 1))." \
$"You may override by specifying --force."
return 1
fi
elif [[ "${cmp_res}" = "==" ]]; then
if [[ ! "$force" ]]; then
# if the module has neither version nor srcversion/checksum, check the binary files instead
local verinfo="$(get_module_verinfo "${dkms_module}")"
if [[ "$(echo "$verinfo" | tr -d '[:space:]')" ]] || diff "${kernels_module}" "${dkms_module}" &>/dev/null; then
echo $"Module ${kernels_module} already installed at version $(echo "$verinfo" | head -n 1), override by specifying --force" >&2
return 1
fi
fi
fi
return 0
}
check_module_args() {
[[ $module && $module_version ]] && return
die 1 $"Arguments <module> and <module-version> are not specified." \
$"Usage: $1 <module>/<module-version> or" \
$" $1 -m <module>/<module-version> or" \
$" $1 -m <module> -v <module-version>"
}
read_conf_or_die() {
read_conf "$@" && return
die 8 $"Bad conf file."\
$"File: ${3:-$conf} does not represent a valid dkms.conf file."
}
run_build_script() {
# $1 = script type
# $2 = script to run
local script_type run
[[ $2 ]] || return 0
case "$1" in
pre_build|post_build)
script_type='build'
;;
*)
script_type='source'
;;
esac
run="$dkms_tree/$module/$module_version/$script_type/$2"
if [[ -x ${run%% *} ]]; then
echo $""
echo $"Running the $1 script:"
(
cd "$dkms_tree/$module/$module_version/$script_type/"
exec $run
)
else
echo $""
warn $"The $1 script is not executable."
fi
}
# Register a DKMS-ified source tree with DKMS.
# This function is smart enough to register the module if we
# passed a source tree or a tarball instead of relying on the source tree
# being unpacked into /usr/src/$module-$module_version.
add_module()
{
# If $archive is set and $module and $module_version are not,
# try loading the tarball passed first.
if [[ $archive_location && ! $module && ! $module_version ]]; then
load_tarball
elif [[ $try_source_tree && ! $module && ! $module_version ]]; then
add_source_tree "$try_source_tree"
fi
# Check that we have all the arguments
check_module_args add
# Do stuff for --rpm_safe_upgrade
if [[ $rpm_safe_upgrade ]]; then
local pppid=$(awk '/PPid:/ {print $2}' /proc/$PPID/status)
local lock_name=$(mktemp_or_die $tmp_location/dkms_rpm_safe_upgrade_lock.$pppid.XXXXXX)
echo "$module-$module_version" >> $lock_name
ps -o lstart --no-headers -p $pppid 2>/dev/null >> $lock_name
fi
# Check that this module-version hasn't already been added
if is_module_added "$module" "$module_version"; then
die 3 $"DKMS tree already contains: $module-$module_version" \
$"You cannot add the same module/version combo more than once."
fi
[[ $conf ]] || conf="$source_tree/$module-$module_version/dkms.conf"
# Check that /usr/src/$module-$module_version exists
if ! [[ -d $source_tree/$module-$module_version ]]; then
die 2 $"Could not find module source directory." \
$"Directory: $source_tree/$module-$module_version does not exist."
fi
# Check the conf file for sanity
read_conf_or_die "$kernelver" "$arch" "$conf"
# Create the necessary dkms tree structure
echo $"Creating symlink $dkms_tree/$module/$module_version/source -> $source_tree/$module-$module_version"
mkdir -p "$dkms_tree/$module/$module_version/build"
ln -s "$source_tree/$module-$module_version" "$dkms_tree/$module/$module_version/source"
# Run the post_add script
run_build_script post_add "$post_add"
}
# Prepare a kernel source or include tree for compiling a module.
# Most modern-ish distros do not require this function at all,
# so it will be removed in a future release.
prepare_kernel()
{
# $1 = kernel version to prepare
# $2 = arch to prepare
set_kernel_source_dir_and_kconfig "$1"
# Check that kernel-source exists
_check_kernel_dir "$1" || {
die 1 $"Your kernel headers for kernel $1 cannot be found at $install_tree/$1/build or $install_tree/$1/source." \
$"Please install the linux-headers-$1 package or use the --kernelsourcedir option to tell DKMS where it's located."
}
}
prepare_mok()
{
if [[ -z "${mok_signing_key}" ]]; then
# No custom key specified, use the default key created by update-secureboot-policy for Ubuntu
# Debian's update-secureboot-policy has no --new-key option
case "$running_distribution" in
ubuntu* )
mok_signing_key="/var/lib/shim-signed/mok/MOK.priv"
mok_certificate="/var/lib/shim-signed/mok/MOK.der"
if [[ ! -f ${mok_signing_key} || ! -f ${mok_certificate} ]]; then
if [[ ! -x "$(command -v update-secureboot-policy)" ]]; then
echo "Binary update-secureboot-policy not found, modules won't be signed"
return
fi
# update-secureboot-policy won't create new key if $mok_certificate exists
if [[ -f ${mok_certificate} ]]; then
rm -f "${mok_certificate}"
fi
echo "Certificate or key are missing, generating them using update-secureboot-policy..."
SHIM_NOTRIGGER=y update-secureboot-policy --new-key &>/dev/null
update-secureboot-policy --enroll-key
fi
;;
esac
fi
if [[ ! ${mok_signing_key} ]]; then
mok_signing_key="/var/lib/dkms/mok.key"
fi
echo "Signing key: $mok_signing_key"
if [[ ! ${mok_certificate} ]]; then
mok_certificate="/var/lib/dkms/mok.pub"
fi
echo "Public certificate (MOK): $mok_certificate"
# scripts/sign-file.c in kernel source also supports using "pkcs11:..." as private key
if [[ $mok_signing_key != "pkcs11:"* ]] && ( [[ ! -f $mok_signing_key || ! -f $mok_certificate ]] ); then
echo "Certificate or key are missing, generating self signed certificate for MOK..."
if ! command -v openssl >/dev/null; then
echo "openssl not found, can't generate key and certificate."
return
fi
openssl req -new -x509 -nodes -days 36500 -subj "/CN=DKMS module signing key" \
-newkey rsa:2048 -keyout "$mok_signing_key" \
-outform DER -out "$mok_certificate" > /dev/null 2>&1
if [[ ! -f ${mok_signing_key} ]]; then
echo "Key file ${mok_signing_key} not found and can't be generated, modules won't be signed"
return
fi
fi
if [[ ! -f ${mok_certificate} ]]; then
echo "Certificate file ${mok_certificate} not found and can't be generated, modules won't be signed"
return
fi
}
prepare_signing()
{
do_signing=0
if [[ ! -f ${kernel_config} ]]; then
echo "Kernel config ${kernel_config} not found, modules won't be signed"
return
fi
if ! grep -q "^CONFIG_MODULE_SIG_HASH=" "${kernel_config}"; then
echo "The kernel is built without module signing facility, modules won't be signed"
return
fi
sign_hash=$(grep "^CONFIG_MODULE_SIG_HASH=" "${kernel_config}" | cut -f2 -d= | sed 's/"//g')
if [[ ! ${sign_file} ]]; then
case "$running_distribution" in