Debian Codegraph @

pkg/storaged/block/resize.jsx

raw
1 /*
2 * Copyright (C) 2023 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import React from "react";
7 import cockpit from "cockpit";
8 import client from "../client.js";
9
10 import {
11 block_name, get_active_usage, teardown_active_usage,
12 undo_temporary_teardown, is_mounted_synch, get_partitions
13 } from "../utils.js";
14 import {
15 existing_passphrase_fields, init_existing_passphrase,
16 request_passphrase_on_error_handler
17 } from "../crypto/keyslots.jsx";
18 import {
19 dialog_open, SizeSlider, BlockingMessage, TeardownMessage, SelectSpaces,
20 init_teardown_usage
21 } from "../dialog.jsx";
22 import { std_reply } from "../stratis/utils.jsx";
23 import { pvs_to_spaces } from "../lvm2/utils.jsx";
24
25 const _ = cockpit.gettext;
26
27 export function check_unused_space(path) {
28 const block = client.blocks[path];
29 const lvm2 = client.blocks_lvm2[path];
30 const lvol = lvm2 && client.lvols[lvm2.LogicalVolume];
31 const part = client.blocks_part[path];
32
33 let size;
34 let min_change;
35
36 if (lvol) {
37 size = lvol.Size;
38 min_change = client.vgroups[lvol.VolumeGroup].ExtentSize;
39 } else if (part) {
40 size = part.Size;
41 min_change = 1024 * 1024;
42 } else {
43 return null;
44 }
45
46 if (size != block.Size) {
47 // Let's ignore inconsistent lvol,part/block combinations.
48 // These happen during a resize and the inconsistency will
49 // eventually go away.
50 return null;
51 }
52
53 let content_path = null;
54 let crypto_overhead = 0;
55
56 const crypto = client.blocks_crypto[block.path];
57 const cleartext = client.blocks_cleartext[block.path];
58 if (crypto) {
59 if (crypto.MetadataSize !== undefined && cleartext) {
60 content_path = cleartext.path;
61 crypto_overhead = crypto.MetadataSize;
62 }
63 } else {
64 content_path = path;
65 }
66
67 const fsys = client.blocks_fsys[content_path];
68 const content_block = client.blocks[content_path];
69 const vdo = content_block ? client.legacy_vdo_overlay.find_by_backing_block(content_block) : null;
70 const stratis_bdev = client.blocks_stratis_blockdev[content_path];
71
72 if (fsys && fsys.Size && (size - fsys.Size - crypto_overhead) > min_change && fsys.Resize) {
73 return {
74 volume_size: size - crypto_overhead,
75 content_size: fsys.Size
76 };
77 }
78
79 if (vdo && (size - vdo.physical_size - crypto_overhead) > min_change) {
80 return {
81 volume_size: size - crypto_overhead,
82 content_size: vdo.physical_size
83 };
84 }
85
86 if (stratis_bdev && (size - Number(stratis_bdev.TotalPhysicalSize) - crypto_overhead) > min_change) {
87 return {
88 volume_size: size - crypto_overhead,
89 content_size: Number(stratis_bdev.TotalPhysicalSize)
90 };
91 }
92
93 return null;
94 }
95
96 function lvol_or_part_and_fsys_resize(client, lvol_or_part, size, offline, passphrase, pvs) {
97 let fsys;
98 let crypto_overhead;
99 let vdo;
100 let stratis_bdev;
101 let orig_size;
102 let block;
103
104 if (lvol_or_part.iface == "org.freedesktop.UDisks2.LogicalVolume") {
105 orig_size = lvol_or_part.Size;
106 block = client.lvols_block[lvol_or_part.path];
107 if (!block)
108 return lvol_or_part.Resize(size, { });
109 } else {
110 orig_size = lvol_or_part.Size;
111 block = client.blocks[lvol_or_part.path];
112 }
113
114 const crypto = client.blocks_crypto[block.path];
115 if (crypto) {
116 const cleartext = client.blocks_cleartext[block.path];
117 if (!cleartext)
118 return;
119 fsys = client.blocks_fsys[cleartext.path];
120 vdo = client.legacy_vdo_overlay.find_by_backing_block(cleartext);
121 stratis_bdev = client.blocks_stratis_blockdev[cleartext.path];
122 crypto_overhead = crypto.MetadataSize;
123 } else {
124 fsys = client.blocks_fsys[block.path];
125 vdo = client.legacy_vdo_overlay.find_by_backing_block(block);
126 stratis_bdev = client.blocks_stratis_blockdev[block.path];
127 crypto_overhead = 0;
128 }
129
130 function fsys_resize() {
131 if (fsys) {
132 // When growing a filesystem, always grow it to fill its
133 // block device. This is always the right thing to do in
134 // Cockpit.
135 //
136 const resize_size = (size > orig_size) ? 0 : size - crypto_overhead;
137
138 // HACK - https://bugzilla.redhat.com/show_bug.cgi?id=1934567
139 //
140 // block_fsys.MountedAt might be out of synch with reality
141 // here if resizing the crypto card accidentally
142 // triggered an unmount. Thus, we check synchronously
143 // whether or not we should be doing a offline resize or
144 // not.
145 //
146 // Another option for us would be to just mount the
147 // filesystem back if that's what we expect, to undo the
148 // bug mentioned above. But let's be a bit more passive
149 // here and hope the bug gets fixed eventually.
150 return (is_mounted_synch(client.blocks[fsys.path])
151 .then(is_mounted => {
152 // When doing an offline resize, we need to first repair the filesystem.
153 if (!is_mounted) {
154 return (fsys.Repair({ })
155 .then(() => fsys.Resize(resize_size, { })));
156 } else {
157 return fsys.Resize(resize_size, { });
158 }
159 }));
160 } else if (vdo) {
161 if (size - crypto_overhead > vdo.physical_size)
162 return vdo.grow_physical();
163 else if (size - crypto_overhead < vdo.physical_size)
164 return Promise.reject(_("VDO backing devices can not be made smaller"));
165 else
166 return Promise.resolve();
167 } else if (stratis_bdev) {
168 const delta = size - crypto_overhead - Number(stratis_bdev.TotalPhysicalSize);
169 if (delta > 0) {
170 const pool = client.stratis_pools[stratis_bdev.Pool];
171 return pool.GrowPhysicalDevice(stratis_bdev.Uuid).then(std_reply);
172 } else if (delta < 0)
173 // This shouldn't happen. But if it does, continuing is harmful, so we throw an error.
174 return Promise.reject(_("Stratis blockdevs can not be made smaller")); // not-covered: safety check
175 else
176 return Promise.resolve();
177 } else if (!block.IdUsage) {
178 // Growing or shrinking unformatted data, nothing to do
179 return Promise.resolve();
180 } else if (size < orig_size) {
181 // This shouldn't happen. But if it does, continuing is harmful, so we throw an error.
182 return Promise.reject(_("Unrecognized data can not be made smaller here.")); // not-covered: safety check
183 } else {
184 // Growing unrecognized content, nothing to do.
185 return Promise.resolve();
186 }
187 }
188
189 function crypto_resize() {
190 // When growing a LUKS device, always grow it to fill its
191 // block device. This is always the right thing to do in
192 // Cockpit.
193 //
194 const resize_size = (size > orig_size) ? 0 : size - crypto_overhead;
195
196 if (crypto) {
197 const opts = { };
198 if (passphrase)
199 opts.passphrase = { t: "s", v: passphrase };
200 return crypto.Resize(resize_size, opts);
201 } else {
202 return Promise.resolve();
203 }
204 }
205
206 function lvol_or_part_resize() {
207 if (size != orig_size) {
208 // Both LogicalVolume and Partition have a Resize method
209 // with the same signature, so this will work on both.
210 return lvol_or_part.Resize(size, { pvs: pvs ? { t: 'ao', v: pvs } : undefined });
211 } else
212 return Promise.resolve();
213 }
214
215 if (size < orig_size) {
216 return fsys_resize().then(crypto_resize)
217 .then(lvol_or_part_resize);
218 } else if (size >= orig_size) {
219 return lvol_or_part_resize().then(crypto_resize)
220 .then(fsys_resize);
221 }
222 }
223
224 export function get_resize_info(client, block, to_fit) {
225 let info;
226 let shrink_excuse;
227 let grow_excuse;
228
229 if (block) {
230 if (block.IdUsage == 'crypto' && client.blocks_crypto[block.path]) {
231 const cleartext = client.blocks_cleartext[block.path];
232
233 if (!cleartext) {
234 info = { };
235 shrink_excuse = grow_excuse = _("Unlock before resizing");
236 } else {
237 return get_resize_info(client, cleartext, to_fit);
238 }
239 } else if (block.IdUsage == 'filesystem') {
240 info = client.fsys_info && client.fsys_info[block.IdType];
241
242 if (!info) {
243 info = { };
244 shrink_excuse = grow_excuse = cockpit.format(_("$0 can not be resized here"),
245 block.IdType);
246 } else {
247 if (!info.can_shrink && !info.can_grow) {
248 shrink_excuse = grow_excuse = cockpit.format(_("$0 can not be resized"),
249 block.IdType);
250 } else {
251 if (!info.can_shrink)
252 shrink_excuse = cockpit.format(_("$0 can not be made smaller"),
253 block.IdType);
254 if (!info.can_grow)
255 grow_excuse = cockpit.format(_("$0 can not be made larger"),
256 block.IdType);
257 }
258 }
259 } else if (client.blocks_stratis_blockdev[block.path]) {
260 info = {
261 can_shrink: false,
262 can_grow: true,
263 grow_needs_unmount: false
264 };
265 shrink_excuse = _("Stratis blockdevs can not be made smaller");
266 } else if (block.IdUsage == 'raid') {
267 info = { };
268 shrink_excuse = grow_excuse = _("Physical volumes can not be resized here");
269 } else if (client.legacy_vdo_overlay.find_by_backing_block(block)) {
270 info = {
271 can_shrink: false,
272 can_grow: true,
273 grow_needs_unmount: false
274 };
275 shrink_excuse = _("VDO backing devices can not be made smaller");
276 } else if (client.blocks_swap[block.path]) {
277 info = {
278 can_shrink: false,
279 can_grow: false,
280 };
281 shrink_excuse = grow_excuse = _("Swap can not be resized here");
282 } else if (!block.IdUsage) {
283 info = {
284 can_shrink: true,
285 can_grow: true,
286 shrink_needs_unmount: false,
287 grow_needs_unmount: false,
288 };
289 } else {
290 info = {
291 can_shrink: false,
292 can_grow: true,
293 grow_needs_unmount: true
294 };
295 shrink_excuse = _("Unrecognized data can not be made smaller here");
296 }
297 if (to_fit) {
298 // Shrink to fit doesn't need to resize the content
299 shrink_excuse = null;
300 }
301 } else {
302 info = { };
303 shrink_excuse = grow_excuse = _("Activate before resizing");
304 }
305
306 return { info, shrink_excuse, grow_excuse };
307 }
308
309 export function free_space_after_part(client, part) {
310 const parts = get_partitions(client, client.blocks[part.Table]);
311
312 function find_it(parts) {
313 for (const p of parts) {
314 if (p.type == "free" && p.start == part.Offset + part.Size)
315 return p.size;
316 if (p.type == "container") {
317 const s = find_it(p.partitions);
318 if (s)
319 return s;
320 }
321 }
322 return false;
323 }
324
325 return find_it(parts) || 0;
326 }
327
328 export function grow_dialog(client, lvol_or_part, info, to_fit) {
329 let title;
330 let block;
331 let name;
332 let orig_size;
333 let max_size;
334 let allow_infinite;
335 let round_size;
336 let has_subvols;
337 let subvols;
338 let pvs_as_spaces;
339 let initial_pvs;
340
341 function compute_max_size(spaces) {
342 const layout = lvol_or_part.Layout;
343 const pvs = spaces.map(s => s.pvol);
344 const n_pvs = pvs.length;
345 const sum = pvs.reduce((sum, pv) => sum + pv.FreeSize, 0);
346 const min = Math.min.apply(null, pvs.map(pv => pv.FreeSize));
347
348 if (!has_subvols) {
349 return sum;
350 } else if (layout == "raid0") {
351 return n_pvs * min;
352 } else if (layout == "raid1") {
353 return min;
354 } else if (layout == "raid10") {
355 return (n_pvs / 2) * min;
356 } else if ((layout == "raid4" || layout == "raid5")) {
357 return (n_pvs - 1) * min;
358 } else if (layout == "raid6") {
359 return (n_pvs - 2) * min;
360 } else
361 return 0; // not-covered: internal error
362 }
363
364 if (lvol_or_part.iface == "org.freedesktop.UDisks2.LogicalVolume") {
365 const vgroup = client.vgroups[lvol_or_part.VolumeGroup];
366 const pool = client.lvols[lvol_or_part.ThinPool];
367
368 pvs_as_spaces = pvs_to_spaces(client, client.vgroups_pvols[vgroup.path].filter(pvol => pvol.FreeSize > 0));
369 subvols = client.lvols_stripe_summary[lvol_or_part.path];
370 has_subvols = subvols && (lvol_or_part.Layout == "mirror" || lvol_or_part.Layout.indexOf("raid") == 0);
371
372 if (!has_subvols)
373 initial_pvs = pvs_as_spaces;
374 else {
375 initial_pvs = [];
376
377 // Step 1: Find the spaces that are already used for a
378 // subvolume. If a subvolume uses more than one, prefer the
379 // one with more available space.
380 for (const sv of subvols) {
381 let sel = null;
382 for (const p in sv) {
383 for (const spc of pvs_as_spaces)
384 if (spc.block.path == p && (!sel || sel.size < spc.size))
385 sel = spc;
386 }
387 if (sel)
388 initial_pvs.push(sel);
389 }
390
391 // Step 2: Select missing one randomly.
392 for (const pv of pvs_as_spaces) {
393 if (initial_pvs.indexOf(pv) == -1 && initial_pvs.length < subvols.length)
394 initial_pvs.push(pv);
395 }
396 }
397
398 title = _("Grow logical volume");
399 block = client.lvols_block[lvol_or_part.path];
400 name = lvol_or_part.Name;
401 orig_size = lvol_or_part.Size;
402 max_size = pool ? pool.Size * 3 : lvol_or_part.Size + compute_max_size(initial_pvs);
403 allow_infinite = !!pool;
404 round_size = vgroup.ExtentSize;
405 } else {
406 has_subvols = false;
407 title = _("Grow partition");
408 block = client.blocks[lvol_or_part.path];
409 name = block_name(block);
410 orig_size = lvol_or_part.Size;
411 max_size = lvol_or_part.Size + free_space_after_part(client, lvol_or_part);
412 allow_infinite = false;
413 round_size = 1024 * 1024;
414 }
415
416 const usage = get_active_usage(client,
417 block && info.grow_needs_unmount ? block.path : null,
418 _("grow"), null,
419 true);
420
421 if (usage.Blocking) {
422 dialog_open({
423 Title: cockpit.format(_("$0 is in use"), name),
424 Body: BlockingMessage(usage)
425 });
426 return;
427 }
428
429 let grow_size;
430 const size_fields = [];
431 if (!to_fit) {
432 if ((has_subvols || lvol_or_part.Layout == "linear") && pvs_as_spaces.length > 1)
433 size_fields.push(
434 SelectSpaces("pvs", _("Physical Volumes"),
435 {
436 spaces: pvs_as_spaces,
437 value: initial_pvs,
438 min_selected: subvols.length,
439 validate: val => {
440 if (has_subvols && subvols.length != val.length)
441 return cockpit.format(_("Exactly $0 physical volumes must be selected"),
442 subvols.length);
443 }
444 }));
445 size_fields.push(
446 SizeSlider("size", _("Size"),
447 {
448 value: orig_size,
449 min: orig_size,
450 max: max_size,
451 allow_infinite,
452 round: round_size,
453 }));
454 } else {
455 grow_size = block.Size;
456 }
457
458 let recovered_passphrase;
459 let passphrase_fields = [];
460 if (block && block.IdType == "crypto_LUKS" && block.IdVersion == 2)
461 passphrase_fields = existing_passphrase_fields(_("Resizing an encrypted filesystem requires unlocking the disk. Please provide a current disk passphrase."));
462
463 function prepare_pvs(pvs) {
464 if (!pvs)
465 return pvs;
466
467 pvs = pvs.map(spc => spc.block.path);
468
469 if (!has_subvols)
470 return pvs;
471
472 const subvol_pvs = [];
473
474 // Step 1: Find PVs that are already used by a subvolume
475 subvols.forEach((sv, idx) => {
476 subvol_pvs[idx] = null;
477 for (const pv in sv) {
478 if (pvs.indexOf(pv) >= 0 && subvol_pvs.indexOf(pv) == -1) {
479 subvol_pvs[idx] = pv;
480 break;
481 }
482 }
483 });
484
485 // Step 2: Use the rest for the leftover subvolumes
486 subvols.forEach((sv, idx) => {
487 if (!subvol_pvs[idx]) {
488 for (const pv of pvs) {
489 if (subvol_pvs.indexOf(pv) == -1) {
490 subvol_pvs[idx] = pv;
491 break;
492 }
493 }
494 }
495 });
496
497 return subvol_pvs;
498 }
499
500 if (!usage.Teardown && size_fields.length + passphrase_fields.length === 0) {
501 return lvol_or_part_and_fsys_resize(client, lvol_or_part, grow_size, info.grow_needs_unmount,
502 null, prepare_pvs(initial_pvs));
503 }
504
505 const dlg = dialog_open({
506 Title: title,
507 Teardown: TeardownMessage(usage),
508 Body: has_subvols && <div><p>{cockpit.format(_("Exactly $0 physical volumes need to be selected, one for each stripe of the logical volume."), subvols.length)}</p><br /></div>,
509 Fields: size_fields.concat(passphrase_fields),
510 update: (dlg, vals, trigger) => {
511 if (vals.pvs) {
512 const max = lvol_or_part.Size + compute_max_size(vals.pvs);
513 if (vals.size > max)
514 dlg.set_values({ size: max });
515 dlg.set_options("size", { max });
516 }
517 },
518 Action: {
519 Title: _("Grow"),
520 disable_on_error: usage.Teardown,
521 action: function (vals) {
522 return teardown_active_usage(client, usage)
523 .then(function () {
524 return (lvol_or_part_and_fsys_resize(client, lvol_or_part,
525 to_fit ? grow_size : vals.size,
526 info.grow_needs_unmount,
527 vals.passphrase || recovered_passphrase,
528 prepare_pvs(vals.pvs))
529 .then(() => undo_temporary_teardown(client, usage))
530 .catch(request_passphrase_on_error_handler(dlg, vals, recovered_passphrase, block)));
531 });
532 }
533 },
534 Inits: [
535 init_teardown_usage(client, usage),
536 passphrase_fields.length
537 ? init_existing_passphrase(block, false, pp => { recovered_passphrase = pp })
538 : null
539 ]
540 });
541 }
542
543 export function shrink_dialog(client, lvol_or_part, info, to_fit) {
544 let title;
545 let block;
546 let name;
547 let orig_size;
548 let round_size;
549
550 if (lvol_or_part.iface == "org.freedesktop.UDisks2.LogicalVolume") {
551 const vgroup = client.vgroups[lvol_or_part.VolumeGroup];
552
553 title = _("Shrink logical volume");
554 block = client.lvols_block[lvol_or_part.path];
555 name = lvol_or_part.Name;
556 orig_size = lvol_or_part.Size;
557 round_size = vgroup.ExtentSize;
558 } else {
559 title = _("Shrink partition");
560 block = client.blocks[lvol_or_part.path];
561 name = block_name(block);
562 orig_size = lvol_or_part.Size;
563 round_size = 1024 * 1024;
564 }
565
566 const usage = get_active_usage(client,
567 block && !to_fit && info.shrink_needs_unmount ? block.path : null,
568 _("shrink"), null,
569 true);
570
571 if (usage.Blocking) {
572 dialog_open({
573 Title: cockpit.format(_("$0 is in use"), name),
574 Body: BlockingMessage(usage)
575 });
576 return;
577 }
578
579 let shrink_size;
580 let size_fields = [];
581 if (!to_fit) {
582 size_fields = [
583 SizeSlider("size", _("Size"),
584 {
585 value: orig_size,
586 max: orig_size,
587 round: round_size,
588 })
589 ];
590 } else {
591 const crypto = client.blocks_crypto[block.path];
592 const cleartext = client.blocks_cleartext[block.path];
593 let content_path = null;
594 let crypto_overhead = 0;
595
596 if (crypto) {
597 if (crypto.MetadataSize !== undefined && cleartext) {
598 content_path = cleartext.path;
599 crypto_overhead = crypto.MetadataSize;
600 }
601 } else {
602 content_path = block.path;
603 }
604
605 const fsys = client.blocks_fsys[content_path];
606 if (fsys)
607 shrink_size = fsys.Size + crypto_overhead;
608
609 const vdo = client.legacy_vdo_overlay.find_by_backing_block(client.blocks[content_path]);
610 if (vdo)
611 shrink_size = vdo.physical_size + crypto_overhead;
612
613 const stratis_bdev = client.blocks_stratis_blockdev[content_path];
614 if (stratis_bdev)
615 shrink_size = Number(stratis_bdev.TotalPhysicalSize) + crypto_overhead;
616
617 if (shrink_size === undefined) {
618 console.warn("Couldn't determine size to shrink to."); // not-covered: safety check
619 return; // not-covered: safety check
620 }
621 }
622
623 let recovered_passphrase;
624 let passphrase_fields = [];
625 if (block && block.IdType == "crypto_LUKS" && block.IdVersion == 2)
626 passphrase_fields = existing_passphrase_fields(_("Resizing an encrypted filesystem requires unlocking the disk. Please provide a current disk passphrase."));
627
628 if (usage.length == 0 && size_fields.length + passphrase_fields.length === 0) {
629 return lvol_or_part_and_fsys_resize(client, lvol_or_part, shrink_size, false, null);
630 }
631
632 const dlg = dialog_open({
633 Title: title,
634 Teardown: TeardownMessage(usage),
635 Fields: size_fields.concat(passphrase_fields),
636 Action: {
637 Title: _("Shrink"),
638 disable_on_error: usage.Teardown,
639 action: function (vals) {
640 return teardown_active_usage(client, usage)
641 .then(function () {
642 return (lvol_or_part_and_fsys_resize(client, lvol_or_part,
643 to_fit ? shrink_size : vals.size,
644 to_fit ? false : info.shrink_needs_unmount,
645 vals.passphrase || recovered_passphrase)
646 .then(() => undo_temporary_teardown(client, usage))
647 .catch(request_passphrase_on_error_handler(dlg, vals, recovered_passphrase, block)));
648 });
649 }
650 },
651 Inits: [
652 init_teardown_usage(client, usage),
653 passphrase_fields.length
654 ? init_existing_passphrase(block, false, pp => { recovered_passphrase = pp })
655 : null
656 ]
657 });
658 }