/usr/lib/python3/dist-packages/UbuntuDrivers/detect.py is in ubuntu-drivers-common 1:0.4.17.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | '''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 apt
from UbuntuDrivers import kerneldetection
system_architecture = apt.apt_pkg.get_architectures()[0]
def system_modaliases():
'''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. The keys of the returned map are
suitable for a PackageKit WhatProvides(MODALIAS) call.
'''
aliases = {}
for path, dirs, files in os.walk('/sys/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:
info = {}
with open(os.path.join(path, 'uevent')) as f:
for l in f:
if l.startswith('MODALIAS='):
modalias = l.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, record):
xorg_video_abi = None
# determine current X.org video driver ABI
try:
for p in apt_cache['xserver-xorg-core'].candidate.provides:
if p.startswith('xorg-video-abi-'):
xorg_video_abi = p
#logging.debug('_check_video_abi_compat(): Current X.org video abi: %s', xorg_video_abi)
break
except (AttributeError, KeyError):
logging.debug('_check_video_abi_compat(): xserver-xorg-core not available, cannot check ABI')
return True
if not xorg_video_abi:
return False
try:
deps = record['Depends']
except KeyError:
return True
if 'xorg-video-abi-' in deps and xorg_video_abi not in deps:
logging.debug('Driver package %s is incompatible with current X.org server ABI %s',
record['Package'], xorg_video_abi)
return False
return True
def _apt_cache_modalias_map(apt_cache):
'''Build a modalias map from an apt.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").
'''
result = {}
for package in apt_cache:
# skip foreign architectures, we usually only want native
# driver packages
if (not package.candidate or
package.candidate.architecture not in ('all', system_architecture)):
continue
# skip packages without a modalias field
try:
m = package.candidate.record['Modaliases']
except (KeyError, AttributeError, UnicodeDecodeError):
continue
# skip incompatible video drivers
if not _check_video_abi_compat(apt_cache, package.candidate.record):
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))
return result
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(apt_cache)
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
bus_map = cache_map.get(modalias.split(':', 1)[0], {})
for alias in bus_map:
if fnmatch.fnmatch(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(pkg):
assert pkg.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
if pkg.candidate.section.startswith('restricted') or \
pkg.candidate.section.startswith('multiverse'):
return False
return True
def _is_package_from_distro(pkg):
if pkg.candidate is None:
return False
for o in pkg.candidate.origins:
if o.origin == 'Ubuntu':
return True
return False
def _pkg_get_module(pkg):
'''Determine module name from apt Package object'''
try:
m = pkg.candidate.record['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.warning('_pkg_get_module %s: package has invalid Modaliases header, cannot determine module', pkg.name)
return None
module = m[:paren]
return module
def _is_manual_install(pkg):
'''Determine if the kernel module from an apt.Package is manually installed.'''
if pkg.installed:
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(pkg)
if not module:
return False
modinfo = subprocess.Popen(['modinfo', module], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
modinfo.communicate()
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(['udevadm', 'hwdb', '--test=' + alias],
universal_newlines=True)
except (OSError, subprocess.CalledProcessError) as e:
logging.debug('_get_db_name(%s, %s): udevadm 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 system_driver_packages(apt_cache=None):
'''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.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.
'''
modaliases = system_modaliases()
if not apt_cache:
apt_cache = apt.Cache()
packages = {}
for alias, syspath in modaliases.items():
for p in packages_for_modalias(apt_cache, alias):
packages[p.name] = {
'modalias': alias,
'syspath': syspath,
'free': _is_package_free(p),
'from_distro': _is_package_from_distro(p),
}
(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:
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 "recommended" flags for fglrx alternatives
fglrx_packages = [p for p in packages if p.startswith('fglrx-')]
if fglrx_packages:
fglrx_packages.sort(key=functools.cmp_to_key(_cmp_gfx_alternatives))
recommended = fglrx_packages[-1]
for p in fglrx_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:
apt_p = apt_cache[p]
packages[p] = {
'free': _is_package_free(apt_p),
'from_distro': _is_package_from_distro(apt_p),
'plugin': plugin,
}
return packages
def system_device_drivers(apt_cache=None):
'''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.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 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:
apt_cache = apt.Cache()
# copy the system_driver_packages() structure into the by-device structure
for pkg, pkginfo in system_driver_packages(apt_cache).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[pkg]):
break
else:
info['manual_install'] = True
# add OS builtin free alternatives to proprietary drivers
_add_builtins(result)
return result
def auto_install_filter(packages):
'''Get packages which are appropriate for automatic installation.
Return the subset of the given list of packages which are appropriate for
automatic installation by the installer. This applies to e. g. the Broadcom
Wifi driver (as there is no alternative), but not to the FGLRX proprietary
graphics driver (as the free driver works well and FGLRX does not provide
KMS).
'''
# any package which matches any of those globs will be accepted
whitelist = ['bcmwl*', 'pvr-omap*', 'virtualbox-guest*', 'nvidia-*', '*-microcode']
allow = []
for pattern in whitelist:
allow.extend(fnmatch.filter(packages, pattern))
result = {}
for p in allow:
if 'recommended' not in packages[p] or packages[p]['recommended']:
result[p] = packages[p]
return result
def detect_plugin_packages(apt_cache=None):
'''Get driver packages from custom detection plugins.
Some driver packages cannot be identified by modaliases, but need some
custom code for determining whether they apply to the system. Read all *.py
files in /usr/share/ubuntu-drivers-common/detect/ or
$UBUNTU_DRIVERS_DETECT_DIR and call detect(apt_cache) on them. Filter the
returned lists for packages which are available for installation, and
return the joined results.
If you already have an existing apt.Cache() object, you can pass it as an
argument for efficiency.
Return pluginname -> [package, ...] map.
'''
packages = {}
plugindir = os.environ.get('UBUNTU_DRIVERS_DETECT_DIR',
'/usr/share/ubuntu-drivers-common/detect/')
if not os.path.isdir(plugindir):
logging.debug('Custom detection plugin directory %s does not exist', plugindir)
return packages
if apt_cache is None:
apt_cache = apt.Cache()
for fname in os.listdir(plugindir):
if not fname.endswith('.py'):
continue
plugin = os.path.join(plugindir, fname)
logging.debug('Loading custom detection plugin %s', plugin)
symb = {}
with open(plugin) as f:
try:
exec(compile(f.read(), plugin, 'exec'), symb)
result = symb['detect'](apt_cache)
logging.debug('plugin %s return value: %s', plugin, result)
except Exception as e:
logging.exception('plugin %s failed:', plugin)
continue
if result is None:
continue
if type(result) not in (list, set):
logging.error('plugin %s returned a bad type %s (must be list or set)', plugin, type(result))
continue
for pkg in result:
if pkg in apt_cache and apt_cache[pkg].candidate:
if _check_video_abi_compat(apt_cache, apt_cache[pkg].candidate.record):
packages.setdefault(fname, []).append(pkg)
else:
logging.debug('Ignoring unavailable package %s from plugin %s', pkg, plugin)
return packages
def _cmp_gfx_alternatives(x, y):
'''Compare two graphics driver names in terms of preference.
-updates always sort after non-updates, as we prefer the stable driver and
only want to offer -updates when the one from release does not support the
card. We never want to recommend -experimental unless it's the only one
available, so sort this last.
'''
if x.endswith('-updates') and not y.endswith('-updates'):
return -1
if not x.endswith('-updates') and y.endswith('-updates'):
return 1
if 'experiment' in x and 'experiment' not in y:
return -1
if 'experiment' not in x and 'experiment' in y:
return 1
if x < y:
return -1
if x > y:
return 1
assert x == y
return 0
def _add_builtins(drivers):
'''Add builtin driver alternatives'''
for device, info in drivers.items():
for pkg in info['drivers']:
# Nouveau is still not good enough, keep recommending the
# proprietary driver
if pkg.startswith('nvidia'):
info['drivers']['xserver-xorg-video-nouveau'] = {
'free': True, 'builtin': True, 'from_distro': True, 'recommended': False}
break
# These days the free driver is working well enough, so recommend
# it
if pkg.startswith('fglrx'):
for d in info['drivers']:
info['drivers'][d]['recommended'] = False
info['drivers']['xserver-xorg-video-ati'] = {
'free': True, 'builtin': True, 'from_distro': True, 'recommended': True}
break
def get_linux_headers(apt_cache):
'''Return the linux headers for the system's kernel'''
kernel_detection = kerneldetection.KernelDetection(apt_cache)
return kernel_detection.get_linux_headers_metapackage()
def get_linux(apt_cache):
'''Return the linux metapackage for the system's kernel'''
kernel_detection = kerneldetection.KernelDetection(apt_cache)
return kernel_detection.get_linux_metapackage()
|