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

yast / yast-bootloader / 14854565708

06 May 2025 07:59AM UTC coverage: 87.379% (-0.04%) from 87.417%
14854565708

Pull #716

github

web-flow
Merge f1f4ee92d into d2595d11d
Pull Request #716: Unifying Bootloader Options for systemd-boot and grub2-bls

92 of 119 new or added lines in 10 files covered. (77.31%)

7 existing lines in 1 file now uncovered.

3434 of 3930 relevant lines covered (87.38%)

12.97 hits per line

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

93.44
/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

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

33
    attr_reader :sections
1✔
34

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

39
    def initialize
1✔
40
      super
62✔
41

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

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

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

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

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

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

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

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

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

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

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

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

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

112
    def read
1✔
113
      super
3✔
114

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

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

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

140
      true
5✔
141
    end
142

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

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

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

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

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

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

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

198
      res
1✔
199
    end
200

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

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

213
  private
1✔
214

215
    def write_kernel_parameter
1✔
216
      # writing kernel parameter to /etc/kernel/cmdline
217
      File.open(File.join(Yast::Installation.destdir, CMDLINE), "w+") do |fw|
5✔
218
        if Yast::Stage.initial # while new installation only
5✔
219
          fw.puts("root=#{Yast::BootStorage.root_partitions.first.name} #{kernel_params.serialize}")
5✔
220
        else # root entry is already available
UNCOV
221
          fw.puts(kernel_params.serialize)
×
222
        end
223
      end
224
    end
225
  end
226
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