From 631eb6621f8259dea90de8ad2b4af8d16a5d95ab Mon Sep 17 00:00:00 2001 From: Alexander Schreiber <als@google.com> Date: Thu, 20 Dec 2007 09:58:35 +0000 Subject: [PATCH] Use a generic Xen hypervisor interface. Abstract the parts of the Xen hypervisor interface shared by both PVM and HVM into a generic interface, subclass the special case PVM from that. Prepatch for adding HVM support. Reviewed-by: iustinp --- lib/constants.py | 4 +- lib/hypervisor.py | 103 ++++++++++++++++++++++++++++------------------ 2 files changed, 65 insertions(+), 42 deletions(-) diff --git a/lib/constants.py b/lib/constants.py index 5dfba2b48..365472f83 100644 --- a/lib/constants.py +++ b/lib/constants.py @@ -149,7 +149,7 @@ INSTANCE_REBOOT_HARD = "hard" INSTANCE_REBOOT_FULL = "full" # Hypervisor constants -HT_XEN30 = "xen-3.0" +HT_XEN_PVM30 = "xen-3.0" HT_FAKE = "fake" -HYPER_TYPES = frozenset([HT_XEN30, HT_FAKE]) +HYPER_TYPES = frozenset([HT_XEN_PVM30, HT_FAKE]) diff --git a/lib/hypervisor.py b/lib/hypervisor.py index e2683c67d..4ba701920 100644 --- a/lib/hypervisor.py +++ b/lib/hypervisor.py @@ -42,8 +42,8 @@ def GetHypervisor(): """ ht_kind = ssconf.SimpleStore().GetHypervisorType() - if ht_kind == constants.HT_XEN30: - cls = XenHypervisor + if ht_kind == constants.HT_XEN_PVM30: + cls = XenPvmHypervisor elif ht_kind == constants.HT_FAKE: cls = FakeHypervisor else: @@ -124,48 +124,19 @@ class BaseHypervisor(object): class XenHypervisor(BaseHypervisor): - """Xen hypervisor interface""" + """Xen generic hypervisor interface + + This is the Xen base class used for both Xen PVM and HVM. It contains + all the functionality that is identical for both. + + """ @staticmethod def _WriteConfigFile(instance, block_devices, extra_args): - """Create a Xen 3.0 config file. + """A Xen instance config file. """ - config = StringIO() - config.write("# this is autogenerated by Ganeti, please do not edit\n#\n") - config.write("kernel = '%s'\n" % constants.XEN_KERNEL) - if os.path.exists(constants.XEN_INITRD): - config.write("ramdisk = '%s'\n" % constants.XEN_INITRD) - config.write("memory = %d\n" % instance.memory) - config.write("vcpus = %d\n" % instance.vcpus) - config.write("name = '%s'\n" % instance.name) - - vif_data = [] - for nic in instance.nics: - nic_str = "mac=%s, bridge=%s" % (nic.mac, nic.bridge) - ip = getattr(nic, "ip", None) - if ip is not None: - nic_str += ", ip=%s" % ip - vif_data.append("'%s'" % nic_str) - - config.write("vif = [%s]\n" % ",".join(vif_data)) - - disk_data = ["'phy:%s,%s,w'" % (rldev.dev_path, cfdev.iv_name) - for cfdev, rldev in block_devices] - config.write("disk = [%s]\n" % ",".join(disk_data)) - - config.write("root = '/dev/sda ro'\n") - config.write("on_poweroff = 'destroy'\n") - config.write("on_reboot = 'restart'\n") - config.write("on_crash = 'restart'\n") - if extra_args: - config.write("extra = '%s'\n" % extra_args) - # just in case it exists - utils.RemoveFile("/etc/xen/auto/%s" % instance.name) - f = open("/etc/xen/%s" % instance.name, "w") - f.write(config.getvalue()) - f.close() - return True + raise NotImplementedError @staticmethod def _RemoveConfigFile(instance): @@ -324,7 +295,7 @@ class XenHypervisor(BaseHypervisor): """Return a command for connecting to the console of an instance. """ - return "xm console %s" % instance.name + raise NotImplementedError def Verify(self): @@ -337,6 +308,58 @@ class XenHypervisor(BaseHypervisor): return "xend daemon is not running" +class XenPvmHypervisor(XenHypervisor): + """Xen PVM hypervisor interface""" + + @staticmethod + def _WriteConfigFile(instance, block_devices, extra_args): + """Create a Xen instance config file. + + """ + config = StringIO() + config.write("# this is autogenerated by Ganeti, please do not edit\n#\n") + config.write("kernel = '%s'\n" % constants.XEN_KERNEL) + if os.path.exists(constants.XEN_INITRD): + config.write("ramdisk = '%s'\n" % constants.XEN_INITRD) + config.write("memory = %d\n" % instance.memory) + config.write("vcpus = %d\n" % instance.vcpus) + config.write("name = '%s'\n" % instance.name) + + vif_data = [] + for nic in instance.nics: + nic_str = "mac=%s, bridge=%s" % (nic.mac, nic.bridge) + ip = getattr(nic, "ip", None) + if ip is not None: + nic_str += ", ip=%s" % ip + vif_data.append("'%s'" % nic_str) + + config.write("vif = [%s]\n" % ",".join(vif_data)) + + disk_data = ["'phy:%s,%s,w'" % (rldev.dev_path, cfdev.iv_name) + for cfdev, rldev in block_devices] + config.write("disk = [%s]\n" % ",".join(disk_data)) + + config.write("root = '/dev/sda ro'\n") + config.write("on_poweroff = 'destroy'\n") + config.write("on_reboot = 'restart'\n") + config.write("on_crash = 'restart'\n") + if extra_args: + config.write("extra = '%s'\n" % extra_args) + # just in case it exists + utils.RemoveFile("/etc/xen/auto/%s" % instance.name) + f = open("/etc/xen/%s" % instance.name, "w") + f.write(config.getvalue()) + f.close() + return True + + @staticmethod + def GetShellCommandForConsole(instance): + """Return a command for connecting to the console of an instance. + + """ + return "xm console %s" % instance.name + + class FakeHypervisor(BaseHypervisor): """Fake hypervisor interface. -- GitLab