-
Notifications
You must be signed in to change notification settings - Fork 22
/
autoyast_converter.rb
260 lines (204 loc) · 7.83 KB
/
autoyast_converter.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
require "yast"
require "bootloader/bootloader_factory"
Yast.import "BootStorage"
module Bootloader
# Represents unsupported bootloader type error
class UnsupportedBootloader < RuntimeError
attr_reader :bootloader_name
def initialize(bootloader_name)
@bootloader_name = bootloader_name
super "Unsupported bootloader '#{bootloader_name}'"
end
end
# Converter between internal configuration model and autoyast serialization of configuration.
class AutoyastConverter
class << self
include Yast::Logger
def import(data)
log.info "import data #{data.inspect}"
bootloader = bootloader_from_data(data)
return bootloader if bootloader.name == "none"
# let it be empty if not defined to keep code simplier as effect is same
data["global"] ||= {}
import_grub2(data, bootloader)
import_stage1(data, bootloader)
import_default(data, bootloader.grub_default)
# TODO: import Initrd
log.warn "autoyast profile contain sections which won't be processed" if data["sections"]
bootloader
end
def export(config)
log.info "exporting config #{config.inspect}"
bootloader_type = config.name
res = { "loader_type" => bootloader_type }
return res if bootloader_type == "none"
res["global"] = {}
global = res["global"]
export_grub2(global, config) if config.name == "grub2"
export_stage1(global, config.stage1) if config.respond_to?(:stage1)
export_default(global, config.grub_default)
res
end
private
def import_grub2(data, bootloader)
return unless bootloader.name == "grub2"
GRUB2_BOOLEAN_MAPPING.each do |key, method|
val = data["global"][key]
next unless val
bootloader.public_send(:"#{method}=", val == "true")
end
end
def import_default(data, default)
DEFAULT_BOOLEAN_MAPPING.each do |key, method|
val = data["global"][key]
next unless val
default.public_send(method).value = val == "true"
end
DEFAULT_STRING_MAPPING.each do |key, method|
val = data["global"][key]
next unless val
default.public_send(:"#{method}=", SYMBOL_PARAM.include?(key) ? val.to_sym : val)
end
DEFAULT_KERNEL_PARAMS_MAPPING.each do |key, method|
val = data["global"][key]
next unless val
default.public_send(method).replace(val)
end
import_timeout(data, default)
end
def import_timeout(data, default)
return unless data["global"]["timeout"]
if data["global"]["hiddenmenu"] == "true"
default.timeout = "0"
default.hidden_timeout = data["global"]["timeout"].to_s if data["global"]["timeout"]
else
default.timeout = data["global"]["timeout"].to_s if data["global"]["timeout"]
default.hidden_timeout = "0"
end
end
STAGE1_DEVICES_MAPPING = {
"boot_root" => :RootPartitionDevice,
"boot_boot" => :BootPartitionDevice,
"boot_mbr" => :mbr_disk,
"boot_extended" => :ExtendedPartitionDevice
}.freeze
def import_stage1(data, bootloader)
return unless bootloader.name == "grub2"
stage1 = bootloader.stage1
if !data["global"]["generic_mbr"].nil?
stage1.generic_mbr = data["global"]["generic_mbr"] == "true"
end
if !data["global"]["activate"].nil?
stage1.activate = data["global"]["activate"] == "true"
# old one from SLE9 ages, it uses boolean and not string
elsif !data["activate"].nil?
stage1.activate = data["activate"]
end
import_stage1_devices(data, stage1)
end
def import_stage1_devices(data, stage1)
# extended partion maybe do not exists, so report it to user
if (data["global"]["boot_extended"] == "true" ||
data["location"] == "extended") &&
Yast::BootStorage.ExtendedPartitionDevice.nil?
raise "boot_extended used in autoyast profile, but there is no extended partition"
end
STAGE1_DEVICES_MAPPING.each do |key, device|
if data["global"][key] == "true" || data["boot_#{key}"]
stage1.add_udev_device(Yast::BootStorage.public_send(device))
end
end
import_custom_devices(data, stage1)
end
def import_custom_devices(data, stage1)
# SLE9 way to define boot device
if data["loader_device"] && !data["loader_device"].empty?
stage1.add_udev_device(data["loader_device"])
end
return if !data["global"]["boot_custom"] || data["global"]["boot_custom"].empty?
data["global"]["boot_custom"].split(",").each do |dev|
stage1.add_udev_device(dev.strip)
end
end
def bootloader_from_data(data)
loader_type = data["loader_type"] || "default"
allowed = BootloaderFactory::SUPPORTED_BOOTLOADERS + ["default"]
raise UnsupportedBootloader, loader_type if !allowed.include?(loader_type)
# ensure it is clear bootloader config
BootloaderFactory.clear_cache
if loader_type == "default"
BootloaderFactory.proposed
else
BootloaderFactory.bootloader_by_name(loader_type)
end
end
STAGE1_MAPPING = {
"activate" => :activate?,
"generic_mbr" => :generic_mbr?,
"boot_root" => :root_partition?,
"boot_boot" => :boot_partition?,
"boot_mbr" => :mbr?,
"boot_extended" => :extended_partition?
}.freeze
def export_stage1(res, stage1)
STAGE1_MAPPING.each do |key, method|
res[key] = stage1.public_send(method) ? "true" : "false"
end
# if there is no separated boot, then root and boot partition is same, so in such case
# have only one true there
res["boot_boot"] = "false" if res["boot_root"] == "true" && res["boot_boot"] == "true"
res["boot_custom"] = stage1.custom_devices.join(",") unless stage1.custom_devices.empty?
end
# only for grub2, not for others
GRUB2_BOOLEAN_MAPPING = {
"trusted_grub" => :trusted_boot
}.freeze
def export_grub2(res, bootloader)
GRUB2_BOOLEAN_MAPPING.each do |key, method|
val = bootloader.public_send(method)
res[key] = val ? "true" : "false" unless val.nil?
end
end
DEFAULT_BOOLEAN_MAPPING = {
"os_prober" => :os_prober
}.freeze
DEFAULT_STRING_MAPPING = {
"gfxmode" => :gfxmode,
"serial" => :serial_console,
"terminal" => :terminal
}.freeze
DEFAULT_KERNEL_PARAMS_MAPPING = {
"append" => :kernel_params,
"xen_append" => :xen_kernel_params,
"xen_kernel_append" => :xen_hypervisor_params
}.freeze
SYMBOL_PARAM = [
"terminal"
].freeze
def export_default(res, default)
DEFAULT_BOOLEAN_MAPPING.each do |key, method|
val = default.public_send(method)
res[key] = val.enabled? ? "true" : "false" if val.defined?
end
DEFAULT_STRING_MAPPING.each do |key, method|
val = default.public_send(method)
res[key] = val.to_s if val
end
DEFAULT_KERNEL_PARAMS_MAPPING.each do |key, method|
val = default.public_send(method)
res[key] = val.serialize unless val.empty?
end
export_timeout(res, default)
end
def export_timeout(res, default)
if default.hidden_timeout.to_s.to_i > 0
res["hiddenmenu"] = "true"
res["timeout"] = default.hidden_timeout.to_s.to_i
else
res["hiddenmenu"] = "false"
res["timeout"] = default.timeout.to_s.to_i
end
end
end
end
end