devices.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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
  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.get_entry_point_one('qubes.devices', self._class)
  42. except KeyError:
  43. devclass = str
  44. if not isinstance(device, devclass):
  45. device = devclass(device)
  46. if device in self:
  47. raise KeyError(
  48. 'device {!r} of class {} already attached to {!r}'.format(
  49. device, self._class, self._vm))
  50. self._vm.fire_event_pre('device-pre-attach:' + self._class, device)
  51. self._set.add(device)
  52. self._vm.fire_event('device-attach:' + self._class, device)
  53. def detach(self, device):
  54. '''Detach (remove) device from domain.
  55. :param str device: device identifier (format is class-dependent)
  56. '''
  57. if device not in self:
  58. raise KeyError(
  59. 'device {!r} of class {} not attached to {!r}'.format(
  60. device, self._class, self._vm))
  61. self._vm.fire_event_pre('device-pre-detach:' + self._class, device)
  62. self._set.remove(device)
  63. self._vm.fire_event('device-detach:' + self._class, device)
  64. def __iter__(self):
  65. return iter(self._set)
  66. def __contains__(self, item):
  67. return item in self._set
  68. def __len__(self):
  69. return len(self._set)
  70. class DeviceManager(dict):
  71. '''Device manager that hold all devices by their classess.
  72. :param vm: VM for which we manage devices
  73. '''
  74. def __init__(self, vm):
  75. super(DeviceManager, self).__init__()
  76. self._vm = vm
  77. def __missing__(self, key):
  78. self[key] = DeviceCollection(self._vm, key)
  79. return self[key]
  80. class RegexDevice(str):
  81. def __init__(self, *args, **kwargs):
  82. super(RegexDevice, self).__init__(*args, **kwargs)
  83. dev_match = self.regex.match(self)
  84. if not dev_match:
  85. raise ValueError('Invalid device identifier: {!r}'.format(self))
  86. for group in self.regex.groupindex:
  87. setattr(self, group, dev_match.group(group))
  88. class PCIDevice(RegexDevice):
  89. regex = re.compile(
  90. r'^(?P<bus>[0-9a-f]+):(?P<device>[0-9a-f]+)\.(?P<function>[0-9a-f]+)$')
  91. class BlockDevice(object):
  92. def __init__(self, path, name, script=None, rw=True, domain=None,
  93. devtype='disk'):
  94. assert name, 'Missing device name'
  95. assert path, 'Missing device path'
  96. self.path = path
  97. self.name = name
  98. self.rw = rw
  99. self.script = script
  100. self.domain = domain
  101. self.devtype = devtype