-
Notifications
You must be signed in to change notification settings - Fork 33
/
detect.py
1547 lines (1259 loc) · 55.7 KB
/
detect.py
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
'''Hardware and driver package detection functionality for Ubuntu systems.'''
# (C) 2012 Canonical Ltd.
# Author: Martin Pitt <martin.pitt@ubuntu.com>
#
# 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.
import os
import logging
import fnmatch
import subprocess
import functools
import re
import json
import apt_pkg
from UbuntuDrivers import kerneldetection
system_architecture = ''
lookup_cache = {}
custom_supported_gpus_json = '/etc/custom_supported_gpus.json'
class NvidiaPkgNameInfo(object):
'''Class to process NVIDIA package names'''
def __init__(self, pkg_name):
self._pkg_name = pkg_name
self._obsolete_name_scheme = False
self._server = False
self._open = False
self._major_ver = -1
self._flavour = ''
self.is_valid = False
self._process_name(self._pkg_name)
def _process_name(self, name):
if 'nvidia' not in name:
logging.debug('NvidiaPkgNameInfo: %s is not an NVIDIA package. Skipping', name)
return
pattern = re.compile('nvidia-([0-9]+)')
match = pattern.match(name)
# Obsolete naming such as nvidia-340
if match:
self._obsolete_name_scheme = True
self._major_ver = int(match.group(1))
self._flavour = self._major_ver
self.is_valid = True
return
# Recent naming such as nvidia-driver-525
pattern = re.compile('nvidia-driver-([0-9]+)(.*)')
match = pattern.match(name)
if match:
self._server = match.group(0).find('-server') != -1
self._open = match.group(0).find('-open') != -1
self._flavour = '%s%s%s' % (match.group(1),
'-server' if self._server else '',
'-open' if self._open else '')
self.is_valid = True
def has_obsolete_name_scheme(self):
return self._obsolete_name_scheme
def is_server(self):
return self._server
def is_open(self):
return self._open
def get_major_version(self):
return self._major_ver
def get_flavour(self):
return self._flavour
def get_apt_arch():
'''Cache system architecture'''
global system_architecture
if not system_architecture:
system_architecture = apt_pkg.get_architectures()[0]
return system_architecture
def system_modaliases(sys_path=None):
'''Get modaliases present in the system.
This ignores devices whose drivers are statically built into the kernel, as
you cannot replace them with other driver packages anyway.
Return a modalias → sysfs path map.
'''
aliases = {}
devices = sys_path and '%s/devices' % (sys_path) or '/sys/devices'
for path, dirs, files in os.walk(devices):
modalias = None
# most devices have modalias files
if 'modalias' in files:
try:
with open(os.path.join(path, 'modalias')) as f:
modalias = f.read().strip()
except IOError as e:
logging.debug('system_modaliases(): Cannot read %s/modalias: %s',
path, e)
continue
# devices on SSB bus only mention the modalias in the uevent file (as
# of 2.6.24)
elif 'ssb' in path and 'uevent' in files:
with open(os.path.join(path, 'uevent')) as fd:
for line in fd:
if line.startswith('MODALIAS='):
modalias = line.split('=', 1)[1].strip()
break
if not modalias:
continue
# ignore drivers which are statically built into the kernel
driverlink = os.path.join(path, 'driver')
modlink = os.path.join(driverlink, 'module')
if os.path.islink(driverlink) and not os.path.islink(modlink):
# logging.debug('system_modaliases(): ignoring device %s which has no module (built into kernel)', path)
continue
aliases[modalias] = path
return aliases
def _check_video_abi_compat(apt_cache, package):
xorg_video_abi = None
if package.name.startswith('nvidia-driver-'):
xorg_driver_name = package.name.replace('nvidia-driver-', 'xserver-xorg-video-nvidia-')
try:
package = apt_cache[xorg_driver_name]
except KeyError:
logging.debug('Cannot find %s package in the cache. Cannot check ABI' % (xorg_driver_name))
return True
depcache = apt_pkg.DepCache(apt_cache)
candidate = depcache.get_candidate_ver(package)
needs_video_abi = False
try:
for dep_list in candidate.depends_list_str.get('Depends'):
for dep_name, dep_ver, dep_op in dep_list:
if dep_name.startswith('xorg-video-abi-'):
needs_video_abi = True
break
except (KeyError, TypeError):
logging.debug('The %s package seems to have no dependencies. Skipping ABI check' % (package))
needs_video_abi = False
if not needs_video_abi:
logging.debug('Skipping check for %s since it does not depend on video abi' % package.name)
return True
# determine current X.org video driver ABI
try:
xorg_core = apt_cache['xserver-xorg-core']
except KeyError:
logging.debug('xserver-xorg-core not available, cannot check ABI')
return True
candidate = depcache.get_candidate_ver(xorg_core)
for provides_name, provides_ver, p_version in candidate.provides_list:
if provides_name.startswith('xorg-video-abi-'):
xorg_video_abi = provides_name
if xorg_video_abi:
abi_pkg = apt_cache[xorg_video_abi]
for dep in abi_pkg.rev_depends_list:
if dep.parent_pkg.name == package.name:
return True
logging.debug('Driver package %s is incompatible with current X.org server ABI %s',
package.name, xorg_video_abi)
logging.debug('%s is not in %s' % (package, [x.parent_pkg for x in abi_pkg.rev_depends_list]))
return False
return True
def _apt_cache_modalias_map(apt_cache):
'''Build a modalias map from an apt_pkg.Cache object.
This filters out uninstallable video drivers (i. e. which depend on a video
ABI that xserver-xorg-core does not provide).
Return a map bus -> modalias -> [package, ...], where "bus" is the prefix of
the modalias up to the first ':' (e. g. "pci" or "usb").
'''
depcache = apt_pkg.DepCache(apt_cache)
records = apt_pkg.PackageRecords(apt_cache)
result = {}
for package in apt_cache.packages:
# skip packages without a modalias field
try:
candidate = depcache.get_candidate_ver(package)
records.lookup(candidate.file_list[0])
m = records['Modaliases']
if not m:
continue
except (KeyError, AttributeError, UnicodeDecodeError):
continue
# skip foreign architectures, we usually only want native
# driver packages
if (package.architecture not in ('all', get_apt_arch())):
continue
if not _check_video_abi_compat(apt_cache, package):
continue
try:
for part in m.split(')'):
part = part.strip(', ')
if not part:
continue
module, lst = part.split('(')
for alias in lst.split(','):
alias = alias.strip()
bus = alias.split(':', 1)[0]
result.setdefault(bus, {}).setdefault(alias, set()).add(package.name)
except ValueError:
logging.error('Package %s has invalid modalias header: %s' % (
package.name, m))
result2 = {}
for bus, alias_map in result.items():
pat = re.compile(
'|'.join([fnmatch.translate(pat) for pat in alias_map.keys()]),
re.IGNORECASE)
result2[bus] = (pat, alias_map)
return result2
def path_get_custom_supported_gpus():
return custom_supported_gpus_json
def package_get_nv_allowing_driver(did):
'''Get nvidia allowing driver for specific devices.
did: 0x1234
Return the situable nvidia driver version for it.
'''
path = path_get_custom_supported_gpus()
version = None
try:
with open(path, "r") as stream:
try:
gpus = list(json.load(stream)['chips'])
for gpu in gpus:
if gpu['devid'] == did:
version = gpu['branch'].split('.')[0]
logging.info("Found a specific nv driver version %s for %s(%s)" %
(version, gpu['name'], did))
break
except Exception:
logging.debug('package_get_nv_allowing_driver(): unexpected json detected.')
pass
except Exception:
logging.debug('package_get_nv_allowing_driver(): unable to read %s' % path)
pass
return version
def packages_for_modalias(apt_cache, modalias):
'''Search packages which match the given modalias.
Return a list of apt.Package objects.
'''
pkgs = set()
apt_cache_hash = hash(package.get_fullname() for package in apt_cache.packages)
try:
cache_map = packages_for_modalias.cache_maps[apt_cache_hash]
except KeyError:
cache_map = _apt_cache_modalias_map(apt_cache)
packages_for_modalias.cache_maps[apt_cache_hash] = cache_map
pat, bus_map = cache_map.get(modalias.split(':', 1)[0], (None, {}))
vid, did = _get_vendor_model_from_alias(modalias)
nvamd = None
found = 0
if vid == "10DE":
nvamd = package_get_nv_allowing_driver("0x" + did)
nvamdn = "nvidia-driver-%s" % nvamd
nvamda = "pci:v000010DEd0000%s*" % did
for p in apt_cache.packages:
if p.get_fullname().split(':')[0] == nvamdn:
bus_map[nvamda] = set([nvamdn])
found = 1
if nvamd is not None and not found:
logging.debug('%s is not in the package pool.' % nvamdn)
if not found:
if pat is None or not pat.match(modalias):
return []
for alias in bus_map:
if fnmatch.fnmatchcase(modalias.lower(), alias.lower()):
for p in bus_map[alias]:
pkgs.add(p)
return [apt_cache[p] for p in pkgs]
packages_for_modalias.cache_maps = {}
def _is_package_free(apt_cache, pkg):
depcache = apt_pkg.DepCache(apt_cache)
candidate = depcache.get_candidate_ver(pkg)
assert candidate is not None
# it would be better to check the actual license, as we do not have
# the component for third-party packages; but this is the best we can do
# at the moment
#
# We can assume the NVIDIA packages to be non-free
if pkg.name.startswith('nvidia'):
return False
records = apt_pkg.PackageRecords(apt_cache)
records.lookup(candidate.file_list[0])
for pfile, _ in pkg.version_list[0].file_list:
if not pfile.component:
# This is probably from the test suite
try:
component = records['Component']
return (component not in ('restricted', 'multiverse'))
except KeyError:
return False
else:
if pfile.component in ('restricted', 'multiverse'):
return False
return True
def _is_package_from_distro(apt_cache, pkg):
depcache = apt_pkg.DepCache(apt_cache)
candidate = depcache.get_candidate_ver(pkg)
if candidate is None:
return False
try:
return (candidate.file_list[0][0].origin == 'Ubuntu')
except KeyError:
return False
def _pkg_get_module(apt_cache, pkg):
'''Determine module name from apt Package object'''
depcache = apt_pkg.DepCache(apt_cache)
candidate = depcache.get_candidate_ver(pkg)
records = apt_pkg.PackageRecords(apt_cache)
records.lookup(candidate.file_list[0])
try:
m = records['Modaliases']
except (KeyError, AttributeError):
logging.debug('_pkg_get_module %s: package has no Modaliases header, cannot determine module', pkg.name)
return None
paren = m.find('(')
if paren <= 0:
logging.debug('_pkg_get_module %s: package has invalid Modaliases header, cannot determine module', pkg.name)
return None
module = m[:paren]
return module
def _pkg_get_support(apt_cache, pkg):
'''Determine support level from apt Package object'''
depcache = apt_pkg.DepCache(apt_cache)
candidate = depcache.get_candidate_ver(pkg)
records = apt_pkg.PackageRecords(apt_cache)
records.lookup(candidate.file_list[0])
try:
support = records['Support']
except (KeyError, AttributeError):
logging.debug('_pkg_get_support %s: package has no Support header, cannot determine support level', pkg.name)
return None
if support not in ('PB', 'NFB', 'LTSB', 'Legacy'):
logging.debug('_pkg_get_support %s: package has invalid Support %s'
'header, cannot determine support level', pkg.name, support)
return None
return support
def _is_nv_allowing_runtimepm_supported(alias, ver):
'''alias: e.g. pci:v000010DEd000024BAsv0000103Csd000089C6bc03sc00i00'''
result = re.search('pci:v0000(.*)d0000(.*)sv(.*)', alias)
vid = result.group(1)
did = result.group(2)
if vid != "10DE":
return False
did = "0x%s" % did
path = path_get_custom_supported_gpus()
try:
with open(path, "r") as stream:
try:
gpus = list(json.load(stream)['chips'])
for gpu in gpus:
if gpu['devid'] == did and 'runtimepm' in gpu['features']:
if gpu['branch'].split('.')[0] != ver:
logging.debug('Candidate version does not match %s != %s' %
(gpu['branch'].split('.')[0], ver))
return False
logging.info("Found runtimepm supports on %s." % did)
return True
except Exception:
logging.debug('_is_nv_allowing_runtimepm_supported(): unexpected json detected')
pass
except Exception:
logging.debug('_is_nv_allowing_runtimepm_supported(): unable to read %s' % path)
pass
return False
def _is_runtimepm_supported(apt_cache, pkg, alias):
'''Check if the package supports runtimepm for the given modalias'''
try:
depcache = apt_pkg.DepCache(apt_cache)
candidate = depcache.get_candidate_ver(pkg)
records = apt_pkg.PackageRecords(apt_cache)
records.lookup(candidate.file_list[0])
section = apt_pkg.TagSection(records.record)
ver = candidate.ver_str.split('.')[0]
m = section['PmAliases']
except (KeyError, AttributeError, UnicodeDecodeError):
return False
else:
if m.find('nvidia(') != 0:
return False
n = m[m.find('(')+1: m.find(')')]
modaliases = n.split(', ')
if _is_nv_allowing_runtimepm_supported(alias, ver):
return True
return any(fnmatch.fnmatch(alias.lower(), regex.lower()) for regex in modaliases)
def is_wayland_session():
'''Check if the current session in on Wayland'''
return os.environ.get('WAYLAND_DISPLAY') is not None
def _is_manual_install(apt_cache, pkg):
'''Determine if the kernel module from an apt.Package is manually installed.'''
if pkg.current_ver:
return False
# special case, as our packages suffix the kmod with _version
if pkg.name.startswith('nvidia'):
module = 'nvidia'
elif pkg.name.startswith('fglrx'):
module = 'fglrx'
else:
module = _pkg_get_module(apt_cache, pkg)
if not module:
return False
try:
modinfo = subprocess.Popen(['modinfo', module], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
modinfo.communicate()
except (OSError, FileNotFoundError, subprocess.CalledProcessError) as e:
logging.debug('_is_manual_install failed: %s', str(e))
return False
else:
if modinfo.returncode == 0:
logging.debug('_is_manual_install %s: builds module %s which is available, manual install',
pkg.name, module)
return True
logging.debug('_is_manual_install %s: builds module %s which is not available, no manual install',
pkg.name, module)
return False
def _get_db_name(syspath, alias):
'''Return (vendor, model) names for given device.
Values are None if unknown.
'''
try:
out = subprocess.check_output(['systemd-hwdb', 'query', alias],
universal_newlines=True)
except (OSError, subprocess.CalledProcessError) as e:
logging.debug('_get_db_name(%s, %s): systemd-hwdb failed: %s', syspath, alias, str(e))
return (None, None)
logging.debug('_get_db_name: output\n%s\n', out)
vendor = None
model = None
for line in out.splitlines():
(k, v) = line.split('=', 1)
if '_VENDOR' in k:
vendor = v
if '_MODEL' in k:
model = v
logging.debug('_get_db_name(%s, %s): vendor "%s", model "%s"', syspath,
alias, vendor, model)
return (vendor, model)
def set_nvidia_kms(value):
'''Set KMS on or off for NVIDIA'''
nvidia_kms_file = '/lib/modprobe.d/nvidia-kms.conf'
kms_text = '''# This file was generated by ubuntu-drivers
# Set value to 1 to enable modesetting, 0 to disable it
options nvidia-drm modeset=%d\n''' % (value)
kms_fd = open(nvidia_kms_file, 'w')
kms_fd.write(kms_text)
kms_fd.close()
def system_driver_packages(apt_cache=None, sys_path=None, freeonly=False, include_oem=True):
'''Get driver packages that are available for the system.
This calls system_modaliases() to determine the system's hardware and then
queries apt about which packages provide drivers for those. It also adds
available packages from detect_plugin_packages().
If you already have an apt_pkg.Cache() object, you should pass it as an
argument for efficiency. If not given, this function creates a temporary
one by itself.
If freeonly is set to True, only free packages (from main and universe) are
considered
Return a dictionary which maps package names to information about them:
driver_package → {'modalias': 'pci:...', ...}
Available information keys are:
'modalias': Modalias for the device that needs this driver (not for
drivers from detect plugins)
'syspath': sysfs directory for the device that needs this driver
(not for drivers from detect plugins)
'plugin': Name of plugin that detected this package (only for
drivers from detect plugins)
'free': Boolean flag whether driver is free, i. e. in the "main"
or "universe" component.
'from_distro': Boolean flag whether the driver is shipped by the distro;
if not, it comes from a (potentially less tested/trusted)
third party source.
'vendor': Human readable vendor name, if available.
'model': Human readable product name, if available.
'recommended': Some drivers (nvidia, fglrx) come in multiple variants and
versions; these have this flag, where exactly one has
recommended == True, and all others False.
'''
global lookup_cache
modaliases = system_modaliases(sys_path)
if not apt_cache:
try:
apt_cache = apt_pkg.Cache(None)
except Exception as ex:
logging.error(ex)
return {}
packages = {}
for alias, syspath in modaliases.items():
for p in packages_for_modalias(apt_cache, alias):
if freeonly and not _is_package_free(apt_cache, p):
continue
if not include_oem and fnmatch.fnmatch(p.name, 'oem-*-meta'):
continue
packages[p.name] = {
'modalias': alias,
'syspath': syspath,
'free': _is_package_free(apt_cache, p),
'from_distro': _is_package_from_distro(apt_cache, p),
'support': _pkg_get_support(apt_cache, p),
'runtimepm': _is_runtimepm_supported(apt_cache, p, alias)
}
(vendor, model) = _get_db_name(syspath, alias)
if vendor is not None:
packages[p.name]['vendor'] = vendor
if model is not None:
packages[p.name]['model'] = model
# Add "recommended" flags for NVidia alternatives
nvidia_packages = [p for p in packages if p.startswith('nvidia-')]
if nvidia_packages:
# Create a cache for looking up drivers to pick the best
# candidate
for key, value in packages.items():
if key.startswith('nvidia-'):
lookup_cache[key] = value
nvidia_packages.sort(key=functools.cmp_to_key(_cmp_gfx_alternatives))
recommended = nvidia_packages[-1]
for p in nvidia_packages:
packages[p]['recommended'] = (p == recommended)
# add available packages which need custom detection code
for plugin, pkgs in detect_plugin_packages(apt_cache).items():
for p in pkgs:
try:
apt_p = apt_cache[p]
packages[p] = {
'free': _is_package_free(apt_cache, apt_p),
'from_distro': _is_package_from_distro(apt_cache, apt_p),
'plugin': plugin,
}
except KeyError:
logging.debug('Package %s plugin not available. Skipping.' % p)
return packages
def _get_vendor_model_from_alias(alias):
modalias_pattern = re.compile('(.+):v(.+)d(.+)sv(.+)sd(.+)bc(.+)i.*')
details = modalias_pattern.match(alias)
if details:
return (details.group(2)[4:], details.group(3)[4:])
return (None, None)
def get_userspace_lrm_meta(apt_cache, pkg_name):
assert pkg_name is not None
metapackage = None
'''Return nvidia-driver-lrm-$flavour metapackage from the main metapackage.
This is useful to see whether any such package is available.
'''
depcache = apt_pkg.DepCache(apt_cache)
nvidia_info = NvidiaPkgNameInfo(pkg_name)
if not nvidia_info.is_valid:
logging.debug('Unsupported driver detected: %s. Skipping' % pkg_name)
return metapackage
if nvidia_info.has_obsolete_name_scheme():
logging.debug('Legacy driver detected: %s. Skipping.' % pkg_name)
return metapackage
candidate_flavour = nvidia_info.get_flavour()
candidate = 'nvidia-driver-lrm-%s' % (candidate_flavour)
try:
package = apt_cache[candidate]
# skip foreign architectures, we usually only want native
# driver packages
package_candidate = depcache.get_candidate_ver(package)
if (candidate and
package_candidate.arch in ('all', get_apt_arch())):
metapackage = candidate
except KeyError:
pass
return metapackage
def _get_headless_no_dkms_metapackage(pkg, apt_cache):
assert pkg is not None
metapackage = None
'''Return headless-no-dkms metapackage from the main metapackage.
This is useful when dealing with packages such as nvidia-driver-$flavour
whose headless-no-dkms metapackage would be nvidia-headless-no-dkms-$flavour
'''
depcache = apt_pkg.DepCache(apt_cache)
name = pkg.name
nvidia_info = NvidiaPkgNameInfo(name)
if not nvidia_info.is_valid:
logging.debug('Unsupported driver detected: %s. Skipping' % name)
return metapackage
if nvidia_info.has_obsolete_name_scheme():
logging.debug('Legacy driver detected: %s. Skipping.' % name)
return metapackage
candidate_flavour = nvidia_info.get_flavour()
candidate = 'nvidia-headless-no-dkms-%s' % (candidate_flavour)
try:
package = apt_cache[candidate]
# skip foreign architectures, we usually only want native
# driver packages
package_candidate = depcache.get_candidate_ver(package)
if (candidate and
package_candidate.arch in ('all', get_apt_arch())):
metapackage = candidate
except KeyError:
pass
return metapackage
def system_device_specific_metapackages(apt_cache=None, sys_path=None, include_oem=True):
'''Get device specific metapackages for this system
This calls system_modaliases() to determine the system's hardware and then
queries apt about which packages provide hardware enablement support for
those.
If you already have an apt_pkg.Cache() object, you should pass it as an
argument for efficiency. If not given, this function creates a temporary
one by itself.
Return a dictionary which maps package names to information about them:
driver_package → {'modalias': 'pci:...', ...}
Available information keys are:
'modalias': Modalias for the device that needs this driver (not for
drivers from detect plugins)
'syspath': sysfs directory for the device that needs this driver
(not for drivers from detect plugins)
'plugin': Name of plugin that detected this package (only for
drivers from detect plugins)
'free': Boolean flag whether driver is free, i. e. in the "main"
or "universe" component.
'from_distro': Boolean flag whether the driver is shipped by the distro;
if not, it comes from a (potentially less tested/trusted)
third party source.
'vendor': Human readable vendor name, if available.
'model': Human readable product name, if available.
'recommended': Always True; we always recommend you install these
packages.
'''
if not include_oem:
return {}
modaliases = system_modaliases(sys_path)
if not apt_cache:
try:
apt_cache = apt_pkg.Cache(None)
except Exception as ex:
logging.error(ex)
return {}
packages = {}
for alias, syspath in modaliases.items():
for p in packages_for_modalias(apt_cache, alias):
if not fnmatch.fnmatch(p.name, 'oem-*-meta'):
continue
packages[p.name] = {
'modalias': alias,
'syspath': syspath,
'free': _is_package_free(apt_cache, p),
'from_distro': _is_package_from_distro(apt_cache, p),
'recommended': True,
'support': _pkg_get_support(apt_cache, p),
}
return packages
def system_gpgpu_driver_packages(apt_cache=None, sys_path=None):
'''Get driver packages, for gpgpu purposes, that are available for the system.
This calls system_modaliases() to determine the system's hardware and then
queries apt about which packages provide drivers for those. Finally, it looks
for the correct metapackage, by calling _get_headless_no_dkms_metapackage().
If you already have an apt_pkg.Cache() object, you should pass it as an
argument for efficiency. If not given, this function creates a temporary
one by itself.
Return a dictionary which maps package names to information about them:
driver_package → {'modalias': 'pci:...', ...}
Available information keys are:
'modalias': Modalias for the device that needs this driver (not for
drivers from detect plugins)
'syspath': sysfs directory for the device that needs this driver
(not for drivers from detect plugins)
'plugin': Name of plugin that detected this package (only for
drivers from detect plugins)
'free': Boolean flag whether driver is free, i. e. in the "main"
or "universe" component.
'from_distro': Boolean flag whether the driver is shipped by the distro;
if not, it comes from a (potentially less tested/trusted)
third party source.
'vendor': Human readable vendor name, if available.
'model': Human readable product name, if available.
'recommended': Some drivers (nvidia, fglrx) come in multiple variants and
versions; these have this flag, where exactly one has
recommended == True, and all others False.
'''
global lookup_cache
vendors_whitelist = ['10de']
modaliases = system_modaliases(sys_path)
if not apt_cache:
try:
apt_cache = apt_pkg.Cache(None)
except Exception as ex:
logging.error(ex)
return {}
packages = {}
for alias, syspath in modaliases.items():
for p in packages_for_modalias(apt_cache, alias):
(vendor, model) = _get_db_name(syspath, alias)
vendor_id, model_id = _get_vendor_model_from_alias(alias)
if (vendor_id is not None) and (vendor_id.lower() in vendors_whitelist):
packages[p.name] = {
'modalias': alias,
'syspath': syspath,
'free': _is_package_free(apt_cache, p),
'from_distro': _is_package_from_distro(apt_cache, p),
'support': _pkg_get_support(apt_cache, p),
}
if vendor is not None:
packages[p.name]['vendor'] = vendor
if model is not None:
packages[p.name]['model'] = model
metapackage = _get_headless_no_dkms_metapackage(p, apt_cache)
if metapackage is not None:
packages[p.name]['metapackage'] = metapackage
# Add "recommended" flags for NVidia alternatives
nvidia_packages = [p for p in packages if p.startswith('nvidia-')]
if nvidia_packages:
# Create a cache for looking up drivers to pick the best
# candidate
for key, value in packages.items():
if key.startswith('nvidia-'):
lookup_cache[key] = value
nvidia_packages.sort(key=functools.cmp_to_key(_cmp_gfx_alternatives_gpgpu))
recommended = nvidia_packages[-1]
for p in nvidia_packages:
packages[p]['recommended'] = (p == recommended)
return packages
def system_device_drivers(apt_cache=None, sys_path=None, freeonly=False):
'''Get by-device driver packages that are available for the system.
This calls system_modaliases() to determine the system's hardware and then
queries apt about which packages provide drivers for each of those. It also
adds available packages from detect_plugin_packages(), using the name of
the detction plugin as device name.
If you already have an apt_pkg.Cache() object, you should pass it as an
argument for efficiency. If not given, this function creates a temporary
one by itself.
If freeonly is set to True, only free packages (from main and universe) are
considered
Return a dictionary which maps devices to available drivers:
device_name → {'modalias': 'pci:...', <device info>,
'drivers': {'pkgname': {<driver package info>}}
A key (device name) is either the sysfs path (for drivers detected through
modaliases) or the detect plugin name (without the full path).
Available keys in <device info>:
'modalias': Modalias for the device that needs this driver (not for
drivers from detect plugins)
'vendor': Human readable vendor name, if available.
'model': Human readable product name, if available.
'drivers': Driver package map for this device, see below. Installing any
of the drivers in that map will make this particular
device work. The keys are the package names of the driver
packages; note that this can be an already installed
default package such as xserver-xorg-video-nouveau which
provides a free alternative to the proprietary NVidia
driver; these will have the 'builtin' flag set.
'manual_install':
None of the driver packages are installed, but the kernel
module that it provides is available; this usually means
that the user manually installed the driver from upstream.
Aavailable keys in <driver package info>:
'builtin': The package is shipped by default in Ubuntu and MUST
NOT be uninstalled. This usually applies to free
drivers like xserver-xorg-video-nouveau.
'free': Boolean flag whether driver is free, i. e. in the "main"
or "universe" component.
'from_distro': Boolean flag whether the driver is shipped by the distro;
if not, it comes from a (potentially less tested/trusted)
third party source.
'recommended': Some drivers (nvidia, fglrx) come in multiple variants and
versions; these have this flag, where exactly one has
recommended == True, and all others False.
'''
result = {}
if not apt_cache:
try:
apt_cache = apt_pkg.Cache(None)
except Exception as ex:
logging.error(ex)
return {}
# copy the system_driver_packages() structure into the by-device structure
for pkg, pkginfo in system_driver_packages(apt_cache, sys_path,
freeonly=freeonly).items():
if 'syspath' in pkginfo:
device_name = pkginfo['syspath']
else:
device_name = pkginfo['plugin']
result.setdefault(device_name, {})
for opt_key in ('modalias', 'vendor', 'model'):
if opt_key in pkginfo:
result[device_name][opt_key] = pkginfo[opt_key]
drivers = result[device_name].setdefault('drivers', {})
drivers[pkg] = {'free': pkginfo['free'], 'from_distro': pkginfo['from_distro']}
if 'recommended' in pkginfo:
drivers[pkg]['recommended'] = pkginfo['recommended']
# now determine the manual_install device flag: this is true iff all driver
# packages are "manually installed"
for driver, info in result.items():
for pkg in info['drivers']:
if not _is_manual_install(apt_cache, apt_cache[pkg]):
break
else:
info['manual_install'] = True
# add OS builtin free alternatives to proprietary drivers
_add_builtins(result)
return result
def get_desktop_package_list(apt_cache, sys_path=None, free_only=False, include_oem=True, driver_string=''):
'''Return the list of packages that should be installed'''
packages = system_driver_packages(
apt_cache, sys_path, freeonly=free_only,
include_oem=include_oem)
packages = auto_install_filter(packages, driver_string)
if not packages:
logging.debug('No drivers found for installation.')
return packages
depcache = apt_pkg.DepCache(apt_cache)
records = apt_pkg.PackageRecords(apt_cache)
# ignore packages which are already installed
to_install = []
for p in packages:
package_obj = apt_cache[p]
if not package_obj.current_ver:
to_install.append(p)
candidate = depcache.get_candidate_ver(package_obj)
records.lookup(candidate.file_list[0])
# See if runtimepm is supported
if records['runtimepm']:
# Create a file for nvidia-prime
try:
pm_fd = open('/run/nvidia_runtimepm_supported', 'w')
pm_fd.write('\n')
pm_fd.close()
except PermissionError:
# No need to error out here, since package
# installation will fail
pass
# Add the matching linux modules package when available
try:
modules_package = get_linux_modules_metapackage(apt_cache, p)
if modules_package and not apt_cache[modules_package].current_ver:
to_install.append(modules_package)
lrm_meta = get_userspace_lrm_meta(apt_cache, p)
if lrm_meta and not apt_cache[lrm_meta].current_ver:
# Add the lrm meta and drop the non lrm one
to_install.append(lrm_meta)