kernels.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program 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
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. ''' This module contains pool implementations for different OS kernels. '''
  23. import os
  24. from qubes.storage import Pool, StoragePoolException, Volume
  25. class LinuxModules(Volume):
  26. ''' A volume representing a ro linux kernel '''
  27. rw = False
  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. @property
  34. def vid(self):
  35. if callable(self._kernel_version):
  36. return self._kernel_version()
  37. return self._kernel_version
  38. @vid.setter
  39. def vid(self, value):
  40. # ignore
  41. pass
  42. @property
  43. def kernels_dir(self):
  44. kernel_version = self.vid
  45. if not kernel_version:
  46. return None
  47. return os.path.join(self.target_dir, kernel_version)
  48. @property
  49. def path(self):
  50. kernels_dir = self.kernels_dir
  51. if not kernels_dir:
  52. return None
  53. return os.path.join(kernels_dir, 'modules.img')
  54. @property
  55. def vmlinuz(self):
  56. kernels_dir = self.kernels_dir
  57. if not kernels_dir:
  58. return None
  59. return os.path.join(kernels_dir, 'vmlinuz')
  60. @property
  61. def initramfs(self):
  62. kernels_dir = self.kernels_dir
  63. if not kernels_dir:
  64. return None
  65. return os.path.join(kernels_dir, 'initramfs')
  66. @property
  67. def revisions(self):
  68. return {}
  69. def is_dirty(self):
  70. return False
  71. def clone(self, source):
  72. if isinstance(source, LinuxModules):
  73. # do nothing
  74. return self
  75. raise StoragePoolException('clone of LinuxModules volume from '
  76. 'different volume type is not supported')
  77. def create(self):
  78. return self
  79. def remove(self):
  80. pass
  81. def commit(self):
  82. return self
  83. def export(self):
  84. return self.path
  85. def is_outdated(self):
  86. return False
  87. def start(self):
  88. path = self.path
  89. if path and not os.path.exists(path):
  90. raise StoragePoolException('Missing kernel modules: %s' % path)
  91. return self
  92. def stop(self):
  93. pass
  94. def verify(self):
  95. if self.vid:
  96. _check_path(self.path)
  97. _check_path(self.vmlinuz)
  98. _check_path(self.initramfs)
  99. def block_device(self):
  100. if self.vid:
  101. return super().block_device()
  102. class LinuxKernel(Pool):
  103. ''' Provides linux kernels '''
  104. driver = 'linux-kernel'
  105. def __init__(self, name=None, dir_path=None):
  106. assert dir_path, 'Missing dir_path'
  107. super(LinuxKernel, self).__init__(name=name)
  108. self.dir_path = dir_path
  109. def init_volume(self, vm, volume_config):
  110. assert not volume_config['rw']
  111. volume_config['pool'] = self
  112. volume = LinuxModules(self.dir_path, lambda: vm.kernel, **volume_config)
  113. return volume
  114. @property
  115. def config(self):
  116. return {
  117. 'name': self.name,
  118. 'dir_path': self.dir_path,
  119. 'driver': LinuxKernel.driver,
  120. }
  121. def destroy(self):
  122. pass
  123. def import_volume(self, dst_pool, dst_volume, src_pool, src_volume):
  124. pass
  125. def setup(self):
  126. pass
  127. @property
  128. def volumes(self):
  129. ''' Return all known kernel volumes '''
  130. return [LinuxModules(self.dir_path,
  131. kernel_version,
  132. pool=self,
  133. name=kernel_version,
  134. internal=True,
  135. rw=False
  136. )
  137. for kernel_version in os.listdir(self.dir_path)]
  138. def _check_path(path):
  139. ''' Raise an :py:class:`qubes.storage.StoragePoolException` if ``path`` does
  140. not exist.
  141. '''
  142. if not os.path.exists(path):
  143. raise StoragePoolException('Missing file: %s' % path)