4 This module provides utilities for discover and check the availability of
7 # Copyright (C) 2015-2016 Red Hat Inc.
8 # Copyright (C) 2012 IBM Corp.
11 # Fam Zheng <famz@redhat.com>
13 # This work is licensed under the terms of the GNU GPL, version 2. See
14 # the COPYING file in the top-level directory.
21 LOG
= logging
.getLogger(__name__
)
23 # Mapping host architecture to any additional architectures it can
24 # support which often includes its 32 bit cousin.
30 def list_accel(qemu_bin
):
32 List accelerators enabled in the QEMU binary.
34 @param qemu_bin (str): path to the QEMU binary.
35 @raise Exception: if failed to run `qemu -accel help`
36 @return a list of accelerator names.
41 out
= subprocess
.check_output([qemu_bin
, '-accel', 'help'],
42 universal_newlines
=True)
44 LOG
.debug("Failed to get the list of accelerators in %s", qemu_bin
)
46 # Skip the first line which is the header.
47 return [acc
.strip() for acc
in out
.splitlines()[1:]]
49 def kvm_available(target_arch
=None, qemu_bin
=None):
51 Check if KVM is available using the following heuristic:
52 - Kernel module is present in the host;
53 - Target and host arches don't mismatch;
54 - KVM is enabled in the QEMU binary.
56 @param target_arch (str): target architecture
57 @param qemu_bin (str): path to the QEMU binary
58 @return True if kvm is available, otherwise False.
60 if not os
.access("/dev/kvm", os
.R_OK | os
.W_OK
):
63 host_arch
= os
.uname()[4]
64 if target_arch
!= host_arch
:
65 if target_arch
!= ADDITIONAL_ARCHES
.get(host_arch
):
67 if qemu_bin
and "kvm" not in list_accel(qemu_bin
):
71 def tcg_available(qemu_bin
):
73 Check if TCG is available.
75 @param qemu_bin (str): path to the QEMU binary
77 return 'tcg' in list_accel(qemu_bin
)