kernels.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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'] = kernel_version
  30. kwargs['source'] = self
  31. super(LinuxModules, self).__init__(**kwargs)
  32. self.kernels_dir = os.path.join(target_dir, kernel_version)
  33. self.path = os.path.join(self.kernels_dir, 'modules.img')
  34. self.vmlinuz = os.path.join(self.kernels_dir, 'vmlinuz')
  35. self.initramfs = os.path.join(self.kernels_dir, 'initramfs')
  36. @property
  37. def revisions(self):
  38. return {}
  39. class LinuxKernel(Pool):
  40. ''' Provides linux kernels '''
  41. driver = 'linux-kernel'
  42. def __init__(self, name=None, dir_path=None):
  43. assert dir_path, 'Missing dir_path'
  44. super(LinuxKernel, self).__init__(name=name)
  45. self.dir_path = dir_path
  46. def init_volume(self, vm, volume_config):
  47. assert not volume_config['rw']
  48. volume = LinuxModules(self.dir_path, vm.kernel, **volume_config)
  49. return volume
  50. def is_dirty(self, volume):
  51. return False
  52. def clone(self, source, target):
  53. return target
  54. def create(self, volume):
  55. return volume
  56. def commit(self, volume):
  57. return volume
  58. @property
  59. def config(self):
  60. return {
  61. 'name': self.name,
  62. 'dir_path': self.dir_path,
  63. 'driver': LinuxKernel.driver,
  64. }
  65. def destroy(self):
  66. pass
  67. def export(self, volume):
  68. return volume.path
  69. def import_volume(self, dst_pool, dst_volume, src_pool, src_volume):
  70. pass
  71. def is_outdated(self, volume):
  72. return False
  73. def remove(self, volume):
  74. pass
  75. def rename(self, volume, old_name, new_name):
  76. return volume
  77. def setup(self):
  78. pass
  79. def start(self, volume):
  80. path = volume.path
  81. if not os.path.exists(path):
  82. raise StoragePoolException('Missing kernel modules: %s' % path)
  83. return volume
  84. def stop(self, volume):
  85. pass
  86. def verify(self, volume):
  87. _check_path(volume.path)
  88. _check_path(volume.vmlinuz)
  89. _check_path(volume.initramfs)
  90. @property
  91. def volumes(self):
  92. ''' Return all known kernel volumes '''
  93. return [LinuxModules(self.dir_path,
  94. kernel_version,
  95. pool=self.name,
  96. name=kernel_version,
  97. internal=True,
  98. rw=False
  99. )
  100. for kernel_version in os.listdir(self.dir_path)]
  101. def _check_path(path):
  102. ''' Raise an :py:class:`qubes.storage.StoragePoolException` if ``path`` does
  103. not exist.
  104. '''
  105. if not os.path.exists(path):
  106. raise StoragePoolException('Missing file: %s' % path)