| 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 { 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 |
import { ExclamationTriangleIcon, ExclamationCircleIcon } from "@patternfly/react-icons"; |
| 14 |
import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 15 |
|
| 16 |
import { StorageButton, StorageLink } from "../storage-controls.jsx"; |
| 17 |
|
| 18 |
import { check_unused_space, get_resize_info, grow_dialog, shrink_dialog } from "../block/resize.jsx"; |
| 19 |
import { StorageCard, StorageDescription, new_card, navigate_to_new_card_location, navigate_away_from_card } from "../pages.jsx"; |
| 20 |
import { block_name, fmt_size, get_active_usage, teardown_active_usage, reload_systemd } from "../utils.js"; |
| 21 |
import { |
| 22 |
dialog_open, TextInput, SelectSpaces, BlockingMessage, TeardownMessage, |
| 23 |
init_teardown_usage |
| 24 |
} from "../dialog.jsx"; |
| 25 |
|
| 26 |
import { lvm2_create_snapshot_action } from "./volume-group.jsx"; |
| 27 |
import { pvs_to_spaces } from "./utils.jsx"; |
| 28 |
|
| 29 |
const _ = cockpit.gettext; |
| 30 |
|
| 31 |
export function lvol_rename(lvol) { |
| 32 |
dialog_open({ |
| 33 |
Title: _("Rename logical volume"), |
| 34 |
Fields: [ |
| 35 |
TextInput("name", _("Name"), |
| 36 |
{ value: lvol.Name }) |
| 37 |
], |
| 38 |
Action: { |
| 39 |
Title: _("Rename"), |
| 40 |
action: function (vals) { |
| 41 |
return lvol.Rename(vals.name, { }); |
| 42 |
} |
| 43 |
} |
| 44 |
}); |
| 45 |
} |
| 46 |
|
| 47 |
export function lvol_delete(lvol, card) { |
| 48 |
const vgroup = client.vgroups[lvol.VolumeGroup]; |
| 49 |
const block = client.lvols_block[lvol.path]; |
| 50 |
const usage = get_active_usage(client, block ? block.path : lvol.path, _("delete")); |
| 51 |
|
| 52 |
if (usage.Blocking) { |
| 53 |
dialog_open({ |
| 54 |
Title: cockpit.format(_("$0 is in use"), lvol.Name), |
| 55 |
Body: BlockingMessage(usage) |
| 56 |
}); |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
dialog_open({ |
| 61 |
Title: cockpit.format(_("Permanently delete logical volume $0/$1?"), vgroup.Name, lvol.Name), |
| 62 |
Teardown: TeardownMessage(usage), |
| 63 |
Action: { |
| 64 |
Danger: _("Deleting a logical volume will delete all data in it."), |
| 65 |
Title: _("Delete"), |
| 66 |
action: async function () { |
| 67 |
await teardown_active_usage(client, usage); |
| 68 |
await lvol.Delete({ 'tear-down': { t: 'b', v: true } }); |
| 69 |
await reload_systemd(); |
| 70 |
navigate_away_from_card(card); |
| 71 |
} |
| 72 |
}, |
| 73 |
Inits: [ |
| 74 |
init_teardown_usage(client, usage) |
| 75 |
] |
| 76 |
}); |
| 77 |
} |
| 78 |
|
| 79 |
function repair(lvol) { |
| 80 |
const vgroup = lvol && client.vgroups[lvol.VolumeGroup]; |
| 81 |
if (!vgroup) |
| 82 |
return; |
| 83 |
|
| 84 |
const summary = client.lvols_stripe_summary[lvol.path]; |
| 85 |
const missing = summary.reduce((sum, sub) => sum + (sub["/"] ?? 0), 0); |
| 86 |
|
| 87 |
function usable(pvol) { |
| 88 |
// must have some free space and not already used for a |
| 89 |
// subvolume other than those that need to be repaired. |
| 90 |
return pvol.FreeSize > 0 && !summary.some(sub => !sub["/"] && sub[pvol.path]); |
| 91 |
} |
| 92 |
|
| 93 |
const pvs_as_spaces = pvs_to_spaces(client, client.vgroups_pvols[vgroup.path].filter(usable)); |
| 94 |
const available = pvs_as_spaces.reduce((sum, spc) => sum + spc.size, 0); |
| 95 |
|
| 96 |
if (available < missing) { |
| 97 |
dialog_open({ |
| 98 |
Title: cockpit.format(_("Unable to repair logical volume $0"), lvol.Name), |
| 99 |
Body: <p>{cockpit.format(_("There is not enough space available that could be used for a repair. At least $0 are needed on physical volumes that are not already used for this logical volume."), |
| 100 |
fmt_size(missing))}</p> |
| 101 |
}); |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
function enough_space(pvs) { |
| 106 |
const selected = pvs.reduce((sum, pv) => sum + pv.size, 0); |
| 107 |
if (selected < missing) |
| 108 |
return cockpit.format(_("An additional $0 must be selected"), fmt_size(missing - selected)); |
| 109 |
} |
| 110 |
|
| 111 |
dialog_open({ |
| 112 |
Title: cockpit.format(_("Repair logical volume $0"), lvol.Name), |
| 113 |
Body: <div><p>{cockpit.format(_("Select the physical volumes that should be used to repair the logical volume. At least $0 are needed."), |
| 114 |
fmt_size(missing))}</p><br /></div>, |
| 115 |
Fields: [ |
| 116 |
SelectSpaces("pvs", _("Physical Volumes"), |
| 117 |
{ |
| 118 |
spaces: pvs_as_spaces, |
| 119 |
validate: enough_space |
| 120 |
}), |
| 121 |
], |
| 122 |
Action: { |
| 123 |
Title: _("Repair"), |
| 124 |
action: function (vals) { |
| 125 |
return lvol.Repair(vals.pvs.map(spc => spc.block.path), { }); |
| 126 |
} |
| 127 |
} |
| 128 |
}); |
| 129 |
} |
| 130 |
|
| 131 |
function deactivate(lvol, block) { |
| 132 |
const vgroup = client.vgroups[lvol.VolumeGroup]; |
| 133 |
const usage = get_active_usage(client, block.path, _("deactivate")); |
| 134 |
|
| 135 |
if (usage.Blocking) { |
| 136 |
dialog_open({ |
| 137 |
Title: cockpit.format(_("$0 is in use"), lvol.Name), |
| 138 |
Body: BlockingMessage(usage) |
| 139 |
}); |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
dialog_open({ |
| 144 |
Title: cockpit.format(_("Deactivate logical volume $0/$1?"), vgroup.Name, lvol.Name), |
| 145 |
Teardown: TeardownMessage(usage, true), |
| 146 |
Action: { |
| 147 |
Title: _("Deactivate"), |
| 148 |
action: async function () { |
| 149 |
await teardown_active_usage(client, usage); |
| 150 |
await lvol.Deactivate({ }); |
| 151 |
await reload_systemd(); |
| 152 |
} |
| 153 |
}, |
| 154 |
Inits: [ |
| 155 |
init_teardown_usage(client, usage, true) |
| 156 |
] |
| 157 |
}); |
| 158 |
} |
| 159 |
|
| 160 |
export function make_block_logical_volume_card(next, vgroup, lvol, block) { |
| 161 |
const unused_space_warning = check_unused_space(block.path); |
| 162 |
const unused_space = !!unused_space_warning; |
| 163 |
const status_code = client.lvols_status[lvol.path]; |
| 164 |
const pool = client.lvols[lvol.ThinPool]; |
| 165 |
|
| 166 |
let { info, shrink_excuse, grow_excuse } = get_resize_info(client, block, unused_space); |
| 167 |
|
| 168 |
if (!unused_space && !grow_excuse && !pool && vgroup.FreeSize == 0) { |
| 169 |
grow_excuse = _("Not enough space to grow"); |
| 170 |
} |
| 171 |
|
| 172 |
let repair_action = null; |
| 173 |
if (status_code == "degraded" || status_code == "degraded-maybe-partial") |
| 174 |
repair_action = { title: _("Repair"), action: () => repair(lvol) }; |
| 175 |
|
| 176 |
const card = new_card({ |
| 177 |
title: _("LVM2 logical volume"), |
| 178 |
next, |
| 179 |
page_location: ["vg", vgroup.Name, lvol.Name], |
| 180 |
page_name: lvol.Name, |
| 181 |
page_size: block.Size, |
| 182 |
for_summary: true, |
| 183 |
has_warning: !!unused_space_warning || !!repair_action, |
| 184 |
has_danger: status_code == "partial", |
| 185 |
job_path: lvol.path, |
| 186 |
component: LVM2LogicalVolumeCard, |
| 187 |
props: { vgroup, lvol, block, unused_space_warning, resize_info: info }, |
| 188 |
actions: [ |
| 189 |
(!unused_space && |
| 190 |
{ |
| 191 |
title: _("Shrink"), |
| 192 |
action: () => shrink_dialog(client, lvol, info), |
| 193 |
excuse: shrink_excuse, |
| 194 |
}), |
| 195 |
(!unused_space && |
| 196 |
{ |
| 197 |
title: _("Grow"), |
| 198 |
action: () => grow_dialog(client, lvol, info), |
| 199 |
excuse: grow_excuse, |
| 200 |
}), |
| 201 |
{ |
| 202 |
title: _("Deactivate"), |
| 203 |
action: () => deactivate(lvol, block), |
| 204 |
}, |
| 205 |
lvm2_create_snapshot_action(lvol), |
| 206 |
repair_action, |
| 207 |
{ |
| 208 |
title: _("Delete"), |
| 209 |
action: () => lvol_delete(lvol, card), |
| 210 |
danger: true, |
| 211 |
}, |
| 212 |
], |
| 213 |
}); |
| 214 |
return card; |
| 215 |
} |
| 216 |
|
| 217 |
const LVM2LogicalVolumeCard = ({ card, vgroup, lvol, block, unused_space_warning, resize_info }) => { |
| 218 |
const unused_space = !!unused_space_warning; |
| 219 |
|
| 220 |
function rename() { |
| 221 |
dialog_open({ |
| 222 |
Title: _("Rename logical volume"), |
| 223 |
Fields: [ |
| 224 |
TextInput("name", _("Name"), |
| 225 |
{ value: lvol.Name }) |
| 226 |
], |
| 227 |
Action: { |
| 228 |
Title: _("Rename"), |
| 229 |
action: async function (vals) { |
| 230 |
await lvol.Rename(vals.name, { }); |
| 231 |
navigate_to_new_card_location(card, ["vg", vgroup.Name, vals.name]); |
| 232 |
} |
| 233 |
} |
| 234 |
}); |
| 235 |
} |
| 236 |
|
| 237 |
function shrink_to_fit() { |
| 238 |
return shrink_dialog(client, lvol, resize_info, true); |
| 239 |
} |
| 240 |
|
| 241 |
function grow_to_fit() { |
| 242 |
return grow_dialog(client, lvol, resize_info, true); |
| 243 |
} |
| 244 |
|
| 245 |
const layout_desc = { |
| 246 |
raid0: _("Striped (RAID 0)"), |
| 247 |
raid1: _("Mirrored (RAID 1)"), |
| 248 |
raid10: _("Striped and mirrored (RAID 10)"), |
| 249 |
raid4: _("Dedicated parity (RAID 4)"), |
| 250 |
raid5: _("Distributed parity (RAID 5)"), |
| 251 |
raid6: _("Double distributed parity (RAID 6)") |
| 252 |
}; |
| 253 |
|
| 254 |
const layout = lvol.Layout; |
| 255 |
|
| 256 |
return ( |
| 257 |
<StorageCard card={card} |
| 258 |
alert={unused_space && |
| 259 |
<Alert variant="warning" |
| 260 |
isInline |
| 261 |
title={_("This logical volume is not completely used by its content.")}> |
| 262 |
{cockpit.format(_("Volume size is $0. Content size is $1."), |
| 263 |
fmt_size(unused_space_warning.volume_size), |
| 264 |
fmt_size(unused_space_warning.content_size))} |
| 265 |
<div className='storage-alert-actions'> |
| 266 |
<StorageButton onClick={shrink_to_fit}>{_("Shrink volume")}</StorageButton> |
| 267 |
<StorageButton onClick={grow_to_fit}>{_("Grow content")}</StorageButton> |
| 268 |
</div> |
| 269 |
</Alert>}> |
| 270 |
<CardBody> |
| 271 |
<DescriptionList className="pf-m-horizontal-on-sm"> |
| 272 |
<StorageDescription title={_("Name")} value={lvol.Name} |
| 273 |
action={<StorageLink onClick={rename}> |
| 274 |
{_("edit")} |
| 275 |
</StorageLink>} /> |
| 276 |
{ !unused_space && |
| 277 |
<StorageDescription title={_("Size")} value={fmt_size(lvol.Size)} /> |
| 278 |
} |
| 279 |
{ (layout && layout != "linear") && |
| 280 |
<StorageDescription title={_("Layout")} value={layout_desc[layout] || layout} /> |
| 281 |
} |
| 282 |
<StructureDescription client={client} lvol={lvol} /> |
| 283 |
</DescriptionList> |
| 284 |
</CardBody> |
| 285 |
</StorageCard>); |
| 286 |
}; |
| 287 |
|
| 288 |
export const StructureDescription = ({ client, lvol }) => { |
| 289 |
const vgroup = client.vgroups[lvol.VolumeGroup]; |
| 290 |
const pvs = (vgroup && client.vgroups_pvols[vgroup.path]) || []; |
| 291 |
|
| 292 |
if (!lvol.Structure || pvs.length <= 1) |
| 293 |
return null; |
| 294 |
|
| 295 |
let status = null; |
| 296 |
const status_code = client.lvols_status[lvol.path]; |
| 297 |
if (status_code == "partial") { |
| 298 |
status = _("This logical volume has lost some of its physical volumes and can no longer be used. You need to delete it and create a new one to take its place."); |
| 299 |
} else if (status_code == "degraded") { |
| 300 |
status = _("This logical volume has lost some of its physical volumes but has not lost any data yet. You should repair it to restore its original redundancy."); |
| 301 |
} else if (status_code == "degraded-maybe-partial") { |
| 302 |
status = _("This logical volume has lost some of its physical volumes but might not have lost any data yet. You might be able to repair it."); |
| 303 |
} |
| 304 |
|
| 305 |
function nice_block_name(block) { |
| 306 |
return block_name(client.blocks[block.CryptoBackingDevice] || block); |
| 307 |
} |
| 308 |
|
| 309 |
function pvs_box(used, block_path) { |
| 310 |
if (block_path != "/") { |
| 311 |
const block = client.blocks[block_path]; |
| 312 |
return <div key={block_path} className="storage-pvs-pv-box"> |
| 313 |
<div className="storage-stripe-pv-box-dev"> |
| 314 |
{block ? nice_block_name(block).replace("/dev/", "") : "???"} |
| 315 |
</div> |
| 316 |
<div>{fmt_size(used)}</div> |
| 317 |
</div>; |
| 318 |
} else { |
| 319 |
return <div key={block_path} className="storage-pvs-pv-box"> |
| 320 |
<div className="storage-pvs-pv-box-dev"> |
| 321 |
{ status_code == "degraded" |
| 322 |
? <ExclamationTriangleIcon className="ct-icon-exclamation-triangle" /> |
| 323 |
: <ExclamationCircleIcon className="ct-icon-times-circle" /> |
| 324 |
} |
| 325 |
</div> |
| 326 |
<div>{fmt_size(used)}</div> |
| 327 |
</div>; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
if (lvol.Layout == "linear") { |
| 332 |
const pvs = client.lvols_stripe_summary[lvol.path]; |
| 333 |
if (!pvs) |
| 334 |
return null; |
| 335 |
|
| 336 |
const stripe = Object.keys(pvs).map((path, i) => |
| 337 |
<FlexItem key={i} className="storage-pvs-box"> |
| 338 |
{pvs_box(pvs[path], path)} |
| 339 |
</FlexItem>); |
| 340 |
|
| 341 |
return ( |
| 342 |
<StorageDescription title={_("Physical volumes")}> |
| 343 |
<Flex spaceItems={{ default: "spaceItemsNone" }} |
| 344 |
alignItems={{ default: "alignItemsStretch" }}> |
| 345 |
{stripe} |
| 346 |
</Flex> |
| 347 |
{status} |
| 348 |
</StorageDescription>); |
| 349 |
} |
| 350 |
|
| 351 |
function stripe_box(used, block_path) { |
| 352 |
if (block_path != "/") { |
| 353 |
const block = client.blocks[block_path]; |
| 354 |
return <div key={block_path} className="storage-stripe-pv-box"> |
| 355 |
<div className="storage-stripe-pv-box-dev"> |
| 356 |
{block ? nice_block_name(block).replace("/dev/", "") : "???"} |
| 357 |
</div> |
| 358 |
<div>{fmt_size(used)}</div> |
| 359 |
</div>; |
| 360 |
} else { |
| 361 |
return <div key={block_path} className="storage-stripe-pv-box"> |
| 362 |
<div className="storage-stripe-pv-box-dev"> |
| 363 |
{ status_code == "degraded" |
| 364 |
? <ExclamationTriangleIcon className="ct-icon-exclamation-triangle" /> |
| 365 |
: <ExclamationCircleIcon className="ct-icon-times-circle" /> |
| 366 |
} |
| 367 |
</div> |
| 368 |
<div>{fmt_size(used)}</div> |
| 369 |
</div>; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
if (lvol.Layout == "mirror" || lvol.Layout.indexOf("raid") == 0) { |
| 374 |
const summary = client.lvols_stripe_summary[lvol.path]; |
| 375 |
if (!summary) |
| 376 |
return null; |
| 377 |
|
| 378 |
const stripes = summary.map((pvs, i) => |
| 379 |
<FlexItem key={i} className="storage-stripe-box"> |
| 380 |
{Object.keys(pvs).map(path => stripe_box(pvs[path], path))} |
| 381 |
</FlexItem>); |
| 382 |
|
| 383 |
return ( |
| 384 |
<StorageDescription title={_("Stripes")}> |
| 385 |
<Flex alignItems={{ default: "alignItemsStretch" }}>{stripes}</Flex> |
| 386 |
{status} |
| 387 |
{lvol.SyncRatio != 1.0 |
| 388 |
? <div>{cockpit.format(_("$0 synchronized"), lvol.SyncRatio * 100 + "%")}</div> |
| 389 |
: null} |
| 390 |
</StorageDescription>); |
| 391 |
} |
| 392 |
|
| 393 |
return null; |
| 394 |
}; |