Skip to content

Commit 6b17f8f

Browse files
authored
Merge pull request #84 from nathanchance/update-can-use-kvm
boot-qemu.py: Simplify can_use_kvm()
2 parents d25085a + 867976c commit 6b17f8f

1 file changed

Lines changed: 37 additions & 35 deletions

File tree

boot-qemu.py

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -93,53 +93,55 @@ def parse_arguments():
9393
return parser.parse_args()
9494

9595

96+
def arm64_have_el1_32():
97+
"""
98+
Calls 'aarch64_32_bit_el1_supported' to see if 32-bit EL1 is supported on
99+
the current machine.
100+
101+
Returns:
102+
True if 32-bit EL1 is supported, false if not
103+
"""
104+
try:
105+
subprocess.run(base_folder.joinpath('utils',
106+
'aarch64_32_bit_el1_supported'),
107+
check=True)
108+
except subprocess.CalledProcessError:
109+
return False
110+
return True
111+
112+
96113
def can_use_kvm(can_test_for_kvm, guest_arch):
97114
"""
98115
Checks that KVM can be used for faster VMs based on:
99116
* User's request
100117
* Whether or not '--no-kvm' was used
101-
* '/dev/kvm' is available
102-
* The guest architecture
103-
* Only 'arm'/'arm32_v7', 'arm64', 'arm64be', 'x86', and 'x86_64'
104-
are supported with KVM
105-
* Availability of hardware virtualization support
106-
* aarch64 may not support accelerated 32-bit guests
107-
* i386 and x86_64 need the virtualization extensions in
108-
'/proc/cpuinfo'
118+
* '/dev/kvm' is readable and writable by the current user
119+
* Implies hardware virtualization support
120+
* The host architecture relative to guest architecture
121+
* aarch64 always supports accelerated aarch64 guests, may support
122+
accelerated aarch32 guests
123+
* x86_64 always supports accelerated 64-bit and 32-bit x86 guests
109124
110125
Parameters:
111-
user_kvm_opt_out (bool): False if user passed in '--no-kvm', True if not
126+
can_test_for_vm (bool): False if user passed in '--no-kvm', True if not
112127
guest_arch (str): The guest architecture being run.
113128
114129
Returns:
115130
True if KVM can be used based on the above parameters, False if not.
116131
"""
117-
if can_test_for_kvm:
118-
# /dev/kvm must exist to use KVM with QEMU
119-
if Path("/dev/kvm").exists():
120-
host_arch = platform.machine()
121-
122-
if host_arch == "aarch64":
123-
# If /dev/kvm exists on aarch64, KVM is supported for aarch64 guests
124-
if "arm64" in guest_arch:
125-
return True
126-
# 32-bit EL1 is not always supported, test for it first
127-
if guest_arch in ("arm", "arm32_v7"):
128-
check_32_bit_el1 = base_folder.joinpath(
129-
"utils", "aarch64_32_bit_el1_supported")
130-
try:
131-
subprocess.run([check_32_bit_el1], check=True)
132-
except subprocess.CalledSubprocessError:
133-
return False
134-
return True
135-
136-
if host_arch == "x86_64" and "x86" in guest_arch:
137-
# Check /proc/cpuinfo for whether or not the machine supports hardware virtualization
138-
cpuinfo = Path("/proc/cpuinfo").read_text(encoding='utf-8')
139-
# SVM is AMD, VMX is Intel
140-
return cpuinfo.count("svm") > 0 or cpuinfo.count("vmx") > 0
141-
142-
# We could not prove that we could use KVM safely so don't try
132+
# /dev/kvm must be readable and writeable to use KVM with QEMU
133+
if can_test_for_kvm and os.access('/dev/kvm', os.R_OK | os.W_OK):
134+
host_arch = platform.machine()
135+
136+
if host_arch == "aarch64":
137+
if guest_arch in ('arm', 'arm32_v7'):
138+
return arm64_have_el1_32()
139+
return "arm64" in guest_arch
140+
141+
if host_arch == "x86_64":
142+
return "x86" in guest_arch
143+
144+
# If we could not prove that we can use KVM safely, don't try
143145
return False
144146

145147

0 commit comments

Comments
 (0)