Skip to content

Commit bb6099d

Browse files
committed
Detect Hyper-V
1 parent 0b41228 commit bb6099d

1 file changed

Lines changed: 43 additions & 26 deletions

File tree

Lib/test/pythoninfo.py

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,15 +1159,23 @@ def get_machine_id():
11591159

11601160

11611161
def detect_virt_windows(info_add):
1162-
# On Windows, use WMI to get the computer manufacturer and the BIOS version
1162+
# On Windows, use WMI to detect the virtualization.
1163+
#
1164+
# Microsoft Hyper-V:
1165+
# - Win32_Bios.Version = "VRTUAL - 1"
1166+
# - Win32_ComputerSystem.Model = "Virtual Machine"
1167+
# - Win32_ComputerSystem.Manufacturer = "Microsoft Corporation"
11631168
#
11641169
# VMware:
1165-
# - Win32_Bios.SerialNumber starts with "VMware-"
11661170
# - Win32_ComputerSystem.Model = "VMware"
11671171
# - Win32_ComputerSystem.Manufacturer = "VMware"
1172+
# - Win32_Bios.SerialNumber starts with "VMware-"
11681173
#
11691174
# QEMU:
11701175
# - Win32_ComputerSystem.Manufacturer = "QEMU"
1176+
# - Win32_ComputerSystem.Model = "Standard PC (Q35 + ICH9, 2009)"
1177+
# - Win32_Bios.Version = "BOCHS - 1"
1178+
# - Win32_Bios.Manufacturer = "EDK II"
11711179
#
11721180
# Parallels:
11731181
# - Win32_Bios.Version = "PARALLELS"
@@ -1177,46 +1185,55 @@ def detect_virt_windows(info_add):
11771185
# - Win32_ComputerSystem.Model = "VirtualBox"
11781186
# - Win32_ComputerSystem.Manufacturer = "innotek GmbH"
11791187

1180-
data = wmi_query("SELECT Model, Manufacturer FROM Win32_ComputerSystem")
1181-
known_virt = (
1188+
KNOWN_VIRT = (
11821189
'QEMU',
11831190
'VMware',
11841191
'VirtualBox',
1192+
'Xen',
1193+
'oVirt',
11851194
)
1186-
model = data.get('Model', '')
1187-
if model in known_virt:
1188-
return model
1189-
manufacturer = data.get('Manufacturer', '')
1190-
if manufacturer in known_virt:
1191-
return manufacturer
1192-
1193-
data = wmi_query("SELECT Version FROM Win32_Bios")
1194-
bios_version = data.get('Version', '')
1195-
if bios_version in known_virt:
1196-
return bios_version
1197-
known_bios_versions = {
1195+
KNOWN_BIOS_VERSIONS = {
1196+
"VRTUAL - 1": "Microsoft Hyper-V",
11981197
"PARALLELS": "Parallels",
11991198
"VBOX": "VirtualBox",
12001199
}
1200+
1201+
computer = wmi_query("SELECT Model, Manufacturer FROM Win32_ComputerSystem")
1202+
computer_model = computer.get('Model', '')
1203+
if computer_model in KNOWN_VIRT:
1204+
return computer_model
1205+
computer_manufacturer = computer.get('Manufacturer', '')
1206+
if computer_manufacturer in KNOWN_VIRT:
1207+
return computer_manufacturer
1208+
1209+
bios = wmi_query("SELECT Version, Manufacturer FROM Win32_Bios")
1210+
1211+
bios_version = bios.get('Version', '')
1212+
if bios_version in KNOWN_VIRT:
1213+
return bios_version
12011214
try:
1202-
return known_bios_versions[bios_version]
1215+
return KNOWN_BIOS_VERSIONS[bios_version]
12031216
except KeyError:
12041217
pass
12051218

1206-
# Log the values to update patterns on new VM
1207-
if model:
1208-
info_add('system.model', model)
1209-
if manufacturer:
1210-
info_add('system.manufacturer', manufacturer)
1219+
bios_manufacturer = bios.get('Manufacturer', '')
1220+
if bios_manufacturer in KNOWN_VIRT:
1221+
return bios_manufacturer
1222+
1223+
# Log the values to update the code if a new VM is discovered
1224+
if computer_model:
1225+
info_add('system.computer.model', computer_model)
1226+
if computer_manufacturer:
1227+
info_add('system.computer.manufacturer', computer_manufacturer)
12111228
if bios_version:
1212-
info_add('system.bios_version', bios_version)
1229+
info_add('system.bios.version', bios_version)
1230+
if bios_manufacturer:
1231+
info_add('system.bios.manufacturer', bios_manufacturer)
12131232

12141233

12151234
def detect_virt(info_add):
12161235
if MS_WINDOWS:
1217-
virt = detect_virt_windows(info_add)
1218-
if virt:
1219-
return virt
1236+
return detect_virt_windows(info_add)
12201237

12211238
# Run systemd-detect-virt command
12221239
virt = run_command(["systemd-detect-virt"], check=False)

0 commit comments

Comments
 (0)