kernels.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/python2
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2015 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. ''' This module contains pool implementations for different OS kernels. '''
  25. import os
  26. from qubes.storage import Pool, StoragePoolException, Volume
  27. class LinuxModules(Volume):
  28. ''' A volume representing a ro linux kernel '''
  29. rw = False
  30. def __init__(self, target_dir, kernel_version, **kwargs):
  31. kwargs['vid'] = kernel_version
  32. kwargs['source'] = self
  33. super(LinuxModules, self).__init__(**kwargs)
  34. self.kernels_dir = os.path.join(target_dir, kernel_version)
  35. self.path = os.path.join(self.kernels_dir, 'modules.img')
  36. self.vmlinuz = os.path.join(self.kernels_dir, 'vmlinuz')
  37. self.initramfs = os.path.join(self.kernels_dir, 'initramfs')
  38. @property
  39. def revisions(self):
  40. return {}
  41. class LinuxKernel(Pool):
  42. ''' Provides linux kernels '''
  43. driver = 'linux-kernel'
  44. def __init__(self, name=None, dir_path=None):
  45. assert dir_path, 'Missing dir_path'
  46. super(LinuxKernel, self).__init__(name=name)
  47. self.dir_path = dir_path
  48. def init_volume(self, vm, volume_config):
  49. assert not volume_config['rw']
  50. volume = LinuxModules(self.dir_path, vm.kernel, **volume_config)
  51. return volume
  52. def is_dirty(self, volume):
  53. return False
  54. def clone(self, source, target):
  55. return target
  56. def create(self, volume):
  57. return volume
  58. def commit(self, volume):
  59. return volume
  60. @property
  61. def config(self):
  62. return {
  63. 'name': self.name,
  64. 'dir_path': self.dir_path,
  65. 'driver': LinuxKernel.driver,
  66. }
  67. def destroy(self):
  68. pass
  69. def export(self, volume):
  70. return volume.path
  71. def import_volume(self, dst_pool, dst_volume, src_pool, src_volume):
  72. pass
  73. def is_outdated(self, volume):
  74. return False
  75. def remove(self, volume):
  76. pass
  77. def rename(self, volume, old_name, new_name):
  78. return volume
  79. def setup(self):
  80. pass
  81. def start(self, volume):
  82. path = volume.path
  83. if not os.path.exists(path):
  84. raise StoragePoolException('Missing kernel modules: %s' % path)
  85. return volume
  86. def stop(self, volume):
  87. pass
  88. def verify(self, volume):
  89. _check_path(volume.path)
  90. _check_path(volume.vmlinuz)
  91. _check_path(volume.initramfs)
  92. @property
  93. def volumes(self):
  94. ''' Return all known kernel volumes '''
  95. return [LinuxModules(self.dir_path,
  96. kernel_version,
  97. pool=self.name,
  98. name=kernel_version,
  99. internal=True,
  100. rw=False
  101. )
  102. for kernel_version in os.listdir(self.dir_path)]
  103. def _check_path(path):
  104. ''' Raise an :py:class:`qubes.storage.StoragePoolException` if ``path`` does
  105. not exist.
  106. '''
  107. if not os.path.exists(path):
  108. raise StoragePoolException('Missing file: %s' % path)