Skip to content

Commit

Permalink
adjust functions and update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wfeldt committed Feb 21, 2020
1 parent 637ed78 commit 069a2af
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/lib/bootloader/grub2.rb
Expand Up @@ -113,8 +113,8 @@ def summary(simple_mode: false)
)
]

result.push secure_boot_summary if Systeminfo.secure_boot_available?
result.push trusted_boot_summary if Systeminfo.trusted_boot_available?
result.push secure_boot_summary if Systeminfo.secure_boot_available?(name)
result.push trusted_boot_summary if Systeminfo.trusted_boot_available?(name)

locations_val = locations
if !locations_val.empty?
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bootloader/grub2_widgets.rb
Expand Up @@ -980,11 +980,11 @@ def generic_mbr_widget?
end

def secure_boot_widget?
Systeminfo.secure_boot_available?
Systeminfo.secure_boot_available?(grub2.name)
end

def trusted_boot_widget?
Systeminfo.trusted_boot_available?
Systeminfo.trusted_boot_available?(grub2.name)
end

def pmbr_widget?
Expand Down
6 changes: 4 additions & 2 deletions src/lib/bootloader/grub_install.rb
Expand Up @@ -15,6 +15,8 @@ class GrubInstall

def initialize(efi: false)
@efi = efi
@grub2_name = "grub2"
@grub2_name += "-efi" if @efi
textdomain "bootloader"
end

Expand All @@ -26,7 +28,7 @@ def initialize(efi: false)
# @param trusted_boot [Boolean] if trusted boot variant should be used
# @return [Array<String>] list of devices for which install failed
def execute(devices: [], secure_boot: false, trusted_boot: false)
if secure_boot && !Systeminfo.secure_boot_available?
if secure_boot && !Systeminfo.secure_boot_available?(@grub2_name)
raise "cannot enable secure boot on this machine"
end

Expand Down Expand Up @@ -74,7 +76,7 @@ def report_failure(exception)
# creates basic command for grub2 install without specifying any stage1
# locations
def basic_cmd(secure_boot, trusted_boot)
if Systeminfo.shim_needed?
if Systeminfo.shim_needed?(@grub2_name, secure_boot)
cmd = ["/usr/sbin/shim-install", "--config-file=/boot/grub2/grub.cfg"]
else
cmd = ["/usr/sbin/grub2-install", "--target=#{target}"]
Expand Down
23 changes: 9 additions & 14 deletions src/lib/bootloader/systeminfo.rb
Expand Up @@ -16,36 +16,31 @@ def secure_boot_active?
efi_supported? || s390_secure_boot_active?
end

# true if boot config uses secure boot
def secure_boot_used?
::Bootloader::BootloaderFactory.current.secure_boot
end

# true if secure boot is (in principle) supported
def secure_boot_supported?
efi_supported? || s390_secure_boot_supported?
end

# true if secure boot setting is available for current boot config
def secure_boot_available?
efi_used? || s390_secure_boot_supported?
def secure_boot_available?(bootloader_name)
efi_used?(bootloader_name) || s390_secure_boot_supported?
end

# true if trusted boot setting is available for current boot config
def trusted_boot_available?
def trusted_boot_available?(bootloader_name)
# for details about grub2 efi trusted boot support see FATE#315831
(
::Bootloader::BootloaderFactory.current.name == "grub2" &&
bootloader_name == "grub2" &&
(Yast::Arch.x86_64 || Yast::Arch.i386)
) || (
::Bootloader::BootloaderFactory.current.name == "grub2-efi" &&
bootloader_name == "grub2-efi" &&
File.exist?("/dev/tpm0")
)
end

# true if UEFI will be used for booting
def efi_used?
::Bootloader::BootloaderFactory.current.name == "grub2-efi"
def efi_used?(bootloader_name)
bootloader_name == "grub2-efi"
end

# true if system can (in principle) boot via UEFI
Expand All @@ -54,8 +49,8 @@ def efi_supported?
end

# true if shim has to be used
def shim_needed?
(Yast::Arch.x86_64 || Yast::Arch.i386) && secure_boot_used? && efi_used?
def shim_needed?(bootloader_name, secure_boot)
(Yast::Arch.x86_64 || Yast::Arch.i386) && secure_boot && efi_used?(bootloader_name)
end

# true if s390 machine has secure boot support
Expand Down
2 changes: 1 addition & 1 deletion test/grub2_test.rb
Expand Up @@ -81,7 +81,7 @@

grub2_install = double(Bootloader::GrubInstall)
expect(grub2_install).to receive(:execute)
.with(devices: ["/dev/sda", "/dev/sdb1"], trusted_boot: false).and_return([])
.with(devices: ["/dev/sda", "/dev/sdb1"], secure_boot: nil, trusted_boot: false).and_return([])
expect(Bootloader::GrubInstall).to receive(:new).with(efi: false).and_return(grub2_install)

subject.trusted_boot = false
Expand Down
1 change: 1 addition & 0 deletions test/grub_install_test.rb
Expand Up @@ -132,6 +132,7 @@ def expect_grub2_install(target, device: nil, removable: false)
subject { Bootloader::GrubInstall.new(efi: false) }

it "raise exception if secure_boot: true passed" do
stub_arch("x86_64")
expect { subject.execute(secure_boot: true) }.to raise_error(RuntimeError)
end

Expand Down

0 comments on commit 069a2af

Please sign in to comment.