From 54a400e996b7cae1bb481de6b83018b005ef2d80 Mon Sep 17 00:00:00 2001 From: Markus Zoeller Date: Mon, 7 Nov 2016 15:57:39 +0100 Subject: [PATCH] libvirt: change get_console_output as prep work for bp/libvirt-virtlogd The implementation of bp/libvirt-virtlogd will make use of log-rotation. This change extracts the reading of the "console.log" file into one method which makes it easier for a following patch to read from multiple rotated log files. This change also adds a note to explain why virt_types other than qemu/kvm depend on the flushing into a "console.log" file as this is not immediately obvious and could be removed by mistake. Change-Id: I390879d1e08de6050c259b1bd4057b28f40d8064 --- nova/virt/libvirt/driver.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py index 9a305dd521ae..76b590e3f38b 100644 --- a/nova/virt/libvirt/driver.py +++ b/nova/virt/libvirt/driver.py @@ -2646,6 +2646,18 @@ class LibvirtDriver(driver.ComputeDriver): return fpath + def _get_console_output_file(self, instance, path): + libvirt_utils.chown(path, os.getuid()) + + with libvirt_utils.file_open(path, 'rb') as fp: + log_data, remaining = utils.last_bytes(fp, + MAX_CONSOLE_BYTES) + if remaining > 0: + LOG.info(_LI('Truncated console log returned, ' + '%d bytes ignored'), remaining, + instance=instance) + return log_data + def get_console_output(self, context, instance): guest = self._host.get_guest(instance) @@ -2669,16 +2681,7 @@ class LibvirtDriver(driver.ComputeDriver): instance=instance) return "" - libvirt_utils.chown(path, os.getuid()) - - with libvirt_utils.file_open(path, 'rb') as fp: - log_data, remaining = utils.last_bytes(fp, - MAX_CONSOLE_BYTES) - if remaining > 0: - LOG.info(_LI('Truncated console log returned, ' - '%d bytes ignored'), remaining, - instance=instance) - return log_data + return self._get_console_output_file(instance, path) # Try 'pty' types pty_consoles = tree.findall("./devices/console[@type='pty']") @@ -2702,15 +2705,15 @@ class LibvirtDriver(driver.ComputeDriver): libvirt_utils.chown(console_log, os.getuid()) data = self._flush_libvirt_console(pty) + # NOTE(markus_z): The virt_types kvm and qemu are the only ones + # which create a dedicated file device for the console logging. + # Other virt_types like xen, lxc, uml, parallels depend on the + # flush of that pty device into the "console.log" file to ensure + # that a series of "get_console_output" calls return the complete + # content even after rebooting a guest. fpath = self._append_to_file(data, console_log) - with libvirt_utils.file_open(fpath, 'rb') as fp: - log_data, remaining = utils.last_bytes(fp, MAX_CONSOLE_BYTES) - if remaining > 0: - LOG.info(_LI('Truncated console log returned, ' - '%d bytes ignored'), - remaining, instance=instance) - return log_data + return self._get_console_output_file(instance, fpath) def get_host_ip_addr(self): ips = compute_utils.get_machine_ips()