Skip to content

Commit

Permalink
Refactor Secure Boot Support and Firmware Handling
Browse files Browse the repository at this point in the history
- Renamed firmware-related attributes to align with VMware conventions.
  • Loading branch information
nofaralfasi committed Jul 28, 2024
1 parent 3e431ee commit d304ae6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
11 changes: 5 additions & 6 deletions lib/fog/libvirt/models/compute/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class Server < Fog::Compute::Server

attribute :cpus
attribute :cputime
attribute :os_firmware
attribute :os_firmware_features
attribute :firmware
attribute :firmware_features
attribute :os_type
attribute :memory_size
attribute :max_memory_size
Expand Down Expand Up @@ -291,8 +291,7 @@ def to_xml
xml.vcpu(cpus)
os_tags = {}

# Set firmware only if it's EFI, BIOS don't need to be set
os_tags[:firmware] = "efi" if os_firmware == "efi"
os_tags[:firmware] = firmware

xml.os(**os_tags) do
type = xml.type(os_type, :arch => arch)
Expand All @@ -302,9 +301,9 @@ def to_xml
xml.boot(:dev => dev)
end

if os_firmware == "efi"
if firmware == "efi" && firmware_features&.any?
xml.firmware do
os_firmware_features.each_pair do |key, value|
firmware_features.each_pair do |key, value|
xml.feature(:name => key, :enabled => value)
end
end
Expand Down
15 changes: 14 additions & 1 deletion lib/fog/libvirt/requests/compute/list_domains.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ def boot_order xml
xml_elements(xml, "domain/os/boot", "dev")
end

def firmware(xml)
xml_elements(xml, "domain/os", "firmware").first || 'bios'
end

def firmware_features(xml)
features = xml_elements(xml, "domain/os/firmware/feature").map do |feature|
[feature[:name], feature[:enabled]]
end
features.to_h
end

def domain_interfaces xml
ifs = xml_elements(xml, "domain/devices/interface")
ifs.map { |i|
Expand Down Expand Up @@ -76,9 +87,11 @@ def domain_to_attributes(dom)
:active => dom.active?,
:display => domain_display(dom.xml_desc),
:boot_order => boot_order(dom.xml_desc),
:firmware => firmware(dom.xml_desc),
:nics => domain_interfaces(dom.xml_desc),
:volumes_path => domain_volumes(dom.xml_desc),
:state => states[dom.info.state]
:state => states[dom.info.state],
:firmware_features => firmware_features(dom.xml_desc)
}
rescue ::Libvirt::RetrieveError, ::Libvirt::Error
# Catch libvirt exceptions to avoid race conditions involving
Expand Down
18 changes: 8 additions & 10 deletions tests/libvirt/models/compute/server_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
attributes = [ :id,
:cpus,
:cputime,
:os_firmware,
:os_firmware_features,
:firmware,
:firmware_features,
:os_type,
:memory_size,
:max_memory_size,
Expand Down Expand Up @@ -92,10 +92,9 @@
test("with efi firmware") do
server = Fog::Libvirt::Compute::Server.new(
{
:os_firmware => "efi",
:os_firmware_features => {
:firmware => "efi",
:firmware_features => {
"secure-boot" => "no",

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 🐶 Avoid comma after the last item of a hash. Raw Output: tests/libvirt/models/compute/server_tests.rb:97:34: C: Style/TrailingCommaInHashLiteral: Avoid comma after the last item of a hash.
"enrolled-keys" => "no"
},
:nics => [],
:volumes => []
Expand All @@ -104,16 +103,15 @@
xml = server.to_xml

os_firmware = xml.include?('<os firmware="efi">')
secure_boot = !xml.include?('<feature name="secure-boot" enabled="no" />')
enrolled_keys = !xml.include?('<feature name="enrolled-keys" enabled="no" />')
secure_boot = xml.include?('<feature name="secure-boot" enabled="no"/>')

os_firmware && secure_boot && enrolled_keys
os_firmware && secure_boot
end
test("with secure boot") do
server = Fog::Libvirt::Compute::Server.new(
{
:os_firmware => "efi",
:os_firmware_features => {
:firmware => "efi",
:firmware_features => {
"secure-boot" => "yes",
"enrolled-keys" => "yes"
},
Expand Down

0 comments on commit d304ae6

Please sign in to comment.