devices.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2015-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License along
  21. # with this program; if not, write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. #
  24. import re
  25. import qubes.utils
  26. class DeviceCollection(object):
  27. '''Bag for devices.
  28. Used as default value for :py:meth:`DeviceManager.__missing__` factory.
  29. :param vm: VM for which we manage devices
  30. :param class_: device class
  31. '''
  32. def __init__(self, vm, class_):
  33. self._vm = vm
  34. self._class = class_
  35. self._set = set()
  36. def attach(self, device):
  37. '''Attach (add) device to domain.
  38. :param str device: device identifier (format is class-dependent)
  39. '''
  40. try:
  41. devclass = qubes.utils.get_entry_point_one(
  42. 'qubes.devices', self._class)
  43. except KeyError:
  44. devclass = str
  45. if not isinstance(device, devclass):
  46. device = devclass(device)
  47. if device in self:
  48. raise KeyError(
  49. 'device {!r} of class {} already attached to {!r}'.format(
  50. device, self._class, self._vm))
  51. self._vm.fire_event_pre('device-pre-attach:' + self._class, device)
  52. self._set.add(device)
  53. self._vm.fire_event('device-attach:' + self._class, device)
  54. def detach(self, device):
  55. '''Detach (remove) device from domain.
  56. :param str device: device identifier (format is class-dependent)
  57. '''
  58. if device not in self:
  59. raise KeyError(
  60. 'device {!r} of class {} not attached to {!r}'.format(
  61. device, self._class, self._vm))
  62. self._vm.fire_event_pre('device-pre-detach:' + self._class, device)
  63. self._set.remove(device)
  64. self._vm.fire_event('device-detach:' + self._class, device)
  65. def __iter__(self):
  66. return iter(self._set)
  67. def __contains__(self, item):
  68. return item in self._set
  69. def __len__(self):
  70. return len(self._set)
  71. class DeviceManager(dict):
  72. '''Device manager that hold all devices by their classess.
  73. :param vm: VM for which we manage devices
  74. '''
  75. def __init__(self, vm):
  76. super(DeviceManager, self).__init__()
  77. self._vm = vm
  78. def __missing__(self, key):
  79. self[key] = DeviceCollection(self._vm, key)
  80. return self[key]
  81. class RegexDevice(str):
  82. regex = None
  83. def __init__(self, *args, **kwargs):
  84. super(RegexDevice, self).__init__(*args, **kwargs)
  85. if self.regex is None:
  86. raise NotImplementedError(
  87. 'You should overload .regex attribute in subclass')
  88. dev_match = self.regex.match(self)
  89. if not dev_match:
  90. raise ValueError('Invalid device identifier: {!r}'.format(self))
  91. for group in self.regex.groupindex:
  92. setattr(self, group, dev_match.group(group))
  93. class PCIDevice(RegexDevice):
  94. regex = re.compile(
  95. r'^(?P<bus>[0-9a-f]+):(?P<device>[0-9a-f]+)\.(?P<function>[0-9a-f]+)$')
  96. class BlockDevice(object):
  97. # pylint: disable=too-few-public-methods
  98. def __init__(self, path, name, script=None, rw=True, domain=None,
  99. devtype='disk'):
  100. assert name, 'Missing device name'
  101. assert path, 'Missing device path'
  102. self.path = path
  103. self.name = name
  104. self.rw = rw
  105. self.script = script
  106. self.domain = domain
  107. self.devtype = devtype