Skip to content

Commit ba56412

Browse files
committed
Detect QEMU and Yandex hypervisors to yield correct {is_,}virtual facts
On OpenBSD, the `hw.product` sysctl(2) is used to detect, i.e. detect, whether the system is virtual or "physical". Some hypervisors, however, provide the relevant bits via `hw.vendor`, so check that as fallback in case the product string does not match. Fix !61.
1 parent 0d02283 commit ba56412

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

lib/facter/resolvers/openbsd/virtual.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ class << self
1010
# :model
1111

1212
VM_GUEST_SYSCTL_NAMES = {
13+
'QEMU' => 'qemu',
1314
'VMM' => 'vmm',
14-
'vServer' => 'vserver',
15+
'Yandex' => 'yandex',
1516
'oracle' => 'virtualbox',
17+
'vServer' => 'vserver',
1618
'xen' => 'xenu',
1719
'none' => nil
1820
}.freeze
@@ -24,17 +26,23 @@ def post_resolve(fact_name, _options)
2426
end
2527

2628
CTL_HW = 6
29+
HW_VENDOR = 14
2730
HW_PRODUCT = 15
2831

2932
def read_facts(fact_name)
3033
require 'facter/resolvers/bsd/ffi/ffi_helper'
3134

3235
vm = Facter::Bsd::FfiHelper.sysctl(:string, [CTL_HW, HW_PRODUCT])
33-
vm = if VM_GUEST_SYSCTL_NAMES.key?(vm)
34-
VM_GUEST_SYSCTL_NAMES[vm]
35-
else
36-
'physical'
37-
end
36+
if VM_GUEST_SYSCTL_NAMES.key?(vm)
37+
vm = VM_GUEST_SYSCTL_NAMES[vm]
38+
else
39+
vm = Facter::Bsd::FfiHelper.sysctl(:string, [CTL_HW, HW_VENDOR])
40+
vm = if VM_GUEST_SYSCTL_NAMES.key?(vm)
41+
VM_GUEST_SYSCTL_NAMES[vm]
42+
else
43+
'physical'
44+
end
45+
end
3846
@fact_list[:vm] = vm
3947
@fact_list[fact_name]
4048
end

0 commit comments

Comments
 (0)