Debian Codegraph @

pkg/storaged/mdraid/mdraid.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 { CardHeader, 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 { VolumeIcon } from "../icons/gnome-icons.jsx";
15 import { StorageButton, StorageLink } from "../storage-controls.jsx";
16 import {
17 StorageCard, StorageDescription, PageTable,
18 new_page, new_card, PAGE_CATEGORY_VIRTUAL,
19 get_crossrefs, navigate_away_from_card
20 } from "../pages.jsx";
21 import { make_block_page } from "../block/create-pages.jsx";
22 import {
23 block_short_name, mdraid_name, encode_filename, decode_filename,
24 fmt_size, fmt_size_long, get_active_usage, teardown_active_usage,
25 get_available_spaces, prepare_available_spaces,
26 reload_systemd, should_ignore,
27 } from "../utils.js";
28
29 import {
30 dialog_open, SelectSpaces,
31 BlockingMessage, TeardownMessage,
32 init_teardown_usage
33 } from "../dialog.jsx";
34
35 import { partitionable_block_actions } from "../partitions/actions.jsx";
36
37 const _ = cockpit.gettext;
38
39 function mdraid_start(mdraid) {
40 return mdraid.Start({ "start-degraded": { t: 'b', v: true } });
41 }
42
43 function mdraid_stop(mdraid) {
44 const block = client.mdraids_block[mdraid.path];
45 const usage = get_active_usage(client, block ? block.path : "", _("stop"));
46
47 if (usage.Blocking) {
48 dialog_open({
49 Title: cockpit.format(_("$0 is in use"), mdraid_name(mdraid)),
50 Body: BlockingMessage(usage),
51 });
52 return;
53 }
54
55 if (usage.Teardown) {
56 dialog_open({
57 Title: cockpit.format(_("Confirm stopping of $0"),
58 mdraid_name(mdraid)),
59 Teardown: TeardownMessage(usage),
60 Action: {
61 Title: _("Stop device"),
62 action: function () {
63 return teardown_active_usage(client, usage)
64 .then(function () {
65 return mdraid.Stop({});
66 });
67 }
68 },
69 Inits: [
70 init_teardown_usage(client, usage)
71 ]
72 });
73 return;
74 }
75
76 return mdraid.Stop({});
77 }
78
79 function mdraid_delete(mdraid, block, card) {
80 async function delete_() {
81 const members = client.mdraids_members[mdraid.path];
82
83 await mdraid.Delete({ 'tear-down': { t: 'b', v: true } });
84
85 // HACK - https://github.com/storaged-project/udisks/issues/1375
86 //
87 // Now wipe the members again. This is necessary when
88 // deleting a MDRAID with metadata 1.0, which stores its
89 // superblock at the end of each member, and the superblock of
90 // the actual content of the mdraid is stored at the very
91 // start of each member (for certain raid levels like a
92 // mirror). The Delete call above will only remove the mdraid
93 // metadata and leave the content superblock in place. Thus,
94 // after deleting, the members might magically turn into the
95 // previous content. Wiping the members again will remove any
96 // such metadata that might cause them to be misidentified.
97
98 for (const b of members) {
99 await b.Format('empty', { });
100 }
101
102 await reload_systemd();
103 }
104
105 const usage = get_active_usage(client, block ? block.path : "", _("delete"));
106
107 if (usage.Blocking) {
108 dialog_open({
109 Title: cockpit.format(_("$0 is in use"), mdraid_name(mdraid)),
110 Body: BlockingMessage(usage)
111 });
112 return;
113 }
114
115 dialog_open({
116 Title: cockpit.format(_("Permanently delete $0?"), mdraid_name(mdraid)),
117 Teardown: TeardownMessage(usage),
118 Action: {
119 Title: _("Delete"),
120 Danger: _("Deleting erases all data on a MDRAID device."),
121 action: async function () {
122 await teardown_active_usage(client, usage);
123 await delete_();
124 navigate_away_from_card(card);
125 }
126 },
127 Inits: [
128 init_teardown_usage(client, usage)
129 ]
130 });
131 }
132
133 function add_disk(mdraid) {
134 function filter_inside_mdraid(spc) {
135 let block = spc.block;
136 if (client.blocks_part[block.path])
137 block = client.blocks[client.blocks_part[block.path].Table];
138 return block && block.MDRaid != mdraid.path;
139 }
140
141 function rescan(path) {
142 // mdraid often forgets to trigger udev, let's do it explicitly
143 return client.wait_for(() => client.blocks[path]).then(block => block.Rescan({ }));
144 }
145
146 dialog_open({
147 Title: _("Add disks"),
148 Fields: [
149 SelectSpaces("disks", _("Disks"),
150 {
151 empty_warning: _("No disks are available."),
152 validate: function (disks) {
153 if (disks.length === 0)
154 return _("At least one disk is needed.");
155 },
156 spaces: get_available_spaces().filter(filter_inside_mdraid)
157 })
158 ],
159 Action: {
160 Title: _("Add"),
161 action: function(vals) {
162 return prepare_available_spaces(client, vals.disks).then(paths =>
163 Promise.all(paths.map(p => mdraid.AddDevice(p, {}).then(() => rescan(p)))));
164 }
165 }
166 });
167 }
168
169 function missing_bitmap(mdraid) {
170 let policy;
171 if (mdraid.ConsistencyPolicy)
172 policy = mdraid.ConsistencyPolicy;
173 else if (mdraid.ActiveDevices.some(a => a[2].indexOf("journal") >= 0))
174 policy = "journal";
175 else if (mdraid.BitmapLocation && decode_filename(mdraid.BitmapLocation) != "none")
176 policy = "bitmap";
177 else
178 policy = "resync";
179
180 return (mdraid.Level != "raid0" &&
181 client.mdraids_members[mdraid.path].some(m => m.Size > 100 * 1024 * 1024 * 1024) &&
182 policy == "resync");
183 }
184
185 export function make_mdraid_page(parent, mdraid) {
186 const block = client.mdraids_block[mdraid.path];
187
188 if (should_ignore(client, mdraid.path))
189 return;
190
191 let add_excuse = false;
192 if (!block)
193 add_excuse = _("MDRAID device must be running");
194
195 const mdraid_card = new_card({
196 title: _("MDRAID device"),
197 next: null,
198 page_location: ["mdraid", mdraid.UUID],
199 page_name: block ? block_short_name(block) : mdraid_name(mdraid),
200 page_icon: VolumeIcon,
201 page_category: PAGE_CATEGORY_VIRTUAL,
202 page_size: mdraid.Size,
203 type_extra: !block && _("stopped"),
204 id_extra: block && _("MDRAID device"),
205 for_summary: true,
206 has_warning: mdraid.Degraded > 0 || missing_bitmap(mdraid),
207 job_path: mdraid.path,
208 component: MDRaidCard,
209 props: { mdraid, block },
210 actions: [
211 (!block &&
212 {
213 title: _("Start"),
214 action: () => mdraid_start(mdraid),
215 tag: "device"
216 }),
217 (mdraid.Level != "raid0" &&
218 {
219 title: _("Add disk"),
220 action: () => add_disk(mdraid),
221 excuse: add_excuse,
222 tag: "disks",
223 }),
224 ...(block ? partitionable_block_actions(block, "device") : []),
225 {
226 title: _("Delete"),
227 action: () => mdraid_delete(mdraid, block, mdraid_card),
228 danger: true,
229 },
230 ],
231 });
232
233 if (!block) {
234 new_page(parent, mdraid_card);
235 } else
236 make_block_page(parent, block, mdraid_card, { partitionable: true });
237 }
238
239 const MDRaidCard = ({ card, mdraid, block }) => {
240 function format_level(str) {
241 return {
242 raid0: _("RAID 0"),
243 raid1: _("RAID 1"),
244 raid4: _("RAID 4"),
245 raid5: _("RAID 5"),
246 raid6: _("RAID 6"),
247 raid10: _("RAID 10")
248 }[str] || cockpit.format(_("RAID ($0)"), str);
249 }
250
251 let level = format_level(mdraid.Level);
252 if (mdraid.NumDevices > 0)
253 level += ", " + cockpit.format(_("$0 disks"), mdraid.NumDevices);
254 if (mdraid.ChunkSize > 0)
255 level += ", " + cockpit.format(_("$0 chunk size"), fmt_size(mdraid.ChunkSize));
256
257 const alerts = [];
258 if (mdraid.Degraded > 0) {
259 const text = cockpit.format(
260 cockpit.ngettext("$0 disk is missing", "$0 disks are missing", mdraid.Degraded),
261 mdraid.Degraded
262 );
263 alerts.push(
264 <Alert isInline variant="danger" key="degraded"
265 title={_("The MDRAID device is in a degraded state")}>
266 {text}
267 </Alert>);
268 }
269
270 function fix_bitmap() {
271 return mdraid.SetBitmapLocation(encode_filename("internal"), { });
272 }
273
274 if (missing_bitmap(mdraid)) {
275 alerts.push(
276 <Alert isInline variant="warning" key="bitmap"
277 title={_("This MDRAID device has no write-intent bitmap. Such a bitmap can reduce synchronization times significantly.")}>
278 <div className="storage-alert-actions">
279 <StorageButton onClick={fix_bitmap}>{_("Add a bitmap")}</StorageButton>
280 </div>
281 </Alert>);
282 }
283
284 return (
285 <StorageCard card={card} alerts={alerts}>
286 <CardBody>
287 <DescriptionList className="pf-m-horizontal-on-sm">
288 <StorageDescription title={_("Name")} value={mdraid_name(mdraid)} />
289 <StorageDescription title={_("RAID level")} value={level} />
290 <StorageDescription title={_("State")} value={block ? _("Running") : _("Not running")}
291 action={block && <StorageLink onClick={() => mdraid_stop(mdraid)}>
292 {_("Stop")}
293 </StorageLink>} />
294 <StorageDescription title={_("UUID")} value={mdraid.UUID} />
295 <StorageDescription title={_("Device")} value={block ? decode_filename(block.PreferredDevice) : "-"} />
296 <StorageDescription title={_("Capacity")} value={fmt_size_long(mdraid.Size)} />
297 </DescriptionList>
298 </CardBody>
299 <CardHeader><strong>{_("Disks")}</strong></CardHeader>
300 <PageTable
301 emptyCaption={_("No disks found")}
302 aria-label={_("MDRAID disks")}
303 crossrefs={get_crossrefs(mdraid)} />
304 </StorageCard>
305 );
306 };