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.
- Accurately report firmware type and Secure Boot status to Foreman.
  • Loading branch information
nofaralfasi committed Jul 25, 2024
1 parent a4fb77c commit 6422a10
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
10 changes: 5 additions & 5 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 @@ -286,7 +286,7 @@ def to_xml
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] = "efi" if firmware == "efi"

xml.os(**os_tags) do
type = xml.type(os_type, :arch => arch)
Expand All @@ -296,9 +296,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
17 changes: 17 additions & 0 deletions lib/fog/libvirt/requests/compute/list_domains.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ def boot_order xml
xml_elements(xml, "domain/os/boot", "dev")
end

# Foreman expects the firmware to be 'uefi_sb' if SB is enabled
def firmware(xml)
firmware_type = xml_elements(xml, "domain/os", "firmware").first || 'bios'
return 'uefi_sb' if firmware_type == 'efi' && secure_boot_enabled?(xml)

firmware_type
end

def secure_boot_enabled?(xml)

Check warning on line 59 in lib/fog/libvirt/requests/compute/list_domains.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Align `.map` with `xml_elements(xml, "domain/os/firmware/feature[@enabled='yes']")` on line 58. Raw Output: lib/fog/libvirt/requests/compute/list_domains.rb:59:32: C: Layout/MultilineMethodCallIndentation: Align `.map` with `xml_elements(xml, "domain/os/firmware/feature[@enabled='yes']")` on line 58.
enabled_features = xml_elements(xml, "domain/os/firmware/feature[@enabled='yes']")
.map { |feature| feature[:name] }

required_features = ['secure-boot', 'enrolled-keys']
required_features.all? { |feature| enabled_features.include?(feature) }
end

def domain_interfaces xml
ifs = xml_elements(xml, "domain/devices/interface")
ifs.map { |i|
Expand Down Expand Up @@ -78,6 +94,7 @@ 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]
Expand Down
14 changes: 6 additions & 8 deletions tests/libvirt/models/compute/server_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
attributes = [ :id,
:cpus,
:cputime,
:os_firmware,
:os_firmware_features,
:firmware,
:firmware_features,
:os_type,
:memory_size,
:max_memory_size,
Expand Down Expand Up @@ -85,10 +85,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",
"enrolled-keys" => "no"
},
:nics => [],
:volumes => []
Expand All @@ -97,10 +96,9 @@
xml = server.to_xml

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.
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(
Expand Down

0 comments on commit 6422a10

Please sign in to comment.