kernels.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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().__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, dir_path):
  127. super().__init__(name=name, revisions_to_keep=0)
  128. self.dir_path = dir_path
  129. def init_volume(self, vm, volume_config):
  130. assert not volume_config['rw']
  131. # migrate old config
  132. if volume_config.get('snap_on_start', False) and not \
  133. volume_config.get('source', None):
  134. volume_config['snap_on_start'] = False
  135. if volume_config.get('save_on_stop', False):
  136. raise NotImplementedError(
  137. 'LinuxKernel pool does not support save_on_stop=True')
  138. volume_config['pool'] = self
  139. volume = LinuxModules(self.dir_path, lambda: vm.kernel, **volume_config)
  140. return volume
  141. @property
  142. def config(self):
  143. return {
  144. 'name': self.name,
  145. 'dir_path': self.dir_path,
  146. 'driver': LinuxKernel.driver,
  147. }
  148. def destroy(self):
  149. pass
  150. def import_volume(self, dst_pool, dst_volume, src_pool, src_volume):
  151. pass
  152. def setup(self):
  153. pass
  154. @property
  155. def revisions_to_keep(self):
  156. return 0
  157. @revisions_to_keep.setter
  158. def revisions_to_keep(self, value):
  159. # pylint: disable=no-self-use
  160. if value:
  161. raise qubes.exc.QubesValueError(
  162. 'LinuxKernel supports only revisions_to_keep=0')
  163. def included_in(self, app):
  164. ''' Check if there is pool containing /var/lib/qubes/vm-kernels '''
  165. return qubes.storage.search_pool_containing_dir(
  166. [pool for pool in app.pools.values() if pool is not self],
  167. self.dir_path)
  168. def list_volumes(self):
  169. ''' Return all known kernel volumes '''
  170. return [LinuxModules(self.dir_path,
  171. kernel_version,
  172. pool=self,
  173. name=kernel_version,
  174. rw=False
  175. )
  176. for kernel_version in os.listdir(self.dir_path)]
  177. def _check_path(path):
  178. ''' Raise an :py:class:`qubes.storage.StoragePoolException` if ``path`` does
  179. not exist.
  180. '''
  181. if not os.path.exists(path):
  182. raise StoragePoolException('Missing file: %s' % path)