Wainer dos Santos Moschetta | 8b272e0 | 2019-12-16 16:14:34 -0300 | [diff] [blame] | 1 | """ |
| 2 | QEMU accel module: |
| 3 | |
| 4 | This module provides utilities for discover and check the availability of |
| 5 | accelerators. |
| 6 | """ |
| 7 | # Copyright (C) 2015-2016 Red Hat Inc. |
| 8 | # Copyright (C) 2012 IBM Corp. |
| 9 | # |
| 10 | # Authors: |
| 11 | # Fam Zheng <famz@redhat.com> |
| 12 | # |
| 13 | # This work is licensed under the terms of the GNU GPL, version 2. See |
| 14 | # the COPYING file in the top-level directory. |
| 15 | # |
| 16 | |
Wainer dos Santos Moschetta | 1650c3e | 2019-12-16 16:14:35 -0300 | [diff] [blame] | 17 | import logging |
Wainer dos Santos Moschetta | 8b272e0 | 2019-12-16 16:14:34 -0300 | [diff] [blame] | 18 | import os |
Wainer dos Santos Moschetta | 1650c3e | 2019-12-16 16:14:35 -0300 | [diff] [blame] | 19 | import subprocess |
| 20 | |
| 21 | LOG = logging.getLogger(__name__) |
Wainer dos Santos Moschetta | 8b272e0 | 2019-12-16 16:14:34 -0300 | [diff] [blame] | 22 | |
| 23 | # Mapping host architecture to any additional architectures it can |
| 24 | # support which often includes its 32 bit cousin. |
| 25 | ADDITIONAL_ARCHES = { |
| 26 | "x86_64" : "i386", |
| 27 | "aarch64" : "armhf" |
| 28 | } |
| 29 | |
Wainer dos Santos Moschetta | 1650c3e | 2019-12-16 16:14:35 -0300 | [diff] [blame] | 30 | def list_accel(qemu_bin): |
| 31 | """ |
| 32 | List accelerators enabled in the QEMU binary. |
| 33 | |
| 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. |
| 37 | """ |
| 38 | if not qemu_bin: |
| 39 | return [] |
| 40 | try: |
| 41 | out = subprocess.check_output([qemu_bin, '-accel', 'help'], |
| 42 | universal_newlines=True) |
| 43 | except: |
| 44 | LOG.debug("Failed to get the list of accelerators in %s", qemu_bin) |
| 45 | raise |
| 46 | # Skip the first line which is the header. |
| 47 | return [acc.strip() for acc in out.splitlines()[1:]] |
| 48 | |
Wainer dos Santos Moschetta | 53a049d | 2019-12-16 16:14:36 -0300 | [diff] [blame] | 49 | def kvm_available(target_arch=None, qemu_bin=None): |
| 50 | """ |
| 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. |
| 55 | |
| 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. |
| 59 | """ |
| 60 | if not os.access("/dev/kvm", os.R_OK | os.W_OK): |
| 61 | return False |
| 62 | if target_arch: |
| 63 | host_arch = os.uname()[4] |
| 64 | if target_arch != host_arch: |
| 65 | if target_arch != ADDITIONAL_ARCHES.get(host_arch): |
| 66 | return False |
| 67 | if qemu_bin and "kvm" not in list_accel(qemu_bin): |
| 68 | return False |
| 69 | return True |
Wainer dos Santos Moschetta | d3ca7bb | 2019-12-16 16:14:37 -0300 | [diff] [blame] | 70 | |
| 71 | def tcg_available(qemu_bin): |
| 72 | """ |
| 73 | Check if TCG is available. |
| 74 | |
| 75 | @param qemu_bin (str): path to the QEMU binary |
| 76 | """ |
| 77 | return 'tcg' in list_accel(qemu_bin) |