Skip to content

Commit

Permalink
host_inventory/linux/virtualization: Fallback to systemd-detect-virt
Browse files Browse the repository at this point in the history
In detecting virtualization, if system-product-name from DMI is
unknown to Specinfra, fallback to `systemd-detect-virt`, which has
better heuristics. It currently returns `{system: nil}` in such a
case.
  • Loading branch information
hanazuki committed Mar 6, 2024
1 parent 4c578ad commit 25a7e76
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/specinfra/host_inventory/virtualization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def get

cmd = backend.command.get(:get_inventory_system_product_name)
ret = backend.run_command(cmd)
if ret.exit_status == 0 and ret.stdout.length > 0
res[:system] = parse_system_product_name(ret.stdout)
if ret.success? and (parsed = parse_system_product_name(ret.stdout))
res[:system] = parsed
return res
end

Expand Down
16 changes: 16 additions & 0 deletions spec/host_inventory/linux/virtualization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@
expect(virt.get).to include(:system => 'openvz')
end

it 'Debian on QEMU KVM should return :system => "kvm"' do
allow(virt.backend).to receive(:run_command).with('grep -Eqa \'docker(/|-[0-9a-f]+)\' /proc/1/cgroup||test -e /.dockerinit') do
CommandResult.new(:stdout => '', :exit_status => 1)
end
allow(virt.backend).to receive(:run_command).with('test -d /proc/vz -a ! -d /proc/bc') do
CommandResult.new(:stdout => '', :exit_status => 1)
end
allow(virt.backend).to receive(:run_command).with('dmidecode -s system-product-name') do
CommandResult.new(:stdout => "Standard PC (Q35 + ICH9, 2009)\n", :exit_status => 0)
end
allow(virt.backend).to receive(:run_command).with('systemd-detect-virt') do
CommandResult.new(:stdout => "kvm\n", :exit_status => 0)
end
expect(virt.get).to include(:system => 'kvm')
end

let(:host_inventory) { nil }
it 'Debian Jessie on KVM should return :system => "kvm"' do
ret = virt.parse_system_product_name("KVM\n")
Expand Down

0 comments on commit 25a7e76

Please sign in to comment.