Skip to content

Commit

Permalink
Implement EFI host creation
Browse files Browse the repository at this point in the history
This implements EFI host creation by specifying the os_firmware
attribute to be efi. It's also possible to set the os_loader attribute
to secure (for UEFI secure boot) or stateless (for AMD SEV). Both secure
and stateless imply the os_firmware to be efi.
  • Loading branch information
ekohl committed Dec 21, 2023
1 parent 7d37d9f commit 9167c0d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/fog/libvirt/models/compute/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class Server < Fog::Compute::Server

attribute :cpus
attribute :cputime
attribute :os_firmware
attribute :os_type
attribute :os_loader
attribute :memory_size
attribute :max_memory_size
attribute :name
Expand Down Expand Up @@ -281,10 +283,28 @@ def to_xml
end

xml.vcpu(cpus)
xml.os do

os_tags = {}

# Secure boot and stateless UEFI both imply an EFI firmware
if ["secure", "stateless"].include?(os_loader)
os_tags[:firmware] = "efi"
elsif os_firmware
os_tags[:firmware] = os_firmware
end

xml.os(**os_tags) do
type = xml.type(os_type, :arch => arch)
type[:machine] = "q35" if ["i686", "x86_64"].include?(arch)

# TODO: can you use both secure and stateless at the same time?
case attributes[:os_loader]
when "secure"
xml.loader(:secure => "yes")
when "stateless"
xml.loader(:stateless => "yes")
end

boot_order.each do |dev|
xml.boot(:dev => dev)
end
Expand Down
24 changes: 24 additions & 0 deletions tests/libvirt/models/compute/server_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
attributes = [ :id,
:cpus,
:cputime,
:os_firmware,
:os_loader,
:os_type,
:memory_size,
:max_memory_size,
Expand Down Expand Up @@ -60,6 +62,7 @@
end
test('be a kind of Fog::Libvirt::Compute::Server') { server.kind_of? Fog::Libvirt::Compute::Server }
tests("serializes to xml") do
test("without firmware") { server.to_xml.match?(%r{<os>}) }

Check warning on line 65 in tests/libvirt/models/compute/server_tests.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Use `//` around regular expression. Raw Output: tests/libvirt/models/compute/server_tests.rb:65:55: C: Style/RegexpLiteral: Use `//` around regular expression.
test("with memory") { server.to_xml.match?(%r{<memory>\d+</memory>}) }
test("with disk of type file") do
xml = server.to_xml
Expand All @@ -78,6 +81,27 @@
xml.match?(/<disk type="block" device="disk">/) && xml.match?(%r{<source dev="/dev/sda"/>})
end
test("with q35 machine type on x86_64") { server.to_xml.match?(%r{<type arch="x86_64" machine="q35">hvm</type>}) }
test("with efi firmware") do
server = Fog::Libvirt::Compute::Server.new(
{
:os_firmware => 'efi',

Check warning on line 87 in tests/libvirt/models/compute/server_tests.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. Raw Output: tests/libvirt/models/compute/server_tests.rb:87:29: C: Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
:nics => [],
:volumes => [],

Check warning on line 89 in tests/libvirt/models/compute/server_tests.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Avoid comma after the last item of a hash. Raw Output: tests/libvirt/models/compute/server_tests.rb:89:27: C: Style/TrailingCommaInHashLiteral: Avoid comma after the last item of a hash.
}
)
server.to_xml.match?(%r{<os firmware="efi">})

Check warning on line 92 in tests/libvirt/models/compute/server_tests.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Use `//` around regular expression. Raw Output: tests/libvirt/models/compute/server_tests.rb:92:30: C: Style/RegexpLiteral: Use `//` around regular expression.
end
test("with secure boot") do
server = Fog::Libvirt::Compute::Server.new(
{
:os_loader => 'secure',

Check warning on line 97 in tests/libvirt/models/compute/server_tests.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. Raw Output: tests/libvirt/models/compute/server_tests.rb:97:27: C: Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
:nics => [],
:volumes => [],

Check warning on line 99 in tests/libvirt/models/compute/server_tests.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Avoid comma after the last item of a hash. Raw Output: tests/libvirt/models/compute/server_tests.rb:99:27: C: Style/TrailingCommaInHashLiteral: Avoid comma after the last item of a hash.
}
)
xml = server.to_xml
xml.include?('<os firmware="efi">') && xml.include?('<loader secure="yes"/>')
end
end
end
end

0 comments on commit 9167c0d

Please sign in to comment.