1"""
2Utilities useful to client control files that test KVM.
3"""
4
5from autotest_lib.client.bin import utils
6from autotest_lib.client.common_lib import error
7
8def get_kvm_arch():
9    """
10    Determines the kvm architecture kernel module that should be loaded.
11
12    @return: "kvm_amd", "kvm_intel", or raise TestError exception
13    """
14    arch_type = ""
15    for line in open("/proc/cpuinfo"):
16        if arch_type == "":
17            if "AuthenticAMD" in line:
18                arch_type = "kvm_amd"
19            elif "GenuineIntel" in line:
20                arch_type = "kvm_intel"
21        elif "flags" in line:
22            if arch_type == "kvm_amd" and "svm" in line:
23                return arch_type
24            if arch_type == "kvm_intel" and "vmx" in line:
25                return arch_type
26    raise error.TestError("CPU Must be AMD or Intel, and must be KVM ready.")
27
28
29def load_kvm():
30    """
31    Loads the appropriate KVM kernel modules
32    """
33    kvm_status = utils.system('modprobe kvm')
34    kvm_amdintel_status = utils.system("modprobe " + kvm_arch)
35    if kvm_status:
36        return kvm_status
37    else:
38        return kvm_amdintel_status
39
40def unload_kvm():
41    """
42    Unloads the appropriate KVM kernel modules
43    """
44    kvm_amdintel_status = utils.system("rmmod " + kvm_arch)
45    kvm_status = utils.system('rmmod kvm')
46    if kvm_status:
47        return kvm_status
48    else:
49        return kvm_amdintel_status
50
51
52kvm_arch = get_kvm_arch()
53