From 3d88e123624694b557f44cad44335bcadc404631 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Wed, 14 Sep 2011 14:38:07 -0700 Subject: fix rescue to use the base image, reset firewall rules, and accept network_info --- nova/virt/libvirt/connection.py | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/nova/virt/libvirt/connection.py b/nova/virt/libvirt/connection.py index 363a20ed0..c7445fe50 100644 --- a/nova/virt/libvirt/connection.py +++ b/nova/virt/libvirt/connection.py @@ -29,9 +29,9 @@ Supports KVM, LXC, QEMU, UML, and XEN. (default: kvm). :libvirt_uri: Override for the default libvirt URI (depends on libvirt_type). :libvirt_xml_template: Libvirt XML Template. -:rescue_image_id: Rescue ami image (default: ami-rescue). -:rescue_kernel_id: Rescue aki image (default: aki-rescue). -:rescue_ramdisk_id: Rescue ari image (default: ari-rescue). +:rescue_image_id: Rescue ami image (None = original image). +:rescue_kernel_id: Rescue aki image (None = original image). +:rescue_ramdisk_id: Rescue ari image (None = original image). :injected_network_template: Template file for injected network :allow_same_net_traffic: Whether to allow in project network traffic @@ -83,9 +83,9 @@ LOG = logging.getLogger('nova.virt.libvirt_conn') FLAGS = flags.FLAGS flags.DECLARE('live_migration_retry_count', 'nova.compute.manager') # TODO(vish): These flags should probably go into a shared location -flags.DEFINE_string('rescue_image_id', 'ami-rescue', 'Rescue ami image') -flags.DEFINE_string('rescue_kernel_id', 'aki-rescue', 'Rescue aki image') -flags.DEFINE_string('rescue_ramdisk_id', 'ari-rescue', 'Rescue ari image') +flags.DEFINE_string('rescue_image_id', None, 'Rescue ami image') +flags.DEFINE_string('rescue_kernel_id', None, 'Rescue aki image') +flags.DEFINE_string('rescue_ramdisk_id', None, 'Rescue ari image') flags.DEFINE_string('libvirt_xml_template', utils.abspath('virt/libvirt.xml.template'), 'Libvirt XML Template') @@ -466,18 +466,22 @@ class LibvirtConnection(driver.ComputeDriver): shutil.rmtree(temp_dir) @exception.wrap_exception() - def reboot(self, instance, network_info): + def reboot(self, instance, network_info, regenerate_xml=False): """Reboot a virtual machine, given an instance reference. This method actually destroys and re-creates the domain to ensure the reboot happens, as the guest OS cannot ignore this action. """ + # NOTE(vish): this should accept block device info virt_dom = self._conn.lookupByName(instance['name']) # NOTE(itoumsn): Use XML delived from the running instance # instead of using to_xml(instance, network_info). This is almost # the ultimate stupid workaround. - xml = virt_dom.XMLDesc(0) + if regenerate_xml: + xml = self.to_xml(instance, network_info, False) + else: + xml = virt_dom.XMLDesc(0) # NOTE(itoumsn): self.shutdown() and wait instead of self.destroy() is # better because we cannot ensure flushing dirty buffers # in the guest OS. But, in case of KVM, shutdown() does not work... @@ -544,11 +548,17 @@ class LibvirtConnection(driver.ComputeDriver): self.destroy(instance, network_info, cleanup=False) xml = self.to_xml(instance, network_info, rescue=True) - rescue_images = {'image_id': FLAGS.rescue_image_id, - 'kernel_id': FLAGS.rescue_kernel_id, - 'ramdisk_id': FLAGS.rescue_ramdisk_id} - self._create_image(context, instance, xml, '.rescue', rescue_images) + rescue_images = { + 'image_id': FLAGS.rescue_image_id or instance['image_ref'], + 'kernel_id': FLAGS.rescue_kernel_id or instance['kernel_id'], + 'ramdisk_id': FLAGS.rescue_ramdisk_id or instance['ramdisk_id'], + } + self._create_image(context, instance, xml, '.rescue', rescue_images, + network_info=network_info) + self.firewall_driver.setup_basic_filtering(instance, network_info) + self.firewall_driver.prepare_instance_filter(instance, network_info) self._create_new_domain(xml) + self.firewall_driver.apply_instance_filter(instance, network_info) def _wait_for_rescue(): """Called at an interval until the VM is running again.""" @@ -557,7 +567,7 @@ class LibvirtConnection(driver.ComputeDriver): try: state = self.get_info(instance_name)['state'] except exception.NotFound: - msg = _("During reboot, %s disappeared.") % instance_name + msg = _("During rescue, %s disappeared.") % instance_name LOG.error(msg) raise utils.LoopingCallDone @@ -570,14 +580,15 @@ class LibvirtConnection(driver.ComputeDriver): return timer.start(interval=0.5, now=True) @exception.wrap_exception() - def unrescue(self, instance, network_info): + def unrescue(self, instance, callback, network_info): """Reboot the VM which is being rescued back into primary images. Because reboot destroys and re-creates instances, unresue should simply call reboot. """ - self.reboot(instance, network_info) + # NOTE(vish): this should accept block device info + self.reboot(instance, network_info, regenerate_xml=True) @exception.wrap_exception() def poll_rescued_instances(self, timeout): -- cgit From d0daaaa320ecb408e721d5e856676a87d7e98ea6 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Wed, 14 Sep 2011 15:28:22 -0700 Subject: write out xml for rescue --- nova/virt/libvirt/connection.py | 52 +++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/nova/virt/libvirt/connection.py b/nova/virt/libvirt/connection.py index c7445fe50..56a0f3bb0 100644 --- a/nova/virt/libvirt/connection.py +++ b/nova/virt/libvirt/connection.py @@ -466,22 +466,20 @@ class LibvirtConnection(driver.ComputeDriver): shutil.rmtree(temp_dir) @exception.wrap_exception() - def reboot(self, instance, network_info, regenerate_xml=False): + def reboot(self, instance, network_info, xml=None): """Reboot a virtual machine, given an instance reference. This method actually destroys and re-creates the domain to ensure the reboot happens, as the guest OS cannot ignore this action. """ - # NOTE(vish): this should accept block device info virt_dom = self._conn.lookupByName(instance['name']) # NOTE(itoumsn): Use XML delived from the running instance # instead of using to_xml(instance, network_info). This is almost # the ultimate stupid workaround. - if regenerate_xml: - xml = self.to_xml(instance, network_info, False) - else: + if not xml: xml = virt_dom.XMLDesc(0) + # NOTE(itoumsn): self.shutdown() and wait instead of self.destroy() is # better because we cannot ensure flushing dirty buffers # in the guest OS. But, in case of KVM, shutdown() does not work... @@ -545,7 +543,15 @@ class LibvirtConnection(driver.ComputeDriver): data recovery. """ - self.destroy(instance, network_info, cleanup=False) + + virt_dom = self._conn.lookupByName(instance['name']) + unrescue_xml = virt_dom.XMLDesc(0) + unrescue_xml_path = os.path.join(FLAGS.instances_path, + instance['name'], + 'unrescue.xml') + f = open(unrescue_xml_path, 'w') + f.write(unrescue_xml) + f.close() xml = self.to_xml(instance, network_info, rescue=True) rescue_images = { @@ -555,29 +561,7 @@ class LibvirtConnection(driver.ComputeDriver): } self._create_image(context, instance, xml, '.rescue', rescue_images, network_info=network_info) - self.firewall_driver.setup_basic_filtering(instance, network_info) - self.firewall_driver.prepare_instance_filter(instance, network_info) - self._create_new_domain(xml) - self.firewall_driver.apply_instance_filter(instance, network_info) - - def _wait_for_rescue(): - """Called at an interval until the VM is running again.""" - instance_name = instance['name'] - - try: - state = self.get_info(instance_name)['state'] - except exception.NotFound: - msg = _("During rescue, %s disappeared.") % instance_name - LOG.error(msg) - raise utils.LoopingCallDone - - if state == power_state.RUNNING: - msg = _("Instance %s rescued successfully.") % instance_name - LOG.info(msg) - raise utils.LoopingCallDone - - timer = utils.LoopingCall(_wait_for_rescue) - return timer.start(interval=0.5, now=True) + self.reboot(instance, network_info, xml=xml) @exception.wrap_exception() def unrescue(self, instance, callback, network_info): @@ -587,8 +571,14 @@ class LibvirtConnection(driver.ComputeDriver): simply call reboot. """ - # NOTE(vish): this should accept block device info - self.reboot(instance, network_info, regenerate_xml=True) + unrescue_xml_path = os.path.join(FLAGS.instances_path, + instance['name'], + 'unrescue.xml') + f = open(unrescue_xml_path) + unrescue_xml = f.read() + f.close() + os.remove(unrescue_xml_path) + self.reboot(instance, network_info, xml=unrescue_xml) @exception.wrap_exception() def poll_rescued_instances(self, timeout): -- cgit From 6b3ac77dabc3891648399c367546474fe3450fd1 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Fri, 16 Sep 2011 09:40:24 -0400 Subject: Add a FakeVirDomainSnapshot and return it from snapshotCreateXML. Fixes libvirt snapshot tests. --- nova/tests/test_libvirt.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index b7c1ef1ab..1924ae050 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -52,6 +52,15 @@ def _concurrency(wait, done, target): done.send() +class FakeVirDomainSnapshot(object): + + def __init__(self, dom=None): + self.dom = dom + + def delete(self, flags): + pass + + class FakeVirtDomain(object): def __init__(self, fake_xml=None): @@ -69,7 +78,7 @@ class FakeVirtDomain(object): """ def snapshotCreateXML(self, *args): - return None + return FakeVirDomainSnapshot(self) def createWithFlags(self, launch_flags): pass -- cgit From e09b3d70e5a4aaa00f5ba62d1eab5d6d57a800e1 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 16 Sep 2011 08:44:25 -0700 Subject: fix permissions --- nova/virt/libvirt/connection.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/nova/virt/libvirt/connection.py b/nova/virt/libvirt/connection.py index 31b08be68..4e91415a7 100644 --- a/nova/virt/libvirt/connection.py +++ b/nova/virt/libvirt/connection.py @@ -814,8 +814,10 @@ class LibvirtConnection(driver.ComputeDriver): utils.execute('mkdir', '-p', container_dir) # NOTE(vish): No need add the suffix to console.log - os.close(os.open(basepath('console.log', ''), - os.O_CREAT | os.O_WRONLY, 0660)) + console_log = basepath('console.log', '') + if os.path.exists(console_log): + utils.execute('chown', os.getuid(), console_log, run_as_root=True) + os.close(os.open(console_log, os.O_CREAT | os.O_WRONLY, 0660)) if not disk_images: disk_images = {'image_id': inst['image_ref'], -- cgit