core-admin/qubes/storage/kernels.py

228 lines
6.3 KiB
Python
Raw Permalink Normal View History

2016-04-01 15:10:44 +02:00
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
# Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
# Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
2016-04-01 15:10:44 +02:00
#
# This library is distributed in the hope that it will be useful,
2016-04-01 15:10:44 +02:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
2016-04-01 15:10:44 +02:00
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
2016-04-01 15:10:44 +02:00
#
''' This module contains pool implementations for different OS kernels. '''
2016-04-01 15:10:44 +02:00
import os
import qubes.exc
import qubes.storage
2016-04-01 15:10:44 +02:00
from qubes.storage import Pool, StoragePoolException, Volume
class LinuxModules(Volume):
''' A volume representing a ro linux kernel '''
2016-04-01 15:10:44 +02:00
def __init__(self, target_dir, kernel_version, **kwargs):
kwargs['vid'] = ''
2020-06-22 16:03:20 +02:00
super().__init__(**kwargs)
self._kernel_version = kernel_version
self.target_dir = target_dir
assert self.revisions_to_keep == 0
assert self.rw is False
@property
def vid(self):
if callable(self._kernel_version):
return self._kernel_version()
return self._kernel_version
@vid.setter
def vid(self, value):
# ignore
pass
@property
def kernels_dir(self):
kernel_version = self.vid
if not kernel_version:
return None
return os.path.join(self.target_dir, kernel_version)
@property
def path(self):
kernels_dir = self.kernels_dir
if not kernels_dir:
return None
return os.path.join(kernels_dir, 'modules.img')
@property
def vmlinuz(self):
kernels_dir = self.kernels_dir
if not kernels_dir:
return None
return os.path.join(kernels_dir, 'vmlinuz')
@property
def initramfs(self):
kernels_dir = self.kernels_dir
if not kernels_dir:
return None
return os.path.join(kernels_dir, 'initramfs')
2016-04-01 15:10:44 +02:00
2016-07-12 18:16:15 +02:00
@property
def revisions(self):
return {}
2016-04-01 15:10:44 +02:00
def is_dirty(self):
return False
def import_volume(self, src_volume):
if isinstance(src_volume, LinuxModules):
# do nothing
return self
raise StoragePoolException('clone of LinuxModules volume from '
'different volume type is not supported')
def create(self):
return self
def remove(self):
pass
def commit(self):
return self
def export(self):
return self.path
def is_outdated(self):
return False
@property
def revisions_to_keep(self):
return 0
@revisions_to_keep.setter
def revisions_to_keep(self, value):
# pylint: disable=no-self-use
if value:
raise qubes.exc.QubesValueError(
'LinuxModules supports only revisions_to_keep=0')
@property
def rw(self):
return False
@rw.setter
def rw(self, value):
# pylint: disable=no-self-use
if value:
raise qubes.exc.QubesValueError(
'LinuxModules supports only read-only volumes')
def start(self):
return self
def stop(self):
pass
def verify(self):
if self.vid:
_check_path(self.vmlinuz)
_check_path(self.initramfs)
def block_device(self):
path = self.path
# create block device for modules.img only if:
# - there is kernel set for the VM
# - that kernel directory contains modules.img file
if path and os.path.exists(path):
return super().block_device()
return None
2016-04-01 15:10:44 +02:00
class LinuxKernel(Pool):
''' Provides linux kernels '''
2016-04-01 15:10:44 +02:00
driver = 'linux-kernel'
def __init__(self, *, name, dir_path):
2020-06-22 16:03:20 +02:00
super().__init__(name=name, revisions_to_keep=0)
2016-04-01 15:10:44 +02:00
self.dir_path = dir_path
def init_volume(self, vm, volume_config):
2016-07-12 18:16:15 +02:00
assert not volume_config['rw']
2016-04-01 15:10:44 +02:00
# migrate old config
if volume_config.get('snap_on_start', False) and not \
volume_config.get('source', None):
volume_config['snap_on_start'] = False
if volume_config.get('save_on_stop', False):
raise NotImplementedError(
'LinuxKernel pool does not support save_on_stop=True')
volume_config['pool'] = self
volume = LinuxModules(self.dir_path, lambda: vm.kernel, **volume_config)
return volume
2016-04-01 15:10:44 +02:00
@property
def config(self):
return {
'name': self.name,
'dir_path': self.dir_path,
'driver': LinuxKernel.driver,
}
2016-04-15 20:33:04 +02:00
def destroy(self):
pass
2016-07-12 18:16:15 +02:00
def import_volume(self, dst_pool, dst_volume, src_pool, src_volume):
pass
2016-04-15 20:33:04 +02:00
def setup(self):
pass
@property
def revisions_to_keep(self):
return 0
@revisions_to_keep.setter
def revisions_to_keep(self, value):
# pylint: disable=no-self-use
if value:
raise qubes.exc.QubesValueError(
'LinuxKernel supports only revisions_to_keep=0')
def included_in(self, app):
''' Check if there is pool containing /var/lib/qubes/vm-kernels '''
return qubes.storage.search_pool_containing_dir(
[pool for pool in app.pools.values() if pool is not self],
self.dir_path)
def list_volumes(self):
''' Return all known kernel volumes '''
return [LinuxModules(self.dir_path,
kernel_version,
pool=self,
name=kernel_version,
2016-07-12 18:16:15 +02:00
rw=False
)
for kernel_version in os.listdir(self.dir_path)]
def _check_path(path):
''' Raise an :py:class:`qubes.storage.StoragePoolException` if ``path`` does
not exist.
'''
if not os.path.exists(path):
raise StoragePoolException('Missing file: %s' % path)