Class: Yast::BootloaderClass

Inherits:
Module
  • Object
show all
Includes:
Logger
Defined in:
src/modules/Bootloader.rb

Constant Summary collapse

BOOLEAN_MAPPING =
{ true => :present, false => :missing }.freeze
FLAVOR_KERNEL_LINE_MAP =
{
  :common    => "append",
  :xen_guest => "xen_append",
  :xen_host  => "xen_kernel_append"
}.freeze

Instance Method Summary collapse

Instance Method Details

#ExportObject

Export bootloader settings to a map

Returns:

  • bootloader settings



73
74
75
76
77
78
79
80
81
# File 'src/modules/Bootloader.rb', line 73

def Export
  config = ::Bootloader::BootloaderFactory.current
  config.read if !config.read? && !config.proposed?
  result = ::Bootloader::AutoyastConverter.export(config)

  log.info "autoyast map for bootloader: #{result.inspect}"

  result
end

#getDefaultSectionString

return default section label

Returns:

  • (String)

    default section label



299
300
301
302
303
304
305
306
# File 'src/modules/Bootloader.rb', line 299

def getDefaultSection
  ReadOrProposeIfNeeded()

  bootloader = Bootloader::BootloaderFactory.current
  return "" unless bootloader.respond_to?(:sections)

  bootloader.sections.default
end

#getLoaderTypeString

Get currently used bootloader, detect if not set yet

Returns:

  • (String)

    botloader type



441
442
443
# File 'src/modules/Bootloader.rb', line 441

def getLoaderType
  ::Bootloader::BootloaderFactory.current.name
end

#Import(data) ⇒ Boolean

Import settings from a map

Parameters:

  • data (Hash)

    map of bootloader settings

Returns:

  • (Boolean)

    true on success



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'src/modules/Bootloader.rb', line 86

def Import(data)
  factory = ::Bootloader::BootloaderFactory
  bootloader_section = ::Bootloader::AutoinstProfile::BootloaderSection.new_from_hashes(data)

  imported_configuration = import_bootloader(bootloader_section)
  return false if imported_configuration.nil?

  factory.clear_cache

  proposed_configuration = factory.bootloader_by_name(imported_configuration.name)
  unless Mode.config # no AutoYaST configuration mode
    proposed_configuration.propose
    proposed_configuration.merge(imported_configuration)
  end
  factory.current = proposed_configuration

  # mark that it is not clear proposal (bsc#1081967)
  Yast::Bootloader.proposed_cfg_changed = true

  true
end

#kernel_param(flavor, key) ⇒ String, ...

Gets value for given parameter in kernel parameters for given flavor.

Examples:

get crashkernel parameter to common kernel

Bootloader.kernel_param(:common, "crashkernel")
=> "256M@64B"

get cio_ignore parameter for xen_host kernel when missing

Bootloader.kernel_param(:xen_host, "cio_ignore")
=> :missing

get verbose parameter for xen_guest which is there

Bootloader.kernel_param(:xen_guest, "verbose")
=> :present

Parameters:

  • flavor (Symbol)

    flavor of kernel, for possible values see #modify_kernel_param

  • key (String)

    of parameter on kernel command line

Returns:

  • (String, :missing, :present)

    Returns string for parameters with value, :missing if key is not there and :present for parameters without value.



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'src/modules/Bootloader.rb', line 333

def kernel_param(flavor, key)
  if flavor == :recovery
    log.warn "Using deprecated recovery flavor"
    return :missing
  end

  ReadOrProposeIfNeeded() # ensure we have some data

  current_bl = ::Bootloader::BootloaderFactory.current
  # currently only grub2 bootloader supported
  return :missing unless current_bl.respond_to?(:grub_default)

  grub_default = current_bl.grub_default
  params = case flavor
  when :common then grub_default.kernel_params
  when :xen_guest then grub_default.xen_kernel_params
  when :xen_host then grub_default.xen_hypervisor_params
  else raise ArgumentError, "Unknown flavor #{flavor}"
  end

  res = params.parameter(key)

  BOOLEAN_MAPPING[res] || res
end

#mainObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'src/modules/Bootloader.rb', line 45

def main
  textdomain "bootloader"

  # installation proposal help variables

  # Configuration was changed during inst. proposal if true
  @proposed_cfg_changed = false

  # old vga value handling function

  # old value of vga parameter of default bootloader section
  @old_vga = nil

  # general functions

  @test_abort = nil
end

#modify_kernel_params(*args) ⇒ Boolean

Modify kernel parameters for installed kernels according to values

Examples:

add crashkernel parameter to common kernel and xen guest

Bootloader.modify_kernel_params(:common, :xen_guest, "crashkernel" => "256M@64M")

same as before just with array passing

targets = [:common, :xen_guest]
Bootloader.modify_kernel_params(targets, "crashkernel" => "256M@64M")

remove cio_ignore parameter for common kernel only

Bootloader.modify_kernel_params("cio_ignore" => :missing)

add cio_ignore parameter for xen host kernel

Bootloader.modify_kernel_params(:xen_host, "cio_ignore" => :present)

Parameters:

  • args (Array)

    parameters to modify. Last parameter is hash with keys and its values, keys are strings and values are :present, :missing or string value. Other parameters specify which kernel flavors are affected. Known values are:

    • :common for non-specific flavor
    • :recovery DEPRECATED: no longer use
    • :xen_guest for xen guest kernels
    • :xen_host for xen host kernels

Returns:

  • (Boolean)

    true if params were modified; false otherwise.

Raises:

  • (ArgumentError)


382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'src/modules/Bootloader.rb', line 382

def modify_kernel_params(*args)
  ReadOrProposeIfNeeded() # ensure we have data to modify
  current_bl = ::Bootloader::BootloaderFactory.current
  # currently only grub2 bootloader supported
  return :missing unless current_bl.respond_to?(:grub_default)

  grub_default = current_bl.grub_default

  values = args.pop
  raise ArgumentError, "Missing parameters to modify #{args.inspect}" if !values.is_a? Hash

  args = [:common] if args.empty? # by default change common kernels only
  args = args.first if args.first.is_a? Array # support array like syntax

  if args.include?(:recovery)
    args.delete(:recovery)
    log.warn "recovery flavor is deprecated and not set"
  end

  remap_values = BOOLEAN_MAPPING.invert
  values.each_key do |key|
    values[key] = remap_values[values[key]] if remap_values.key?(values[key])
  end

  params = args.map do |flavor|
    case flavor
    when :common then grub_default.kernel_params
    when :xen_guest then grub_default.xen_kernel_params
    when :xen_host then grub_default.xen_hypervisor_params
    else raise ArgumentError, "Unknown flavor #{flavor}"
    end
  end

  changed = false
  values.each do |key, value|
    params.each do |param|
      old_val = param.parameter(key)
      next if old_val == value

      changed = true
      # at first clean old entries
      matcher = CFA::Matcher.new(key: key)
      param.remove_parameter(matcher)

      case value
      when false then next # already done
      when Array
        value.each { |val| param.add_parameter(key, val) }
      else
        param.add_parameter(key, value)
      end
    end
  end

  changed
end

#ProposeObject

Propose bootloader settings



214
215
216
217
218
219
220
221
# File 'src/modules/Bootloader.rb', line 214

def Propose
  log.info "Proposing configuration"
  ::Bootloader::BootloaderFactory.current.propose

  log.info "Proposed settings: #{Export()}"

  nil
end

#ReadBoolean

Read settings from disk

Returns:

  • (Boolean)

    true on success



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'src/modules/Bootloader.rb', line 110

def Read
  log.info "Reading configuration"
  # run Progress bar
  stages = [
    # progress stage, text in dialog (short, infinitiv)
    _("Check boot loader"),
    # progress stage, text in dialog (short, infinitiv)
    _("Load boot loader settings")
  ]
  titles = [
    # progress step, text in dialog (short)
    _("Checking boot loader..."),
    # progress step, text in dialog (short)
    _("Reading partitioning..."),
    # progress step, text in dialog (short)
    _("Loading boot loader settings...")
  ]
  # dialog header
  Progress.New(
    _("Initializing Boot Loader Configuration"),
    " ",
    3,
    stages,
    titles,
    ""
  )

  Progress.NextStage
  return false if testAbort

  Progress.NextStage
  return false if testAbort

  begin
    ::Bootloader::BootloaderFactory.current.read
  rescue ::Bootloader::UnsupportedBootloader => e
    ret = Yast::Report.AnyQuestion(_("Unsupported Bootloader"),
      _("Unsupported bootloader '%s' detected. Use proposal of supported configuration instead?") %
        e.bootloader_name,
      _("Use"),
      _("Quit"),
      :yes) # focus proposing new one
    return false unless ret

    ::Bootloader::BootloaderFactory.current = ::Bootloader::BootloaderFactory.proposed
    ::Bootloader::BootloaderFactory.current.propose
  rescue ::Bootloader::BrokenConfiguration, ::Bootloader::UnsupportedOption => e
    msg = if e.is_a?(::Bootloader::BrokenConfiguration)
      # TRANSLATORS: %s stands for readon why yast cannot process it
      _("YaST cannot process current bootloader configuration (%s). " \
        "Propose new configuration from scratch?") % e.reason
    else
      e.message
    end

    ret = Yast::Report.AnyQuestion(_("Unsupported Configuration"),
      # TRANSLATORS: %s stands for readon why yast cannot process it
      msg,
      _("Propose"),
      _("Quit"),
      :yes) # focus proposing new one
    return false unless ret

    ::Bootloader::BootloaderFactory.current = ::Bootloader::BootloaderFactory.proposed
    ::Bootloader::BootloaderFactory.current.propose
  rescue Errno::EACCES
    # If the access to any needed file (e.g., grub.cfg when using GRUB bootloader) is not
    # allowed, just abort the execution. Using Yast::Confirm.MustBeRoot early in the
    # wizard/client is not enough since it allows continue.

    Yast2::Popup.show(
      # TRANSLATORS: pop-up message, beware the line breaks
      _("The module is running without enough privileges to perform all possible actions.\n\n" \
        "Cannot continue. Please, try again as root."),
      headline: :error
    )

    return false
  end

  Progress.Finish

  true
end

#ReadOrProposeIfNeededObject

Check whether settings were read or proposed, if not, decide what to do and read or propose settings



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'src/modules/Bootloader.rb', line 447

def ReadOrProposeIfNeeded
  current_bl = ::Bootloader::BootloaderFactory.current
  return if current_bl.read? || current_bl.proposed?

  if Mode.config || (Stage.initial && !Mode.update)
    Propose()
  else
    progress_orig = Progress.set(false)
    if Stage.initial && Mode.update
      # SCR has been currently set to inst-sys. So we have
      # set the SCR to installed system in order to read
      # grub settings
      old_SCR = WFM.SCRGetDefault
      new_SCR = WFM.SCROpen("chroot=#{Yast::Installation.destdir}:scr",
        false)
      WFM.SCRSetDefault(new_SCR)
    end
    Read()
    if Stage.initial && Mode.update
      # settings have been read from the target system
      current_bl.read
      # reset target system to inst-sys
      WFM.SCRSetDefault(old_SCR)
      WFM.SCRClose(new_SCR)
    end
    Progress.set(progress_orig)
  end
end

#ResetObject

Reset bootloader settings



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'src/modules/Bootloader.rb', line 196

def Reset
  return if Mode.autoinst

  log.info "Resetting configuration"

  ::Bootloader::BootloaderFactory.clear_cache
  if Stage.initial
    config = ::Bootloader::BootloaderFactory.proposed
    config.propose
  else
    config = ::Bootloader::BootloaderFactory.system
    config.read
  end
  ::Bootloader::BootloaderFactory.current = config
  nil
end

#Summary(simple_mode: false) ⇒ Object

Display bootloader summary

Returns:

  • a list of summary lines



225
226
227
228
229
230
231
232
233
# File 'src/modules/Bootloader.rb', line 225

def Summary(simple_mode: false)
  # kokso: additional warning that root partition is nfs type -> bootloader will not be installed
  if BootStorage.boot_filesystem.is?(:nfs)
    log.info "Bootloader::Summary() -> Boot partition is nfs type, bootloader will not be installed."
    return [_("The boot partition is of type NFS. Bootloader cannot be installed.")]
  end

  ::Bootloader::BootloaderFactory.current.summary(simple_mode: simple_mode)
end

#testAbortBoolean

Check whether abort was pressed

Returns:

  • (Boolean)

    true if abort was pressed



65
66
67
68
69
# File 'src/modules/Bootloader.rb', line 65

def testAbort
  return false if Mode.commandline

  UI.PollInput == :abort
end

#UpdateBoolean

Update the whole configuration

Returns:

  • (Boolean)

    true on success



237
238
239
# File 'src/modules/Bootloader.rb', line 237

def Update
  Write() # write also reads the configuration and updates it
end

#WriteBoolean

Write bootloader settings to disk

Returns:

  • (Boolean)

    true on success



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'src/modules/Bootloader.rb', line 243

def Write
  ReadOrProposeIfNeeded()

  mark_as_changed

  log.info "Writing bootloader configuration"

  stages = [
    _("Prepare system"),
    _("Create initrd"),
    _("Save boot loader configuration")
  ]
  titles = [
    _("Preparing system..."),
    _("Creating initrd..."),
    _("Saving boot loader configuration...")
  ]

  if Mode.normal
    Progress.New(_("Saving Boot Loader Configuration"), " ", stages.size, stages, titles, "")
    Progress.NextStage
  else
    Progress.Title(titles[0])
  end

  # Prepare system
  progress_state = Progress.set(false)
  if !::Bootloader::BootloaderFactory.current.prepare
    log.error("System could not be prepared successfully, required packages were not installed")
    Yast2::Popup.show(_("Cannot continue without install required packages"))
    return false
  end
  Progress.set(progress_state)

  transactional = Package.IsTransactionalSystem

  # Create initrd
  Progress.NextStage
  Progress.Title(titles[1]) unless Mode.normal

  write_initrd || log.error("Error occurred while creating initrd") if !transactional

  # Save boot loader configuration
  Progress.NextStage
  Progress.Title(titles[2]) unless Mode.normal
  ::Bootloader::BootloaderFactory.current.write(etc_only: transactional)
  if transactional
    # all writing to target is done in specific transactional command
    Yast::Execute.on_target!("transactional-update", "--continue", "bootloader")
  end

  true
end