-
Notifications
You must be signed in to change notification settings - Fork 20
/
AutoinstRAID.rb
271 lines (232 loc) · 7.73 KB
/
AutoinstRAID.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
# encoding: utf-8
# File: modules/AutoinstRAID.ycp
# Module: Auto-Installation
# Summary: RAID
# Authors: Anas Nashif <nashif@suse.de>
#
# $Id$
require "yast"
module Yast
class AutoinstRAIDClass < Module
def main
textdomain "autoinst"
Yast.import "Storage"
Yast.import "Partitions"
Yast.import "AutoinstStorage"
Yast.include self, "partitioning/raid_lib.rb"
@ExistingRAID = {}
@old_available = false
@raid = {}
# Local variables
@region = [0, 0]
# Temporary copy of variable from Storage
@targetMap = {}
AutoinstRAID()
end
# Constructor
# @return [void]
def AutoinstRAID
nil
end
# Initialize
def Init
@raid = Builtins.filter(AutoinstStorage.AutoTargetMap) do |k, v|
k == "/dev/md"
end
return false if Builtins.size(@raid) == 0
@ExistingRAID = Builtins.filter(Storage.GetTargetMap) do |k, v|
k == "/dev/md"
end
Builtins.y2milestone("Existing RAID: %1", @ExistingRAID)
@old_available = true if Ops.greater_than(Builtins.size(@ExistingRAID), 0)
true
end
# Return existing MDs
# @return [Array] list of existing MDs
def ExistingMDs(md)
Builtins.filter(get_possible_rds(Storage.GetTargetMap)) do |part|
Ops.get_string(part, "raid_name", "-1") == md
end
end
# Return deleted MDs
# @return [Array] list of deleted MDs
# useless
# global define list DeletedMDs ( string md ) {
# list<list<map> > ret = [];
# foreach( string dev, map devmap, Storage::GetTargetMap(),
# ``{
# ret = add( ret,
# filter( map part, devmap["partitions"]:[],
# ``(
# part["raid_name"]:"" == md
# &&
# part["delete"]:false
# &&
# part["fsid"]:0 == Partitions::fsid_raid
# )
# )
# );
# });
#
# return( flatten(ret) );
# }
# Delete MDs
# @return [Array] list of deleted MDs
# useless
# global define list DeleteMDs () {
# list to_bo_deleted = [];
# foreach( string dev, map devmap, Storage::GetTargetMap(), ``{
# foreach( map part, devmap["partitions"]:[], ``{
# if (part["old_raid_name"]:"" != "" && part["delete"]:false)
# {
# to_bo_deleted= add(to_bo_deleted, part["old_raid_name"]:"");
# }
# });
# });
#
# y2milestone("mds to delete: %1", to_bo_deleted);
#
# if (old_available)
# {
# list<map> mds = Storage::GetTargetMap()["/dev/md","partitions"]:[];
# list<map> new_mds = maplist( map md, mds ,``{
# if ( contains(to_bo_deleted, md["device"]:""))
# {
# md["delete"] = true;
# }
# return (md);
# });
# y2milestone("new_mds: %1", new_mds );
# map allmds = targetMap["/dev/md"]:$[];
# allmds["partitions"] = new_mds;
# targetMap["/dev/md"] = allmds;
# Storage::SetTargetMap(targetMap);
# }
# }
def remove_possible_mds
prefer_remove = Ops.get_boolean(
@raid,
["/dev/md", "prefer_remove"],
false
)
if @old_available && prefer_remove
existing_mds = Ops.get_list(
@ExistingRAID,
["/dev/md", "partitions"],
[]
)
modified_mds = Builtins.maplist(existing_mds) do |md|
Ops.set(md, "delete", true)
deep_copy(md)
end
allmds = Convert.convert(
Ops.get(@ExistingRAID, "/dev/md", {}),
:from => "map",
:to => "map <string, any>"
)
Ops.set(allmds, "partitions", modified_mds)
tg = Storage.GetTargetMap
Ops.set(tg, "/dev/md", allmds)
Storage.SetTargetMap(tg)
# update our variable
Ops.set(@ExistingRAID, "/dev/md", allmds)
end
true
end
# Create RAID Configuration
# @return [Boolean]
def Write
remove_possible_mds
raid_partitions = Ops.get_list(@raid, ["/dev/md", "partitions"], [])
_RaidList = Builtins.maplist(raid_partitions) do |md|
use = Ops.get_string(@raid, ["/dev/md", "use"], "none")
dev = md.fetch("raid_options",{})["raid_name"]
if !dev || dev.empty?
dev = "/dev/md"+md.fetch("partition_nr", 0).to_s
end
md["device"] = dev
if md["crypt_fs"]
Storage.SetCryptPwd(dev, Ops.get_string(md, "crypt_key", ""))
md["enc_type"] = md["format"] ? :luks : :twofish
end
Builtins.y2milestone("Working on %1", md)
chunk_size = 4
options = Ops.get_map(md, "raid_options", {})
raid_type = Ops.get_string(options, "raid_type", "raid1")
chunk_size = 128 if raid_type == "raid5"
chunk_size = 32 if raid_type == "raid0"
if !Builtins.haskey(options, "raid_type")
Ops.set(options, "raid_type", raid_type)
end
sel_chunksize = Builtins.tointeger(
Ops.get_string(options, "chunk_size", "0")
)
if sel_chunksize != 0
chunk_size = sel_chunksize
Ops.set(options, "chunk_size", chunk_size)
end
if raid_type == "raid5" &&
Ops.get_string(options, "parity_algorithm", "") == ""
Ops.set(options, "parity_algorithm", "left_symmetric")
end
Ops.set(md, "nr", Ops.get_integer(md, "partition_nr", 0))
if !Builtins.haskey(md, "create")
Ops.set(md, "create", true)
Ops.set(md, "status", "create")
end
Ops.set(md, "format", false) if !Builtins.haskey(md, "format")
if Ops.get_boolean(md, "format", false)
Ops.set(md, "used_fs", Ops.get_symbol(md, "filesystem", Partitions.DefaultFs))
end
md = AutoinstStorage.AddFilesysData(md, md)
Ops.set(md, "type", :sw_raid)
if Builtins.haskey(md, "raid_options")
md = Builtins.remove(md, "raid_options")
end
Ops.set(
md,
"devices",
Ops.get(
AutoinstStorage.raid2device,
Ops.get_string(md, "device", ""),
[]
)
)
if AutoinstStorage.root_btrfs?(md)
AutoinstStorage.configure_root_btrfs(md, device: "/dev/md", data: @raid["/dev/md"])
end
Builtins.union(md, options)
end
allraid = []
if Ops.greater_than(
Builtins.size(
Ops.get_list(@ExistingRAID, ["/dev/md", "partitions"], [])
),
0
)
allraid = Builtins.union(
Ops.get_list(@ExistingRAID, ["/dev/md", "partitions"], []),
_RaidList
)
else
allraid = deep_copy(_RaidList)
end
Builtins.y2milestone("All RAID: %1", allraid)
_RaidMap = { "partitions" => allraid, "type" => :CT_MD }
Builtins.y2milestone("RaidMap: %1", _RaidMap)
tg = Storage.GetTargetMap
Ops.set(tg, "/dev/md", _RaidMap)
Storage.SetTargetMap(tg)
Builtins.y2milestone("Targets: %1", Storage.GetTargetMap)
true
end
publish :variable => :ExistingRAID, :type => "map <string, map>"
publish :variable => :old_available, :type => "boolean"
publish :function => :AutoinstRAID, :type => "void ()"
publish :function => :Init, :type => "boolean ()"
publish :function => :ExistingMDs, :type => "list (string)"
publish :function => :Write, :type => "boolean ()"
end
AutoinstRAID = AutoinstRAIDClass.new
AutoinstRAID.main
end