From: Eduardo Habkost <ehabkost@redhat.com>
To: Amador Pahim <apahim@redhat.com>
Cc: qemu-devel@nongnu.org, stefanha@gmail.com, famz@redhat.com,
berrange@redhat.com, mreitz@redhat.com, kwolf@redhat.com,
armbru@redhat.com, crosa@redhat.com, ldoktor@redhat.com
Subject: Re: [Qemu-devel] [PATCH v5 4/6] qemu.py: cleanup launch()
Date: Tue, 25 Jul 2017 17:42:33 -0300 [thread overview]
Message-ID: <20170725204233.GV2757@localhost.localdomain> (raw)
In-Reply-To: <20170725171014.25193-5-apahim@redhat.com>
On Tue, Jul 25, 2017 at 07:10:12PM +0200, Amador Pahim wrote:
> launch() is currently taking care of a number of flows, each one if its
> own exception treatment, depending on the VM state and the files
> creation state.
>
> This patch makes launch() more resilient, off-loading the core calls to
> the new _launch() and calling shutdown() if any exception is raised by
> _launch(), making sure VM will be terminated and cleaned up.
>
> Signed-off-by: Amador Pahim <apahim@redhat.com>
> ---
> scripts/qemu.py | 42 +++++++++++++++++++++++++-----------------
> 1 file changed, 25 insertions(+), 17 deletions(-)
>
> diff --git a/scripts/qemu.py b/scripts/qemu.py
> index 56142ed59b..45a63e8e9d 100644
> --- a/scripts/qemu.py
> +++ b/scripts/qemu.py
> @@ -99,8 +99,11 @@ 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()
> + try:
> + with open(self._qemu_log_path, "r") as fh:
> + self._iolog = fh.read()
> + except IOError:
> + pass
I don't like the idea of ignoring errors unconditionally. It's
OK to ignore the file if we are recovering from a crash and
didn't even create it, but it's not OK if we ran QEMU
successfully and we really want to load the log file.
Maybe an optional ignore_errors argument to shutdown() and its
helpers, to tell the shutdown functions that it is really OK to
ignore errors?
>
> def _base_args(self):
> if isinstance(self._monitor_address, tuple):
> @@ -126,23 +129,28 @@ class QEMUMachine(object):
> self._remove_if_exists(self._qemu_log_path)
>
> def launch(self):
> - '''Launch the VM and establish a QMP connection'''
> - devnull = open('/dev/null', 'rb')
> - qemulog = open(self._qemu_log_path, 'wb')
This was moved inside the try block.
This means we may try to read the log file even if we failed to
create it. This will have funny side-effects if the log file
already existed and we didn't have permissions to write to it.
For example:
("/var/tmp/myvm.log" was created by another user)
>>> m = qemu.QEMUMachine(binary='/usr/bin/qemu-system-x86_64', name='myvm')
>>> m.launch()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "qemu.py", line 151, in launch
self.shutdown()
File "qemu.py", line 182, in shutdown
self._post_shutdown()
File "qemu.py", line 130, in _post_shutdown
self._remove_if_exists(self._qemu_log_path)
File "qemu.py", line 83, in _remove_if_exists
os.remove(path)
OSError: [Errno 1] Operation not permitted: '/var/tmp/myvm.log'
>>> m.get_log()
'old log file\n'
>>>
> + '''
> + Try to launch the VM and make sure we cleanup on exception.
> + '''
> + if self.is_running():
> + return
Why exactly is this necessary?
Calling launch() twice is likely to be a mistake (e.g. what if
self.args was changed?). I would raise an Exception instead.
> +
> 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._post_launch()
> + self._launch()
> except:
> - if self.is_running():
> - self._popen.kill()
> - self._popen.wait()
> - self._load_io_log()
> - self._post_shutdown()
> + self.shutdown()
> raise
>
> + def _launch(self):
> + '''Launch the VM and establish a QMP connection.'''
> + devnull = open('/dev/null', 'rb')
> + qemulog = open(self._qemu_log_path, 'wb')
> + self._pre_launch()
> + args = self._wrapper + [self._binary] + self._base_args() + self._args
This looks broken:
>>> m.launch()
Error launching VM.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "qemu.py", line 147, in launch
args = self._wrapper + [self._binary] + self._base_args() + self._args
AttributeError: 'QEMUMachine' object has no attribute '_args'
> + self._popen = subprocess.Popen(args, stdin=devnull, stdout=qemulog,
> + stderr=subprocess.STDOUT, shell=False)
> + self._post_launch()
> +
> def shutdown(self):
> '''Terminate the VM and clean up'''
> if self.is_running():
> @@ -156,8 +164,8 @@ class QEMUMachine(object):
> if exitcode < 0:
> sys.stderr.write('qemu received signal %i\n' % -exitcode)
>
> - self._load_io_log()
> - self._post_shutdown()
It looks like the existing code isn't safe, and can call
_post_shutdown() before _post_launch() was called.
What if QEMUQtestMachine._pre_launch() failed because the qtest
socket is already in use by another process? We shouldn't delete
a socket that doesn't even belong to us.
I suggest setting self._qemu_log_path, self._monitor_address,
self._qtest_path only after the files were really created, and
make _post_shutdown() delete the files only if those attributes
are not None (meaning we will only delete files that we created).
> + self._load_io_log()
> + self._post_shutdown()
>
> underscore_to_dash = string.maketrans('_', '-')
> def qmp(self, cmd, conv_keys=True, **args):
> --
> 2.13.3
>
--
Eduardo
next prev parent reply other threads:[~2017-07-25 20:42 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-07-25 17:10 [Qemu-devel] [PATCH v5 0/6] scripts/qemu.py fixes and cleanups Amador Pahim
2017-07-25 17:10 ` [Qemu-devel] [PATCH v5 1/6] qemu.py: make 'args' public Amador Pahim
2017-07-27 14:18 ` Stefan Hajnoczi
2017-07-27 14:59 ` Amador Pahim
2017-07-25 17:10 ` [Qemu-devel] [PATCH v5 2/6] qemu.py: use poll() instead of 'returncode' Amador Pahim
2017-07-27 14:11 ` Stefan Hajnoczi
2017-07-25 17:10 ` [Qemu-devel] [PATCH v5 3/6] qemu.py: cleanup message on negative exit code Amador Pahim
2017-07-25 19:08 ` Cleber Rosa
2017-07-25 19:51 ` Eduardo Habkost
2017-07-27 8:21 ` Amador Pahim
2017-07-30 20:24 ` Eduardo Habkost
2017-07-27 14:12 ` Stefan Hajnoczi
2017-07-25 17:10 ` [Qemu-devel] [PATCH v5 4/6] qemu.py: cleanup launch() Amador Pahim
2017-07-25 19:17 ` Cleber Rosa
2017-07-25 20:42 ` Eduardo Habkost [this message]
2017-07-27 7:56 ` Amador Pahim
2017-07-25 17:10 ` [Qemu-devel] [PATCH v5 5/6] qemu.py: make sure shutdown() is called before launching again Amador Pahim
2017-07-25 19:58 ` Eduardo Habkost
2017-07-25 17:10 ` [Qemu-devel] [PATCH v5 6/6] qemu.py: include qemu command line and output on launch error Amador Pahim
2017-07-25 21:17 ` Eduardo Habkost
2017-07-27 8:01 ` Amador Pahim
2017-07-27 13:31 ` [Qemu-devel] [PATCH v5 0/6] scripts/qemu.py fixes and cleanups no-reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170725204233.GV2757@localhost.localdomain \
--to=ehabkost@redhat.com \
--cc=apahim@redhat.com \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=crosa@redhat.com \
--cc=famz@redhat.com \
--cc=kwolf@redhat.com \
--cc=ldoktor@redhat.com \
--cc=mreitz@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).