-
Notifications
You must be signed in to change notification settings - Fork 153
/
dkms.in
2976 lines (2656 loc) · 103 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
#
# Exit status values and error messages used by dkms:
# (as parameters to die, diewarn, report_build_problem, exit)
#
# 0: global: SUCCESS
# 0: remove_module(): Remove cancelled because --rpm_safe_upgrade scenario detected.
# 1: ldtarball: You must be root to use this command with the --force option.
# 1: check_root(): You must be root to use this command.
# 1: check_rw_dkms_tree(): No write access to DKMS tree at ...
# 1: mktemp_or_die(): Unable to make temporary file/directory.
# 1: check_module_args(): Arguments <module> and <module-version> are not specified.
# 1: prepare_kernel(): Your kernel headers for kernel ... cannot be found ...
# 1: run_match(): Invalid number of parameters passed.
# 1: global: If more than one arch is specified on the command line, then there "must be an equal number of kernel versions also specified (1:1 relationship).
# 2: global: Unknown option: ...
# 2: global: You cannot specify a kernel version and also specify --all on the command line.
# 2: run_match(): The templatekernel and the specified kernel version are the same.
# 2: add_module(): Could not find module source directory.
# 2: load_tarball(): ... does not exist.
# 3: global: You cannot specify an arch and also specify --all on the command line.
# 3: load_tarball(): Tarball does not appear to be a correctly formed DKMS archive. No dkms.conf found within it.
# 3: run_match(): The module: $module is not located in the DKMS tree.
# 3: add_module(): You cannot add the same module/version combo more than once.
# 3: prepare_build(): This module/version has already been built on: ...
# 3: module_is_added_or_die(): The module/version combo: ... is not located in the DKMS tree.
# 4: global: Cannot specify more than one action.
# 4: distro_version(): System is missing os-release file.
# 4: read_conf(): Could not locate dkms.conf file.
# 4: have_one_kernel(): The action ... does not support multiple kernel version parameters on the command line.
# 4: module_is_broken_and_die(): ... is broken!
# 5: check_all_is_banned(): The action ... does not support the --all parameter.
# 5: prepare_build(): Patch ... as specified in dkms.conf contains '..' path component.
# 5: prepare_build(): Patch ... as specified in dkms.conf cannot be found in ...
# 5: install: This module/version combo is already installed for kernel ...
# 6: prepare_build(): Application of patch ... failed.
# 6: install: You cannot install a module onto a non-existant kernel.
# 6: install: Installation aborted.
# 6: install: Install Failed (depmod problems). Module rolled back to built state.
# 6: make_tarball(): Modules must already be in the built state before using mktarball.
# 6: make_tarball(): Failed to make tarball.
# 7: load_tarball(): No valid dkms.conf in dkms_source_tree or dkms_binaries_only.
# 7: actual_build(): Build of ... failed for: ...
# 8: load_tarball(): ... is already added!
# 8: global: You have specified both --binaries-only and --source-only.
# 8: read_conf_or_die(): Bad conf file.
# 8: prepare_build(): The directory $source_dir does not appear to have module source located within it.
# 9: add_source_tree(): ... must contain a dkms.conf file!
# 9: make_tarball(): Missing write permissions for ...
# 9: load_tarball(): Unable to install ... using rpm.
# 10: add_source_tree(): Malformed dkms.conf file. Cannot load source tree.
# 10: build: Bad return status for module build on kernel: ...
# 11: autoinstall: One or more modules failed to install during autoinstall.
# 12: setup_kernels_arches(): Could not determine architecture.
# 13: build: Aborting build of module ... for kernel ... due to missing BUILD_DEPENDS: ...
# 14: kernel_prerm: dkms kernel_prerm for kernel ... failed for module(s) ...
# 77: skipped due to BUILD_EXCLUSIVE
# 101: install: pre_install failed, aborting install.
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
compress_gzip_opts compress_xz_opts compress_zstd_opts"
# 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
DIR="$install_tree/$KVER/build"
echo $DIR
else
echo $kernel_source_dir
fi
}
_check_kernel_dir() {
DIR=$(_get_kernel_dir $1)
test -e $DIR/include
return $?
}
on_exit()
{
local exitcode_on_exit=$? # must be one line or $? captures rc from `local`
local j
j="$(jobs -p)"
[[ -n "$j" ]] && kill $j 2>/dev/null
[[ $make_tarball_rm_temp_dir_name ]] && eval "$make_tarball_rm_temp_dir_name"
[[ $load_tarball_rm_temp_dir_name ]] && eval "$load_tarball_rm_temp_dir_name"
exit $exitcode_on_exit
}
unset make_tarball_rm_temp_dir_name load_tarball_rm_temp_dir_name
trap on_exit EXIT
# 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 (including stderr) to this file
# $4 = background, if you want print . each 3 seconds while command runs
local cmd
local cmd_description
local cmd_output_file
local cmd_mode
local exitval
local progresspid
cmd="$1"
cmd_description="$2"
cmd_output_file="$3"
cmd_mode="$4"
exitval=0
[[ $verbose ]] && echo -e "$cmd" || echo -en "$cmd_description..."
if [[ $cmd_mode == background && ! $verbose && $package_name != dkms*_test ]]; then
while true ; do
sleep 3
echo -n "."
done &
progresspid=$!
fi
if [[ -n "$cmd_output_file" ]]; then
( eval "$cmd" ) >> "$cmd_output_file" 2>&1
exitval=$?
elif [[ -z "$cmd_output_file" && $cmd_mode == background && ! $verbose ]]; then
( eval "$cmd" ) >/dev/null 2>&1
exitval=$?
else
( eval "$cmd" )
exitval=$?
fi
if [ -n "$progresspid" ]; then
kill "$progresspid" >/dev/null 2>&1
wait "$progresspid" 2>/dev/null
fi
if (( exitval > 0 )); then
echo -en "(bad exit status: $exitval)"
# Print the failing command without the clunky redirection
[[ ! $verbose ]] && echo -e "\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 " kernel_postinst | kernel_prerm }"
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-include/config/auto.conf-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] [--help]"
}
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
local test2
test1="$1/$2$module_uncompressed_suffix"
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" -type f -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. Do not use modules.dep
# as it might not be available (in Red Hat removal is triggered with kernel-core but modules.dep
# is contained in kernel-modules-core).
set_module_suffix()
{
# $1 = the kernel to base the module_suffix on
kernel_test="${1:-$(uname -r)}"
module_uncompressed_suffix=".ko"
find $install_tree/$kernel_test/ -name "*.ko.gz" | grep -q . && module_compressed_suffix=".gz"
find $install_tree/$kernel_test/ -name "*.ko.xz" | grep -q . && module_compressed_suffix=".xz"
find $install_tree/$kernel_test/ -name "*.ko.zst" | grep -q . && 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
if [[ -e "${kernel_source_dir}/include/config/auto.conf" ]]; then
kernel_config="${kernel_source_dir}/include/config/auto.conf"
else
kernel_config="${kernel_source_dir}/.config"
fi
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
[[ $1 =~ kernel_(postinst|prerm) ]] && die 4 "The action $1 requires exactly one kernel version parameter on the command line."
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
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
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
orig_location="$1"
[[ ${addon_modules_dir} ]] && echo "/${addon_modules_dir}" && return
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
to_source_file="$1"; shift
declare -a -r export_envs=("$@")
local tmpfile
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
local read_conf_file
return_value=0
read_conf_file="$dkms_tree/$module/$module_version/source/dkms.conf"
# Set variables supported in dkms.conf files (eg. $kernelver)
local kernelver
local arch
kernelver="$1"
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
local array_size
local s
array_size=0
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 2>&1 | grep -q clang; then
make_command="${make_command} LLVM=1"
fi
elif [[ -e "${kernel_config}" ]]; then
if grep -q CONFIG_CC_IS_CLANG=y "${kernel_config}"; then
make_command="${make_command} LLVM=1"
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
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
local res
local i
lib_tree="$install_tree/$1"
res=
i=0
if [[ -n $3 ]]; then
# Magic split into array syntax saves trivial awk and cut calls.
local -a obs
local -a my
local obsolete
obs=(${3//-/ })
my=(${1//-/ })
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 in kernel $3." >&2
echo "We will avoid installing 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 "Cannot do version sanity checking because multiple ${4}$module_suffix modules were found in kernel $1."
return 0
fi
local dkms_module
dkms_module=$(compressed_or_uncompressed "$dkms_tree/$module/$module_version/$1/$2/module/" "${4}")
local cmp_res
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
verinfo="$(get_module_verinfo "${dkms_module}")"
if [[ "$(echo "$verinfo" | tr -d '[:space:]')" ]] || diff "${kernels_module}" "${dkms_module}" &>/dev/null; then
verinfo=$(echo "$verinfo" | head -n 1)
if [[ $verinfo ]]; then
echo "Module ${kernels_module} already installed at version ${verinfo}, override by specifying --force" >&2
else
echo "Module ${kernels_module} already installed (unversioned module), override by specifying --force" >&2
fi
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
local 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
local lock_name
pppid=$(awk '/PPid:/ {print $2}' /proc/$PPID/status)
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