Debian Codegraph @

pkg/storaged/btrfs/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 { CardHeader, CardBody } from "@patternfly/react-core/dist/esm/components/Card/index.js";
11 import { DescriptionList } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js";
12 import { VolumeIcon } from "../icons/gnome-icons.jsx";
13
14 import {
15 new_card, new_page, PAGE_CATEGORY_VIRTUAL,
16 get_crossrefs, ChildrenTable, PageTable, StorageCard, StorageDescription,
17 navigate_away_from_card
18 } from "../pages.jsx";
19 import { StorageUsageBar, StorageLink } from "../storage-controls.jsx";
20 import {
21 fmt_size_long, validate_fsys_label, should_ignore,
22 get_active_usage, teardown_active_usage,
23 reload_systemd,
24 } from "../utils.js";
25 import { btrfs_usage } from "./utils.jsx";
26 import {
27 dialog_open, TextInput,
28 BlockingMessage, TeardownMessage,
29 init_teardown_usage
30 } from "../dialog.jsx";
31 import { make_btrfs_subvolume_pages } from "./subvolume.jsx";
32 import { btrfs_device_actions } from "./device.jsx";
33
34 const _ = cockpit.gettext;
35
36 /*
37 * Udisks is a disk/block library so it manages that, btrfs turns this a bit
38 * around and has one "volume" which can have multiple blocks by a unique uuid.
39 *
40 * Cockpit shows Btrfs as following:
41 *
42 * -> btrfs subvolume
43 * -> btrfs volume
44 * -> btrfs device
45 * -> block device
46 */
47
48 async function btrfs_delete(uuid, card) {
49 const volume = client.uuids_btrfs_volume[uuid];
50 const name = volume.data.label || uuid;
51
52 const usage = get_active_usage(client, volume.path, _("delete"), undefined, false, undefined, true);
53
54 if (usage.Blocking) {
55 dialog_open({
56 Title: cockpit.format(_("$0 is in use"), name),
57 Body: BlockingMessage(usage)
58 });
59 return;
60 }
61
62 dialog_open({
63 Title: cockpit.format(_("Permanently delete $0?"), name),
64 Teardown: TeardownMessage(usage),
65 Action: {
66 Danger: _("Deleting erases all data on a btrfs volume."),
67 Title: _("Delete"),
68 disable_on_error: usage.Teardown,
69 action: async function () {
70 await teardown_active_usage(client, usage);
71 const blocks = client.uuids_btrfs_blocks[uuid];
72 for (let i = 0; i < blocks.length; i++) {
73 // All block devices share the same Configuration
74 // property, so we only tear down the first.
75 await blocks[i].Format("empty", { 'tear-down': { t: 'b', v: i == 0 } });
76 }
77 await reload_systemd();
78 navigate_away_from_card(card);
79 }
80 },
81 Inits: [
82 init_teardown_usage(client, usage)
83 ]
84 });
85 }
86
87 export function make_btrfs_volume_page(parent, uuid) {
88 const block_devices = client.uuids_btrfs_blocks[uuid];
89 const block_btrfs = client.blocks_fsys_btrfs[block_devices[0].path];
90 const volume = client.uuids_btrfs_volume[uuid];
91 const use = btrfs_usage(client, volume);
92
93 if (block_devices.some(blk => should_ignore(client, blk.path)))
94 return;
95
96 // Single-device btrfs volumes are shown directly on the page of
97 // their device; they don't get a standalone "btrfs volume" page.
98 if (block_btrfs.data.num_devices === 1)
99 return;
100
101 const name = block_btrfs.data.label || uuid;
102 const btrfs_volume_card = new_card({
103 title: _("btrfs volume"),
104 next: null,
105 page_location: ["btrfs-volume", uuid],
106 page_name: name,
107 page_icon: VolumeIcon,
108 page_category: PAGE_CATEGORY_VIRTUAL,
109 page_size: use[1],
110 component: BtrfsVolumeCard,
111 props: { block_devices, uuid, use },
112 actions: [
113 {
114 title: _("Delete filesystem"),
115 action: () => btrfs_delete(uuid, btrfs_volume_card),
116 danger: true,
117 },
118 ],
119 });
120
121 const subvolumes_card = make_btrfs_subvolumes_card(btrfs_volume_card, null, null);
122
123 const subvolumes_page = new_page(parent, subvolumes_card);
124 make_btrfs_subvolume_pages(subvolumes_page, volume);
125 }
126
127 function rename_dialog(block_btrfs, label, rw_mount_point) {
128 dialog_open({
129 Title: _("Change label"),
130 Fields: [
131 TextInput("name", _("Name"),
132 {
133 validate: name => validate_fsys_label(name, "btrfs"),
134 value: label
135 })
136 ],
137 Action: {
138 Title: _("Save"),
139 action: async function (vals) {
140 if (rw_mount_point) {
141 await cockpit.spawn(["btrfs", "filesystem", "label", rw_mount_point, vals.name],
142 { superuser: true });
143 const block = client.blocks[block_btrfs.path];
144 await block.Rescan({});
145 } else
146 await block_btrfs.SetLabel(vals.name, {});
147 }
148 }
149 });
150 }
151
152 export const BtrfsLabelDescription = ({ block_btrfs }) => {
153 const label = block_btrfs.data.label || "-";
154
155 // We can change the label when at least one filesystem subvolume
156 // is mounted rw, or when nothing is mounted.
157
158 let rw_mount_point = null;
159 let is_mounted = false;
160 const mount_points = client.btrfs_mounts[block_btrfs.data.uuid];
161 for (const id in mount_points) {
162 const mp = mount_points[id];
163 if (mp.mount_points.length > 0)
164 is_mounted = true;
165 if (mp.rw_mount_points.length > 0 && !rw_mount_point)
166 rw_mount_point = mp.rw_mount_points[0];
167 }
168
169 let excuse = null;
170 if (is_mounted && !rw_mount_point)
171 excuse = _("Filesystem is mounted read-only");
172
173 return <StorageDescription title={_("Label")}
174 value={label}
175 action={
176 <StorageLink onClick={() => rename_dialog(block_btrfs, label, rw_mount_point)}
177 excuse={excuse}>
178 {_("edit")}
179 </StorageLink>}
180 />;
181 };
182
183 const BtrfsVolumeCard = ({ card, block_devices, uuid, use }) => {
184 const block_btrfs = client.blocks_fsys_btrfs[block_devices[0].path];
185
186 return (
187 <StorageCard card={card}>
188 <CardBody>
189 <DescriptionList className="pf-m-horizontal-on-sm">
190 <BtrfsLabelDescription block_btrfs={block_btrfs} />
191 <StorageDescription title={_("UUID")} value={uuid} />
192 <StorageDescription title={_("Capacity")} value={fmt_size_long(use[1])} />
193 <StorageDescription title={_("Usage")}>
194 <StorageUsageBar key="s" stats={use} />
195 </StorageDescription>
196 </DescriptionList>
197 </CardBody>
198 <CardHeader><strong>{_("btrfs devices")}</strong></CardHeader>
199 <PageTable
200 emptyCaption={_("No devices found")}
201 aria-label={_("btrfs device")}
202 crossrefs={get_crossrefs(uuid)} />
203 </StorageCard>
204 );
205 };
206
207 export function make_btrfs_subvolumes_card(next, block, backing_block) {
208 return new_card({
209 title: _("btrfs subvolumes"),
210 next,
211 actions: btrfs_device_actions(block, backing_block),
212 component: BtrfsSubVolumesCard,
213 });
214 }
215
216 const BtrfsSubVolumesCard = ({ card }) => {
217 return (
218 <StorageCard card={card}>
219 <ChildrenTable
220 emptyCaption={_("No subvolumes")}
221 aria-label={_("btrfs subvolumes")}
222 page={card.page} />
223 </StorageCard>
224 );
225 };