core: split HVM template into separate class

This commit is contained in:
Marek Marczykowski-Górecki 2013-11-25 07:14:06 +01:00
parent 2891a15302
commit 5033b53543
3 changed files with 93 additions and 15 deletions

View File

@ -87,9 +87,6 @@ class QubesHVm(QubesVm):
if self.guiagent_installed: if self.guiagent_installed:
self._start_guid_first = False self._start_guid_first = False
# The QubesHVM can be a template itself, so collect appvms based on it
self.appvms = QubesVmCollection()
@property @property
def type(self): def type(self):
return "HVM" return "HVM"
@ -97,13 +94,9 @@ class QubesHVm(QubesVm):
def is_appvm(self): def is_appvm(self):
return True return True
def is_template(self):
# Any non-template based HVM can be a template itself
return self.template is None
@classmethod @classmethod
def is_template_compatible(cls, template): def is_template_compatible(cls, template):
if template and (not template.is_template() or template.type != "HVM"): if template and (not template.is_template() or template.type != "TemplateHVM"):
return False return False
return True return True
@ -322,9 +315,6 @@ class QubesHVm(QubesVm):
return -1 return -1
def start(self, *args, **kwargs): def start(self, *args, **kwargs):
for vm in self.appvms.values():
if vm.is_running():
raise QubesException("Cannot start HVM template while VMs based on it are running")
if self.template and self.template.is_running(): if self.template and self.template.is_running():
raise QubesException("Cannot start the HVM while its template is running") raise QubesException("Cannot start the HVM while its template is running")
try: try:

View File

@ -0,0 +1,77 @@
#!/usr/bin/python2
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
# Copyright (C) 2013 Marek Marczykowski <marmarek@invisiblethingslab.com>
#
# 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.
#
#
import os
import os.path
import subprocess
import stat
import sys
import re
from qubes.qubes import QubesHVm,register_qubes_vm_class,xs,xc,dry_run
from qubes.qubes import QubesException,QubesVmCollection
from qubes.qubes import system_path,defaults
class QubesTemplateHVm(QubesHVm):
"""
A class that represents an HVM template. A child of QubesHVm.
"""
# In which order load this VM type from qubes.xml
load_order = 50
def get_attrs_config(self):
attrs_config = super(QubesTemplateHVm, self).get_attrs_config()
attrs_config['dir_path']['eval'] = 'value if value is not None else os.path.join(system_path["qubes_templates_dir"], self.name)'
attrs_config['label']['default'] = defaults["template_label"]
return attrs_config
def __init__(self, **kwargs):
super(QubesTemplateHVm, self).__init__(**kwargs)
self.appvms = QubesVmCollection()
@property
def type(self):
return "TemplateHVM"
@property
def updateable(self):
return True
def is_template(self):
return True
@classmethod
def is_template_compatible(cls, template):
return False
def start(self, *args, **kwargs):
for vm in self.appvms.values():
if vm.is_running():
raise QubesException("Cannot start HVM template while VMs based on it are running")
super(QubesTemplateHVm, self).start(*args, **kwargs)
register_qubes_vm_class(QubesTemplateHVm)

View File

@ -39,7 +39,9 @@ def main():
parser.add_option ("-p", "--proxy", action="store_true", dest="proxyvm", default=False, parser.add_option ("-p", "--proxy", action="store_true", dest="proxyvm", default=False,
help="Create ProxyVM") help="Create ProxyVM")
parser.add_option ("-H", "--hvm", action="store_true", dest="hvm", default=False, parser.add_option ("-H", "--hvm", action="store_true", dest="hvm", default=False,
help="Create HVM (implies --standalone)") help="Create HVM (standalone unless --template option used)")
parser.add_option ("--hvm-template", action="store_true", dest="hvm_template", default=False,
help="Create HVM template")
parser.add_option ("-n", "--net", action="store_true", dest="netvm", default=False, parser.add_option ("-n", "--net", action="store_true", dest="netvm", default=False,
help="Create NetVM") help="Create NetVM")
parser.add_option ("-s", "--standalone", action="store_true", dest="standalone", default=False, parser.add_option ("-s", "--standalone", action="store_true", dest="standalone", default=False,
@ -61,8 +63,8 @@ def main():
parser.error ("You must specify VM name!") parser.error ("You must specify VM name!")
vmname = args[0] vmname = args[0]
if options.netvm and options.proxyvm: if (options.netvm + options.proxyvm + options.hvm + options.hvm_template) > 1:
parser.error ("You must specify at most one of --proxy and --net") parser.error ("You must specify at most one VM type switch")
if os.geteuid() == 0: if os.geteuid() == 0:
print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems." print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
@ -91,6 +93,13 @@ def main():
print >> sys.stderr, "root.img can be specified only for standalone VMs" print >> sys.stderr, "root.img can be specified only for standalone VMs"
exit (1) exit (1)
if options.hvm_template and options.template is not None:
print >> sys.stderr, "Template VM cannot be based on another template"
exit (1)
if options.hvm and not options.template:
options.standalone = True
if options.root is not None and not os.path.exists(options.root): if options.root is not None and not os.path.exists(options.root):
print >> sys.stderr, "File specified as root.img does not exists" print >> sys.stderr, "File specified as root.img does not exists"
exit (1) exit (1)
@ -115,7 +124,7 @@ def main():
if (options.verbose): if (options.verbose):
print "--> Using TemplateVM: {0}".format(template.name) print "--> Using TemplateVM: {0}".format(template.name)
elif not options.hvm: elif not options.hvm and not options.hvm_template:
if qvm_collection.get_default_template() is None: if qvm_collection.get_default_template() is None:
print >> sys.stderr, "No default TemplateVM defined!" print >> sys.stderr, "No default TemplateVM defined!"
exit (1) exit (1)
@ -136,6 +145,8 @@ def main():
vmtype = "QubesProxyVm" vmtype = "QubesProxyVm"
elif options.hvm: elif options.hvm:
vmtype = "QubesHVm" vmtype = "QubesHVm"
elif options.hvm_template:
vmtype = "QubesTemplateHVm"
else: else:
vmtype = "QubesAppVm" vmtype = "QubesAppVm"