Debian Codegraph @

pkg/storaged/legacy-vdo/legacy-vdo.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 { Alert } from "@patternfly/react-core/dist/esm/components/Alert/index.js";
11 import { Card, CardBody } from '@patternfly/react-core/dist/esm/components/Card/index.js';
12 import { DescriptionList, DescriptionListDescription, DescriptionListGroup, DescriptionListTerm } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js";
13
14 import { block_short_name, get_active_usage, teardown_active_usage, fmt_size, decode_filename, reload_systemd } from "../utils.js";
15 import {
16 dialog_open, SizeSlider, BlockingMessage, TeardownMessage, init_teardown_usage
17 } from "../dialog.jsx";
18 import { StorageButton, StorageOnOff } from "../storage-controls.jsx";
19
20 import { StorageCard, new_page, new_card } from "../pages.jsx";
21 import { make_block_page } from "../block/create-pages.jsx";
22
23 import inotify_py from "inotify.py";
24 import vdo_monitor_py from "./vdo-monitor.py";
25
26 const _ = cockpit.gettext;
27
28 export function make_legacy_vdo_page(parent, vdo, backing_block, next_card) {
29 const block = client.slashdevs_block[vdo.dev];
30
31 function stop() {
32 const usage = get_active_usage(client, block ? block.path : "/", _("stop"));
33
34 if (usage.Blocking) {
35 dialog_open({
36 Title: cockpit.format(_("$0 is in use"), vdo.name),
37 Body: BlockingMessage(usage),
38 });
39 return;
40 }
41
42 if (usage.Teardown) {
43 dialog_open({
44 Title: cockpit.format(_("Confirm stopping of $0"),
45 vdo.name),
46 Teardown: TeardownMessage(usage),
47 Action: {
48 Title: _("Stop"),
49 action: function () {
50 return teardown_active_usage(client, usage)
51 .then(function () {
52 return vdo.stop();
53 });
54 }
55 },
56 Inits: [
57 init_teardown_usage(client, usage)
58 ]
59 });
60 } else {
61 return vdo.stop();
62 }
63 }
64
65 function delete_() {
66 const usage = get_active_usage(client, block ? block.path : "/", _("delete"));
67
68 if (usage.Blocking) {
69 dialog_open({
70 Title: cockpit.format(_("$0 is in use"), vdo.name),
71 Body: BlockingMessage(usage),
72 });
73 return;
74 }
75
76 function wipe_with_teardown(block) {
77 return block.Format("empty", { 'tear-down': { t: 'b', v: true } }).then(reload_systemd);
78 }
79
80 function teardown_configs() {
81 if (block) {
82 return wipe_with_teardown(block);
83 } else {
84 return vdo.start()
85 .then(function () {
86 return client.wait_for(() => client.slashdevs_block[vdo.dev])
87 .then(function (block) {
88 return wipe_with_teardown(block)
89 .catch(error => {
90 // systemd might have mounted it, let's try unmounting
91 const block_fsys = client.blocks_fsys[block.path];
92 if (block_fsys) {
93 return block_fsys.Unmount({})
94 .then(() => wipe_with_teardown(block));
95 } else {
96 return Promise.reject(error);
97 }
98 });
99 });
100 });
101 }
102 }
103
104 dialog_open({
105 Title: cockpit.format(_("Permanently delete $0?"), vdo.name),
106 Body: TeardownMessage(usage),
107 Action: {
108 Title: _("Delete"),
109 Danger: _("Deleting erases all data on a VDO device."),
110 action: function () {
111 return (teardown_active_usage(client, usage)
112 .then(teardown_configs)
113 .then(function () {
114 return vdo.remove();
115 }));
116 }
117 },
118 Inits: [
119 init_teardown_usage(client, usage)
120 ]
121 });
122 }
123
124 const vdo_card = new_card({
125 title: cockpit.format(_("VDO device $0"), vdo.name),
126 next: next_card,
127 page_location: ["vdo", vdo.name],
128 page_name: block_short_name(backing_block),
129 page_size: vdo.logical_size,
130 job_path: backing_block.path,
131 component: VDODetails,
132 props: { client, vdo },
133 actions: [
134 (block
135 ? { title: _("Stop"), action: stop }
136 : { title: _("Start"), action: () => vdo.start() }
137 ),
138 { title: _("Delete"), action: delete_, danger: true }
139 ],
140 });
141
142 if (block) {
143 make_block_page(parent, block, vdo_card);
144 } else {
145 new_page(parent, vdo_card);
146 }
147 }
148
149 class VDODetails extends React.Component {
150 constructor() {
151 super();
152 this.poll_path = null;
153 this.state = { stats: null };
154 }
155
156 ensure_polling(enable) {
157 const client = this.props.client;
158 const vdo = this.props.vdo;
159 const block = client.slashdevs_block[vdo.dev];
160 const path = enable && block ? vdo.dev : null;
161
162 let buf = "";
163
164 if (this.poll_path === path)
165 return;
166
167 if (this.poll_path) {
168 this.poll_process.close();
169 this.setState({ stats: null });
170 }
171
172 if (path)
173 this.poll_process = cockpit.spawn([client.legacy_vdo_overlay.python, "--", "-", path], { superuser: "require" })
174 .input(inotify_py + vdo_monitor_py)
175 .stream((data) => {
176 buf += data;
177 const lines = buf.split("\n");
178 buf = lines[lines.length - 1];
179 if (lines.length >= 2) {
180 this.setState({ stats: JSON.parse(lines[lines.length - 2]) });
181 }
182 });
183 this.poll_path = path;
184 }
185
186 componentDidMount() {
187 this.ensure_polling(true);
188 }
189
190 componentDidUpdate() {
191 this.ensure_polling(true);
192 }
193
194 componentWillUnmount() {
195 this.ensure_polling(false);
196 }
197
198 render() {
199 const client = this.props.client;
200 const vdo = this.props.vdo;
201 const block = client.slashdevs_block[vdo.dev];
202 const backing_block = client.slashdevs_block[vdo.backing_dev];
203
204 function force_delete() {
205 return vdo.force_remove();
206 }
207
208 if (vdo.broken) {
209 return (
210 <Card isPlain>
211 <Alert variant='danger' isInline
212 title={_("The creation of this VDO device did not finish and the device can't be used.")}
213 actionClose={<StorageButton onClick={force_delete}>
214 {_("Remove device")}
215 </StorageButton>} />
216 </Card>
217 );
218 }
219
220 const alerts = [];
221 if (backing_block && backing_block.Size > vdo.physical_size)
222 alerts.push(
223 <Alert variant='warning' isInline key="unused"
224 actionClose={<StorageButton onClick={vdo.grow_physical}>{_("Grow to take all space")}</StorageButton>}
225 title={_("This VDO device does not use all of its backing device.")}>
226 { cockpit.format(_("Only $0 of $1 are used."),
227 fmt_size(vdo.physical_size),
228 fmt_size(backing_block.Size))}
229 </Alert>);
230
231 function grow_logical() {
232 dialog_open({
233 Title: cockpit.format(_("Grow logical size of $0"), vdo.name),
234 Fields: [
235 SizeSlider("lsize", _("Logical size"),
236 {
237 max: 5 * vdo.logical_size,
238 min: vdo.logical_size,
239 round: 512,
240 value: vdo.logical_size,
241 allow_infinite: true
242 })
243 ],
244 Action: {
245 Title: _("Grow"),
246 action: function (vals) {
247 if (vals.lsize > vdo.logical_size)
248 return vdo.grow_logical(vals.lsize).then(() => {
249 if (block && block.IdUsage == "filesystem")
250 return cockpit.spawn(["fsadm", "resize",
251 decode_filename(block.Device)],
252 { superuser: "require", err: "message" });
253 });
254 }
255 }
256 });
257 }
258
259 function fmt_perc(num) {
260 if (num || num == 0)
261 return num + "%";
262 else
263 return "--";
264 }
265
266 const stats = this.state.stats;
267
268 const header = (
269 <StorageCard card={this.props.card} alerts={alerts}>
270 <CardBody>
271 <DescriptionList className="pf-m-horizontal-on-sm">
272 <DescriptionListGroup>
273 <DescriptionListTerm>{_("Device file")}</DescriptionListTerm>
274 <DescriptionListDescription>{vdo.dev}</DescriptionListDescription>
275 </DescriptionListGroup>
276
277 <DescriptionListGroup>
278 <DescriptionListTerm>{_("Physical")}</DescriptionListTerm>
279 <DescriptionListDescription>
280 { stats
281 ? cockpit.format(_("$0 data + $1 overhead used of $2 ($3)"),
282 fmt_size(stats.dataBlocksUsed * stats.blockSize),
283 fmt_size(stats.overheadBlocksUsed * stats.blockSize),
284 fmt_size(vdo.physical_size),
285 fmt_perc(stats.usedPercent))
286 : fmt_size(vdo.physical_size)
287 }
288 </DescriptionListDescription>
289 </DescriptionListGroup>
290
291 <DescriptionListGroup>
292 <DescriptionListTerm>{_("Logical")}</DescriptionListTerm>
293 <DescriptionListDescription>
294 { stats
295 ? cockpit.format(_("$0 used of $1 ($2 saved)"),
296 fmt_size(stats.logicalBlocksUsed * stats.blockSize),
297 fmt_size(vdo.logical_size),
298 fmt_perc(stats.savingPercent))
299 : fmt_size(vdo.logical_size)
300 }
301 &nbsp; <StorageButton onClick={grow_logical}>{_("Grow")}</StorageButton>
302 </DescriptionListDescription>
303 </DescriptionListGroup>
304
305 <DescriptionListGroup>
306 <DescriptionListTerm>{_("Index memory")}</DescriptionListTerm>
307 <DescriptionListDescription>{fmt_size(vdo.index_mem * 1024 * 1024 * 1024)}</DescriptionListDescription>
308 </DescriptionListGroup>
309
310 <DescriptionListGroup>
311 <DescriptionListTerm>{_("Compression")}</DescriptionListTerm>
312 <DescriptionListDescription>
313 <StorageOnOff state={vdo.compression}
314 aria-label={_("Use compression")}
315 onChange={() => vdo.set_compression(!vdo.compression)} />
316 </DescriptionListDescription>
317 </DescriptionListGroup>
318
319 <DescriptionListGroup>
320 <DescriptionListTerm>{_("Deduplication")}</DescriptionListTerm>
321 <DescriptionListDescription>
322 <StorageOnOff state={vdo.deduplication}
323 aria-label={_("Use deduplication")}
324 onChange={() => vdo.set_deduplication(!vdo.deduplication)} />
325 </DescriptionListDescription>
326 </DescriptionListGroup>
327 </DescriptionList>
328 </CardBody>
329 </StorageCard>
330 );
331
332 return header;
333 }
334 }