• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

yast / yast-bootloader / 14857311768

06 May 2025 10:23AM UTC coverage: 87.382% (-0.04%) from 87.417%
14857311768

Pull #716

github

schubi2
added alias for Agama
Pull Request #716: Unifying Bootloader Options for systemd-boot and grub2-bls

93 of 120 new or added lines in 10 files covered. (77.5%)

6 existing lines in 1 file now uncovered.

3435 of 3931 relevant lines covered (87.38%)

12.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.5
/src/lib/bootloader/systemdboot.rb
1
# frozen_string_literal: true
2

3
require "fileutils"
1✔
4
require "yast"
1✔
5
require "bootloader/sysconfig"
1✔
6
require "bootloader/cpu_mitigations"
1✔
7
require "bootloader/bls"
1✔
8
require "bootloader/bls_sections"
1✔
9
require "cfa/grub2/default"
1✔
10

11
Yast.import "Report"
1✔
12
Yast.import "Arch"
1✔
13
Yast.import "ProductFeatures"
1✔
14
Yast.import "BootStorage"
1✔
15
Yast.import "Stage"
1✔
16

17
module Bootloader
1✔
18
  # Represents systemd bootloader with efi target
19
  class SystemdBoot < BootloaderBase
1✔
20
    include Yast::Logger
1✔
21
    include Yast::I18n
1✔
22

23
    CMDLINE = "/etc/kernel/cmdline"
1✔
24

25
    # @!attribute timeout
26
    #   @return [Integer] menu timeout
27
    attr_accessor :timeout
1✔
28
    alias_method :menu_timeout, :timeout # Agama compatible
1✔
29

30
    # @!attribute secure_boot
31
    #   @return [Boolean] current secure boot setting
32
    attr_accessor :secure_boot
1✔
33

34
    attr_reader :sections
1✔
35

36
    # @!attribute pmbr_action
37
    #   @return [:remove, :add, :nothing]
38
    attr_accessor :pmbr_action
1✔
39

40
    def initialize
1✔
41
      super
62✔
42

43
      textdomain "bootloader"
62✔
44
      # For kernel parameters we are using the same data structure
45
      # like grub2 in order to be compatible with all calls.
46
      @kernel_container = ::CFA::Grub2::Default.new
62✔
47
      @explicit_cpu_mitigations = false
62✔
48
      @pmbr_action = :nothing
62✔
49
      @sections = ::Bootloader::BlsSections.new
62✔
50
    end
51

52
    def kernel_params
1✔
53
      @kernel_container.kernel_params
33✔
54
    end
55

56
    # rubocop:disable Metrics/AbcSize
57
    def merge(other)
1✔
58
      log.info "merging: timeout: #{timeout}=>#{other.timeout}"
1✔
59
      log.info "         secure_boot: #{secure_boot}=>#{other.secure_boot}"
1✔
60
      log.info "         mitigations: #{cpu_mitigations.to_human_string}=>" \
1✔
61
               "#{other.cpu_mitigations.to_human_string}"
62
      log.info "         pmbr_action: #{pmbr_action}=>#{other.pmbr_action}"
1✔
63
      log.info "         kernel_params: #{kernel_params.serialize}=>" \
1✔
64
               "#{other.kernel_params.serialize}"
65
      log.info "         default menu: #{@sections.default}=>" \
1✔
66
               "#{other.sections.default}"
67
      super
1✔
68
      self.timeout = other.timeout unless other.timeout.nil?
1✔
69
      self.secure_boot = other.secure_boot unless other.secure_boot.nil?
1✔
70
      self.pmbr_action = other.pmbr_action if other.pmbr_action
1✔
71

72
      kernel_serialize = kernel_params.serialize
1✔
73
      # handle specially noresume as it should lead to remove all other resume
74
      kernel_serialize.gsub!(/resume=\S+/, "") if other.kernel_params.parameter("noresume")
1✔
75

76
      # prevent double cpu_mitigations params
77
      kernel_serialize.gsub!(/mitigations=\S+/, "") if other.kernel_params.parameter("mitigations")
1✔
78

79
      new_kernel_params = "#{kernel_serialize} #{other.kernel_params.serialize}"
1✔
80
      # deduplicate identicatel parameter. Keep always the last one ( so reverse is needed ).
81
      new_params = new_kernel_params.split.reverse.uniq.reverse.join(" ")
1✔
82

83
      @kernel_container.kernel_params.replace(new_params)
1✔
84

85
      # explicitly set mitigations means overwrite of our
86
      self.cpu_mitigations = other.cpu_mitigations if other.explicit_cpu_mitigations
1✔
87

88
      @sections.default = other.sections.default if other.sections.default
1✔
89

90
      log.info "merging result: timeout: #{timeout}"
1✔
91
      log.info "                secure_boot: #{secure_boot}"
1✔
92
      log.info "                mitigations: #{cpu_mitigations.to_human_string}"
1✔
93
      log.info "                kernel_params: #{kernel_params.serialize}"
1✔
94
      log.info "                pmbr_action: #{pmbr_action}"
1✔
95
      log.info "                default menu: #{@sections.default}"
1✔
96
    end
97
    # rubocop:enable Metrics/AbcSize
98

99
    def cpu_mitigations
1✔
100
      CpuMitigations.from_kernel_params(kernel_params)
6✔
101
    end
102

103
    def explicit_cpu_mitigations
1✔
104
      @explicit_cpu_mitigations ? cpu_mitigations : nil
1✔
105
    end
106

107
    def cpu_mitigations=(value)
1✔
UNCOV
108
      log.info "set mitigations to #{value.to_human_string}"
×
109
      @explicit_cpu_mitigations = true
×
110
      value.modify_kernel_params(kernel_params)
×
111
    end
112

113
    def read
1✔
114
      super
3✔
115

116
      @sections.read
3✔
117
      self.timeout = Bls.menu_timeout
3✔
118
      self.secure_boot = Systeminfo.secure_boot_active?
3✔
119

120
      lines = ""
3✔
121
      filename = File.join(Yast::Installation.destdir, CMDLINE)
3✔
122
      if File.exist?(filename)
3✔
123
        File.open(filename).each do |line|
3✔
124
          lines = + line
3✔
125
        end
126
      end
127
      @kernel_container.kernel_params.replace(lines)
3✔
128
    end
129

130
    # Write bootloader settings to disk
131
    def write(etc_only: false)
1✔
132
      super
5✔
133
      log.info("Writing settings...")
5✔
134
      Bls.install_bootloader if Yast::Stage.initial # while new installation only (currently)
5✔
135
      write_kernel_parameter
5✔
136
      Bls.create_menu_entries
5✔
137
      Bls.write_menu_timeout(timeout)
5✔
138
      @sections.write
5✔
139
      Pmbr.write_efi(pmbr_action)
5✔
140

141
      true
5✔
142
    end
143

144
    def propose
1✔
145
      super
3✔
146
      log.info("Propose settings...")
3✔
147
      if @kernel_container.kernel_params.empty?
3✔
148
        kernel_line = Yast::BootArch.DefaultKernelParams(Yast::BootStorage.propose_resume)
3✔
149
        @kernel_container.kernel_params.replace(kernel_line)
3✔
150
      end
151
      self.timeout = Yast::ProductFeatures.GetIntegerFeature("globals", "boot_timeout").to_i
3✔
152
      self.secure_boot = Systeminfo.secure_boot_supported?
3✔
153
      # for UEFI always remove PMBR flag on disk (bnc#872054)
154
      self.pmbr_action = :remove
3✔
155
    end
156

157
    # Secure boot setting shown in summary screen.
158
    # sdbootutil intialize secure boot if shim has been installed.
159
    #
160
    # @return [String]
161
    def secure_boot_summary
1✔
162
      link = if secure_boot
2✔
UNCOV
163
        "<a href=\"disable_secure_boot\">(#{_("disable")})</a>"
×
164
      else
165
        "<a href=\"enable_secure_boot\">(#{_("enable")})</a>"
2✔
166
      end
167

168
      "#{_("Secure Boot:")} #{status_string(secure_boot)} #{link}"
2✔
169
    end
170

171
    # Display bootloader summary
172
    # @return a list of summary lines
173
    def summary(*)
1✔
174
      result = [
175
        Yast::Builtins.sformat(
2✔
176
          _("Boot Loader Type: %1"),
177
          "Systemd Boot"
178
        )
179
      ]
180
      result << secure_boot_summary if Systeminfo.secure_boot_available?(name)
2✔
181
      result
2✔
182
    end
183

184
    def name
1✔
185
      "systemd-boot"
12✔
186
    end
187

188
    def packages
1✔
189
      res = super
1✔
190
      res << "sdbootutil" << "systemd-boot"
1✔
191

192
      case Yast::Arch.architecture
1✔
193
      when "x86_64"
194
        res << "shim" if secure_boot
1✔
195
      else
UNCOV
196
        log.warn "Unknown architecture #{Yast::Arch.architecture} for systemdboot"
×
197
      end
198

199
      res
1✔
200
    end
201

202
    def delete
1✔
203
      log.warn("is currently not supported")
16✔
204
    end
205

206
    # overwrite BootloaderBase version to save secure boot
207
    def write_sysconfig(prewrite: false)
1✔
UNCOV
208
      sysconfig = Bootloader::Sysconfig.new(bootloader: name,
×
209
        secure_boot: secure_boot, trusted_boot: false,
210
        update_nvram: false)
UNCOV
211
      prewrite ? sysconfig.pre_write : sysconfig.write
×
212
    end
213

214
  private
1✔
215

216
    def write_kernel_parameter
1✔
217
      # writing kernel parameter to /etc/kernel/cmdline
218
      File.open(File.join(Yast::Installation.destdir, CMDLINE), "w+") do |fw|
5✔
219
        if Yast::Stage.initial # while new installation only
5✔
220
          fw.puts("root=#{Yast::BootStorage.root_partitions.first.name} #{kernel_params.serialize}")
5✔
221
        else # root entry is already available
UNCOV
222
          fw.puts(kernel_params.serialize)
×
223
        end
224
      end
225
    end
226
  end
227
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc