2016-03-02 12:17:29 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2015-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
|
2016-03-24 22:15:24 +01:00
|
|
|
# Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
|
2016-03-02 12:17:29 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
2016-03-02 12:17:29 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2016-03-02 12:17:29 +01:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2016-03-02 12:17:29 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
2016-03-02 12:17:29 +01:00
|
|
|
#
|
|
|
|
|
2016-06-26 04:07:15 +02:00
|
|
|
'''API for various types of devices.
|
|
|
|
|
|
|
|
Main concept is that some domain main
|
|
|
|
expose (potentially multiple) devices, which can be attached to other domains.
|
2017-06-26 01:49:57 +02:00
|
|
|
Devices can be of different buses (like 'pci', 'usb', etc). Each device
|
|
|
|
bus is implemented by an extension.
|
2016-06-26 04:07:15 +02:00
|
|
|
|
|
|
|
Devices are identified by pair of (backend domain, `ident`), where `ident` is
|
2017-05-01 02:23:12 +02:00
|
|
|
:py:class:`str` and can contain only characters from `[a-zA-Z0-9._-]` set.
|
2016-06-26 04:07:15 +02:00
|
|
|
|
|
|
|
Such extension should provide:
|
|
|
|
- `qubes.devices` endpoint - a class descendant from
|
|
|
|
:py:class:`qubes.devices.DeviceInfo`, designed to hold device description (
|
2017-06-26 01:49:57 +02:00
|
|
|
including bus-specific properties)
|
|
|
|
- handle `device-attach:bus` and `device-detach:bus` events for
|
2016-06-26 04:07:15 +02:00
|
|
|
performing the attach/detach action; events are fired even when domain isn't
|
2017-06-26 01:52:13 +02:00
|
|
|
running and extension should be prepared for this; handlers for those events
|
|
|
|
can be coroutines
|
2017-06-26 01:49:57 +02:00
|
|
|
- handle `device-list:bus` event - list devices exposed by particular
|
2016-06-26 04:07:15 +02:00
|
|
|
domain; it should return list of appropriate DeviceInfo objects
|
2017-06-26 01:49:57 +02:00
|
|
|
- handle `device-get:bus` event - get one device object exposed by this
|
2016-06-26 04:07:15 +02:00
|
|
|
domain of given identifier
|
|
|
|
- handle `device-list-attached:class` event - list currently attached
|
|
|
|
devices to this domain
|
2017-07-25 01:24:30 +02:00
|
|
|
- fire `device-list-change:class` event when device list change is detected
|
|
|
|
(new/removed device)
|
2017-06-26 01:52:13 +02:00
|
|
|
|
|
|
|
Note that device-listing event handlers can not be asynchronous. This for
|
|
|
|
example means you can not call qrexec service there. This is intentional to
|
|
|
|
keep device listing operation cheap. You need to design the extension to take
|
|
|
|
this into account (for example by using QubesDB).
|
2017-07-25 01:24:30 +02:00
|
|
|
|
|
|
|
Extension may use QubesDB watch API (QubesVM.watch_qdb_path(path), then handle
|
|
|
|
`domain-qdb-change:path`) to detect changes and fire
|
|
|
|
`device-list-change:class` event.
|
2016-06-26 04:07:15 +02:00
|
|
|
'''
|
2017-06-26 01:52:13 +02:00
|
|
|
import asyncio
|
2017-01-18 22:16:46 +01:00
|
|
|
|
2016-04-28 16:00:29 +02:00
|
|
|
import qubes.utils
|
2016-03-17 13:06:13 +01:00
|
|
|
|
2016-09-03 02:13:02 +02:00
|
|
|
class DeviceNotAttached(qubes.exc.QubesException, KeyError):
|
|
|
|
'''Trying to detach not attached device'''
|
|
|
|
pass
|
|
|
|
|
|
|
|
class DeviceAlreadyAttached(qubes.exc.QubesException, KeyError):
|
|
|
|
'''Trying to attach already attached device'''
|
|
|
|
pass
|
|
|
|
|
2017-09-01 12:57:13 +02:00
|
|
|
class DeviceInfo(object):
|
|
|
|
''' Holds all information about a device '''
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
def __init__(self, backend_domain, ident, description=None,
|
|
|
|
frontend_domain=None):
|
|
|
|
#: domain providing this device
|
|
|
|
self.backend_domain = backend_domain
|
|
|
|
#: device identifier (unique for given domain and device type)
|
|
|
|
self.ident = ident
|
|
|
|
# allow redefining those as dynamic properties in subclasses
|
|
|
|
try:
|
|
|
|
#: human readable description/name of the device
|
|
|
|
self.description = description
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
#: (running) domain to which device is currently attached
|
|
|
|
self.frontend_domain = frontend_domain
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if hasattr(self, 'regex'):
|
|
|
|
# pylint: disable=no-member
|
|
|
|
dev_match = self.regex.match(ident)
|
|
|
|
if not dev_match:
|
|
|
|
raise ValueError('Invalid device identifier: {!r}'.format(
|
|
|
|
ident))
|
|
|
|
|
|
|
|
for group in self.regex.groupindex:
|
|
|
|
setattr(self, group, dev_match.group(group))
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash((self.backend_domain, self.ident))
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return (
|
|
|
|
self.backend_domain == other.backend_domain and
|
|
|
|
self.ident == other.ident
|
|
|
|
)
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
if isinstance(other, DeviceInfo):
|
|
|
|
return (self.backend_domain, self.ident) < \
|
|
|
|
(other.backend_domain, other.ident)
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{!s}:{!s}'.format(self.backend_domain, self.ident)
|
|
|
|
|
2016-09-03 02:13:02 +02:00
|
|
|
|
2017-04-01 01:30:23 +02:00
|
|
|
class DeviceAssignment(object): # pylint: disable=too-few-public-methods
|
|
|
|
''' Maps a device to a frontend_domain. '''
|
|
|
|
|
|
|
|
def __init__(self, backend_domain, ident, options=None, persistent=False,
|
2017-06-12 09:46:26 +02:00
|
|
|
bus=None):
|
2017-04-01 01:30:23 +02:00
|
|
|
self.backend_domain = backend_domain
|
|
|
|
self.ident = ident
|
2017-04-15 18:42:33 +02:00
|
|
|
self.options = options or {}
|
2017-04-01 01:30:23 +02:00
|
|
|
self.persistent = persistent
|
2017-06-12 09:46:26 +02:00
|
|
|
self.bus = bus
|
2017-04-01 01:30:23 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "[%s]:%s" % (self.backend_domain, self.ident)
|
|
|
|
|
|
|
|
def __hash__(self):
|
2017-05-15 21:04:50 +02:00
|
|
|
# it's important to use the same hash as DeviceInfo
|
2017-04-01 01:30:23 +02:00
|
|
|
return hash((self.backend_domain, self.ident))
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if not isinstance(self, other.__class__):
|
2017-05-01 02:25:50 +02:00
|
|
|
return NotImplemented
|
2017-04-01 01:30:23 +02:00
|
|
|
|
|
|
|
return self.backend_domain == other.backend_domain \
|
|
|
|
and self.ident == other.ident
|
|
|
|
|
2017-05-15 13:06:22 +02:00
|
|
|
def clone(self):
|
|
|
|
'''Clone object instance'''
|
|
|
|
return self.__class__(
|
|
|
|
self.backend_domain,
|
|
|
|
self.ident,
|
|
|
|
self.options,
|
|
|
|
self.persistent,
|
2017-06-12 09:46:26 +02:00
|
|
|
self.bus,
|
2017-05-15 13:06:22 +02:00
|
|
|
)
|
|
|
|
|
2017-05-22 00:53:38 +02:00
|
|
|
@property
|
|
|
|
def device(self):
|
|
|
|
'''Get DeviceInfo object corresponding to this DeviceAssignment'''
|
2017-06-12 09:46:26 +02:00
|
|
|
return self.backend_domain.devices[self.bus][self.ident]
|
2017-05-22 00:53:38 +02:00
|
|
|
|
2017-05-15 13:06:22 +02:00
|
|
|
|
2016-03-02 12:17:29 +01:00
|
|
|
class DeviceCollection(object):
|
|
|
|
'''Bag for devices.
|
|
|
|
|
|
|
|
Used as default value for :py:meth:`DeviceManager.__missing__` factory.
|
|
|
|
|
|
|
|
:param vm: VM for which we manage devices
|
2017-06-12 09:46:26 +02:00
|
|
|
:param bus: device bus
|
2016-06-26 04:07:15 +02:00
|
|
|
|
|
|
|
This class emits following events on VM object:
|
|
|
|
|
|
|
|
.. event:: device-attach:<class> (device)
|
|
|
|
|
|
|
|
Fired when device is attached to a VM.
|
|
|
|
|
2017-06-26 01:52:13 +02:00
|
|
|
Handler for this event can be asynchronous (a coroutine).
|
|
|
|
|
2016-06-26 04:07:15 +02:00
|
|
|
:param device: :py:class:`DeviceInfo` object to be attached
|
|
|
|
|
|
|
|
.. event:: device-pre-attach:<class> (device)
|
|
|
|
|
|
|
|
Fired before device is attached to a VM
|
|
|
|
|
2017-06-26 01:52:13 +02:00
|
|
|
Handler for this event can be asynchronous (a coroutine).
|
|
|
|
|
2016-06-26 04:07:15 +02:00
|
|
|
:param device: :py:class:`DeviceInfo` object to be attached
|
|
|
|
|
|
|
|
.. event:: device-detach:<class> (device)
|
|
|
|
|
|
|
|
Fired when device is detached from a VM.
|
|
|
|
|
2017-06-26 01:52:13 +02:00
|
|
|
Handler for this event can be asynchronous (a coroutine).
|
|
|
|
|
2016-06-26 04:07:15 +02:00
|
|
|
:param device: :py:class:`DeviceInfo` object to be attached
|
|
|
|
|
|
|
|
.. event:: device-pre-detach:<class> (device)
|
|
|
|
|
|
|
|
Fired before device is detached from a VM
|
|
|
|
|
2017-06-26 01:52:13 +02:00
|
|
|
Handler for this event can be asynchronous (a coroutine).
|
|
|
|
|
2016-06-26 04:07:15 +02:00
|
|
|
:param device: :py:class:`DeviceInfo` object to be attached
|
|
|
|
|
|
|
|
.. event:: device-list:<class>
|
|
|
|
|
|
|
|
Fired to get list of devices exposed by a VM. Handlers of this
|
|
|
|
event should return a list of py:class:`DeviceInfo` objects (or
|
|
|
|
appropriate class specific descendant)
|
|
|
|
|
|
|
|
.. event:: device-get:<class> (ident)
|
|
|
|
|
|
|
|
Fired to get a single device, given by the `ident` parameter.
|
|
|
|
Handlers of this event should either return appropriate object of
|
|
|
|
:py:class:`DeviceInfo`, or :py:obj:`None`. Especially should not
|
|
|
|
raise :py:class:`exceptions.KeyError`.
|
|
|
|
|
|
|
|
.. event:: device-list-attached:<class> (persistent)
|
|
|
|
|
|
|
|
Fired to get list of currently attached devices to a VM. Handlers
|
|
|
|
of this event should return list of devices actually attached to
|
|
|
|
a domain, regardless of its settings.
|
|
|
|
|
2016-03-02 12:17:29 +01:00
|
|
|
'''
|
|
|
|
|
2017-06-12 09:46:26 +02:00
|
|
|
def __init__(self, vm, bus):
|
2016-03-02 12:17:29 +01:00
|
|
|
self._vm = vm
|
2017-06-12 09:46:26 +02:00
|
|
|
self._bus = bus
|
2017-04-08 04:09:46 +02:00
|
|
|
self._set = PersistentCollection()
|
2016-03-02 12:17:29 +01:00
|
|
|
|
2016-06-26 04:07:15 +02:00
|
|
|
self.devclass = qubes.utils.get_entry_point_one(
|
2017-06-12 09:46:26 +02:00
|
|
|
'qubes.devices', self._bus)
|
2016-03-02 12:17:29 +01:00
|
|
|
|
2017-06-26 01:52:13 +02:00
|
|
|
@asyncio.coroutine
|
2017-04-08 04:09:46 +02:00
|
|
|
def attach(self, device_assignment: DeviceAssignment):
|
2016-03-02 12:17:29 +01:00
|
|
|
'''Attach (add) device to domain.
|
|
|
|
|
2016-06-26 04:07:15 +02:00
|
|
|
:param DeviceInfo device: device object
|
2016-03-02 12:17:29 +01:00
|
|
|
'''
|
|
|
|
|
2017-06-12 09:46:26 +02:00
|
|
|
if device_assignment.bus is None:
|
|
|
|
device_assignment.bus = self._bus
|
2017-04-15 16:44:42 +02:00
|
|
|
else:
|
2017-06-12 09:46:26 +02:00
|
|
|
assert device_assignment.bus == self._bus, \
|
2017-05-22 00:53:38 +02:00
|
|
|
"Trying to attach DeviceAssignment of a different device class"
|
|
|
|
|
2017-04-08 04:09:46 +02:00
|
|
|
if not device_assignment.persistent and self._vm.is_halted():
|
2017-04-15 16:44:42 +02:00
|
|
|
raise qubes.exc.QubesVMNotRunningError(self._vm,
|
|
|
|
"Devices can only be attached non-persistent to a running vm")
|
2017-05-22 00:53:38 +02:00
|
|
|
device = device_assignment.device
|
2017-04-08 04:09:46 +02:00
|
|
|
if device in self.assignments():
|
2016-09-03 02:13:02 +02:00
|
|
|
raise DeviceAlreadyAttached(
|
2017-04-08 04:09:46 +02:00
|
|
|
'device {!s} of class {} already attached to {!s}'.format(
|
2017-06-12 09:46:26 +02:00
|
|
|
device, self._bus, self._vm))
|
2017-06-26 01:52:13 +02:00
|
|
|
yield from self._vm.fire_event_async('device-pre-attach:' + self._bus,
|
|
|
|
pre_event=True,
|
2017-05-15 21:04:02 +02:00
|
|
|
device=device, options=device_assignment.options)
|
2017-04-08 04:09:46 +02:00
|
|
|
if device_assignment.persistent:
|
|
|
|
self._set.add(device_assignment)
|
2017-06-26 01:52:13 +02:00
|
|
|
yield from self._vm.fire_event_async('device-attach:' + self._bus,
|
2017-05-15 21:04:02 +02:00
|
|
|
device=device, options=device_assignment.options)
|
2016-03-02 12:17:29 +01:00
|
|
|
|
2017-06-26 01:52:13 +02:00
|
|
|
def load_persistent(self, device_assignment: DeviceAssignment):
|
|
|
|
'''Load DeviceAssignment retrieved from qubes.xml
|
|
|
|
|
|
|
|
This can be used only for loading qubes.xml, when VM events are not
|
|
|
|
enabled yet.
|
|
|
|
'''
|
|
|
|
assert not self._vm.events_enabled
|
|
|
|
assert device_assignment.persistent
|
|
|
|
device_assignment.bus = self._bus
|
|
|
|
self._set.add(device_assignment)
|
|
|
|
|
2017-09-01 12:42:02 +02:00
|
|
|
def update_persistent(self, device: DeviceInfo, persistent: bool):
|
|
|
|
'''Update `persistent` flag of already attached device.
|
|
|
|
'''
|
|
|
|
|
|
|
|
if self._vm.is_halted():
|
|
|
|
raise qubes.exc.QubesVMNotStartedError(self._vm,
|
|
|
|
'VM must be running to modify device persistence flag')
|
|
|
|
assignments = [a for a in self.assignments() if a.device == device]
|
|
|
|
if not assignments:
|
|
|
|
raise qubes.exc.QubesValueError('Device not assigned')
|
|
|
|
assert len(assignments) == 1
|
|
|
|
assignment = assignments[0]
|
|
|
|
|
|
|
|
# be careful to use already present assignment, not the provided one
|
|
|
|
# - to not change options as a side effect
|
|
|
|
if persistent and device not in self._set:
|
|
|
|
assignment.persistent = True
|
|
|
|
self._set.add(assignment)
|
|
|
|
elif not persistent and device in self._set:
|
|
|
|
self._set.discard(assignment)
|
|
|
|
|
2017-06-26 01:52:13 +02:00
|
|
|
@asyncio.coroutine
|
2017-04-08 04:09:46 +02:00
|
|
|
def detach(self, device_assignment: DeviceAssignment):
|
2016-03-02 12:17:29 +01:00
|
|
|
'''Detach (remove) device from domain.
|
|
|
|
|
2016-06-26 04:07:15 +02:00
|
|
|
:param DeviceInfo device: device object
|
2016-03-02 12:17:29 +01:00
|
|
|
'''
|
|
|
|
|
2017-06-12 09:46:26 +02:00
|
|
|
if device_assignment.bus is None:
|
|
|
|
device_assignment.bus = self._bus
|
2017-05-22 00:53:38 +02:00
|
|
|
else:
|
2017-06-12 09:46:26 +02:00
|
|
|
assert device_assignment.bus == self._bus, \
|
2017-05-22 00:53:38 +02:00
|
|
|
"Trying to attach DeviceAssignment of a different device class"
|
|
|
|
|
2017-04-08 04:09:46 +02:00
|
|
|
if device_assignment in self._set and not self._vm.is_halted():
|
2017-04-15 16:44:42 +02:00
|
|
|
raise qubes.exc.QubesVMNotHaltedError(self._vm,
|
2017-04-08 04:09:46 +02:00
|
|
|
"Can not remove a persistent attachment from a non halted vm")
|
|
|
|
if device_assignment not in self.assignments():
|
2016-09-03 02:13:02 +02:00
|
|
|
raise DeviceNotAttached(
|
|
|
|
'device {!s} of class {} not attached to {!s}'.format(
|
2017-06-12 09:46:26 +02:00
|
|
|
device_assignment.ident, self._bus, self._vm))
|
2017-04-08 04:09:46 +02:00
|
|
|
|
2017-05-22 00:53:38 +02:00
|
|
|
device = device_assignment.device
|
2017-06-26 01:52:13 +02:00
|
|
|
yield from self._vm.fire_event_async('device-pre-detach:' + self._bus,
|
2017-06-23 17:29:09 +02:00
|
|
|
pre_event=True, device=device)
|
2017-04-08 04:09:46 +02:00
|
|
|
if device in self._set:
|
|
|
|
device_assignment.persistent = True
|
|
|
|
self._set.discard(device_assignment)
|
|
|
|
|
2017-06-26 01:52:13 +02:00
|
|
|
yield from self._vm.fire_event_async('device-detach:' + self._bus,
|
|
|
|
device=device)
|
2016-03-02 12:17:29 +01:00
|
|
|
|
2017-04-08 04:09:46 +02:00
|
|
|
def attached(self):
|
|
|
|
'''List devices which are (or may be) attached to this vm '''
|
2017-07-27 15:24:14 +02:00
|
|
|
attached = self._vm.fire_event('device-list-attached:' + self._bus,
|
|
|
|
persistent=None)
|
2017-04-15 19:12:39 +02:00
|
|
|
if attached:
|
|
|
|
return [dev for dev, _ in attached]
|
|
|
|
|
|
|
|
return []
|
2017-04-08 04:09:46 +02:00
|
|
|
|
|
|
|
def persistent(self):
|
|
|
|
''' Devices persistently attached and safe to access before libvirt
|
|
|
|
bootstrap.
|
|
|
|
'''
|
2017-05-22 00:53:38 +02:00
|
|
|
return [a.device for a in self._set]
|
2017-04-08 04:09:46 +02:00
|
|
|
|
|
|
|
def assignments(self, persistent=None):
|
|
|
|
'''List assignments for devices which are (or may be) attached to the
|
|
|
|
vm.
|
2016-06-26 04:07:15 +02:00
|
|
|
|
|
|
|
Devices may be attached persistently (so they are included in
|
|
|
|
:file:`qubes.xml`) or not. Device can also be in :file:`qubes.xml`,
|
|
|
|
but be temporarily detached.
|
|
|
|
|
2017-04-08 04:09:46 +02:00
|
|
|
:param bool persistent: only include devices which are or are not
|
|
|
|
attached persistently.
|
2016-06-26 04:07:15 +02:00
|
|
|
'''
|
2017-04-08 04:09:46 +02:00
|
|
|
|
2017-07-03 23:08:04 +02:00
|
|
|
try:
|
|
|
|
devices = self._vm.fire_event('device-list-attached:' + self._bus,
|
|
|
|
persistent=persistent)
|
2017-09-02 18:00:27 +02:00
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
self._vm.log.exception('Failed to list {} devices'.format(
|
2017-07-03 23:08:04 +02:00
|
|
|
self._bus))
|
|
|
|
if persistent is True:
|
|
|
|
# don't break app.save()
|
|
|
|
return self._set
|
|
|
|
else:
|
|
|
|
raise
|
2017-05-15 21:04:50 +02:00
|
|
|
result = set()
|
2017-04-15 19:12:39 +02:00
|
|
|
for dev, options in devices:
|
2017-05-10 01:29:10 +02:00
|
|
|
if dev in self._set and not persistent:
|
2017-04-08 04:09:46 +02:00
|
|
|
continue
|
|
|
|
elif dev in self._set:
|
2017-05-15 21:04:50 +02:00
|
|
|
result.add(self._set.get(dev))
|
2017-05-10 01:29:10 +02:00
|
|
|
elif dev not in self._set and persistent:
|
2017-04-08 04:09:46 +02:00
|
|
|
continue
|
|
|
|
else:
|
2017-05-15 21:04:50 +02:00
|
|
|
result.add(
|
2017-05-22 00:53:38 +02:00
|
|
|
DeviceAssignment(
|
|
|
|
backend_domain=dev.backend_domain,
|
|
|
|
ident=dev.ident, options=options,
|
2017-06-12 09:46:26 +02:00
|
|
|
bus=self._bus))
|
2017-05-15 21:04:50 +02:00
|
|
|
if persistent is not False:
|
|
|
|
result.update(self._set)
|
2017-04-08 04:09:46 +02:00
|
|
|
return result
|
2016-06-26 04:07:15 +02:00
|
|
|
|
|
|
|
def available(self):
|
|
|
|
'''List devices exposed by this vm'''
|
2017-06-12 09:46:26 +02:00
|
|
|
devices = self._vm.fire_event('device-list:' + self._bus)
|
2016-06-26 04:07:15 +02:00
|
|
|
return devices
|
2016-03-02 12:17:29 +01:00
|
|
|
|
|
|
|
def __iter__(self):
|
2016-06-26 04:07:15 +02:00
|
|
|
return iter(self.available())
|
2016-03-02 12:17:29 +01:00
|
|
|
|
2016-06-26 04:07:15 +02:00
|
|
|
def __getitem__(self, ident):
|
|
|
|
'''Get device object with given ident.
|
2016-03-02 12:17:29 +01:00
|
|
|
|
2016-06-26 04:07:15 +02:00
|
|
|
:returns: py:class:`DeviceInfo`
|
2016-03-02 12:17:29 +01:00
|
|
|
|
2016-06-26 04:07:15 +02:00
|
|
|
If domain isn't running, it is impossible to check device validity,
|
|
|
|
so return UnknownDevice object. Also do the same for non-existing
|
|
|
|
devices - otherwise it will be impossible to detach already
|
|
|
|
disconnected device.
|
2016-03-02 12:17:29 +01:00
|
|
|
|
2016-06-26 04:07:15 +02:00
|
|
|
:raises AssertionError: when multiple devices with the same ident are
|
|
|
|
found
|
|
|
|
'''
|
2017-06-12 09:46:26 +02:00
|
|
|
dev = self._vm.fire_event('device-get:' + self._bus, ident=ident)
|
2016-06-26 04:07:15 +02:00
|
|
|
if dev:
|
|
|
|
assert len(dev) == 1
|
|
|
|
return dev[0]
|
2017-04-15 20:04:38 +02:00
|
|
|
|
|
|
|
return UnknownDevice(self._vm, ident)
|
2016-03-02 12:17:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DeviceManager(dict):
|
|
|
|
'''Device manager that hold all devices by their classess.
|
|
|
|
|
|
|
|
:param vm: VM for which we manage devices
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, vm):
|
|
|
|
super(DeviceManager, self).__init__()
|
|
|
|
self._vm = vm
|
|
|
|
|
|
|
|
def __missing__(self, key):
|
|
|
|
self[key] = DeviceCollection(self._vm, key)
|
|
|
|
return self[key]
|
|
|
|
|
|
|
|
|
2016-06-26 04:07:15 +02:00
|
|
|
class UnknownDevice(DeviceInfo):
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
'''Unknown device - for example exposed by domain not running currently'''
|
|
|
|
|
|
|
|
def __init__(self, backend_domain, ident, description=None,
|
2017-06-12 09:46:26 +02:00
|
|
|
frontend_domain=None):
|
2016-06-26 04:07:15 +02:00
|
|
|
if description is None:
|
|
|
|
description = "Unknown device"
|
|
|
|
super(UnknownDevice, self).__init__(backend_domain, ident, description,
|
2017-06-12 09:46:26 +02:00
|
|
|
frontend_domain)
|
2016-06-15 19:08:00 +02:00
|
|
|
|
2016-03-24 22:15:24 +01:00
|
|
|
|
2017-03-17 13:08:37 +01:00
|
|
|
class PersistentCollection(object):
|
|
|
|
|
|
|
|
''' Helper object managing persistent `DeviceAssignment`s.
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._dict = {}
|
|
|
|
|
|
|
|
def add(self, assignment: DeviceAssignment):
|
|
|
|
''' Add assignment to collection '''
|
2017-06-12 09:46:26 +02:00
|
|
|
assert assignment.persistent
|
2017-03-17 13:08:37 +01:00
|
|
|
vm = assignment.backend_domain
|
|
|
|
ident = assignment.ident
|
|
|
|
key = (vm, ident)
|
|
|
|
assert key not in self._dict
|
|
|
|
|
|
|
|
self._dict[key] = assignment
|
|
|
|
|
|
|
|
def discard(self, assignment):
|
|
|
|
''' Discard assignment from collection '''
|
2017-06-12 09:46:26 +02:00
|
|
|
assert assignment.persistent
|
2017-03-17 13:08:37 +01:00
|
|
|
vm = assignment.backend_domain
|
|
|
|
ident = assignment.ident
|
|
|
|
key = (vm, ident)
|
|
|
|
if key not in self._dict:
|
|
|
|
raise KeyError
|
|
|
|
del self._dict[key]
|
|
|
|
|
|
|
|
def __contains__(self, device) -> bool:
|
2017-05-10 01:29:10 +02:00
|
|
|
return (device.backend_domain, device.ident) in self._dict
|
2017-03-17 13:08:37 +01:00
|
|
|
|
|
|
|
def get(self, device: DeviceInfo) -> DeviceAssignment:
|
|
|
|
''' Returns the corresponding `qubes.devices.DeviceAssignment` for the
|
|
|
|
device. '''
|
2017-05-10 01:29:10 +02:00
|
|
|
return self._dict[(device.backend_domain, device.ident)]
|
2017-03-17 13:08:37 +01:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self._dict.values().__iter__()
|
|
|
|
|
|
|
|
def __len__(self) -> int:
|
|
|
|
return len(self._dict.keys())
|