From 7e234a4a8de206baecaed5f902e29b5a3415f8b6 Mon Sep 17 00:00:00 2001 From: Marek Marczykowski Date: Sat, 9 Jul 2011 17:52:47 +0200 Subject: [PATCH] dom0: store dispid in QubesDisposableVm object and generate proper IP (#247) --- dom0/qvm-core/qubes.py | 24 +++++++++++++++++++++--- dom0/restore/qfile-daemon-dvm | 3 ++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/dom0/qvm-core/qubes.py b/dom0/qvm-core/qubes.py index cca132d9..94307d02 100755 --- a/dom0/qvm-core/qubes.py +++ b/dom0/qvm-core/qubes.py @@ -1286,6 +1286,7 @@ class QubesNetVm(QubesVm): self.netid = netid self.__network = "10.137.{0}.0".format(netid) self.netprefix = "10.137.{0}.".format(netid) + self.dispnetprefix = "10.138.{0}.".format(netid) self.__netmask = vm_default_netmask self.__gateway = self.netprefix + "1" self.__secondary_dns = self.netprefix + "254" @@ -1333,6 +1334,11 @@ class QubesNetVm(QubesVm): assert lo >= 2 and lo <= 254, "Wrong IP address for VM" return self.netprefix + "{0}".format(lo) + def get_ip_for_dispvm(self, dispid): + lo = dispid % 254 + 1 + assert lo >= 1 and lo <= 254, "Wrong IP address for VM" + return self.dispnetprefix + "{0}".format(lo) + def get_config_params(self, source_template=None): args = super(QubesNetVm, self).get_config_params(source_template) args['kernelopts'] = ' swiotlb=force pci=nomsi' @@ -1594,16 +1600,27 @@ class QubesDisposableVm(QubesVm): template_vm = kwargs["template_vm"] assert template_vm is not None, "Missing template_vm for DisposableVM!" + self.dispid = kwargs.pop("dispid") + super(QubesDisposableVm, self).__init__(dir_path="/nonexistent", **kwargs) @property def type(self): return "DisposableVM" + @property + def ip(self): + if self.netvm_vm is not None: + return self.netvm_vm.get_ip_for_dispvm(self.dispid) + else: + return None + + def get_xml_attrs(self): attrs = {} attrs["qid"] = str(self.qid) attrs["name"] = self.name + attrs["dispid"] = str(self.dispid) attrs["template_qid"] = str(self.template_vm.qid) attrs["label"] = self.label.name return attrs @@ -1703,13 +1720,13 @@ class QubesVmCollection(dict): self[vm.qid]=vm return vm - def add_new_disposablevm(self, name, template_vm, + def add_new_disposablevm(self, name, template_vm, dispid, label = None): qid = self.get_new_unused_qid() vm = QubesDisposableVm (qid=qid, name=name, template_vm=template_vm, netvm_vm = self.get_default_netvm_vm(), - label=label) + label=label, dispid=dispid) if not self.verify_new_vm (vm): assert False, "Wrong VM description!" @@ -2203,12 +2220,13 @@ class QubesVmCollection(dict): kwargs = {} attr_list = ("qid", "name", "template_qid", - "label") + "label", "dispid") for attribute in attr_list: kwargs[attribute] = element.get(attribute) kwargs["qid"] = int(kwargs["qid"]) + kwargs["dispid"] = int(kwargs["dispid"]) kwargs["template_qid"] = int(kwargs["template_qid"]) template_vm = self[kwargs.pop("template_qid")] diff --git a/dom0/restore/qfile-daemon-dvm b/dom0/restore/qfile-daemon-dvm index eb9a42bd..67cc8496 100755 --- a/dom0/restore/qfile-daemon-dvm +++ b/dom0/restore/qfile-daemon-dvm @@ -78,7 +78,8 @@ class QfileDaemonDvm: sys.stderr.write( 'Domain ' + disptempl + ' does not exist ?') qvm_collection.unlock_db() return None - qvm_collection.add_new_disposablevm(disp_name, vm_disptempl.template_vm, label=vm.label) + dispid=int(disp_name[4:]) + qvm_collection.add_new_disposablevm(disp_name, vm_disptempl.template_vm, label=vm.label, dispid=dispid) qvm_collection.save() qvm_collection.unlock_db()