kernels.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  6. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. #
  21. ''' This module contains pool implementations for different OS kernels. '''
  22. import os
  23. import qubes.exc
  24. import qubes.storage
  25. from qubes.storage import Pool, StoragePoolException, Volume
  26. class LinuxModules(Volume):
  27. ''' A volume representing a ro linux kernel '''
  28. def __init__(self, target_dir, kernel_version, **kwargs):
  29. kwargs['vid'] = ''
  30. super(LinuxModules, self).__init__(**kwargs)
  31. self._kernel_version = kernel_version
  32. self.target_dir = target_dir
  33. assert self.revisions_to_keep == 0
  34. assert self.rw is False
  35. @property
  36. def vid(self):
  37. if callable(self._kernel_version):
  38. return self._kernel_version()
  39. return self._kernel_version
  40. @vid.setter
  41. def vid(self, value):
  42. # ignore
  43. pass
  44. @property
  45. def kernels_dir(self):
  46. kernel_version = self.vid
  47. if not kernel_version:
  48. return None
  49. return os.path.join(self.target_dir, kernel_version)
  50. @property
  51. def path(self):
  52. kernels_dir = self.kernels_dir
  53. if not kernels_dir:
  54. return None
  55. return os.path.join(kernels_dir, 'modules.img')
  56. @property
  57. def vmlinuz(self):
  58. kernels_dir = self.kernels_dir
  59. if not kernels_dir:
  60. return None
  61. return os.path.join(kernels_dir, 'vmlinuz')
  62. @property
  63. def initramfs(self):
  64. kernels_dir = self.kernels_dir
  65. if not kernels_dir:
  66. return None
  67. return os.path.join(kernels_dir, 'initramfs')
  68. @property
  69. def revisions(self):
  70. return {}
  71. def is_dirty(self):
  72. return False
  73. def import_volume(self, src_volume):
  74. if isinstance(src_volume, LinuxModules):
  75. # do nothing
  76. return self
  77. raise StoragePoolException('clone of LinuxModules volume from '
  78. 'different volume type is not supported')
  79. def create(self):
  80. return self
  81. def remove(self):
  82. pass
  83. def commit(self):
  84. return self
  85. def export(self):
  86. return self.path
  87. def is_outdated(self):
  88. return False
  89. @property
  90. def revisions_to_keep(self):
  91. return 0
  92. @revisions_to_keep.setter
  93. def revisions_to_keep(self, value):
  94. # pylint: disable=no-self-use
  95. if value:
  96. raise qubes.exc.QubesValueError(
  97. 'LinuxModules supports only revisions_to_keep=0')
  98. @property
  99. def rw(self):
  100. return False
  101. @rw.setter
  102. def rw(self, value):
  103. # pylint: disable=no-self-use
  104. if value:
  105. raise qubes.exc.QubesValueError(
  106. 'LinuxModules supports only read-only volumes')
  107. def start(self):
  108. return self
  109. def stop(self):
  110. pass
  111. def verify(self):
  112. if self.vid:
  113. _check_path(self.vmlinuz)
  114. _check_path(self.initramfs)
  115. def block_device(self):
  116. path = self.path
  117. # create block device for modules.img only if:
  118. # - there is kernel set for the VM
  119. # - that kernel directory contains modules.img file
  120. if path and os.path.exists(path):
  121. return super().block_device()
  122. return None
  123. class LinuxKernel(Pool):
  124. ''' Provides linux kernels '''
  125. driver = 'linux-kernel'
  126. def __init__(self, name=None, dir_path=None):
  127. assert dir_path, 'Missing dir_path'
  128. super(LinuxKernel, self).__init__(name=name, revisions_to_keep=0)
  129. self.dir_path = dir_path
  130. def init_volume(self, vm, volume_config):
  131. assert not volume_config['rw']
  132. # migrate old config
  133. if volume_config.get('snap_on_start', False) and not \
  134. volume_config.get('source', None):
  135. volume_config['snap_on_start'] = False
  136. if volume_config.get('save_on_stop', False):
  137. raise NotImplementedError(
  138. 'LinuxKernel pool does not support save_on_stop=True')
  139. volume_config['pool'] = self
  140. volume = LinuxModules(self.dir_path, lambda: vm.kernel, **volume_config)
  141. return volume
  142. @property
  143. def config(self):
  144. return {
  145. 'name': self.name,
  146. 'dir_path': self.dir_path,
  147. 'driver': LinuxKernel.driver,
  148. }
  149. def destroy(self):
  150. pass
  151. def import_volume(self, dst_pool, dst_volume, src_pool, src_volume):
  152. pass
  153. def setup(self):
  154. pass
  155. @property
  156. def revisions_to_keep(self):
  157. return 0
  158. @revisions_to_keep.setter
  159. def revisions_to_keep(self, value):
  160. # pylint: disable=no-self-use
  161. if value:
  162. raise qubes.exc.QubesValueError(
  163. 'LinuxModules supports only revisions_to_keep=0')
  164. def included_in(self, app):
  165. ''' Check if there is pool containing /var/lib/qubes/vm-kernels '''
  166. return qubes.storage.search_pool_containing_dir(
  167. [pool for pool in app.pools.values() if pool is not self],
  168. self.dir_path)
  169. @property
  170. def volumes(self):
  171. ''' Return all known kernel volumes '''
  172. return [LinuxModules(self.dir_path,
  173. kernel_version,
  174. pool=self,
  175. name=kernel_version,
  176. rw=False
  177. )
  178. for kernel_version in os.listdir(self.dir_path)]
  179. def _check_path(path):
  180. ''' Raise an :py:class:`qubes.storage.StoragePoolException` if ``path`` does
  181. not exist.
  182. '''
  183. if not os.path.exists(path):
  184. raise StoragePoolException('Missing file: %s' % path)