Class: Bootloader::Grub2Base

Inherits:
BootloaderBase show all
Includes:
Yast::I18n, Yast::Logger
Defined in:
src/lib/bootloader/grub2base.rb

Overview

Common base for GRUB2 specialized classes rubocop:disable Metrics/ClassLength

Direct Known Subclasses

Grub2, Grub2EFI

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BootloaderBase

#prepare, #proposed?, #read?, #summary, #write_sysconfig

Constructor Details

#initializeGrub2Base

Returns a new instance of Grub2Base.


69
70
71
72
73
74
75
76
77
78
79
# File 'src/lib/bootloader/grub2base.rb', line 69

def initialize
  super

  textdomain "bootloader"
  @password = ::Bootloader::GRUB2Pwd.new
  @grub_default = ::CFA::Grub2::Default.new
  @sections = ::Bootloader::Sections.new
  @pmbr_action = :nothing
  @explicit_cpu_mitigations = false
  @update_nvram = true
end

Instance Attribute Details

#consoleObject

Returns the value of attribute console.


63
64
65
# File 'src/lib/bootloader/grub2base.rb', line 63

def console
  @console
end

#grub_defaultObject

Returns the value of attribute grub_default.


45
46
47
# File 'src/lib/bootloader/grub2base.rb', line 45

def grub_default
  @grub_default
end

#passwordObject

Returns the value of attribute password.


40
41
42
# File 'src/lib/bootloader/grub2base.rb', line 40

def password
  @password
end

#pmbr_actionObject

Returns the value of attribute pmbr_action.


47
48
49
# File 'src/lib/bootloader/grub2base.rb', line 47

def pmbr_action
  @pmbr_action
end

#sectionsObject (readonly)

Returns the value of attribute sections.


42
43
44
# File 'src/lib/bootloader/grub2base.rb', line 42

def sections
  @sections
end

#secure_bootBoolean

Returns current secure boot setting.

Returns:

  • (Boolean)

    current secure boot setting


55
56
57
# File 'src/lib/bootloader/grub2base.rb', line 55

def secure_boot
  @secure_boot
end

#stage1Object

Returns the value of attribute stage1.


67
68
69
# File 'src/lib/bootloader/grub2base.rb', line 67

def stage1
  @stage1
end

#trusted_bootBoolean

Returns current trusted boot setting.

Returns:

  • (Boolean)

    current trusted boot setting


51
52
53
# File 'src/lib/bootloader/grub2base.rb', line 51

def trusted_boot
  @trusted_boot
end

#update_nvramBoolean

Returns current update nvram setting.

Returns:

  • (Boolean)

    current update nvram setting


59
60
61
# File 'src/lib/bootloader/grub2base.rb', line 59

def update_nvram
  @update_nvram
end

Instance Method Details

#cpu_mitigationsObject


99
100
101
# File 'src/lib/bootloader/grub2base.rb', line 99

def cpu_mitigations
  CpuMitigations.from_kernel_params(grub_default.kernel_params)
end

#cpu_mitigations=(value) ⇒ Object


107
108
109
110
111
# File 'src/lib/bootloader/grub2base.rb', line 107

def cpu_mitigations=(value)
  log.info "setting mitigations to #{value}"
  @explicit_cpu_mitigations = true
  value.modify_kernel_params(grub_default.kernel_params)
end

#disable_serial_consoleObject


216
217
218
219
220
# File 'src/lib/bootloader/grub2base.rb', line 216

def disable_serial_console
  @console = nil
  grub_default.kernel_params.remove_parameter(serial_console_matcher)
  grub_default.serial_console = ""
end

#enable_serial_console(console_arg_string) ⇒ Object


205
206
207
208
209
210
211
212
213
214
# File 'src/lib/bootloader/grub2base.rb', line 205

def enable_serial_console(console_arg_string)
  @console = SerialConsole.load_from_console_args(console_arg_string)
  raise ::Bootloader::InvalidSerialConsoleArguments unless @console

  grub_default.serial_console = console.console_args

  placer = CFA::ReplacePlacer.new(serial_console_matcher)
  kernel_params = grub_default.kernel_params
  kernel_params.add_parameter("console", console.kernel_args, placer)
end

#explicit_cpu_mitigationsObject


103
104
105
# File 'src/lib/bootloader/grub2base.rb', line 103

def explicit_cpu_mitigations
  @explicit_cpu_mitigations ? cpu_mitigations : nil
end

#include_os_prober_package?Boolean

Checks if the os-prober package should be included.

This default implementation checks if os-prober is supported on the current architecture (all except s/390) and if the package is available (not all products include it).

Returns:

  • (Boolean)

    true if the os-prober package should be included; false otherwise.


201
202
203
# File 'src/lib/bootloader/grub2base.rb', line 201

def include_os_prober_package?
  OsProber.available?
end

#merge(other) ⇒ Object


175
176
177
178
179
180
181
182
183
184
185
186
# File 'src/lib/bootloader/grub2base.rb', line 175

def merge(other)
  super

  merge_grub_default(other)
  merge_password(other)
  merge_pmbr_action(other)
  merge_sections(other)

  self.trusted_boot = other.trusted_boot unless other.trusted_boot.nil?
  self.secure_boot = other.secure_boot unless other.secure_boot.nil?
  self.update_nvram = other.update_nvram unless other.update_nvram.nil?
end

#packagesObject


188
189
190
191
192
# File 'src/lib/bootloader/grub2base.rb', line 188

def packages
  res = super
  res << OsProber.package_name if include_os_prober_package?
  res
end

#pmbr_setup(*devices) ⇒ Object

set pmbr flags on boot disks TODO: move it to own place


85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'src/lib/bootloader/grub2base.rb', line 85

def pmbr_setup(*devices)
  return if @pmbr_action == :nothing

  action_parted = case @pmbr_action
  when :add    then "on"
  when :remove then "off"
  else raise "invalid action #{action}"
  end

  devices.each do |dev|
    Yast::Execute.locally("/usr/sbin/parted", "-s", dev, "disk_set", "pmbr_boot", action_parted)
  end
end

#proposeObject


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'src/lib/bootloader/grub2base.rb', line 148

def propose
  super

  propose_os_probing
  propose_terminal
  propose_timeout
  propose_encrypted

  if grub_default.kernel_params.empty?
    kernel_line = Yast::BootArch.DefaultKernelParams(propose_resume)
    grub_default.kernel_params.replace(kernel_line)
  end
  grub_default.gfxmode ||= "auto"
  grub_default.recovery_entry.disable unless grub_default.recovery_entry.defined?
  grub_default.distributor ||= ""
  grub_default.default = "saved"
  # always propose true as grub2 itself detect if btrfs used
  grub_default.generic_set("SUSE_BTRFS_SNAPSHOT_BOOTING", "true")

  propose_serial
  propose_xen_hypervisor

  self.trusted_boot = false
  self.secure_boot = Systeminfo.secure_boot_active?
  self.update_nvram = true
end

#readObject


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'src/lib/bootloader/grub2base.rb', line 113

def read
  super

  begin
    grub_default.load
  rescue Errno::ENOENT
    raise BrokenConfiguration, _("File /etc/default/grub missing on system")
  end

  grub_cfg = CFA::Grub2::GrubCfg.new
  begin
    grub_cfg.load
  rescue Errno::ENOENT
    # there may not need to be grub.cfg generated (bnc#976534),(bsc#1124064)
    log.info "/boot/grub2/grub.cfg is missing. Defaulting to empty one."
  end
  @sections = ::Bootloader::Sections.new(grub_cfg)
  log.info "grub sections: #{@sections.all}"

  self.trusted_boot = Systeminfo.trusted_boot_active?
  self.secure_boot = Systeminfo.secure_boot_active?
  self.update_nvram = Systeminfo.update_nvram_active?
end

#serial_console?Boolean

Returns:

  • (Boolean)

222
223
224
# File 'src/lib/bootloader/grub2base.rb', line 222

def serial_console?
  !console.nil?
end

#writeObject


137
138
139
140
141
142
143
144
145
146
# File 'src/lib/bootloader/grub2base.rb', line 137

def write
  super

  log.info "writing /etc/default/grub #{grub_default.inspect}"
  grub_default.save
  @sections.write
  @password.write
  Yast::Execute.on_target("/usr/sbin/grub2-mkconfig", "-o", "/boot/grub2/grub.cfg",
    env: systemwide_locale)
end