Debian Codegraph @

pkg/storaged/lvm2/physical-volume.jsx

raw
1 /*
2 * Copyright (C) 2023 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7 import React from "react";
8 import client from "../client";
9
10 import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
11 import { CardBody } from "@patternfly/react-core/dist/esm/components/Card/index.js";
12 import { DescriptionList } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js";
13
14 import { StorageCard, StorageDescription, new_card, register_crossref } from "../pages.jsx";
15 import { std_format_action } from "../block/actions.jsx";
16 import { std_lock_action } from "../crypto/actions.jsx";
17 import { StorageUsageBar } from "../storage-controls.jsx";
18
19 const _ = cockpit.gettext;
20
21 export function make_lvm2_physical_volume_card(next, backing_block, content_block) {
22 const block_pvol = client.blocks_pvol[content_block.path];
23 const vgroup = block_pvol && client.vgroups[block_pvol.VolumeGroup];
24
25 const pv_card = new_card({
26 title: _("LVM2 physical volume"),
27 location: {
28 label: vgroup ? vgroup.Name : null,
29 to: vgroup ? ["vg", vgroup.Name] : null,
30 },
31 next,
32 page_size: (block_pvol
33 ? <StorageUsageBar stats={[block_pvol.Size - block_pvol.FreeSize, block_pvol.Size]} short />
34 : backing_block.Size),
35 component: LVM2PhysicalVolumeCard,
36 props: { backing_block, content_block },
37 actions: [
38 std_lock_action(backing_block, content_block),
39 std_format_action(backing_block, content_block),
40 ]
41 });
42
43 function pvol_remove() {
44 return vgroup.RemoveDevice(block_pvol.path, true, {});
45 }
46
47 function pvol_empty_and_remove() {
48 return (vgroup.EmptyDevice(block_pvol.path, {})
49 .then(function() {
50 vgroup.RemoveDevice(block_pvol.path, true, {});
51 }));
52 }
53
54 if (vgroup) {
55 const pvols = client.vgroups_pvols[vgroup.path] || [];
56 let remove_action = null;
57 let remove_excuse = null;
58
59 if (vgroup.MissingPhysicalVolumes && vgroup.MissingPhysicalVolumes.length > 0) {
60 remove_excuse = _("Volume group is missing physical volumes");
61 } else if (pvols.length === 1) {
62 remove_excuse = _("Last cannot be removed");
63 } else if (block_pvol.FreeSize < block_pvol.Size) {
64 if (block_pvol.Size <= vgroup.FreeSize)
65 remove_action = pvol_empty_and_remove;
66 else
67 remove_excuse = _("Not enough free space");
68 } else {
69 remove_action = pvol_remove;
70 }
71
72 register_crossref({
73 key: vgroup,
74 card: pv_card,
75 actions: [
76 {
77 title: _("Remove"),
78 action: remove_action,
79 excuse: remove_excuse,
80 },
81 ],
82 size: <StorageUsageBar stats={[block_pvol.Size - block_pvol.FreeSize, block_pvol.Size]} short />,
83 });
84 }
85
86 return pv_card;
87 }
88
89 export const LVM2PhysicalVolumeCard = ({ card, backing_block, content_block }) => {
90 const block_pvol = client.blocks_pvol[content_block.path];
91 const vgroup = block_pvol && client.vgroups[block_pvol.VolumeGroup];
92
93 return (
94 <StorageCard card={card}>
95 <CardBody>
96 <DescriptionList className="pf-m-horizontal-on-sm">
97 <StorageDescription title={_("Volume group")}>
98 {vgroup
99 ? <Button variant="link" isInline role="link"
100 onClick={() => cockpit.location.go(["vg", vgroup.Name])}>
101 {vgroup.Name}
102 </Button>
103 : "-"
104 }
105 </StorageDescription>
106 <StorageDescription title={_("UUID")} value={content_block.IdUUID} />
107 { block_pvol &&
108 <StorageDescription title={_("Usage")}>
109 <StorageUsageBar key="s"
110 stats={[block_pvol.Size - block_pvol.FreeSize,
111 block_pvol.Size]} />
112 </StorageDescription>
113 }
114 </DescriptionList>
115 </CardBody>
116 </StorageCard>);
117 };