summaryrefslogtreecommitdiff
path: root/tools/virt-host-validate-qemu.c
blob: 096419869ca1610ad2777147024ec82c795ffd5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * virt-host-validate-qemu.c: Sanity check a QEMU hypervisor host
 *
 * Copyright (C) 2012 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include "virt-host-validate-qemu.h"
#include "virt-host-validate-common.h"
#include "virarch.h"
#include "virbitmap.h"
#include "vircgroup.h"

int virHostValidateQEMU(void)
{
    g_autoptr(virBitmap) flags = NULL;
    int ret = 0;
    bool hasHwVirt = false;
    bool hasVirtFlag = false;
    virArch arch = virArchFromHost();
    const char *kvmhint = _("Check that CPU and firmware supports virtualization "
                            "and kvm module is loaded");

    if (!(flags = virHostValidateGetCPUFlags()))
        return -1;

    switch ((int)arch) {
    case VIR_ARCH_I686:
    case VIR_ARCH_X86_64:
        hasVirtFlag = true;
        kvmhint = _("Check that the 'kvm-intel' or 'kvm-amd' modules are "
                    "loaded & the BIOS has enabled virtualization");
        if (virBitmapIsBitSet(flags, VIR_HOST_VALIDATE_CPU_FLAG_SVM) ||
            virBitmapIsBitSet(flags, VIR_HOST_VALIDATE_CPU_FLAG_VMX))
            hasHwVirt = true;
        break;
    case VIR_ARCH_S390:
    case VIR_ARCH_S390X:
        hasVirtFlag = true;
        if (virBitmapIsBitSet(flags, VIR_HOST_VALIDATE_CPU_FLAG_SIE))
            hasHwVirt = true;
        break;
    case VIR_ARCH_PPC64:
    case VIR_ARCH_PPC64LE:
        hasVirtFlag = true;
        hasHwVirt = true;
        break;
    default:
        hasHwVirt = false;
    }

    if (hasVirtFlag) {
        virHostMsgCheck("QEMU", "%s", _("for hardware virtualization"));
        if (hasHwVirt) {
            virHostMsgPass();
        } else {
            virHostMsgFail(VIR_HOST_VALIDATE_FAIL,
                           _("Host not compatible with KVM; HW virtualization CPU features not found. "
                             "Only emulated CPUs are available; performance will be significantly limited"));
            ret = -1;
        }
    }

    if (hasHwVirt || !hasVirtFlag) {
        if (virHostValidateDeviceExists("QEMU", "/dev/kvm",
                                        VIR_HOST_VALIDATE_FAIL,
                                        kvmhint) <0)
            ret = -1;
        else if (virHostValidateDeviceAccessible("QEMU", "/dev/kvm",
                                                 VIR_HOST_VALIDATE_FAIL,
                                                 _("Check /dev/kvm is world writable or you are in "
                                                   "a group that is allowed to access it")) < 0)
            ret = -1;
    }

    if (arch == VIR_ARCH_PPC64 || arch == VIR_ARCH_PPC64LE) {
        virHostMsgCheck("QEMU", "%s", _("for PowerPC KVM module loaded"));

        if (!virHostKernelModuleIsLoaded("kvm_hv"))
            virHostMsgFail(VIR_HOST_VALIDATE_WARN,
                          _("Load kvm_hv for better performance"));
        else
            virHostMsgPass();
    }

    if (virHostValidateDeviceExists("QEMU", "/dev/vhost-net",
                                    VIR_HOST_VALIDATE_WARN,
                                    _("Load the 'vhost_net' module to improve performance "
                                      "of virtio networking")) < 0)
        ret = -1;

    if (virHostValidateDeviceExists("QEMU", "/dev/net/tun",
                                    VIR_HOST_VALIDATE_FAIL,
                                    _("Load the 'tun' module to enable networking for QEMU guests")) < 0)
        ret = -1;

    if (virHostValidateCGroupControllers("QEMU",
                                         (1 << VIR_CGROUP_CONTROLLER_MEMORY) |
                                         (1 << VIR_CGROUP_CONTROLLER_CPU) |
                                         (1 << VIR_CGROUP_CONTROLLER_CPUACCT) |
                                         (1 << VIR_CGROUP_CONTROLLER_CPUSET) |
                                         (1 << VIR_CGROUP_CONTROLLER_DEVICES) |
                                         (1 << VIR_CGROUP_CONTROLLER_BLKIO),
                                         VIR_HOST_VALIDATE_WARN) < 0) {
        ret = -1;
    }

    if (virHostValidateIOMMU("QEMU",
                             VIR_HOST_VALIDATE_WARN) < 0)
        ret = -1;

    if (virHostValidateSecureGuests("QEMU",
                                    VIR_HOST_VALIDATE_WARN) < 0)
        ret = -1;

    return ret;
}