From nobody Wed Nov 5 02:55:35 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1500900630477273.9242674729602; Mon, 24 Jul 2017 05:50:30 -0700 (PDT) Received: from localhost ([::1]:54716 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dZcod-0004xu-Ry for importer@patchew.org; Mon, 24 Jul 2017 08:50:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48666) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dZcl1-0002Ke-7y for qemu-devel@nongnu.org; Mon, 24 Jul 2017 08:46:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dZckx-0001Xg-A1 for qemu-devel@nongnu.org; Mon, 24 Jul 2017 08:46:43 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60560) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dZckx-0001Wj-0u for qemu-devel@nongnu.org; Mon, 24 Jul 2017 08:46:39 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DEA2B7EA96 for ; Mon, 24 Jul 2017 12:46:37 +0000 (UTC) Received: from t460p.pahim.org.com (ovpn-204-126.brq.redhat.com [10.40.204.126]) by smtp.corp.redhat.com (Postfix) with ESMTP id 342C168D29; Mon, 24 Jul 2017 12:46:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com DEA2B7EA96 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=apahim@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com DEA2B7EA96 From: Amador Pahim To: qemu-devel@nongnu.org Date: Mon, 24 Jul 2017 14:44:38 +0200 Message-Id: <20170724124438.27344-3-apahim@redhat.com> In-Reply-To: <20170724124438.27344-1-apahim@redhat.com> References: <20170724124438.27344-1-apahim@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 24 Jul 2017 12:46:38 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 2/2] qemu.py: cleanup and fixes X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, ldoktor@redhat.com, ehabkost@redhat.com, Amador Pahim , armbru@redhat.com, mreitz@redhat.com, crosa@redhat.com, famz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" is_running(): - Use Popen.poll() instead of Popen.returncode to check whether the VM is running or not. exitcode(): - Use Popen.poll() instead of Popen.returncode to return an updated exit code. _load_io_log(): - Add a try/except to prevent raising exception when qemu io file does not exist. launch(): - If VM is already running, do nothing. - If vm is not running but was not cleaned up, call shutdown() before launching again. - Offload the core of this method to _launch(). - Load the args and try to call _launch(). - Make sure we cleanup on exception. - On exception, print an error message with the qemu command line and output. _launch(): - Execute _pre_launch(), subprocess.Popen() and self._post_launch(). - No try/except here. Any exceptions will be handled by the caller. shutdown(): - Make sure self._popen is not None before calling self._popen.wait(). - Cleanup the message on negative exit codes. - Always execute self._load_io_log() and self._post_shutdown(). Signed-off-by: Amador Pahim --- scripts/qemu.py | 69 ++++++++++++++++++++++++++++++++++++++---------------= ---- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/scripts/qemu.py b/scripts/qemu.py index fb83e3f998..76d4880f36 100644 --- a/scripts/qemu.py +++ b/scripts/qemu.py @@ -39,6 +39,7 @@ class QEMUMachine(object): self._iolog =3D None self._socket_scm_helper =3D socket_scm_helper self._debug =3D debug + self._shutdown_pending =3D False =20 # This can be used to add an unused monitor instance. def add_monitor_telnet(self, ip, port): @@ -86,12 +87,12 @@ class QEMUMachine(object): raise =20 def is_running(self): - return self._popen and (self._popen.returncode is None) + return self._popen and (self._popen.poll() is None) =20 def exitcode(self): if self._popen is None: return None - return self._popen.returncode + return self._popen.poll() =20 def get_pid(self): if not self.is_running(): @@ -99,8 +100,11 @@ class QEMUMachine(object): return self._popen.pid =20 def _load_io_log(self): - with open(self._qemu_log_path, "r") as fh: - self._iolog =3D fh.read() + try: + with open(self._qemu_log_path, "r") as fh: + self._iolog =3D fh.read() + except IOError: + pass =20 def _base_args(self): if isinstance(self._monitor_address, tuple): @@ -126,25 +130,43 @@ class QEMUMachine(object): self._remove_if_exists(self._qemu_log_path) =20 def launch(self): - '''Launch the VM and establish a QMP connection''' - devnull =3D open('/dev/null', 'rb') - qemulog =3D open(self._qemu_log_path, 'wb') + ''' + Try to launch the VM and make sure we cleanup and expose the + command line/output in case of exception. + ''' + if self.is_running(): + return + + if self._shutdown_pending: + sys.stderr.write('shutdown() was not called after previous ' + 'launch(). Calling now.\n') + self.shutdown() + try: - self._pre_launch() - args =3D self._wrapper + [self._binary] + self._base_args() + = self._args - self._popen =3D subprocess.Popen(args, stdin=3Ddevnull, stdout= =3Dqemulog, - stderr=3Dsubprocess.STDOUT, she= ll=3DFalse) - self._post_launch() + args =3D None + args =3D self._wrapper + [self._binary] + self._base_args() + = self.args + self._launch(args) + self._shutdown_pending =3D True except: - if self.is_running(): - self._popen.kill() - self._popen.wait() - self._load_io_log() - self._post_shutdown() + self.shutdown() + sys.stderr.write('Error launching VM.\n%s%s' % + ('Command:\n%s\n' % + ' '.join(args) if args else '', + 'Output:\n%s\n' % + ''.join(self._iolog) if self._iolog else '')) raise =20 + def _launch(self, args): + '''Launch the VM and establish a QMP connection.''' + devnull =3D open('/dev/null', 'rb') + qemulog =3D open(self._qemu_log_path, 'wb') + self._pre_launch() + self._popen =3D subprocess.Popen(args, stdin=3Ddevnull, stdout=3Dq= emulog, + stderr=3Dsubprocess.STDOUT, shell= =3DFalse) + self._post_launch() + def shutdown(self): - '''Terminate the VM and clean up''' + '''Terminate the VM and clean up.''' if self.is_running(): try: self._qmp.cmd('quit') @@ -152,11 +174,12 @@ class QEMUMachine(object): except: self._popen.kill() =20 - exitcode =3D self._popen.wait() - if exitcode < 0: - sys.stderr.write('qemu received signal %i: %s\n' % (-exitc= ode, ' '.join(self._args))) - self._load_io_log() - self._post_shutdown() + if self._popen and self._popen.wait() < 0: + sys.stderr.write('qemu received signal %i\n' % -self.exitcode(= )) + + self._load_io_log() + self._post_shutdown() + self._shutdown_pending =3D False =20 underscore_to_dash =3D string.maketrans('_', '-') def qmp(self, cmd, conv_keys=3DTrue, **args): --=20 2.13.3