From bc0fed4b43d2e7c7440423cddfded08cdefd163d Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Fri, 25 Jan 2013 12:31:22 +0100
Subject: [PATCH] kvm: fix bug while fetching -device list

_GetKVMOutput expects the command to succeed, but unfortunately on some
versions of kvm "-device ?" will output a correct list of devices, while
exiting with an error code.

To fix this we accept failure in that case (note that this doesn't
change the situation when this command is not supported, as in that case
the output will still not contain the correct string).

This patch also updates the docstring, adding the parameter types and
description.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/hypervisor/hv_kvm.py | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/lib/hypervisor/hv_kvm.py b/lib/hypervisor/hv_kvm.py
index 20fddca53..7a0a209f4 100644
--- a/lib/hypervisor/hv_kvm.py
+++ b/lib/hypervisor/hv_kvm.py
@@ -589,10 +589,13 @@ class KVMHypervisor(hv_base.BaseHypervisor):
   _KVMOPT_HELP = "help"
   _KVMOPT_MLIST = "mlist"
   _KVMOPT_DEVICELIST = "devicelist"
+
+  # Command to execute to get the output from kvm, and whether to
+  # accept the output even on failure.
   _KVMOPTS_CMDS = {
-    _KVMOPT_HELP: ["--help"],
-    _KVMOPT_MLIST: ["-M", "?"],
-    _KVMOPT_DEVICELIST: ["-device", "?"],
+    _KVMOPT_HELP: (["--help"], False),
+    _KVMOPT_MLIST: (["-M", "?"], False),
+    _KVMOPT_DEVICELIST: (["-device", "?"], True),
   }
 
   def __init__(self):
@@ -1713,14 +1716,20 @@ class KVMHypervisor(hv_base.BaseHypervisor):
   def _GetKVMOutput(cls, kvm_path, option):
     """Return the output of a kvm invocation
 
+    @type kvm_path: string
+    @param kvm_path: path to the kvm executable
+    @type option: a key of _KVMOPTS_CMDS
+    @param option: kvm option to fetch the output from
     @return: output a supported kvm invocation
     @raise errors.HypervisorError: when the KVM help output cannot be retrieved
 
     """
     assert option in cls._KVMOPTS_CMDS, "Invalid output option"
 
-    result = utils.RunCmd([kvm_path] + cls._KVMOPTS_CMDS[option])
-    if result.failed:
+    optlist, can_fail = cls._KVMOPTS_CMDS[option]
+
+    result = utils.RunCmd([kvm_path] + optlist)
+    if result.failed and not can_fail:
       raise errors.HypervisorError("Unable to get KVM %s output" %
                                     " ".join(cls._KVMOPTS_CMDS[option]))
     return result.output
-- 
GitLab