| 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 { block_short_name, fmt_size, mdraid_name } from "../utils.js"; |
| 17 |
import { std_lock_action } from "../crypto/actions.jsx"; |
| 18 |
|
| 19 |
const _ = cockpit.gettext; |
| 20 |
|
| 21 |
export function make_mdraid_disk_card(next, backing_block, content_block) { |
| 22 |
const mdraid = client.mdraids[content_block.MDRaidMember]; |
| 23 |
const mdraid_block = mdraid && client.mdraids_block[mdraid.path]; |
| 24 |
|
| 25 |
const disk_card = new_card({ |
| 26 |
title: _("MDRAID disk"), |
| 27 |
next, |
| 28 |
location: { |
| 29 |
label: mdraid_block ? block_short_name(mdraid_block) : (mdraid ? mdraid_name(mdraid) : null), |
| 30 |
to: mdraid ? ["mdraid", mdraid.UUID] : null, |
| 31 |
}, |
| 32 |
component: MDRaidDiskCard, |
| 33 |
props: { backing_block, content_block, mdraid }, |
| 34 |
actions: [ |
| 35 |
std_lock_action(backing_block, content_block), |
| 36 |
std_format_action(backing_block, content_block), |
| 37 |
] |
| 38 |
}); |
| 39 |
|
| 40 |
if (mdraid) { |
| 41 |
const members = client.mdraids_members[mdraid.path] || []; |
| 42 |
let n_spares = 0; |
| 43 |
let n_recovering = 0; |
| 44 |
mdraid.ActiveDevices.forEach(function(as) { |
| 45 |
if (as[2].indexOf("spare") >= 0) { |
| 46 |
if (as[1] < 0) |
| 47 |
n_spares += 1; |
| 48 |
else |
| 49 |
n_recovering += 1; |
| 50 |
} |
| 51 |
}); |
| 52 |
|
| 53 |
const active_state = mdraid.ActiveDevices.find(as => as[0] == content_block.path); |
| 54 |
|
| 55 |
const state_text = (state) => { |
| 56 |
return { |
| 57 |
faulty: _("Failed"), |
| 58 |
in_sync: _("In sync"), |
| 59 |
spare: active_state[1] < 0 ? _("Spare") : _("Recovering"), |
| 60 |
write_mostly: _("Write-mostly"), |
| 61 |
blocked: _("Blocked") |
| 62 |
}[state] || cockpit.format(_("Unknown ($0)"), state); |
| 63 |
}; |
| 64 |
|
| 65 |
const slot = active_state && active_state[1] >= 0 && active_state[1].toString(); |
| 66 |
let states = active_state && active_state[2].map(state_text).join(", "); |
| 67 |
|
| 68 |
if (slot) |
| 69 |
states = cockpit.format(_("Slot $0"), slot) + ", " + states; |
| 70 |
|
| 71 |
const is_in_sync = (active_state && active_state[2].indexOf("in_sync") >= 0); |
| 72 |
const is_recovering = (active_state && active_state[2].indexOf("spare") >= 0 && active_state[1] >= 0); |
| 73 |
|
| 74 |
let remove_excuse = null; |
| 75 |
if (!mdraid_block) |
| 76 |
remove_excuse = _("The MDRAID device must be running"); |
| 77 |
else if ((is_in_sync && n_recovering > 0) || is_recovering) |
| 78 |
remove_excuse = _("MDRAID device is recovering"); |
| 79 |
else if (is_in_sync && n_spares < 1) |
| 80 |
remove_excuse = _("Need a spare disk"); |
| 81 |
else if (members.length <= 1) |
| 82 |
remove_excuse = _("Last disk can not be removed"); |
| 83 |
|
| 84 |
let remove_action = null; |
| 85 |
if (mdraid.Level != "raid0") |
| 86 |
remove_action = { |
| 87 |
title: _("Remove"), |
| 88 |
action: () => mdraid.RemoveDevice(content_block.path, { wipe: { t: 'b', v: true } }), |
| 89 |
excuse: remove_excuse |
| 90 |
}; |
| 91 |
|
| 92 |
register_crossref({ |
| 93 |
key: mdraid, |
| 94 |
card: disk_card, |
| 95 |
actions: [ |
| 96 |
remove_action |
| 97 |
], |
| 98 |
size: fmt_size(content_block.Size), |
| 99 |
extra: states, |
| 100 |
}); |
| 101 |
} |
| 102 |
|
| 103 |
return disk_card; |
| 104 |
} |
| 105 |
|
| 106 |
export const MDRaidDiskCard = ({ card, backing_block, content_block, mdraid }) => { |
| 107 |
return ( |
| 108 |
<StorageCard card={card}> |
| 109 |
<CardBody> |
| 110 |
<DescriptionList className="pf-m-horizontal-on-sm"> |
| 111 |
<StorageDescription title={_("MDRAID device")}> |
| 112 |
{mdraid |
| 113 |
? <Button variant="link" isInline role="link" |
| 114 |
onClick={() => cockpit.location.go(["mdraid", mdraid.UUID])}> |
| 115 |
{mdraid_name(mdraid)} |
| 116 |
</Button> |
| 117 |
: "-" |
| 118 |
} |
| 119 |
</StorageDescription> |
| 120 |
</DescriptionList> |
| 121 |
</CardBody> |
| 122 |
</StorageCard> |
| 123 |
); |
| 124 |
}; |