Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
schubi2 committed Nov 15, 2024
1 parent 96d5b8b commit 6fb8f2d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 88 deletions.
57 changes: 57 additions & 0 deletions src/lib/bootloader/bls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require "fileutils"
require "yast"
require "bootloader/sysconfig"
require "bootloader/cpu_mitigations"
require "cfa/grub2/default"

Yast.import "Report"

module Bootloader
# Represents bls compatile system calls which can be used
# e.g. by grub2-bls and systemd-boot
class Bls
include Yast::Logger
include Yast::I18n

SDBOOTUTIL = "/usr/bin/sdbootutil"

def self.create_menu_entries
Yast::Execute.on_target!(SDBOOTUTIL, "--verbose", "add-all-kernels")
rescue Cheetah::ExecutionFailed => e
Yast::Report.Error(
format(_(
"Cannot create boot menu entry:\n" \
"Command `%{command}`.\n" \
"Error output: %{stderr}"
), command: e.commands.inspect, stderr: e.stderr)
)
end

def self.install_bootloader
Yast::Execute.on_target!(SDBOOTUTIL, "--verbose",
"install")
rescue Cheetah::ExecutionFailed => e
Yast::Report.Error(
format(_(
"Cannot install bootloader:\n" \
"Command `%{command}`.\n" \
"Error output: %{stderr}"
), command: e.commands.inspect, stderr: e.stderr)
)
end

def self.write_menu_timeout(timeout)
Yast::Execute.on_target(SDBOOTUTIL, "set-timeout", timeout)
rescue Cheetah::ExecutionFailed => e
Yast::Report.Error(
format(_(
"Cannot write boot menu timeout:\n" \
"Command `%{command}`.\n" \
"Error output: %{stderr}"
), command: e.commands.inspect, stderr: e.stderr)
)
end
end
end
10 changes: 2 additions & 8 deletions src/lib/bootloader/bls_sections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "json"
require "yast"
require "yast2/execute"
require "bootloader/bls"

Yast.import "Misc"

Expand Down Expand Up @@ -42,7 +43,7 @@ def default=(value)
def write
return if @default.empty?

write_default
Bls.write_default_menu(@default)
end

def read
Expand All @@ -69,13 +70,6 @@ def read_default
grubenv_path)
end

# write default entry
def write_default
# Execute.on_target can return nil if call failed. It shows users error popup, but bootloader
# can continue with not selected default section.
Yast::Execute.on_target("/usr/bin/sdbootutil", "set-default", @default)
end

# @return [Array] return array of entries or []
def read_entries
output = Yast::Execute.on_target(
Expand Down
41 changes: 6 additions & 35 deletions src/lib/bootloader/grub2bls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "yast"
require "bootloader/bootloader_base"
require "bootloader/bls"
require "bootloader/bls_sections"

Yast.import "Arch"
Expand Down Expand Up @@ -85,11 +86,12 @@ def proposed?

# writes configuration to target disk
def write(*)
install_bootloader if Yast::Stage.initial # while new installation only (currently)
create_menu_entries
install_bootloader
Bls.install_bootloader if Yast::Stage.initial # while new installation only (currently)
Bls.create_menu_entries
Bls.install_bootloader
@sections.write
write_menu_timeout
Bls.write_menu_timeout(grub_default.timeout)

# writing kernel parameter to /etc/kernel/cmdline
File.open(File.join(Yast::Installation.destdir, CMDLINE), "w+") do |fw|
fw.puts(grub_default.kernel_params.serialize)
Expand Down Expand Up @@ -172,41 +174,10 @@ def read_menu_timeout
log.info "Boot timeout: #{grub_default.timeout}"
end

def write_menu_timeout
# Execute.on_target can return nil if call failed. It shows users error popup, but bootloader
# can continue with not selected default section.
Yast::Execute.on_target(SDBOOTUTIL, "set-timeout", grub_default.timeout)
end

def merge_sections(other)
return if !other.sections.default || other.sections.default.empty?

@sections.default = other.sections.default
end

def create_menu_entries
Yast::Execute.on_target!(SDBOOTUTIL, "--verbose", "add-all-kernels")
rescue Cheetah::ExecutionFailed => e
Yast::Report.Error(
format(_(
"Cannot create grub2-bls menu entry:\n" \
"Command `%{command}`.\n" \
"Error output: %{stderr}"
), command: e.commands.inspect, stderr: e.stderr)
)
end

def install_bootloader
Yast::Execute.on_target!(SDBOOTUTIL, "--verbose",
"install")
rescue Cheetah::ExecutionFailed => e
Yast::Report.Error(
format(_(
"Cannot install grub2-bls bootloader:\n" \
"Command `%{command}`.\n" \
"Error output: %{stderr}"
), command: e.commands.inspect, stderr: e.stderr)
)
end
end
end
50 changes: 5 additions & 45 deletions src/lib/bootloader/systemdboot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "yast"
require "bootloader/sysconfig"
require "bootloader/cpu_mitigations"
require "bootloader/bls"
require "cfa/systemd_boot"
require "cfa/grub2/default"

Expand Down Expand Up @@ -112,9 +113,10 @@ def read
def write(etc_only: false)
super
log.info("Writing settings...")
install_bootloader if Yast::Stage.initial # while new installation only (currently)
create_menu_entries
write_menu_timeout
Bls.install_bootloader if Yast::Stage.initial # while new installation only (currently)
write_kernel_parameter
Bls.create_menu_entries
Bls.write_menu_timeout

true
end
Expand Down Expand Up @@ -189,8 +191,6 @@ def write_sysconfig(prewrite: false)

private

SDBOOTUTIL = "/usr/bin/sdbootutil"

def write_kernel_parameter
# writing kernel parameter to /etc/kernel/cmdline
File.open(File.join(Yast::Installation.destdir, CMDLINE), "w+") do |fw|
Expand All @@ -202,22 +202,6 @@ def write_kernel_parameter
end
end

def create_menu_entries
write_kernel_parameter

begin
Yast::Execute.on_target!(SDBOOTUTIL, "--verbose", "add-all-kernels")
rescue Cheetah::ExecutionFailed => e
Yast::Report.Error(
format(_(
"Cannot create systemd-boot menu entry:\n" \
"Command `%{command}`.\n" \
"Error output: %{stderr}"
), command: e.commands.inspect, stderr: e.stderr)
)
end
end

def read_menu_timeout
config = CFA::SystemdBoot.load
return unless config.menu_timeout
Expand All @@ -228,29 +212,5 @@ def read_menu_timeout
config.menu_timeout.to_i
end
end

def write_menu_timeout
config = CFA::SystemdBoot.load
config.menu_timeout = if menu_timeout == -1
"menu-force"
else
menu_timeout.to_s
end
config.save
end

def install_bootloader
Yast::Execute.on_target!(SDBOOTUTIL, "--verbose",
"install")
rescue Cheetah::ExecutionFailed => e
Yast::Report.Error(
format(_(
"Cannot install systemd bootloader:\n" \
"Command `%{command}`.\n" \
"Error output: %{stderr}"
), command: e.commands.inspect, stderr: e.stderr)
)
nil
end
end
end

0 comments on commit 6fb8f2d

Please sign in to comment.