Debian Codegraph @

pkg/storaged/lvm2/create-logical-volume-dialog.jsx

raw
1 /*
2 * Copyright (C) 2016 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7
8 import { dialog_open, TextInput, SelectOne, Message, SelectSpaces, SelectOneRadio, SizeSlider, CheckBoxes } from "../dialog.jsx";
9 import { validate_lvm2_name } from "../utils.js";
10
11 import { pvs_to_spaces, next_default_logical_volume_name } from "./utils.jsx";
12 import { getPackageManager } from "packagemanager.js";
13
14 const _ = cockpit.gettext;
15
16 async function install_package(name, progress) {
17 let package_manager = null;
18 try {
19 package_manager = await getPackageManager();
20 } catch (err) {
21 return;
22 }
23
24 return package_manager.check_missing_packages([name], p => progress(_("Checking installed software"), p.cancel))
25 .then(data => {
26 if (data.unavailable_names.length > 0)
27 return Promise.reject(new Error(
28 cockpit.format(_("$0 is not available from any repository."), data.unavailable_names[0])));
29 // let's be cautious here, we really don't expect removals
30 if (data.remove_names.length > 0)
31 return Promise.reject(new Error(
32 cockpit.format(_("Installing $0 would remove $1."), name, data.remove_names[0])));
33
34 return package_manager.install_missing_packages(data, p => progress(_("Installing packages"), p.cancel));
35 });
36 }
37
38 export function create_logical_volume(client, vgroup) {
39 if (vgroup.FreeSize == 0)
40 return;
41
42 const pvs_as_spaces = pvs_to_spaces(client, client.vgroups_pvols[vgroup.path].filter(pvol => pvol.FreeSize > 0));
43
44 const can_do_layouts = !!vgroup.CreatePlainVolumeWithLayout && pvs_as_spaces.length > 1;
45
46 const purposes = [
47 {
48 value: "block",
49 title: _("Block device for filesystems"),
50 },
51 { value: "pool", title: _("Pool for thinly provisioned volumes") }
52 /* Not implemented
53 { value: "cache", Title: _("Cache") }
54 */
55 ];
56
57 const layouts = [
58 {
59 value: "linear",
60 title: _("Linear"),
61 min_pvs: 1,
62 },
63 {
64 value: "raid0",
65 title: _("Striped (RAID 0)"),
66 min_pvs: 2,
67 },
68 {
69 value: "raid1",
70 title: _("Mirrored (RAID 1)"),
71 min_pvs: 2,
72 },
73 {
74 value: "raid10",
75 title: _("Striped and mirrored (RAID 10)"),
76 min_pvs: 4,
77 },
78 {
79 value: "raid5",
80 title: _("Distributed parity (RAID 5)"),
81 min_pvs: 3,
82 },
83 {
84 value: "raid6",
85 title: _("Double distributed parity (RAID 6)"),
86 min_pvs: 5,
87 }
88 ];
89
90 const vdo_package = client.get_config("vdo_package", null);
91 const need_vdo_install = vdo_package && !(client.features.lvm_create_vdo || client.features.legacy_vdo);
92
93 if (client.features.lvm_create_vdo || client.features.legacy_vdo || vdo_package)
94 purposes.push({ value: "vdo", title: _("VDO filesystem volume (compression/deduplication)") });
95
96 /* For layouts with redundancy, CreatePlainVolumeWithLayout will
97 * create as many subvolumes as there are selected PVs. This has
98 * the nice effect of making the calculation of the maximum size of
99 * such a volume trivial.
100 */
101
102 function max_size(vals) {
103 const layout = vals.layout;
104 const pvs = vals.pvs.map(s => s.pvol);
105 const n_pvs = pvs.length;
106 const sum = pvs.reduce((sum, pv) => sum + pv.FreeSize, 0);
107 const min = Math.min.apply(null, pvs.map(pv => pv.FreeSize));
108
109 function metasize(datasize) {
110 const default_regionsize = 2 * 1024 * 1024;
111 const regions = Math.ceil(datasize / default_regionsize);
112 const bytes = 2 * 4096 + Math.ceil(regions / 8);
113 return vgroup.ExtentSize * Math.ceil(bytes / vgroup.ExtentSize);
114 }
115
116 if (layout == "linear") {
117 return sum;
118 } else if (layout == "raid0" && n_pvs >= 2) {
119 return n_pvs * min;
120 } else if (layout == "raid1" && n_pvs >= 2) {
121 return min - metasize(min);
122 } else if (layout == "raid10" && n_pvs >= 4) {
123 return Math.floor(n_pvs / 2) * (min - metasize(min));
124 } else if ((layout == "raid4" || layout == "raid5") && n_pvs >= 3) {
125 return (n_pvs - 1) * (min - metasize(min));
126 } else if (layout == "raid6" && n_pvs >= 5) {
127 return (n_pvs - 2) * (min - metasize(min));
128 } else
129 return 0; // not-covered: internal error
130 }
131
132 const layout_descriptions = {
133 linear: _("Data will be stored on the selected physical volumes without any additional redundancy or performance improvements."),
134 raid0: _("Data will be stored on the selected physical volumes in an alternating fashion to improve performance. At least two volumes need to be selected."),
135 raid1: _("Data will be stored as two or more copies on the selected physical volumes, to improve reliability. At least two volumes need to be selected."),
136 raid10: _("Data will be stored as two copies and also in an alternating fashion on the selected physical volumes, to improve both reliability and performance. At least four volumes need to be selected."),
137 raid4: _("Data will be stored on the selected physical volumes so that one of them can be lost without affecting the data. At least three volumes need to be selected."),
138 raid5: _("Data will be stored on the selected physical volumes so that one of them can be lost without affecting the data. Data is also stored in an alternating fashion to improve performance. At least three volumes need to be selected."),
139 raid6: _("Data will be stored on the selected physical volumes so that up to two of them can be lost at the same time without affecting the data. Data is also stored in an alternating fashion to improve performance. At least five volumes need to be selected."),
140 };
141
142 function compute_layout_choices(pvs) {
143 return layouts.filter(l => l.min_pvs <= pvs.length);
144 }
145
146 for (const lay of layouts)
147 lay.disabled = pvs_as_spaces.length < lay.min_pvs;
148
149 function min_pvs_explanation(pvs, min) {
150 if (pvs.length <= min)
151 return cockpit.format(_("All $0 selected physical volumes are needed for the chosen layout."),
152 pvs.length);
153 return null;
154 }
155
156 dialog_open({
157 Title: _("Create logical volume"),
158 Fields: [
159 TextInput("name", _("Name"),
160 {
161 value: next_default_logical_volume_name(client, vgroup, "lvol"),
162 validate: validate_lvm2_name
163 }),
164 SelectOne("purpose", _("Purpose"),
165 {
166 value: "block",
167 choices: purposes
168 }),
169 Message(cockpit.format(_("The $0 package will be installed to create VDO devices."), vdo_package),
170 {
171 visible: vals => vals.purpose === 'vdo' && need_vdo_install,
172 }),
173 SelectSpaces("pvs", _("Physical Volumes"),
174 {
175 spaces: pvs_as_spaces,
176 value: pvs_as_spaces,
177 visible: vals => can_do_layouts && vals.purpose === 'block',
178 min_selected: 1,
179 validate: (val, vals) => {
180 if (vals.layout == "raid10" && (vals.pvs.length % 2) !== 0)
181 return _("RAID10 needs an even number of physical volumes");
182 },
183 explanation: min_pvs_explanation(pvs_as_spaces, 1)
184 }),
185 SelectOneRadio("layout", _("Layout"),
186 {
187 vertical: true,
188 value: "linear",
189 choices: compute_layout_choices(pvs_as_spaces),
190 visible: vals => can_do_layouts && vals.purpose === 'block',
191 explanation: layout_descriptions.linear
192 }),
193 SizeSlider("size", _("Size"),
194 {
195 visible: vals => vals.purpose !== 'vdo',
196 max: vgroup.FreeSize,
197 round: vgroup.ExtentSize
198 }),
199 /* VDO parameters */
200 SizeSlider("vdo_psize", _("Size"),
201 {
202 visible: vals => vals.purpose === 'vdo',
203 min: 5 * 1024 * 1024 * 1024,
204 max: vgroup.FreeSize,
205 round: vgroup.ExtentSize
206 }),
207 SizeSlider("vdo_lsize", _("Logical size"),
208 {
209 visible: vals => vals.purpose === 'vdo',
210 value: vgroup.FreeSize,
211 // visually point out that this can be over-provisioned
212 max: vgroup.FreeSize * 3,
213 allow_infinite: true,
214 round: vgroup.ExtentSize
215 }),
216
217 CheckBoxes("vdo_options", _("Options"),
218 {
219 visible: vals => vals.purpose === 'vdo',
220 fields: [
221 {
222 tag: "compression",
223 title: _("Compression"),
224 tooltip: _("Save space by compressing individual blocks with LZ4")
225 },
226 {
227 tag: "deduplication",
228 title: _("Deduplication"),
229 tooltip: _("Save space by storing identical data blocks just once")
230 },
231 ],
232 value: {
233 compression: true,
234 deduplication: true,
235 }
236 }),
237 ],
238 update: (dlg, vals, trigger) => {
239 if (vals.purpose == 'block' && (trigger == "layout" || trigger == "pvs" || trigger == "purpose")) {
240 for (const lay of layouts) {
241 if (lay.value == vals.layout) {
242 dlg.set_options("pvs", {
243 min_selected: lay.min_pvs,
244 explanation: min_pvs_explanation(vals.pvs, lay.min_pvs)
245 });
246 }
247 }
248 dlg.set_options("layout",
249 {
250 choices: compute_layout_choices(vals.pvs),
251 explanation: layout_descriptions[vals.layout]
252 });
253 const max = max_size(vals);
254 const old_max = dlg.get_options("size").max;
255 if (vals.size > max || vals.size == old_max)
256 dlg.set_values({ size: max });
257 dlg.set_options("size", { max });
258 } else if (trigger == "purpose") {
259 dlg.set_options("size", { max: vgroup.FreeSize });
260 }
261 },
262 Action: {
263 Title: _("Create"),
264 action: (vals, progress) => {
265 if (vals.purpose == "block") {
266 if (!can_do_layouts)
267 return vgroup.CreatePlainVolume(vals.name, vals.size, { });
268 else {
269 return vgroup.CreatePlainVolumeWithLayout(vals.name, vals.size, vals.layout,
270 vals.pvs.map(spc => spc.block.path),
271 { });
272 }
273 } else if (vals.purpose == "pool")
274 return vgroup.CreateThinPoolVolume(vals.name, vals.size, { });
275 else if (vals.purpose == "vdo") {
276 return (need_vdo_install ? install_package(vdo_package, progress) : Promise.resolve())
277 .then(() => {
278 progress(_("Creating VDO device")); // not cancellable any more
279 return vgroup.CreateVDOVolume(
280 // HACK: emulate lvcreate's automatic pool name creation until
281 // https://github.com/storaged-project/udisks/issues/939
282 vals.name, next_default_logical_volume_name(client, vgroup, "vpool"),
283 vals.vdo_psize, vals.vdo_lsize,
284 0, // default index memory
285 vals.vdo_options.compression, vals.vdo_options.deduplication,
286 "auto", { });
287 });
288 }
289 }
290 }
291 });
292 }