Skip to contentSkip to conversation

scripts/qemu.py fixes and cleanups

PATCH Superseded Amador Pahim 6 participants 41 messages Sep 1, 2017 – Sep 21, 2017

v8 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v8 cover scripts/qemu.py fixes and cleanups

Changes v1->v2:
- Style fixes to make checkpatch.pl happy.
- Rebased.
Changes v2->v3:
- Fix typo in patch 3 ("qemu.py: make 'args' public") commit message.
Changes v3->v4:
- Squash the 2 first commits since they are co-dependant.
- Cleanup launch() and shutdown().
- Reorder the commits, putting the rename of self._args first.
- Rebased.
Changes v4->v5:
- Break the cleanup commit into logical changes and include in the commit messages the rationale for making them.
Changes v5->v6:
- Remove the commit to rename self._args.
- Fix is_running() return before first call to maunch().
- Use python logging system.
- Include the full command line on negative exit code error message.
- Use os.path.null instead of /dev/null.
- Improve the control over the created/deleted files.
Changes v6->v7:
- Split commits in self-contained/atomic changes.
- Addressed the comments from previous version, basically improving the logging messages and the control over created files. See individual commit messages for details.
Changes v7->v8:
- Rebased.
- Reorder commits to avoid break->fix sequence.
- Split commits "use poll() instead of 'returncode'" and "refactor launch()".
- Don't ignore errors in _load_io_log(). Instead, check if we created the file before reading it.
- Use LOG.warn() instead of LOG.debug() for the negative exit code message.
- Fix the exception name called in commits "launch vm only if it's not running" and "don't launch again before shutdown()".
- Minor style fixes.

Amador Pahim (13):
qemu.py: fix is_running() return before first launch() qemu.py: avoid writing to stdout/stderr qemu.py: use os.path.null instead of /dev/null qemu.py: improve message on negative exit code qemu.py: include debug information on launch error qemu.py: make sure we only remove files we create qemu.py: close _qemu_log_path on cleanup qemu.py: refactor launch()
qemu.py: always cleanup on shutdown() qemu.py: use poll() instead of 'returncode' qemu.py: cleanup redundant calls in launch() qemu.py: launch vm only if it's not running qemu.py: don't launch again before shutdown()

 scripts/qemu.py | 136 +++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 101 insertions(+), 35 deletions(-)
Signature · 1 line
2.13.5
#1 lore raw

PATCH v8 1/13 qemu.py: fix is_running() return before first launch()

is_running() returns None when called before the first time we call launch():

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

import qemu
vm = qemu.QEMUMachine('qemu-system-x86_64')
vm.is_running()

It should return False instead. This patch fixes that.

For consistence, this patch removes the parenthesis from the second clause as it's not really needed.

---
 scripts/qemu.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/qemu.py +1 -1
index 880e3e8219..0ae5d39414 100644
@@ -86,7 +86,7 @@ class QEMUMachine(object):
             raise
 
     def is_running(self):
-        return self._popen and (self._popen.returncode is None)
+        return self._popen is not None and self._popen.returncode is None
 
     def exitcode(self):
         if self._popen is None:
Signature · 1 line
2.13.5
Reviewed-by on v8 1/13
#2 lore raw

Re: [Qemu-devel] [PATCH v8 01/13] qemu.py: fix is_running() return before first launch()

Quoted 8 lines · Amador Pahim

is_running() returns None when called before the first time we call launch():

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

import qemu
vm = qemu.QEMUMachine('qemu-system-x86_64')
vm.is_running()

Quoted 6 lines

It should return False instead. This patch fixes that.

For consistence, this patch removes the parenthesis from the second clause as it's not really needed.

#3 lore raw

PATCH v8 2/13 qemu.py: avoid writing to stdout/stderr

In reply to Amador Pahim

This module should not write directly to stdout/stderr. Instead, it should either raise exceptions or just log the messages and let the callers handle them and decide what to do. For example, scripts could choose to send the log messages stderr or/and write them to a file if verbose or debugging mode is enabled.

This patch replaces the writes to stderr by an exception in the send_fd_scm() when _socket_scm_helper is not set or not present. In the same method, the subprocess Popen will now redirect the stdout/stderr to logging.debug instead of writing to system stderr. As consequence, since the Popen.communicate() is now used (in order to get the stdout), the further call to wait() became redundant and was replaced by Popen.returncode.

The shutdown() message on negative exit code will now be logged to logging.warn instead of written to system stderr.

---
 scripts/qemu.py | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
scripts/qemu.py +22 -8
index 0ae5d39414..aca6fa4d82 100644
@@ -13,6 +13,7 @@
 #
 
 import errno
+import logging
 import string
 import os
 import sys
@@ -20,6 +21,15 @@ import subprocess
 import qmp.qmp
 
 
+LOG = logging.getLogger(__name__)
+
+
+class QEMUMachineError(Exception):
+    """
+    Exception called when an error in QEMUMachine happens.
+    """
+
+
 class QEMUMachine(object):
     '''A QEMU VM'''
 
@@ -62,18 +72,21 @@ class QEMUMachine(object):
         # In iotest.py, the qmp should always use unix socket.
         assert self._qmp.is_scm_available()
         if self._socket_scm_helper is None:
-            print >>sys.stderr, "No path to socket_scm_helper set"
-            return -1
+            raise QEMUMachineError("No path to socket_scm_helper set")
         if os.path.exists(self._socket_scm_helper) == False:
-            print >>sys.stderr, "%s does not exist" % self._socket_scm_helper
-            return -1
+            raise QEMUMachineError("%s does not exist" %
+                                   self._socket_scm_helper)
         fd_param = ["%s" % self._socket_scm_helper,
                     "%d" % self._qmp.get_sock_fd(),
                     "%s" % fd_file_path]
         devnull = open('/dev/null', 'rb')
-        p = subprocess.Popen(fd_param, stdin=devnull, stdout=sys.stdout,
-                             stderr=sys.stderr)
-        return p.wait()
+        p = subprocess.Popen(fd_param, stdin=devnull, stdout=subprocess.PIPE,
+                             stderr=subprocess.STDOUT)
+        output = p.communicate()[0]
+        if output:
+            LOG.debug(output)
+
+        return p.returncode
 
     @staticmethod
     def _remove_if_exists(path):
@@ -154,7 +167,8 @@ class QEMUMachine(object):
 
             exitcode = self._popen.wait()
             if exitcode < 0:
-                sys.stderr.write('qemu received signal %i: %s\n' % (-exitcode, ' '.join(self._args)))
+                LOG.warn('qemu received signal %i: %s', -exitcode,
+                          ' '.join(self._args))
             self._load_io_log()
             self._post_shutdown()
 
Signature · 1 line
2.13.5
Reviewed-by on v8 2/13
#4 lore raw

Re: [Qemu-devel] [PATCH v8 02/13] qemu.py: avoid writing to stdout/stderr

Quoted 14 lines · Amador Pahim

This module should not write directly to stdout/stderr. Instead, it should either raise exceptions or just log the messages and let the callers handle them and decide what to do. For example, scripts could choose to send the log messages stderr or/and write them to a file if verbose or debugging mode is enabled.

This patch replaces the writes to stderr by an exception in the send_fd_scm() when _socket_scm_helper is not set or not present. In the same method, the subprocess Popen will now redirect the stdout/stderr to logging.debug instead of writing to system stderr. As consequence, since the Popen.communicate() is now used (in order to get the stdout), the further call to wait() became redundant and was replaced by Popen.returncode.

Quoted 4 lines

The shutdown() message on negative exit code will now be logged to logging.warn instead of written to system stderr.

#5 lore raw

Re: [Qemu-devel] [PATCH v8 02/13] qemu.py: avoid writing to stdout/stderr

In reply to Amador Pahim

Am 01.09.2017 um 13:28 hat Amador Pahim geschrieben:

Quoted 35 lines

This module should not write directly to stdout/stderr. Instead, it should either raise exceptions or just log the messages and let the callers handle them and decide what to do. For example, scripts could choose to send the log messages stderr or/and write them to a file if verbose or debugging mode is enabled.

This patch replaces the writes to stderr by an exception in the send_fd_scm() when _socket_scm_helper is not set or not present. In the same method, the subprocess Popen will now redirect the stdout/stderr to logging.debug instead of writing to system stderr. As consequence, since the Popen.communicate() is now used (in order to get the stdout), the further call to wait() became redundant and was replaced by Popen.returncode.

The shutdown() message on negative exit code will now be logged to logging.warn instead of written to system stderr.

---
 scripts/qemu.py | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
scripts/qemu.py +1 -0
index 0ae5d39414..aca6fa4d82 100644
@@ -13,6 +13,7 @@
 #
 
 import errno
+import logging
 import string
 import os
 import sys

scripts/qemu.py import subprocess · line 24

Quoted 4 lines
scripts/qemu.py +1 -0
@@ -20,6 +21,15 @@ import subprocess
 import qmp.qmp
 
 
+LOG = logging.getLogger(__name__)

This logger isn't properly initialised, so whenever it would actually be used, the only thing that ends up in the qemu-iotests output is:

+No handlers could be found for logger "qemu"

The actual error message (that was previously logged to stderr) doesn't make it to the output any more.

I find it sad how the recent test infrastructure "improvements" tend to break the tests instead of making them easier to work with. Please be a bit more careful when touching this code, and *test* your code before you submit it. I expect from anyone that they test their code, but if we don't even manage to do that for the tests themselves, I'm losing hope.

Kevin

#6 lore raw

Re: [Qemu-devel] [PATCH v8 02/13] qemu.py: avoid writing to stdout/stderr

Quoted 46 lines · Kevin Wolf

Am 01.09.2017 um 13:28 hat Amador Pahim geschrieben:

Quoted 35 lines

This module should not write directly to stdout/stderr. Instead, it should either raise exceptions or just log the messages and let the callers handle them and decide what to do. For example, scripts could choose to send the log messages stderr or/and write them to a file if verbose or debugging mode is enabled.

This patch replaces the writes to stderr by an exception in the send_fd_scm() when _socket_scm_helper is not set or not present. In the same method, the subprocess Popen will now redirect the stdout/stderr to logging.debug instead of writing to system stderr. As consequence, since the Popen.communicate() is now used (in order to get the stdout), the further call to wait() became redundant and was replaced by Popen.returncode.

The shutdown() message on negative exit code will now be logged to logging.warn instead of written to system stderr.

---
 scripts/qemu.py | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
scripts/qemu.py +1 -0
index 0ae5d39414..aca6fa4d82 100644
@@ -13,6 +13,7 @@
 #
 
 import errno
+import logging
 import string
 import os
 import sys

scripts/qemu.py import subprocess · line 24

Quoted 4 lines
scripts/qemu.py +1 -0
@@ -20,6 +21,15 @@ import subprocess
 import qmp.qmp
 
 
+LOG = logging.getLogger(__name__)

This logger isn't properly initialised, so whenever it would actually be used, the only thing that ends up in the qemu-iotests output is:

+No handlers could be found for logger "qemu"

Quoted 8 lines

The actual error message (that was previously logged to stderr) doesn't make it to the output any more.

I find it sad how the recent test infrastructure "improvements" tend to break the tests instead of making them easier to work with. Please be a bit more careful when touching this code, and *test* your code before you submit it. I expect from anyone that they test their code, but if we don't even manage to do that for the tests themselves, I'm losing hope.

Is the test that was broken triggered as part of "make check"? What can we do to ensure it is?

Signature · 1 line
Eduardo
#7 lore raw

PATCH v8 3/13 qemu.py: use os.path.null instead of /dev/null

In reply to Amador Pahim

For increased portability, let's use os.path.devnull.

---
 scripts/qemu.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/qemu.py +2 -2
index aca6fa4d82..a6e06291ea 100644
@@ -79,7 +79,7 @@ class QEMUMachine(object):
         fd_param = ["%s" % self._socket_scm_helper,
                     "%d" % self._qmp.get_sock_fd(),
                     "%s" % fd_file_path]
-        devnull = open('/dev/null', 'rb')
+        devnull = open(os.path.devnull, 'rb')
         p = subprocess.Popen(fd_param, stdin=devnull, stdout=subprocess.PIPE,
                              stderr=subprocess.STDOUT)
         output = p.communicate()[0]
@@ -140,7 +140,7 @@ class QEMUMachine(object):
 
     def launch(self):
         '''Launch the VM and establish a QMP connection'''
-        devnull = open('/dev/null', 'rb')
+        devnull = open(os.path.devnull, 'rb')
         qemulog = open(self._qemu_log_path, 'wb')
         try:
             self._pre_launch()
Signature · 1 line
2.13.5
Reviewed-by on v8 3/13
#8 lore raw

Re: [Qemu-devel] [PATCH v8 03/13] qemu.py: use os.path.null instead of /dev/null

Quoted 3 lines · Amador Pahim

For increased portability, let's use os.path.devnull.

Quoted 27 lines
---
  scripts/qemu.py | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
scripts/qemu.py +2 -2
index aca6fa4d82..a6e06291ea 100644
@@ -79,7 +79,7 @@ class QEMUMachine(object):
          fd_param = ["%s" % self._socket_scm_helper,
                      "%d" % self._qmp.get_sock_fd(),
                      "%s" % fd_file_path]
-        devnull = open('/dev/null', 'rb')
+        devnull = open(os.path.devnull, 'rb')
          p = subprocess.Popen(fd_param, stdin=devnull, stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT)
          output = p.communicate()[0]
@@ -140,7 +140,7 @@ class QEMUMachine(object):
 
      def launch(self):
          '''Launch the VM and establish a QMP connection'''
-        devnull = open('/dev/null', 'rb')
+        devnull = open(os.path.devnull, 'rb')
          qemulog = open(self._qemu_log_path, 'wb')
          try:
              self._pre_launch()
#10 lore raw

PATCH v8 4/13 qemu.py: improve message on negative exit code

In reply to Amador Pahim

The current message shows 'self._args', which contains only part of the options used in the Qemu command line.

This patch makes the qemu full args list an instance variable and then uses it in the negative exit code message.

Message was moved outside the 'if is_running' block to make sure it will be logged if the VM finishes before the call to shutdown().

---
 scripts/qemu.py | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
scripts/qemu.py +19 -7
index a6e06291ea..670c048569 100644
@@ -49,6 +49,7 @@ class QEMUMachine(object):
         self._iolog = None
         self._socket_scm_helper = socket_scm_helper
         self._debug = debug
+        self._qemu_full_args = None
 
     # This can be used to add an unused monitor instance.
     def add_monitor_telnet(self, ip, port):
@@ -140,13 +141,18 @@ class QEMUMachine(object):
 
     def launch(self):
         '''Launch the VM and establish a QMP connection'''
+        self._qemu_full_args = None
         devnull = open(os.path.devnull, 'rb')
         qemulog = open(self._qemu_log_path, 'wb')
         try:
             self._pre_launch()
-            args = self._wrapper + [self._binary] + self._base_args() + self._args
-            self._popen = subprocess.Popen(args, stdin=devnull, stdout=qemulog,
-                                           stderr=subprocess.STDOUT, shell=False)
+            self._qemu_full_args = (self._wrapper + [self._binary] +
+                                    self._base_args() + self._args)
+            self._popen = subprocess.Popen(self._qemu_full_args,
+                                           stdin=devnull,
+                                           stdout=qemulog,
+                                           stderr=subprocess.STDOUT,
+                                           shell=False)
             self._post_launch()
         except:
             if self.is_running():
@@ -164,14 +170,20 @@ class QEMUMachine(object):
                 self._qmp.close()
             except:
                 self._popen.kill()
+            self._popen.wait()
 
-            exitcode = self._popen.wait()
-            if exitcode < 0:
-                LOG.warn('qemu received signal %i: %s', -exitcode,
-                          ' '.join(self._args))
             self._load_io_log()
             self._post_shutdown()
 
+        exitcode = self.exitcode()
+        if exitcode is not None and exitcode < 0:
+            msg = 'qemu received signal %i: %s'
+            if self._qemu_full_args:
+                command = ' '.join(self._qemu_full_args)
+            else:
+                command = ''
+            LOG.warn(msg, exitcode, command)
+
     underscore_to_dash = string.maketrans('_', '-')
     def qmp(self, cmd, conv_keys=True, **args):
         '''Invoke a QMP command and return the result dict'''
Signature · 1 line
2.13.5
Reviewed-by on v8 4/13
#11 lore raw

Re: [Qemu-devel] [PATCH v8 04/13] qemu.py: improve message on negative exit code

Quoted 30 lines · Amador Pahim

The current message shows 'self._args', which contains only part of the options used in the Qemu command line.

This patch makes the qemu full args list an instance variable and then uses it in the negative exit code message.

Message was moved outside the 'if is_running' block to make sure it will be logged if the VM finishes before the call to shutdown().

---
 scripts/qemu.py | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
scripts/qemu.py +1 -0
index a6e06291ea..670c048569 100644
@@ -49,6 +49,7 @@ class QEMUMachine(object):
         self._iolog = None
         self._socket_scm_helper = socket_scm_helper
         self._debug = debug
+        self._qemu_full_args = None
 
     # This can be used to add an unused monitor instance.
     def add_monitor_telnet(self, ip, port):
@@ -140,13 +141,18 @@ class QEMUMachine(object):
 
     def launch(self):
         '''Launch the VM and establish a QMP connection'''

scripts/qemu.py class QEMUMachine(object): · line 150

Quoted 10 lines
scripts/qemu.py +3 -3
+        self._qemu_full_args = None
         devnull = open(os.path.devnull, 'rb')
         qemulog = open(self._qemu_log_path, 'wb')
         try:
             self._pre_launch()
-            args = self._wrapper + [self._binary] + self._base_args() + self._args
-            self._popen = subprocess.Popen(args, stdin=devnull, stdout=qemulog,
-                                           stderr=subprocess.STDOUT, shell=False)
+            self._qemu_full_args = (self._wrapper + [self._binary] +
+                                    self._base_args() + self._args)

The parentheses seem superfluous. With those removed:

Quoted 37 lines
scripts/qemu.py +15 -4
+            self._popen = subprocess.Popen(self._qemu_full_args,
+                                           stdin=devnull,
+                                           stdout=qemulog,
+                                           stderr=subprocess.STDOUT,
+                                           shell=False)
             self._post_launch()
         except:
             if self.is_running():
@@ -164,14 +170,20 @@ class QEMUMachine(object):
                 self._qmp.close()
             except:
                 self._popen.kill()
+            self._popen.wait()
 
-            exitcode = self._popen.wait()
-            if exitcode < 0:
-                LOG.warn('qemu received signal %i: %s', -exitcode,
-                          ' '.join(self._args))
             self._load_io_log()
             self._post_shutdown()
 
+        exitcode = self.exitcode()
+        if exitcode is not None and exitcode < 0:
+            msg = 'qemu received signal %i: %s'
+            if self._qemu_full_args:
+                command = ' '.join(self._qemu_full_args)
+            else:
+                command = ''
+            LOG.warn(msg, exitcode, command)
+
     underscore_to_dash = string.maketrans('_', '-')
     def qmp(self, cmd, conv_keys=True, **args):
         '''Invoke a QMP command and return the result dict'''
Signature · 1 line
2.13.5
#12 lore raw

Re: [Qemu-devel] [PATCH v8 04/13] qemu.py: improve message on negative exit code

Am 05.09.2017 um 05:02 hat Fam Zheng geschrieben:

Quoted 44 lines · Amador Pahim
Quoted 30 lines

The current message shows 'self._args', which contains only part of the options used in the Qemu command line.

This patch makes the qemu full args list an instance variable and then uses it in the negative exit code message.

Message was moved outside the 'if is_running' block to make sure it will be logged if the VM finishes before the call to shutdown().

---
 scripts/qemu.py | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
scripts/qemu.py +1 -0
index a6e06291ea..670c048569 100644
@@ -49,6 +49,7 @@ class QEMUMachine(object):
         self._iolog = None
         self._socket_scm_helper = socket_scm_helper
         self._debug = debug
+        self._qemu_full_args = None
 
     # This can be used to add an unused monitor instance.
     def add_monitor_telnet(self, ip, port):
@@ -140,13 +141,18 @@ class QEMUMachine(object):
 
     def launch(self):
         '''Launch the VM and establish a QMP connection'''

scripts/qemu.py class QEMUMachine(object): · line 150

Quoted 10 lines
scripts/qemu.py +3 -3
+        self._qemu_full_args = None
         devnull = open(os.path.devnull, 'rb')
         qemulog = open(self._qemu_log_path, 'wb')
         try:
             self._pre_launch()
-            args = self._wrapper + [self._binary] + self._base_args() + self._args
-            self._popen = subprocess.Popen(args, stdin=devnull, stdout=qemulog,
-                                           stderr=subprocess.STDOUT, shell=False)
+            self._qemu_full_args = (self._wrapper + [self._binary] +
+                                    self._base_args() + self._args)

The parentheses seem superfluous. With those removed:

Quoted 1 line

Congratulations, with this advice you just killed all Python-based qemu-iotests and filled up my inbox with CI failure messages. :-)

+Traceback (most recent call last):
+ File "194", line 22, in <module>
+ import iotests
+ File "/home/kwolf/source/qemu/tests/qemu-iotests/iotests.py", line 27, in <module>
+ import qtest
+ File "/home/kwolf/source/qemu/tests/qemu-iotests/../../scripts/qtest.py", line 16, in <module>
+ import qemu
+ File "/home/kwolf/source/qemu/tests/qemu-iotests/../../scripts/qemu.py", line 196
+ self._qemu_full_args = self._wrapper + [self._binary] +
+ ^
+SyntaxError: invalid syntax

Kevin

#13 lore raw

Re: [Qemu-devel] [PATCH v8 04/13] qemu.py: improve message on negative exit code

Quoted 45 lines · Kevin Wolf

Am 05.09.2017 um 05:02 hat Fam Zheng geschrieben:

Quoted 44 lines · Amador Pahim
Quoted 30 lines

The current message shows 'self._args', which contains only part of the options used in the Qemu command line.

This patch makes the qemu full args list an instance variable and then uses it in the negative exit code message.

Message was moved outside the 'if is_running' block to make sure it will be logged if the VM finishes before the call to shutdown().

---
 scripts/qemu.py | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
scripts/qemu.py +1 -0
index a6e06291ea..670c048569 100644
@@ -49,6 +49,7 @@ class QEMUMachine(object):
         self._iolog = None
         self._socket_scm_helper = socket_scm_helper
         self._debug = debug
+        self._qemu_full_args = None
 
     # This can be used to add an unused monitor instance.
     def add_monitor_telnet(self, ip, port):
@@ -140,13 +141,18 @@ class QEMUMachine(object):
 
     def launch(self):
         '''Launch the VM and establish a QMP connection'''

scripts/qemu.py class QEMUMachine(object): · line 150

Quoted 10 lines
scripts/qemu.py +3 -3
+        self._qemu_full_args = None
         devnull = open(os.path.devnull, 'rb')
         qemulog = open(self._qemu_log_path, 'wb')
         try:
             self._pre_launch()
-            args = self._wrapper + [self._binary] + self._base_args() + self._args
-            self._popen = subprocess.Popen(args, stdin=devnull, stdout=qemulog,
-                                           stderr=subprocess.STDOUT, shell=False)
+            self._qemu_full_args = (self._wrapper + [self._binary] +
+                                    self._base_args() + self._args)

The parentheses seem superfluous. With those removed:

Quoted 4 lines
Quoted 1 line

Congratulations, with this advice you just killed all Python-based qemu-iotests and filled up my inbox with CI failure messages. :-)

Oops. Why doesn't python understand such multi line expressions impllied by "+" just like in "[,]"?

Iotests on patchew is not far away. (The docker test-block pull request was bounced a few times, unfortunately, for unrelated reasons)

Fam

#14 lore raw

Re: [Qemu-devel] [PATCH v8 04/13] qemu.py: improve message on negative exit code

Am 18.09.2017 um 07:39 hat Fam Zheng geschrieben:

Quoted 46 lines · Kevin Wolf
Quoted 45 lines

Am 05.09.2017 um 05:02 hat Fam Zheng geschrieben:

Quoted 44 lines · Amador Pahim
Quoted 30 lines

The current message shows 'self._args', which contains only part of the options used in the Qemu command line.

This patch makes the qemu full args list an instance variable and then uses it in the negative exit code message.

Message was moved outside the 'if is_running' block to make sure it will be logged if the VM finishes before the call to shutdown().

---
 scripts/qemu.py | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
scripts/qemu.py +1 -0
index a6e06291ea..670c048569 100644
@@ -49,6 +49,7 @@ class QEMUMachine(object):
         self._iolog = None
         self._socket_scm_helper = socket_scm_helper
         self._debug = debug
+        self._qemu_full_args = None
 
     # This can be used to add an unused monitor instance.
     def add_monitor_telnet(self, ip, port):
@@ -140,13 +141,18 @@ class QEMUMachine(object):
 
     def launch(self):
         '''Launch the VM and establish a QMP connection'''

scripts/qemu.py class QEMUMachine(object): · line 150

Quoted 10 lines
scripts/qemu.py +3 -3
+        self._qemu_full_args = None
         devnull = open(os.path.devnull, 'rb')
         qemulog = open(self._qemu_log_path, 'wb')
         try:
             self._pre_launch()
-            args = self._wrapper + [self._binary] + self._base_args() + self._args
-            self._popen = subprocess.Popen(args, stdin=devnull, stdout=qemulog,
-                                           stderr=subprocess.STDOUT, shell=False)
+            self._qemu_full_args = (self._wrapper + [self._binary] +
+                                    self._base_args() + self._args)

The parentheses seem superfluous. With those removed:

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Congratulations, with this advice you just killed all Python-based qemu-iotests and filled up my inbox with CI failure messages. :-)

Oops. Why doesn't python understand such multi line expressions impllied by "+" just like in "[,]"?

To be honest, I didn't know that either until now. But apparently the different kinds of brackets (including those around function arguments) are the only things that allow multiline expressions in Python.

Quoted 2 lines

Iotests on patchew is not far away. (The docker test-block pull request was bounced a few times, unfortunately, for unrelated reasons)

Nice!

Kevin

#15 lore raw

Re: [Qemu-devel] [PATCH v8 04/13] qemu.py: improve message on negative exit code

Quoted 10 lines · Kevin Wolf
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines

scripts/qemu.py class QEMUMachine(object): · line 150

Quoted 2 lines
scripts/qemu.py +2 -0
+            self._qemu_full_args = (self._wrapper + [self._binary] +
+                                    self._base_args() + self._args)

The parentheses seem superfluous. With those removed:

Congratulations, with this advice you just killed all Python-based qemu-iotests and filled up my inbox with CI failure messages. :-)

Quoted 6 lines
Quoted 2 lines

Oops. Why doesn't python understand such multi line expressions impllied by "+" just like in "[,]"?

To be honest, I didn't know that either until now. But apparently the different kinds of brackets (including those around function arguments) are the only things that allow multiline expressions in Python.

Yes, and now I read that PEP recommends using brackets for multilines compared to backslashes. I was totally wrong with this advice.

Fam

#16 lore raw

PATCH v8 5/13 qemu.py: include debug information on launch error

In reply to Amador Pahim

When launching a VM, if an exception happens and the VM is not initiated, it might be useful to see the qemu command line and the qemu command output.

This patch creates that message. Notice that self._iolog needs to be cleaned up in the beginning of the launch() to make sure we will not expose the qemu log from a previous launch if the current one fails.

---
 scripts/qemu.py | 7 +++++++
 1 file changed, 7 insertions(+)
scripts/qemu.py +7 -0
index 670c048569..3ebe5ee0a4 100644
@@ -141,6 +141,7 @@ class QEMUMachine(object):
 
     def launch(self):
         '''Launch the VM and establish a QMP connection'''
+        self._iolog = None
         self._qemu_full_args = None
         devnull = open(os.path.devnull, 'rb')
         qemulog = open(self._qemu_log_path, 'wb')
@@ -160,6 +161,12 @@ class QEMUMachine(object):
                 self._popen.wait()
             self._load_io_log()
             self._post_shutdown()
+
+            LOG.debug('Error launching VM')
+            if self._qemu_full_args:
+                LOG.debug('Command: %r', ' '.join(self._qemu_full_args))
+            if self._iolog:
+                LOG.debug('Output: %r', self._iolog)
             raise
 
     def shutdown(self):
Signature · 1 line
2.13.5
Reviewed-by on v8 5/13
#17 lore raw

Re: [Qemu-devel] [PATCH v8 05/13] qemu.py: include debug information on launch error

Quoted 41 lines · Amador Pahim

When launching a VM, if an exception happens and the VM is not initiated, it might be useful to see the qemu command line and the qemu command output.

This patch creates that message. Notice that self._iolog needs to be cleaned up in the beginning of the launch() to make sure we will not expose the qemu log from a previous launch if the current one fails.

---
 scripts/qemu.py | 7 +++++++
 1 file changed, 7 insertions(+)
scripts/qemu.py +7 -0
index 670c048569..3ebe5ee0a4 100644
@@ -141,6 +141,7 @@ class QEMUMachine(object):
 
     def launch(self):
         '''Launch the VM and establish a QMP connection'''
+        self._iolog = None
         self._qemu_full_args = None
         devnull = open(os.path.devnull, 'rb')
         qemulog = open(self._qemu_log_path, 'wb')
@@ -160,6 +161,12 @@ class QEMUMachine(object):
                 self._popen.wait()
             self._load_io_log()
             self._post_shutdown()
+
+            LOG.debug('Error launching VM')
+            if self._qemu_full_args:
+                LOG.debug('Command: %r', ' '.join(self._qemu_full_args))
+            if self._iolog:
+                LOG.debug('Output: %r', self._iolog)
             raise
 
     def shutdown(self):
Signature · 1 line
2.13.5

It would be nice if these messages are added to the exception obj, but it's already an improvement over what we have now, so:

#18 lore raw

PATCH v8 6/13 qemu.py: make sure we only remove files we create

In reply to Amador Pahim

To launch a VM, we need to create basically two files: the monitor socket (if it's a UNIX socket) and the qemu log file.

For the qemu log file, we currently just open the path, which will create the file if it does not exist or overwrite the file if it does exist.

For the monitor socket, if it already exists, we are currently removing it, even if it's not created by us.

This patch moves to pre_launch() the responsibility to make sure we only create files that are not pre-existent and to populate a list of controlled files. This list will then be used as the reference of files to remove during the cleanup (post_shutdown()).

---
 scripts/qemu.py | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)
scripts/qemu.py +23 -7
index 3ebe5ee0a4..c26e1412f9 100644
@@ -41,6 +41,7 @@ class QEMUMachine(object):
             monitor_address = os.path.join(test_dir, name + "-monitor.sock")
         self._monitor_address = monitor_address
         self._qemu_log_path = os.path.join(test_dir, name + ".log")
+        self._qemu_log_fd = None
         self._popen = None
         self._binary = binary
         self._args = list(args) # Force copy args in case we modify them
@@ -50,6 +51,7 @@ class QEMUMachine(object):
         self._socket_scm_helper = socket_scm_helper
         self._debug = debug
         self._qemu_full_args = None
+        self._created_files = []
 
     # This can be used to add an unused monitor instance.
     def add_monitor_telnet(self, ip, port):
@@ -128,30 +130,44 @@ class QEMUMachine(object):
                 '-display', 'none', '-vga', 'none']
 
     def _pre_launch(self):
-        self._qmp = qmp.qmp.QEMUMonitorProtocol(self._monitor_address, server=True,
-                                                debug=self._debug)
+        try:
+            self._qmp = qmp.qmp.QEMUMonitorProtocol(self._monitor_address,
+                                                    server=True,
+                                                    debug=self._debug)
+        except:
+            raise
+        else:
+            if not isinstance(self._monitor_address, tuple):
+                self._created_files.append(self._monitor_address)
+
+        try:
+            flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY
+            os.open(self._qemu_log_path, flags)
+        except:
+            raise
+        else:
+            self._created_files.append(self._qemu_log_path)
+            self._qemu_log_fd = open(self._qemu_log_path, 'wb')
 
     def _post_launch(self):
         self._qmp.accept()
 
     def _post_shutdown(self):
-        if not isinstance(self._monitor_address, tuple):
-            self._remove_if_exists(self._monitor_address)
-        self._remove_if_exists(self._qemu_log_path)
+        while self._created_files:
+            self._remove_if_exists(self._created_files.pop())
 
     def launch(self):
         '''Launch the VM and establish a QMP connection'''
         self._iolog = None
         self._qemu_full_args = None
         devnull = open(os.path.devnull, 'rb')
-        qemulog = open(self._qemu_log_path, 'wb')
         try:
             self._pre_launch()
             self._qemu_full_args = (self._wrapper + [self._binary] +
                                     self._base_args() + self._args)
             self._popen = subprocess.Popen(self._qemu_full_args,
                                            stdin=devnull,
-                                           stdout=qemulog,
+                                           stdout=self._qemu_log_fd,
                                            stderr=subprocess.STDOUT,
                                            shell=False)
             self._post_launch()
Signature · 1 line
2.13.5
#19 lore raw

Re: [Qemu-devel] [PATCH v8 06/13] qemu.py: make sure we only remove files we create

Quoted 42 lines · Amador Pahim

To launch a VM, we need to create basically two files: the monitor socket (if it's a UNIX socket) and the qemu log file.

For the qemu log file, we currently just open the path, which will create the file if it does not exist or overwrite the file if it does exist.

For the monitor socket, if it already exists, we are currently removing it, even if it's not created by us.

This patch moves to pre_launch() the responsibility to make sure we only create files that are not pre-existent and to populate a list of controlled files. This list will then be used as the reference of files to remove during the cleanup (post_shutdown()).

---
 scripts/qemu.py | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)
scripts/qemu.py +2 -0
index 3ebe5ee0a4..c26e1412f9 100644
@@ -41,6 +41,7 @@ class QEMUMachine(object):
             monitor_address = os.path.join(test_dir, name + "-monitor.sock")
         self._monitor_address = monitor_address
         self._qemu_log_path = os.path.join(test_dir, name + ".log")
+        self._qemu_log_fd = None
         self._popen = None
         self._binary = binary
         self._args = list(args) # Force copy args in case we modify them
@@ -50,6 +51,7 @@ class QEMUMachine(object):
         self._socket_scm_helper = socket_scm_helper
         self._debug = debug
         self._qemu_full_args = None
+        self._created_files = []
 
     # This can be used to add an unused monitor instance.
     def add_monitor_telnet(self, ip, port):
@@ -128,30 +130,44 @@ class QEMUMachine(object):
                 '-display', 'none', '-vga', 'none']

scripts/qemu.py class QEMUMachine(object): · line 138

Quoted 10 lines
scripts/qemu.py +6 -2
 
     def _pre_launch(self):
-        self._qmp = qmp.qmp.QEMUMonitorProtocol(self._monitor_address, server=True,
-                                                debug=self._debug)
+        try:
+            self._qmp = qmp.qmp.QEMUMonitorProtocol(self._monitor_address,
+                                                    server=True,
+                                                    debug=self._debug)
+        except:
+            raise

What's the point of "except: raise"? It seems useless.

scripts/qemu.py class QEMUMachine(object): · line 145

Quoted 7 lines
scripts/qemu.py +7 -0
+        else:
+            if not isinstance(self._monitor_address, tuple):
+                self._created_files.append(self._monitor_address)
+
+        try:
+            flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY
+            os.open(self._qemu_log_path, flags)

Why change to os.open() instead of open()?

Quoted 36 lines
scripts/qemu.py +8 -5
+        except:
+            raise
+        else:
+            self._created_files.append(self._qemu_log_path)
+            self._qemu_log_fd = open(self._qemu_log_path, 'wb')
 
     def _post_launch(self):
         self._qmp.accept()
 
     def _post_shutdown(self):
-        if not isinstance(self._monitor_address, tuple):
-            self._remove_if_exists(self._monitor_address)
-        self._remove_if_exists(self._qemu_log_path)
+        while self._created_files:
+            self._remove_if_exists(self._created_files.pop())
 
     def launch(self):
         '''Launch the VM and establish a QMP connection'''
         self._iolog = None
         self._qemu_full_args = None
         devnull = open(os.path.devnull, 'rb')
-        qemulog = open(self._qemu_log_path, 'wb')
         try:
             self._pre_launch()
             self._qemu_full_args = (self._wrapper + [self._binary] +
                                     self._base_args() + self._args)
             self._popen = subprocess.Popen(self._qemu_full_args,
                                            stdin=devnull,
-                                           stdout=qemulog,
+                                           stdout=self._qemu_log_fd,
                                            stderr=subprocess.STDOUT,
                                            shell=False)
             self._post_launch()
Signature · 1 line
2.13.5

Fam

#20 lore raw

Re: [Qemu-devel] [PATCH v8 06/13] qemu.py: make sure we only remove files we create

Quoted 44 lines · Fam Zheng
Quoted 43 lines · Amador Pahim

To launch a VM, we need to create basically two files: the monitor socket (if it's a UNIX socket) and the qemu log file.

For the qemu log file, we currently just open the path, which will create the file if it does not exist or overwrite the file if it does exist.

For the monitor socket, if it already exists, we are currently removing it, even if it's not created by us.

This patch moves to pre_launch() the responsibility to make sure we only create files that are not pre-existent and to populate a list of controlled files. This list will then be used as the reference of files to remove during the cleanup (post_shutdown()).

---
 scripts/qemu.py | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)
scripts/qemu.py +2 -0
index 3ebe5ee0a4..c26e1412f9 100644
@@ -41,6 +41,7 @@ class QEMUMachine(object):
             monitor_address = os.path.join(test_dir, name + "-monitor.sock")
         self._monitor_address = monitor_address
         self._qemu_log_path = os.path.join(test_dir, name + ".log")
+        self._qemu_log_fd = None
         self._popen = None
         self._binary = binary
         self._args = list(args) # Force copy args in case we modify them
@@ -50,6 +51,7 @@ class QEMUMachine(object):
         self._socket_scm_helper = socket_scm_helper
         self._debug = debug
         self._qemu_full_args = None
+        self._created_files = []
 
     # This can be used to add an unused monitor instance.
     def add_monitor_telnet(self, ip, port):
@@ -128,30 +130,44 @@ class QEMUMachine(object):
                 '-display', 'none', '-vga', 'none']
 
Quoted 11 lines

scripts/qemu.py class QEMUMachine(object): · line 138

Quoted 9 lines
scripts/qemu.py +6 -2
     def _pre_launch(self):
-        self._qmp = qmp.qmp.QEMUMonitorProtocol(self._monitor_address, server=True,
-                                                debug=self._debug)
+        try:
+            self._qmp = qmp.qmp.QEMUMonitorProtocol(self._monitor_address,
+                                                    server=True,
+                                                    debug=self._debug)
+        except:
+            raise

What's the point of "except: raise"? It seems useless.

The point is to execute the block in the else only when no exception happens. When some exception happens, I want to raise it without executing the else block.

Quoted 10 lines

scripts/qemu.py class QEMUMachine(object): · line 145

Quoted 8 lines
scripts/qemu.py +7 -0
+        else:
+            if not isinstance(self._monitor_address, tuple):
+                self._created_files.append(self._monitor_address)
+
+        try:
+            flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY
+            os.open(self._qemu_log_path, flags)

Why change to os.open() instead of open()?

I want to create the file only if it does not exist. The open() flag 'x' is available only in python 3.3. For python <3.3, we need the os.open() to have that feature.

Quoted 39 lines
Quoted 35 lines
scripts/qemu.py +8 -6
+        except:
+            raise
+        else:
+            self._created_files.append(self._qemu_log_path)
+            self._qemu_log_fd = open(self._qemu_log_path, 'wb')
 
     def _post_launch(self):
         self._qmp.accept()
 
     def _post_shutdown(self):
-        if not isinstance(self._monitor_address, tuple):
-            self._remove_if_exists(self._monitor_address)
-        self._remove_if_exists(self._qemu_log_path)
+        while self._created_files:
+            self._remove_if_exists(self._created_files.pop())
 
     def launch(self):
         '''Launch the VM and establish a QMP connection'''
         self._iolog = None
         self._qemu_full_args = None
         devnull = open(os.path.devnull, 'rb')
-        qemulog = open(self._qemu_log_path, 'wb')
         try:
             self._pre_launch()
             self._qemu_full_args = (self._wrapper + [self._binary] +
                                     self._base_args() + self._args)
             self._popen = subprocess.Popen(self._qemu_full_args,
                                            stdin=devnull,
-                                           stdout=qemulog,
+                                           stdout=self._qemu_log_fd,
                                            stderr=subprocess.STDOUT,
                                            shell=False)
             self._post_launch()
--
Quoted 2 lines

2.13.5

Fam

#21 lore raw

Re: [Qemu-devel] [PATCH v8 06/13] qemu.py: make sure we only remove files we create

Quoted 57 lines · Amador Pahim
Quoted 44 lines · Fam Zheng
Quoted 43 lines · Amador Pahim

To launch a VM, we need to create basically two files: the monitor socket (if it's a UNIX socket) and the qemu log file.

For the qemu log file, we currently just open the path, which will create the file if it does not exist or overwrite the file if it does exist.

For the monitor socket, if it already exists, we are currently removing it, even if it's not created by us.

This patch moves to pre_launch() the responsibility to make sure we only create files that are not pre-existent and to populate a list of controlled files. This list will then be used as the reference of files to remove during the cleanup (post_shutdown()).

---
 scripts/qemu.py | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)
scripts/qemu.py +2 -0
index 3ebe5ee0a4..c26e1412f9 100644
@@ -41,6 +41,7 @@ class QEMUMachine(object):
             monitor_address = os.path.join(test_dir, name + "-monitor.sock")
         self._monitor_address = monitor_address
         self._qemu_log_path = os.path.join(test_dir, name + ".log")
+        self._qemu_log_fd = None
         self._popen = None
         self._binary = binary
         self._args = list(args) # Force copy args in case we modify them
@@ -50,6 +51,7 @@ class QEMUMachine(object):
         self._socket_scm_helper = socket_scm_helper
         self._debug = debug
         self._qemu_full_args = None
+        self._created_files = []
 
     # This can be used to add an unused monitor instance.
     def add_monitor_telnet(self, ip, port):
@@ -128,30 +130,44 @@ class QEMUMachine(object):
                 '-display', 'none', '-vga', 'none']
 
Quoted 11 lines

scripts/qemu.py class QEMUMachine(object): · line 138

Quoted 9 lines
scripts/qemu.py +6 -2
     def _pre_launch(self):
-        self._qmp = qmp.qmp.QEMUMonitorProtocol(self._monitor_address, server=True,
-                                                debug=self._debug)
+        try:
+            self._qmp = qmp.qmp.QEMUMonitorProtocol(self._monitor_address,
+                                                    server=True,
+                                                    debug=self._debug)
+        except:
+            raise

What's the point of "except: raise"? It seems useless.

Quoted 3 lines

The point is to execute the block in the else only when no exception happens. When some exception happens, I want to raise it without executing the else block.

Isn't this exactly what Python does when an exception is raised with no "try" block?

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
scripts/qemu.py +7 -0
+        else:
+            if not isinstance(self._monitor_address, tuple):
+                self._created_files.append(self._monitor_address)
+
+        try:
+            flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY
+            os.open(self._qemu_log_path, flags)
Quoted 5 lines
Quoted 1 line

Why change to os.open() instead of open()?

I want to create the file only if it does not exist. The open() flag 'x' is available only in python 3.3. For python <3.3, we need the os.open() to have that feature.

I'm not sure this extra complexity is really necessary. We could fix all that by using mkdtemp() and deleting the temporary directory on shutdown.

Quoted 40 lines
Quoted 40 lines
Quoted 36 lines
scripts/qemu.py +8 -6
+        except:
+            raise
+        else:
+            self._created_files.append(self._qemu_log_path)
+            self._qemu_log_fd = open(self._qemu_log_path, 'wb')
 
     def _post_launch(self):
         self._qmp.accept()
 
     def _post_shutdown(self):
-        if not isinstance(self._monitor_address, tuple):
-            self._remove_if_exists(self._monitor_address)
-        self._remove_if_exists(self._qemu_log_path)
+        while self._created_files:
+            self._remove_if_exists(self._created_files.pop())
 
     def launch(self):
         '''Launch the VM and establish a QMP connection'''
         self._iolog = None
         self._qemu_full_args = None
         devnull = open(os.path.devnull, 'rb')
-        qemulog = open(self._qemu_log_path, 'wb')
         try:
             self._pre_launch()
             self._qemu_full_args = (self._wrapper + [self._binary] +
                                     self._base_args() + self._args)
             self._popen = subprocess.Popen(self._qemu_full_args,
                                            stdin=devnull,
-                                           stdout=qemulog,
+                                           stdout=self._qemu_log_fd,
                                            stderr=subprocess.STDOUT,
                                            shell=False)
             self._post_launch()
--
Quoted 2 lines

2.13.5

Fam

Signature · 1 line
Eduardo
#22 lore raw

Re: [Qemu-devel] [PATCH v8 06/13] qemu.py: make sure we only remove files we create

Quoted 58 lines · Eduardo Habkost
Quoted 57 lines · Amador Pahim
Quoted 55 lines · Fam Zheng
Quoted 42 lines · Amador Pahim

To launch a VM, we need to create basically two files: the monitor socket (if it's a UNIX socket) and the qemu log file.

For the qemu log file, we currently just open the path, which will create the file if it does not exist or overwrite the file if it does exist.

For the monitor socket, if it already exists, we are currently removing it, even if it's not created by us.

This patch moves to pre_launch() the responsibility to make sure we only create files that are not pre-existent and to populate a list of controlled files. This list will then be used as the reference of files to remove during the cleanup (post_shutdown()).

---
 scripts/qemu.py | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)
scripts/qemu.py +2 -0
index 3ebe5ee0a4..c26e1412f9 100644
@@ -41,6 +41,7 @@ class QEMUMachine(object):
             monitor_address = os.path.join(test_dir, name + "-monitor.sock")
         self._monitor_address = monitor_address
         self._qemu_log_path = os.path.join(test_dir, name + ".log")
+        self._qemu_log_fd = None
         self._popen = None
         self._binary = binary
         self._args = list(args) # Force copy args in case we modify them
@@ -50,6 +51,7 @@ class QEMUMachine(object):
         self._socket_scm_helper = socket_scm_helper
         self._debug = debug
         self._qemu_full_args = None
+        self._created_files = []
 
     # This can be used to add an unused monitor instance.
     def add_monitor_telnet(self, ip, port):
@@ -128,30 +130,44 @@ class QEMUMachine(object):
                 '-display', 'none', '-vga', 'none']

scripts/qemu.py class QEMUMachine(object): · line 138

Quoted 10 lines
scripts/qemu.py +6 -2
 
     def _pre_launch(self):
-        self._qmp = qmp.qmp.QEMUMonitorProtocol(self._monitor_address, server=True,
-                                                debug=self._debug)
+        try:
+            self._qmp = qmp.qmp.QEMUMonitorProtocol(self._monitor_address,
+                                                    server=True,
+                                                    debug=self._debug)
+        except:
+            raise

What's the point of "except: raise"? It seems useless.

Quoted 6 lines
Quoted 3 lines

The point is to execute the block in the else only when no exception happens. When some exception happens, I want to raise it without executing the else block.

Isn't this exactly what Python does when an exception is raised with no "try" block?

Sure, cleaning this up.

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines

scripts/qemu.py class QEMUMachine(object): · line 145

Quoted 11 lines
scripts/qemu.py +7 -0
+        else:
+            if not isinstance(self._monitor_address, tuple):
+                self._created_files.append(self._monitor_address)
+
+        try:
+            flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY
+            os.open(self._qemu_log_path, flags)

Why change to os.open() instead of open()?

Quoted 7 lines
Quoted 3 lines

I want to create the file only if it does not exist. The open() flag 'x' is available only in python 3.3. For python <3.3, we need the os.open() to have that feature.

I'm not sure this extra complexity is really necessary. We could fix all that by using mkdtemp() and deleting the temporary directory on shutdown.

I thought about that, but I foresee the question: hat happens if between the mkdtemp and the file creation (i.e. self._qemu_log_path) someone goes in that directory and creates a file with the same name of the self._qemu_log_path? Are we going to overwrite it? Ok, very unlikely, but possible. This extra step takes care of that.

Quoted 44 lines
Quoted 40 lines
Quoted 40 lines
Quoted 39 lines
scripts/qemu.py +8 -6
+        except:
+            raise
+        else:
+            self._created_files.append(self._qemu_log_path)
+            self._qemu_log_fd = open(self._qemu_log_path, 'wb')
 
     def _post_launch(self):
         self._qmp.accept()
 
     def _post_shutdown(self):
-        if not isinstance(self._monitor_address, tuple):
-            self._remove_if_exists(self._monitor_address)
-        self._remove_if_exists(self._qemu_log_path)
+        while self._created_files:
+            self._remove_if_exists(self._created_files.pop())
 
     def launch(self):
         '''Launch the VM and establish a QMP connection'''
         self._iolog = None
         self._qemu_full_args = None
         devnull = open(os.path.devnull, 'rb')
-        qemulog = open(self._qemu_log_path, 'wb')
         try:
             self._pre_launch()
             self._qemu_full_args = (self._wrapper + [self._binary] +
                                     self._base_args() + self._args)
             self._popen = subprocess.Popen(self._qemu_full_args,
                                            stdin=devnull,
-                                           stdout=qemulog,
+                                           stdout=self._qemu_log_fd,
                                            stderr=subprocess.STDOUT,
                                            shell=False)
             self._post_launch()
--

2.13.5

Quoted 1 line
Quoted 1 line

Fam

--
Eduardo

#23 lore raw

Re: [Qemu-devel] [PATCH v8 06/13] qemu.py: make sure we only remove files we create

Quoted 4 lines · Amador Pahim
Quoted 3 lines · Eduardo Habkost
Quoted 2 lines · Amador Pahim
Quoted 1 line · Fam Zheng

On Fri, 09/01 13:28, Amador Pahim wrote:

[...]

Quoted 18 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

scripts/qemu.py class QEMUMachine(object): · line 145

Quoted 7 lines
scripts/qemu.py +7 -0
+        else:
+            if not isinstance(self._monitor_address, tuple):
+                self._created_files.append(self._monitor_address)
+
+        try:
+            flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY
+            os.open(self._qemu_log_path, flags)

Why change to os.open() instead of open()?

Quoted 7 lines
Quoted 3 lines

I want to create the file only if it does not exist. The open() flag 'x' is available only in python 3.3. For python <3.3, we need the os.open() to have that feature.

I'm not sure this extra complexity is really necessary. We could fix all that by using mkdtemp() and deleting the temporary directory on shutdown.

Quoted 5 lines

I thought about that, but I foresee the question: hat happens if between the mkdtemp and the file creation (i.e. self._qemu_log_path) someone goes in that directory and creates a file with the same name of the self._qemu_log_path? Are we going to overwrite it? Ok, very unlikely, but possible. This extra step takes care of that.

If someone creates a file inside a directory we created using mkdtemp(), we will just delete it. Why would that be a problem?

Signature · 1 line
Eduardo
#24 lore raw

Re: [Qemu-devel] [PATCH v8 06/13] qemu.py: make sure we only remove files we create

Quoted 24 lines · Eduardo Habkost
Quoted 4 lines · Amador Pahim
Quoted 3 lines · Eduardo Habkost
Quoted 2 lines · Amador Pahim
Quoted 1 line · Fam Zheng

On Fri, 09/01 13:28, Amador Pahim wrote:

[...]

Quoted 18 lines
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
scripts/qemu.py +7 -0
+        else:
+            if not isinstance(self._monitor_address, tuple):
+                self._created_files.append(self._monitor_address)
+
+        try:
+            flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY
+            os.open(self._qemu_log_path, flags)
Quoted 5 lines
Quoted 1 line

Why change to os.open() instead of open()?

I want to create the file only if it does not exist. The open() flag 'x' is available only in python 3.3. For python <3.3, we need the os.open() to have that feature.

I'm not sure this extra complexity is really necessary. We could fix all that by using mkdtemp() and deleting the temporary directory on shutdown.

Quoted 8 lines
Quoted 5 lines

I thought about that, but I foresee the question: hat happens if between the mkdtemp and the file creation (i.e. self._qemu_log_path) someone goes in that directory and creates a file with the same name of the self._qemu_log_path? Are we going to overwrite it? Ok, very unlikely, but possible. This extra step takes care of that.

If someone creates a file inside a directory we created using mkdtemp(), we will just delete it. Why would that be a problem?

Ok then. That simplifies the control a lot.
Thanks.

Quoted 3 lines

--
Eduardo

#25 lore raw

PATCH v8 7/13 qemu.py: close _qemu_log_path on cleanup

In reply to Amador Pahim

We are opening the _qemu_log_path during the launch() but we are forgetting to close it.

This patch makes sure we will close the self._qemu_log_path during the cleanup (post_shutdown()).

---
 scripts/qemu.py | 3 +++
 1 file changed, 3 insertions(+)
scripts/qemu.py +3 -0
index c26e1412f9..1b77fec48b 100644
@@ -153,6 +153,9 @@ class QEMUMachine(object):
         self._qmp.accept()
 
     def _post_shutdown(self):
+        if self._qemu_log_fd is not None:
+            self._qemu_log_fd.close()
+
         while self._created_files:
             self._remove_if_exists(self._created_files.pop())
 
Signature · 1 line
2.13.5
Reviewed-by on v8 7/13
#26 lore raw

Re: [Qemu-devel] [PATCH v8 07/13] qemu.py: close _qemu_log_path on cleanup

Quoted 28 lines · Amador Pahim

We are opening the _qemu_log_path during the launch() but we are forgetting to close it.

This patch makes sure we will close the self._qemu_log_path during the cleanup (post_shutdown()).

---
 scripts/qemu.py | 3 +++
 1 file changed, 3 insertions(+)
scripts/qemu.py +3 -0
index c26e1412f9..1b77fec48b 100644
@@ -153,6 +153,9 @@ class QEMUMachine(object):
         self._qmp.accept()
 
     def _post_shutdown(self):
+        if self._qemu_log_fd is not None:
+            self._qemu_log_fd.close()
+
         while self._created_files:
             self._remove_if_exists(self._created_files.pop())
 
Signature · 1 line
2.13.5
#27 lore raw

PATCH v8 8/13 qemu.py: refactor launch()

In reply to Amador Pahim

This is just an refactor to separate the exception handler from the actual launch procedure, improving the readability and making future maintenances in this piece of code easier.

---
 scripts/qemu.py | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
scripts/qemu.py +18 -11
index 1b77fec48b..06f07bfaa2 100644
@@ -160,20 +160,14 @@ class QEMUMachine(object):
             self._remove_if_exists(self._created_files.pop())
 
     def launch(self):
-        '''Launch the VM and establish a QMP connection'''
+        '''
+        Try to launch the VM and make sure we cleanup and expose the
+        command line/output in case of exception.
+        '''
         self._iolog = None
         self._qemu_full_args = None
-        devnull = open(os.path.devnull, 'rb')
         try:
-            self._pre_launch()
-            self._qemu_full_args = (self._wrapper + [self._binary] +
-                                    self._base_args() + self._args)
-            self._popen = subprocess.Popen(self._qemu_full_args,
-                                           stdin=devnull,
-                                           stdout=self._qemu_log_fd,
-                                           stderr=subprocess.STDOUT,
-                                           shell=False)
-            self._post_launch()
+            self._launch()
         except:
             if self.is_running():
                 self._popen.kill()
@@ -188,6 +182,19 @@ class QEMUMachine(object):
                 LOG.debug('Output: %r', self._iolog)
             raise
 
+    def _launch(self):
+        '''Launch the VM and establish a QMP connection'''
+        devnull = open(os.path.devnull, 'rb')
+        self._pre_launch()
+        self._qemu_full_args = (self._wrapper + [self._binary] +
+                                self._base_args() + self._args)
+        self._popen = subprocess.Popen(self._qemu_full_args,
+                                       stdin=devnull,
+                                       stdout=self._qemu_log_fd,
+                                       stderr=subprocess.STDOUT,
+                                       shell=False)
+        self._post_launch()
+
     def shutdown(self):
         '''Terminate the VM and clean up'''
         if self.is_running():
Signature · 1 line
2.13.5
Reviewed-by on v8 8/13
#28 lore raw

Re: [Qemu-devel] [PATCH v8 08/13] qemu.py: refactor launch()

Quoted 5 lines · Amador Pahim

This is just an refactor to separate the exception handler from the actual launch procedure, improving the readability and making future maintenances in this piece of code easier.

#29 lore raw

PATCH v8 9/13 qemu.py: always cleanup on shutdown()

In reply to Amador Pahim

Currently we only cleanup on shutdown() if the VM is running.

To make sure we will always cleanup, this patch makes the cleanup to always happen in shutdown, regardless the VM running state.

---
 scripts/qemu.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/qemu.py +2 -2
index 06f07bfaa2..ede734328b 100644
@@ -205,8 +205,8 @@ class QEMUMachine(object):
                 self._popen.kill()
             self._popen.wait()
 
-            self._load_io_log()
-            self._post_shutdown()
+        self._load_io_log()
+        self._post_shutdown()
 
         exitcode = self.exitcode()
         if exitcode is not None and exitcode < 0:
Signature · 1 line
2.13.5
Reviewed-by on v8 9/13
#30 lore raw

Re: [Qemu-devel] [PATCH v8 09/13] qemu.py: always cleanup on shutdown()

Quoted 6 lines · Amador Pahim

Currently we only cleanup on shutdown() if the VM is running.

To make sure we will always cleanup, this patch makes the cleanup to always happen in shutdown, regardless the VM running state.

#31 lore raw

PATCH v8 10/13 qemu.py: use poll() instead of 'returncode'

In reply to Amador Pahim

The 'returncode' Popen attribute is not guaranteed to be updated. It actually depends on a call to either poll(), wait() or communicate().

On the other hand, poll() will: "Check if child process has terminated. Set and return returncode attribute."

Let's use the poll() to check whether the process is running and to get the updated process exit code, when the process is finished.

---
 scripts/qemu.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/qemu.py +2 -2
index ede734328b..87a2212b77 100644
@@ -102,12 +102,12 @@ class QEMUMachine(object):
             raise
 
     def is_running(self):
-        return self._popen is not None and self._popen.returncode is None
+        return self._popen is not None and self._popen.poll() is None
 
     def exitcode(self):
         if self._popen is None:
             return None
-        return self._popen.returncode
+        return self._popen.poll()
 
     def get_pid(self):
         if not self.is_running():
Signature · 1 line
2.13.5
Reviewed-by on v8 10/13
#32 lore raw

Re: [Qemu-devel] [PATCH v8 10/13] qemu.py: use poll() instead of 'returncode'

Quoted 10 lines · Amador Pahim

The 'returncode' Popen attribute is not guaranteed to be updated. It actually depends on a call to either poll(), wait() or communicate().

On the other hand, poll() will: "Check if child process has terminated. Set and return returncode attribute."

Let's use the poll() to check whether the process is running and to get the updated process exit code, when the process is finished.

#33 lore raw

PATCH v8 11/13 qemu.py: cleanup redundant calls in launch()

In reply to Amador Pahim

Now that shutdown() is guaranteed to always execute self._load_io_log() and self._post_shutdown(), their calls in 'except' became redundant and we can safely replace it by a call to shutdown().

Due to this change, shutdown() can be now called even before the creation of the _qemu_log_path. So, to avoid errors with _load_io_log(), this patch makes sure we will only read the _qemu_log_path if it was previously created by us.

---
 scripts/qemu.py | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
scripts/qemu.py +4 -7
index 87a2212b77..03e4cc34b7 100644
@@ -115,8 +115,9 @@ class QEMUMachine(object):
         return self._popen.pid
 
     def _load_io_log(self):
-        with open(self._qemu_log_path, "r") as fh:
-            self._iolog = fh.read()
+        if self._qemu_log_path in self._created_files:
+            with open(self._qemu_log_path, "r") as fh:
+                self._iolog = fh.read()
 
     def _base_args(self):
         if isinstance(self._monitor_address, tuple):
@@ -169,11 +170,7 @@ class QEMUMachine(object):
         try:
             self._launch()
         except:
-            if self.is_running():
-                self._popen.kill()
-                self._popen.wait()
-            self._load_io_log()
-            self._post_shutdown()
+            self.shutdown()
 
             LOG.debug('Error launching VM')
             if self._qemu_full_args:
Signature · 1 line
2.13.5
Reviewed-by on v8 11/13
#34 lore raw

Re: [Qemu-devel] [PATCH v8 11/13] qemu.py: cleanup redundant calls in launch()

Quoted 47 lines · Amador Pahim

Now that shutdown() is guaranteed to always execute self._load_io_log() and self._post_shutdown(), their calls in 'except' became redundant and we can safely replace it by a call to shutdown().

Due to this change, shutdown() can be now called even before the creation of the _qemu_log_path. So, to avoid errors with _load_io_log(), this patch makes sure we will only read the _qemu_log_path if it was previously created by us.

---
 scripts/qemu.py | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
scripts/qemu.py +4 -7
index 87a2212b77..03e4cc34b7 100644
@@ -115,8 +115,9 @@ class QEMUMachine(object):
         return self._popen.pid
 
     def _load_io_log(self):
-        with open(self._qemu_log_path, "r") as fh:
-            self._iolog = fh.read()
+        if self._qemu_log_path in self._created_files:
+            with open(self._qemu_log_path, "r") as fh:
+                self._iolog = fh.read()
 
     def _base_args(self):
         if isinstance(self._monitor_address, tuple):
@@ -169,11 +170,7 @@ class QEMUMachine(object):
         try:
             self._launch()
         except:
-            if self.is_running():
-                self._popen.kill()
-                self._popen.wait()
-            self._load_io_log()
-            self._post_shutdown()
+            self.shutdown()
 
             LOG.debug('Error launching VM')
             if self._qemu_full_args:
Signature · 1 line
2.13.5
#35 lore raw

PATCH v8 12/13 qemu.py: launch vm only if it's not running

In reply to Amador Pahim

A new call to launch() with a running VM will fall in exception and consequently call shutdown().

This patch makes launch() to raise an exception when it's called with VM already running.

---
 scripts/qemu.py | 3 +++
 1 file changed, 3 insertions(+)
scripts/qemu.py +3 -0
index 03e4cc34b7..363f649a7e 100644
@@ -165,6 +165,9 @@ class QEMUMachine(object):
         Try to launch the VM and make sure we cleanup and expose the
         command line/output in case of exception.
         '''
+        if self.is_running():
+            raise QEMUMachineError('VM already running')
+
         self._iolog = None
         self._qemu_full_args = None
         try:
Signature · 1 line
2.13.5
Reviewed-by on v8 12/13
#36 lore raw

Re: [Qemu-devel] [PATCH v8 12/13] qemu.py: launch vm only if it's not running

Quoted 29 lines · Amador Pahim

A new call to launch() with a running VM will fall in exception and consequently call shutdown().

This patch makes launch() to raise an exception when it's called with VM already running.

---
 scripts/qemu.py | 3 +++
 1 file changed, 3 insertions(+)
scripts/qemu.py +3 -0
index 03e4cc34b7..363f649a7e 100644
@@ -165,6 +165,9 @@ class QEMUMachine(object):
         Try to launch the VM and make sure we cleanup and expose the
         command line/output in case of exception.
         '''
+        if self.is_running():
+            raise QEMUMachineError('VM already running')
+
         self._iolog = None
         self._qemu_full_args = None
         try:
Signature · 1 line
2.13.5
#37 lore raw

PATCH v8 13/13 qemu.py: don't launch again before shutdown()

In reply to Amador Pahim

If a VM is launched, files are created and a cleanup is required before a new launch. This cleanup is executed by shutdown(), so shutdown() must be called even if the VM is manually terminated (i.e. using kill).

This patch creates a control to make sure launch() will not be executed again if shutdown() is not called after the previous launch().

---
 scripts/qemu.py | 7 +++++++
 1 file changed, 7 insertions(+)
scripts/qemu.py +7 -0
index 363f649a7e..05fca4d268 100644
@@ -52,6 +52,7 @@ class QEMUMachine(object):
         self._debug = debug
         self._qemu_full_args = None
         self._created_files = []
+        self._pending_shutdown = False
 
     # This can be used to add an unused monitor instance.
     def add_monitor_telnet(self, ip, port):
@@ -168,10 +169,14 @@ class QEMUMachine(object):
         if self.is_running():
             raise QEMUMachineError('VM already running')
 
+        if self._pending_shutdown:
+            raise QEMUMachineError('Shutdown pending after previous launch')
+
         self._iolog = None
         self._qemu_full_args = None
         try:
             self._launch()
+            self._pending_shutdown = True
         except:
             self.shutdown()
 
@@ -217,6 +222,8 @@ class QEMUMachine(object):
                 command = ''
             LOG.warn(msg, exitcode, command)
 
+        self._pending_shutdown = False
+
     underscore_to_dash = string.maketrans('_', '-')
     def qmp(self, cmd, conv_keys=True, **args):
         '''Invoke a QMP command and return the result dict'''
Signature · 1 line
2.13.5
#38 lore raw

Re: [Qemu-devel] [PATCH v8 13/13] qemu.py: don't launch again before shutdown()

Quoted 17 lines · Amador Pahim

If a VM is launched, files are created and a cleanup is required before a new launch. This cleanup is executed by shutdown(), so shutdown() must be called even if the VM is manually terminated (i.e. using kill).

This patch creates a control to make sure launch() will not be executed again if shutdown() is not called after the previous launch().

---
 scripts/qemu.py | 7 +++++++
 1 file changed, 7 insertions(+)
scripts/qemu.py +0 -0
index 363f649a7e..05fca4d268 100644

scripts/qemu.py class QEMUMachine(object): · line 55

Quoted 4 lines
scripts/qemu.py +1 -0
@@ -52,6 +52,7 @@ class QEMUMachine(object):
         self._debug = debug
         self._qemu_full_args = None
         self._created_files = []
+        self._pending_shutdown = False

The name is very poor, it can be confused as "user requested a shutdown but the action is pending for some reason". Maybe "self._launched"?

Fam

Quoted 31 lines
scripts/qemu.py +6 -0
     # This can be used to add an unused monitor instance.
     def add_monitor_telnet(self, ip, port):
@@ -168,10 +169,14 @@ class QEMUMachine(object):
         if self.is_running():
             raise QEMUMachineError('VM already running')
 
+        if self._pending_shutdown:
+            raise QEMUMachineError('Shutdown pending after previous launch')
+
         self._iolog = None
         self._qemu_full_args = None
         try:
             self._launch()
+            self._pending_shutdown = True
         except:
             self.shutdown()
 
@@ -217,6 +222,8 @@ class QEMUMachine(object):
                 command = ''
             LOG.warn(msg, exitcode, command)
 
+        self._pending_shutdown = False
+
     underscore_to_dash = string.maketrans('_', '-')
     def qmp(self, cmd, conv_keys=True, **args):
         '''Invoke a QMP command and return the result dict'''
Signature · 1 line
2.13.5
#39 lore raw
In reply to Amador Pahim

On Fri, Sep 01, 2017 at 01:28:16PM +0200, Amador Pahim wrote:

Please update the patches with Reviewed-bys from past revisions so I know which patches I've already reviewed.

If someone has posted a R-b then it should be included in the next revision unless you substantially modify that patch.

Thanks,
Stefan

#40 lore raw
In reply to Amador Pahim

Patches 01-05 were queued on my python-next branch:
https://github.com/ehabkost/qemu/commits/python-next

Please use python-next as base for v9, as I plan to submit a pull request with the contents of python-next soon.

Quoted 60 lines · Amador Pahim

Changes v1->v2:
- Style fixes to make checkpatch.pl happy.
- Rebased.
Changes v2->v3:
- Fix typo in patch 3 ("qemu.py: make 'args' public") commit message.
Changes v3->v4:
- Squash the 2 first commits since they are co-dependant.
- Cleanup launch() and shutdown().
- Reorder the commits, putting the rename of self._args first.
- Rebased.
Changes v4->v5:
- Break the cleanup commit into logical changes and include in the commit messages the rationale for making them.
Changes v5->v6:
- Remove the commit to rename self._args.
- Fix is_running() return before first call to maunch().
- Use python logging system.
- Include the full command line on negative exit code error message.
- Use os.path.null instead of /dev/null.
- Improve the control over the created/deleted files.
Changes v6->v7:
- Split commits in self-contained/atomic changes.
- Addressed the comments from previous version, basically improving the logging messages and the control over created files. See individual commit messages for details.
Changes v7->v8:
- Rebased.
- Reorder commits to avoid break->fix sequence.
- Split commits "use poll() instead of 'returncode'" and "refactor launch()".
- Don't ignore errors in _load_io_log(). Instead, check if we created the file before reading it.
- Use LOG.warn() instead of LOG.debug() for the negative exit code message.
- Fix the exception name called in commits "launch vm only if it's not running" and "don't launch again before shutdown()".
- Minor style fixes.

Amador Pahim (13):
qemu.py: fix is_running() return before first launch() qemu.py: avoid writing to stdout/stderr qemu.py: use os.path.null instead of /dev/null qemu.py: improve message on negative exit code qemu.py: include debug information on launch error qemu.py: make sure we only remove files we create qemu.py: close _qemu_log_path on cleanup qemu.py: refactor launch()
qemu.py: always cleanup on shutdown() qemu.py: use poll() instead of 'returncode' qemu.py: cleanup redundant calls in launch() qemu.py: launch vm only if it's not running qemu.py: don't launch again before shutdown()

 scripts/qemu.py | 136 +++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 101 insertions(+), 35 deletions(-)
Signature · 1 line
2.13.5
Signature · 1 line
Eduardo