From 3be8bb31fbfb47b054d1a49050d2d53c255f7789 Mon Sep 17 00:00:00 2001 From: Michal Rostecki Date: Sun, 4 Oct 2015 22:07:50 +0200 Subject: [PATCH] Support more VM types in qvm-grow-root QubesOS/qubes-issues#1268 --- core-modules/001QubesResizableVm.py | 67 +++++++++++++++++++++++++++++ core-modules/003QubesTemplateVm.py | 16 ++++--- core-modules/01QubesAppVm.py | 10 ++++- core-modules/01QubesHVm.py | 36 +++++----------- 4 files changed, 97 insertions(+), 32 deletions(-) create mode 100644 core-modules/001QubesResizableVm.py diff --git a/core-modules/001QubesResizableVm.py b/core-modules/001QubesResizableVm.py new file mode 100644 index 00000000..cf9bcf1e --- /dev/null +++ b/core-modules/001QubesResizableVm.py @@ -0,0 +1,67 @@ +#!/usr/bin/python2 +# -*- encoding: utf8 -*- +# +# The Qubes OS Project, http://www.qubes-os.org +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# + +from __future__ import ( + absolute_import, + division, + print_function, + unicode_literals, +) + +from qubes.qubes import ( + register_qubes_vm_class, + QubesException, + QubesVm, +) + + +class QubesResizableVm(QubesVm): + + def resize_root_img(self, size): + if self.template: + raise QubesException("Cannot resize root.img of template-based VM" + ". Resize the root.img of the template " + "instead.") + + if self.is_running(): + raise QubesException("Cannot resize root.img of running VM") + + if size < self.get_root_img_sz(): + raise QubesException( + "For your own safety shringing of root.img is disabled. If " + "you really know what you are doing, use 'truncate' manually.") + + f_root = open(self.root_img, "a+b") + f_root.truncate(size) + f_root.close() + + +class QubesResizableVmWithResize2fs(QubesResizableVm): + + def resize_root_img(self, size): + super(QubesResizableVmWithResize2fs, self).resize_root_img(size) + self.start() + self.run("sudo resize2fs /dev/mapper/dmroot") + self.shutdown() + + +register_qubes_vm_class(QubesResizableVm) +register_qubes_vm_class(QubesResizableVmWithResize2fs) diff --git a/core-modules/003QubesTemplateVm.py b/core-modules/003QubesTemplateVm.py index e5c992ce..4d9a6c79 100644 --- a/core-modules/003QubesTemplateVm.py +++ b/core-modules/003QubesTemplateVm.py @@ -23,14 +23,20 @@ # import os -import subprocess import sys -from qubes.qubes import QubesVm,register_qubes_vm_class,dry_run -from qubes.qubes import QubesVmCollection,QubesException,QubesVmLabels -from qubes.qubes import defaults,system_path,vm_files,vmm +from qubes.qubes import ( + defaults, + dry_run, + register_qubes_vm_class, + system_path, + vmm, + QubesResizableVmWithResize2fs, + QubesVmCollection, +) -class QubesTemplateVm(QubesVm): + +class QubesTemplateVm(QubesResizableVmWithResize2fs): """ A class that represents an TemplateVM. A child of QubesVm. """ diff --git a/core-modules/01QubesAppVm.py b/core-modules/01QubesAppVm.py index dd119a0e..9a46fb47 100644 --- a/core-modules/01QubesAppVm.py +++ b/core-modules/01QubesAppVm.py @@ -24,9 +24,15 @@ import os.path -from qubes.qubes import QubesVm,QubesVmLabel,register_qubes_vm_class,system_path +from qubes.qubes import ( + register_qubes_vm_class, + system_path, + QubesResizableVmWithResize2fs, + QubesVmLabel, +) -class QubesAppVm(QubesVm): + +class QubesAppVm(QubesResizableVmWithResize2fs): """ A class that represents an AppVM. A child of QubesVm. """ diff --git a/core-modules/01QubesHVm.py b/core-modules/01QubesHVm.py index 8ce214ac..c3b692a8 100644 --- a/core-modules/01QubesHVm.py +++ b/core-modules/01QubesHVm.py @@ -26,16 +26,20 @@ import os import os.path import signal import subprocess -import stat import sys -import re import shutil -import stat from xml.etree import ElementTree -from qubes.qubes import QubesVm,register_qubes_vm_class,vmm,dry_run -from qubes.qubes import system_path,defaults -from qubes.qubes import QubesException +from qubes.qubes import ( + dry_run, + defaults, + register_qubes_vm_class, + system_path, + vmm, + QubesException, + QubesResizableVm, +) + system_path["config_template_hvm"] = '/usr/share/qubes/vm-template-hvm.xml' @@ -44,7 +48,7 @@ defaults["hvm_private_img_size"] = 2*1024*1024*1024 defaults["hvm_memory"] = 512 -class QubesHVm(QubesVm): +class QubesHVm(QubesResizableVm): """ A class that represents an HVM. A child of QubesVm. """ @@ -235,24 +239,6 @@ class QubesHVm(QubesVm): self.storage.resize_private_img(size) - def resize_root_img(self, size): - if self.template: - raise QubesException("Cannot resize root.img of template-based VM" - ". Resize the root.img of the template " - "instead.") - - if self.is_running(): - raise QubesException("Cannot resize root.img of running HVM") - - if size < self.get_root_img_sz(): - raise QubesException( - "For your own safety shringing of root.img is disabled. If " - "you really know what you are doing, use 'truncate' manually.") - - f_root = open (self.root_img, "a+b") - f_root.truncate (size) - f_root.close () - def get_config_params(self): params = super(QubesHVm, self).get_config_params()