/
Bootloader.rb
488 lines (408 loc) · 15.1 KB
/
Bootloader.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
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
438
439
440
441
442
443
444
445
446
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# encoding: utf-8
# File:
# modules/Bootloader.ycp
#
# Module:
# Bootloader installation and configuration
#
# Summary:
# Bootloader installation and configuration base module
#
# Authors:
# Jiri Srain <jsrain@suse.cz>
# Olaf Dabrunz <od@suse.de>
#
# $Id$
#
require "yast"
require "bootloader/exceptions"
require "bootloader/sysconfig"
require "bootloader/bootloader_factory"
require "bootloader/autoyast_converter"
require "cfa/matcher"
Yast.import "UI"
Yast.import "Arch"
Yast.import "BootStorage"
Yast.import "Initrd"
Yast.import "Mode"
Yast.import "Progress"
Yast.import "Report"
Yast.import "Stage"
module Yast
class BootloaderClass < Module
include Yast::Logger
BOOLEAN_MAPPING = { true => :present, false => :missing }.freeze
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
# Check whether abort was pressed
# @return [Boolean] true if abort was pressed
def testAbort
return false if Mode.commandline
UI.PollInput == :abort
end
# bnc #419197 yast2-bootloader does not correctly initialise libstorage
# Function try initialize yast2-storage
# if other module used it then don't continue with initialize
# @return [Boolean] true on success
def checkUsedStorage
true
end
# Export bootloader settings to a map
# @return bootloader settings
def Export
Yast::BootStorage.detect_disks
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
# Import settings from a map
# @param [Hash] data map of bootloader settings
# @return [Boolean] true on success
def Import(data)
# AutoYaST configuration mode. There is no access to the system
Yast::BootStorage.detect_disks
factory = ::Bootloader::BootloaderFactory
imported_configuration = ::Bootloader::AutoyastConverter.import(data)
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
true
end
# Read settings from disk
# @return [Boolean] true on success
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)
_("Read partitioning"),
# 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 !checkUsedStorage
BootStorage.detect_disks
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 => e
ret = Yast::Report.AnyQuestion(_("Broken Configuration"),
# TRANSLATORS: %s stands for readon why yast cannot process it
_("YaST cannot process current bootloader configuration (%s). " \
"Propose new configuration from scratch?") % e.reason,
_("Propose"),
_("Quit"),
:yes) # focus proposing new one
return false unless ret
::Bootloader::BootloaderFactory.current = ::Bootloader::BootloaderFactory.proposed
::Bootloader::BootloaderFactory.current.propose
end
Progress.Finish
true
end
# Reset bootloader settings
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
# Propose bootloader settings
def Propose
log.info "Proposing configuration"
# always have a current target map available in the log
log.info "unfiltered target map: #{Storage.GetTargetMap.inspect}"
::Bootloader::BootloaderFactory.current.propose
log.info "Proposed settings: #{Export()}"
nil
end
# Display bootloader summary
# @return a list of summary lines
def Summary(simple_mode: false)
# kokso: additional warning that root partition is nfs type -> bootloader will not be installed
if BootStorage.disk_with_boot_partition == "/dev/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
# Update the whole configuration
# @return [Boolean] true on success
def Update
Write() # write also reads the configuration and updates it
end
# Write bootloader settings to disk
# @return [Boolean] true on success
def Write
ReadOrProposeIfNeeded()
mark_as_changed
log.info "Writing bootloader configuration"
# run Progress bar
stages = [
# progress stage, text in dialog (short)
_("Create initrd"),
# progress stage, text in dialog (short)
_("Save boot loader configuration")
]
titles = [
# progress step, text in dialog (short)
_("Creating initrd..."),
# progress step, text in dialog (short)
_("Saving boot loader configuration...")
]
# progress bar caption
if Mode.normal
# progress line
Progress.New(
_("Saving Boot Loader Configuration"),
" ",
stages.size,
stages,
titles,
""
)
Progress.NextStage
else
Progress.Title(titles[0])
end
ret = write_initrd
log.error "Error occurred while creating initrd" unless ret
Progress.NextStep
Progress.Title(titles[1]) unless Mode.normal
::Bootloader::BootloaderFactory.current.write
true
end
# return default section label
# @return [String] default section label
def getDefaultSection
ReadOrProposeIfNeeded()
bootloader = Bootloader::BootloaderFactory.current
return "" unless bootloader.respond_to?(:sections)
bootloader.sections.default
end
FLAVOR_KERNEL_LINE_MAP = {
:common => "append",
:xen_guest => "xen_append",
:xen_host => "xen_kernel_append"
}.freeze
# Gets value for given parameter in kernel parameters for given flavor.
# @param [Symbol] flavor flavor of kernel, for possible values see #modify_kernel_param
# @param [String] key of parameter on kernel command line
# @return [String,:missing,:present] Returns string for parameters with value,
# `:missing` if key is not there and `:present` for parameters without value.
#
# @example get crashkernel parameter to common kernel
# Bootloader.kernel_param(:common, "crashkernel")
# => "256M@64B"
#
# @example get cio_ignore parameter for xen_host kernel when missing
# Bootloader.kernel_param(:xen_host, "cio_ignore")
# => :missing
#
# @example get verbose parameter for xen_guest which is there
# Bootloader.kernel_param(:xen_guest, "verbose")
# => :present
#
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
# Modify kernel parameters for installed kernels according to values
# @param [Array] args 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
# @return [Boolean] true if params were modified; false otherwise.
#
# @example add crashkernel parameter to common kernel and xen guest
# Bootloader.modify_kernel_params(:common, :xen_guest, "crashkernel" => "256M@64M")
#
# @example same as before just with array passing
# targets = [:common, :xen_guest]
# Bootloader.modify_kernel_params(targets, "crashkernel" => "256M@64M")
#
# @example remove cio_ignore parameter for common kernel only
# Bootloader.modify_kernel_params("cio_ignore" => :missing)
#
# @example add cio_ignore parameter for xen host kernel
# Bootloader.modify_kernel_params(:xen_host, "cio_ignore" => :present)
#
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 true then param.add_parameter(key, value)
when Array
value.each { |val| param.add_parameter(key, val) }
else
param.add_parameter(key, value)
end
end
end
changed
end
# Get currently used bootloader, detect if not set yet
# @return [String] botloader type
def getLoaderType
::Bootloader::BootloaderFactory.current.name
end
# Check whether settings were read or proposed, if not, decide
# what to do and read or propose settings
def ReadOrProposeIfNeeded
current_bl = ::Bootloader::BootloaderFactory.current
return if current_bl.read? || current_bl.proposed?
if Mode.config
log.info "Initialize libstorage in readonly mode" # bnc#942360
Storage.InitLibstorage(true)
log.info "Not reading settings in Mode::config ()"
Propose()
elsif Stage.initial && !Mode.update
Propose()
else
progress_orig = Progress.set(false)
Read()
Progress.set(progress_orig)
end
end
private
def mark_as_changed
# always run mkinitrd at the end of S/390 installation (bsc#933177)
# otherwise cio_ignore settings are not honored in initrd
Initrd.changed = true if Arch.s390 && Stage.initial
end
NONSPLASH_VGA_VALUES = ["", "false", "ask"].freeze
# regenerates initrd if needed
# @return boolean true if succeed
def write_initrd
return true unless Initrd.changed
# save initrd
Initrd.Write
end
publish :function => :Export, :type => "map ()"
publish :function => :Import, :type => "boolean (map)"
publish :function => :Propose, :type => "void ()"
publish :function => :Read, :type => "boolean ()"
publish :function => :Reset, :type => "void ()"
publish :function => :Write, :type => "boolean ()"
publish :function => :getDefaultSection, :type => "string ()"
publish :function => :getLoaderType, :type => "string ()"
publish :variable => :proposed_cfg_changed, :type => "boolean"
publish :function => :blRead, :type => "boolean (boolean, boolean)"
publish :function => :blSave, :type => "boolean (boolean, boolean, boolean)"
publish :function => :blWidgetMaps, :type => "map <string, map <string, any>> ()"
publish :function => :blDialogs, :type => "map <string, symbol ()> ()"
publish :variable => :test_abort, :type => "boolean ()"
publish :function => :Summary, :type => "list <string> ()"
publish :function => :Update, :type => "boolean ()"
publish :function => :WriteInstallation, :type => "boolean ()"
end
Bootloader = BootloaderClass.new
Bootloader.main
end