2016-03-02 12:17:29 +01:00
|
|
|
#!/usr/bin/python2 -O
|
|
|
|
# vim: fileencoding=utf-8
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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
|
|
|
#
|
|
|
|
# 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.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
2016-04-28 16:00:29 +02:00
|
|
|
import qubes.utils
|
2016-03-17 13:06:13 +01:00
|
|
|
|
2016-03-24 22:15:24 +01: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
|
|
|
|
:param class_: device class
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, vm, class_):
|
|
|
|
self._vm = vm
|
|
|
|
self._class = class_
|
|
|
|
self._set = set()
|
|
|
|
|
|
|
|
|
|
|
|
def attach(self, device):
|
|
|
|
'''Attach (add) device to domain.
|
|
|
|
|
|
|
|
:param str device: device identifier (format is class-dependent)
|
|
|
|
'''
|
|
|
|
|
2016-03-17 13:06:13 +01:00
|
|
|
try:
|
2016-04-28 16:00:29 +02:00
|
|
|
devclass = qubes.utils.get_entry_point_one(
|
|
|
|
'qubes.devices', self._class)
|
2016-03-17 13:06:13 +01:00
|
|
|
except KeyError:
|
|
|
|
devclass = str
|
|
|
|
|
|
|
|
if not isinstance(device, devclass):
|
|
|
|
device = devclass(device)
|
|
|
|
|
2016-03-02 12:17:29 +01:00
|
|
|
if device in self:
|
|
|
|
raise KeyError(
|
|
|
|
'device {!r} of class {} already attached to {!r}'.format(
|
|
|
|
device, self._class, self._vm))
|
2016-04-03 03:40:41 +02:00
|
|
|
self._vm.fire_event_pre('device-pre-attach:' + self._class, device)
|
2016-03-02 12:17:29 +01:00
|
|
|
self._set.add(device)
|
2016-04-03 03:40:41 +02:00
|
|
|
self._vm.fire_event('device-attach:' + self._class, device)
|
2016-03-02 12:17:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
def detach(self, device):
|
|
|
|
'''Detach (remove) device from domain.
|
|
|
|
|
|
|
|
:param str device: device identifier (format is class-dependent)
|
|
|
|
'''
|
|
|
|
|
|
|
|
if device not in self:
|
|
|
|
raise KeyError(
|
|
|
|
'device {!r} of class {} not attached to {!r}'.format(
|
|
|
|
device, self._class, self._vm))
|
2016-04-03 03:40:41 +02:00
|
|
|
self._vm.fire_event_pre('device-pre-detach:' + self._class, device)
|
2016-03-02 12:17:29 +01:00
|
|
|
self._set.remove(device)
|
2016-04-03 03:40:41 +02:00
|
|
|
self._vm.fire_event('device-detach:' + self._class, device)
|
2016-03-02 12:17:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self._set)
|
|
|
|
|
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
return item in self._set
|
|
|
|
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._set)
|
|
|
|
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
|
|
|
|
class RegexDevice(str):
|
2016-06-02 22:02:06 +02:00
|
|
|
regex = None
|
2016-03-02 12:17:29 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(RegexDevice, self).__init__(*args, **kwargs)
|
|
|
|
|
2016-06-02 22:02:06 +02:00
|
|
|
if self.regex is None:
|
|
|
|
raise NotImplementedError(
|
|
|
|
'You should overload .regex attribute in subclass')
|
|
|
|
|
2016-03-02 12:17:29 +01:00
|
|
|
dev_match = self.regex.match(self)
|
|
|
|
if not dev_match:
|
|
|
|
raise ValueError('Invalid device identifier: {!r}'.format(self))
|
|
|
|
|
|
|
|
for group in self.regex.groupindex:
|
|
|
|
setattr(self, group, dev_match.group(group))
|
|
|
|
|
|
|
|
|
|
|
|
class PCIDevice(RegexDevice):
|
|
|
|
regex = re.compile(
|
|
|
|
r'^(?P<bus>[0-9a-f]+):(?P<device>[0-9a-f]+)\.(?P<function>[0-9a-f]+)$')
|
2016-03-24 22:15:24 +01:00
|
|
|
|
2016-06-15 19:08:00 +02:00
|
|
|
@property
|
|
|
|
def libvirt_name(self):
|
2016-07-20 17:52:49 +02:00
|
|
|
return 'pci_0000_{}_{}_{}'.format(self.bus, self.device, self.function)
|
2016-06-15 19:08:00 +02:00
|
|
|
|
2016-03-24 22:15:24 +01:00
|
|
|
|
|
|
|
class BlockDevice(object):
|
2016-06-02 22:02:06 +02:00
|
|
|
# pylint: disable=too-few-public-methods
|
2016-03-24 22:15:24 +01:00
|
|
|
def __init__(self, path, name, script=None, rw=True, domain=None,
|
|
|
|
devtype='disk'):
|
|
|
|
assert name, 'Missing device name'
|
|
|
|
assert path, 'Missing device path'
|
|
|
|
self.path = path
|
|
|
|
self.name = name
|
|
|
|
self.rw = rw
|
|
|
|
self.script = script
|
|
|
|
self.domain = domain
|
|
|
|
self.devtype = devtype
|