From feaaaa75fad902f05204a3e632318d4150432492 Mon Sep 17 00:00:00 2001 From: Bahtiar `kalkin-` Gadimov Date: Sat, 21 Nov 2015 13:22:27 +0100 Subject: [PATCH 1/4] Move the vmdir logic from XenPool to Pool Any storage implementation needs this logic for saving the vm config and `*.desktop` files. --- core/storage/__init__.py | 67 ++++++++++++++++++++++++++++++++++++++-- core/storage/xen.py | 64 ++------------------------------------ 2 files changed, 68 insertions(+), 63 deletions(-) diff --git a/core/storage/__init__.py b/core/storage/__init__.py index 8e2ac720..0c302082 100644 --- a/core/storage/__init__.py +++ b/core/storage/__init__.py @@ -30,7 +30,7 @@ import subprocess import sys import qubes.qubesutils -from qubes.qubes import QubesException, defaults, system_path, vm_files +from qubes.qubes import QubesException, defaults, system_path CONFIG_FILE = '/etc/qubes/storage.conf' @@ -321,4 +321,67 @@ class StoragePoolException(QubesException): class Pool(object): - pass + def __init__(self, vm, dir_path): + assert vm is not None + assert dir_path is not None + + self.vm = vm + self.dir_path = dir_path + + self.create_dir_if_not_exists(self.dir_path) + + self.vmdir = self.vmdir_path(vm, self.dir_path) + + appvms_path = os.path.join(self.dir_path, 'appvms') + self.create_dir_if_not_exists(appvms_path) + + servicevms_path = os.path.join(self.dir_path, 'servicevms') + self.create_dir_if_not_exists(servicevms_path) + + vm_templates_path = os.path.join(self.dir_path, 'vm-templates') + self.create_dir_if_not_exists(vm_templates_path) + + def vmdir_path(self, vm, pool_dir): + """ Returns the path to vmdir depending on the type of the VM. + + The default QubesOS file storage saves the vm images in three + different directories depending on the ``QubesVM`` type: + + * ``appvms`` for ``QubesAppVm`` or ``QubesHvm`` + * ``vm-templates`` for ``QubesTemplateVm`` or ``QubesTemplateHvm`` + * ``servicevms`` for any subclass of ``QubesNetVm`` + + Args: + vm: a QubesVM + pool_dir: the root directory of the pool + + Returns: + string (str) absolute path to the directory where the vm files + are stored + """ + # TODO: This is a hack, circular dependencies problem? + from qubes.qubes import (QubesAppVm, QubesDisposableVm, QubesHVm, + QubesNetVm, QubesTemplateHVm, QubesTemplateVm) + vm_type = type(vm) + + if vm_type in [QubesAppVm, QubesHVm]: + subdir = 'appvms' + elif vm_type in [QubesTemplateVm, QubesTemplateHVm]: + subdir = 'vm-templates' + elif issubclass(vm_type, QubesNetVm): + subdir = 'servicevms' + elif vm_type is QubesDisposableVm: + subdir = 'appvms' + return os.path.join(pool_dir, subdir, vm.template.name + '-dvm') + else: + raise QubesException(str(vm_type) + ' unknown vm type') + + return os.path.join(pool_dir, subdir, vm.name) + + def create_dir_if_not_exists(self, path): + """ Check if a directory exists in if not create it. + + This method does not create any parent directories. + """ + if not os.path.exists(path): + os.mkdir(path) diff --git a/core/storage/xen.py b/core/storage/xen.py index 1cd5dc97..11ff9b67 100644 --- a/core/storage/xen.py +++ b/core/storage/xen.py @@ -28,9 +28,7 @@ import re import subprocess import sys -from qubes.qubes import (QubesAppVm, QubesDisposableVm, QubesException, - QubesHVm, QubesNetVm, QubesTemplateHVm, - QubesTemplateVm, defaults, vm_files) +from qubes.qubes import QubesException, vm_files from qubes.storage import Pool, QubesVmStorage @@ -275,64 +273,8 @@ class XenStorage(QubesVmStorage): class XenPool(Pool): def __init__(self, vm, dir_path): - assert vm is not None - assert dir_path is not None - - appvms_path = os.path.join(dir_path, 'appvms') - servicevms_path = os.path.join(dir_path, 'servicevms') - vm_templates_path = os.path.join(dir_path, 'vm-templates') - - self._create_dir_if_not_exists(dir_path) - self._create_dir_if_not_exists(appvms_path) - self._create_dir_if_not_exists(servicevms_path) - self._create_dir_if_not_exists(vm_templates_path) - - self.vmdir = self._vmdir_path(vm, dir_path) - self.vm = vm - self.dir_path = dir_path + super(XenPool, self).__init__(vm, dir_path) def getStorage(self): """ Returns an instantiated ``XenStorage``. """ - return defaults['storage_class'](self.vm, vmdir=self.vmdir) - - def _vmdir_path(self, vm, pool_dir): - """ Get the vm dir depending on the type of the VM. - - The default QubesOS file storage saves the vm images in three - different directories depending on the ``QubesVM`` type: - - * ``appvms`` for ``QubesAppVm`` or ``QubesHvm`` - * ``vm-templates`` for ``QubesTemplateVm`` or ``QubesTemplateHvm`` - * ``servicevms`` for any subclass of ``QubesNetVm`` - - Args: - vm: a QubesVM - pool_dir: the root directory of the pool - - Returns: - string (str) absolute path to the directory where the vm files - are stored - """ - vm_type = type(vm) - - if vm_type in [QubesAppVm, QubesHVm]: - subdir = 'appvms' - elif vm_type in [QubesTemplateVm, QubesTemplateHVm]: - subdir = 'vm-templates' - elif issubclass(vm_type, QubesNetVm): - subdir = 'servicevms' - elif vm_type is QubesDisposableVm: - subdir = 'appvms' - return os.path.join(pool_dir, subdir, vm.template.name + '-dvm') - else: - raise QubesException(str(vm_type) + ' unknown vm type') - - return os.path.join(pool_dir, subdir, vm.name) - - def _create_dir_if_not_exists(self, path): - """ Check if a directory exists in if not it is created. - - This method does not create any parent directories. - """ - if not os.path.exists(path): - os.mkdir(path) + return XenStorage(self.vm, vmdir=self.vmdir) From 9b23576ff6a93a5ac2e90772e0af1bbdda117dc2 Mon Sep 17 00:00:00 2001 From: Bahtiar `kalkin-` Gadimov Date: Sun, 22 Nov 2015 17:18:42 +0100 Subject: [PATCH 2/4] Provide method format_disk_dev() to all storages The method XenStorage._format_disk_dev() generates the xml config for a device. It is not specific to the Xen file storage implementation. It can and must be reused by other storage implementations --- core/storage/__init__.py | 19 +++++++++++++++++++ core/storage/xen.py | 35 ++++++++--------------------------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/core/storage/__init__.py b/core/storage/__init__.py index 0c302082..530198e3 100644 --- a/core/storage/__init__.py +++ b/core/storage/__init__.py @@ -65,6 +65,25 @@ class QubesVmStorage(object): # Additional drive (currently used only by HVM) self.drive = None + def format_disk_dev(self, path, script, vdev, rw=True, type="disk", + domain=None): + if path is None: + return '' + template = " \n" \ + " \n" \ + " \n" \ + " \n" \ + "{params}" \ + " \n" + params = "" + if not rw: + params += " \n" + if domain: + params += " \n" % domain + if script: + params += "