| 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, ChildrenTable, PageTable, |
| 18 |
new_page, new_card, get_crossrefs, PAGE_CATEGORY_VIRTUAL, |
| 19 |
navigate_to_new_card_location, navigate_away_from_card |
| 20 |
} from "../pages.jsx"; |
| 21 |
import { |
| 22 |
fmt_size_long, get_active_usage, teardown_active_usage, for_each_async, |
| 23 |
validate_lvm2_name, |
| 24 |
get_available_spaces, prepare_available_spaces, |
| 25 |
reload_systemd, should_ignore, |
| 26 |
contains_rootfs, |
| 27 |
} from "../utils.js"; |
| 28 |
|
| 29 |
import { |
| 30 |
dialog_open, SelectSpaces, TextInput, |
| 31 |
BlockingMessage, TeardownMessage, |
| 32 |
init_teardown_usage |
| 33 |
} from "../dialog.jsx"; |
| 34 |
|
| 35 |
import { create_logical_volume } from "./create-logical-volume-dialog.jsx"; |
| 36 |
import { make_block_logical_volume_card } from "./block-logical-volume.jsx"; |
| 37 |
import { make_vdo_pool_card } from "./vdo-pool.jsx"; |
| 38 |
import { make_thin_pool_logical_volume_page } from "./thin-pool-logical-volume.jsx"; |
| 39 |
import { make_inactive_logical_volume_page } from "./inactive-logical-volume.jsx"; |
| 40 |
import { make_unsupported_logical_volume_page } from "./unsupported-logical-volume.jsx"; |
| 41 |
import { make_block_page } from "../block/create-pages.jsx"; |
| 42 |
|
| 43 |
const _ = cockpit.gettext; |
| 44 |
|
| 45 |
function vgroup_rename(client, vgroup, card) { |
| 46 |
dialog_open({ |
| 47 |
Title: _("Rename volume group"), |
| 48 |
Fields: [ |
| 49 |
TextInput("name", _("Name"), |
| 50 |
{ |
| 51 |
value: vgroup.Name, |
| 52 |
validate: validate_lvm2_name |
| 53 |
}) |
| 54 |
], |
| 55 |
Action: { |
| 56 |
Title: _("Rename"), |
| 57 |
Danger: contains_rootfs(client, vgroup.path) |
| 58 |
? _("This volume group contains the root filesystem. Renaming it might require further changes to the bootloader configuration or kernel command line.") |
| 59 |
: null, |
| 60 |
action: async function (vals) { |
| 61 |
await vgroup.Rename(vals.name, { }); |
| 62 |
navigate_to_new_card_location(card, ["vg", vals.name]); |
| 63 |
} |
| 64 |
} |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
function vgroup_delete(client, vgroup, card) { |
| 69 |
const usage = get_active_usage(client, vgroup.path, _("delete")); |
| 70 |
|
| 71 |
if (usage.Blocking) { |
| 72 |
dialog_open({ |
| 73 |
Title: cockpit.format(_("$0 is in use"), |
| 74 |
vgroup.Name), |
| 75 |
Body: BlockingMessage(usage) |
| 76 |
}); |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
dialog_open({ |
| 81 |
Title: cockpit.format(_("Permanently delete $0?"), vgroup.Name), |
| 82 |
Teardown: TeardownMessage(usage), |
| 83 |
Action: { |
| 84 |
Danger: _("Deleting erases all data on a volume group."), |
| 85 |
Title: _("Delete"), |
| 86 |
disable_on_error: usage.Teardown, |
| 87 |
action: async function () { |
| 88 |
await teardown_active_usage(client, usage); |
| 89 |
await vgroup.Delete(true, { 'tear-down': { t: 'b', v: true } }); |
| 90 |
await reload_systemd(); |
| 91 |
navigate_away_from_card(card); |
| 92 |
} |
| 93 |
}, |
| 94 |
Inits: [ |
| 95 |
init_teardown_usage(client, usage) |
| 96 |
] |
| 97 |
}); |
| 98 |
} |
| 99 |
|
| 100 |
function create_snapshot(lvol) { |
| 101 |
dialog_open({ |
| 102 |
Title: _("Create snapshot"), |
| 103 |
Fields: [ |
| 104 |
TextInput("name", _("Name"), |
| 105 |
{ validate: validate_lvm2_name }), |
| 106 |
], |
| 107 |
Action: { |
| 108 |
Title: _("Create"), |
| 109 |
action: function (vals) { |
| 110 |
return lvol.CreateSnapshot(vals.name, vals.size || 0, { }); |
| 111 |
} |
| 112 |
} |
| 113 |
}); |
| 114 |
} |
| 115 |
|
| 116 |
export function lvm2_create_snapshot_action(lvol) { |
| 117 |
if (!client.lvols[lvol.ThinPool]) |
| 118 |
return null; |
| 119 |
|
| 120 |
return { title: _("Create snapshot"), action: () => create_snapshot(lvol) }; |
| 121 |
} |
| 122 |
|
| 123 |
function make_generic_logical_volume_card(next, vgroup, lvol) { |
| 124 |
let result = next; |
| 125 |
if (client.vdo_vols[lvol.path]) |
| 126 |
result = make_vdo_pool_card(result, vgroup, lvol); |
| 127 |
return result; |
| 128 |
} |
| 129 |
|
| 130 |
export function make_lvm2_logical_volume_page(parent, vgroup, lvol) { |
| 131 |
const generic_card = make_generic_logical_volume_card(null, vgroup, lvol); |
| 132 |
|
| 133 |
if (lvol.Type == "pool") { |
| 134 |
make_thin_pool_logical_volume_page(parent, vgroup, lvol); |
| 135 |
} else { |
| 136 |
const block = client.lvols_block[lvol.path]; |
| 137 |
if (block) { |
| 138 |
const lv_card = make_block_logical_volume_card(generic_card, vgroup, lvol, block); |
| 139 |
make_block_page(parent, block, lv_card); |
| 140 |
} else { |
| 141 |
// If we can't find the block for a active volume, UDisks2 |
| 142 |
// or something below is probably misbehaving, and we show |
| 143 |
// it as "unsupported". |
| 144 |
if (lvol.Active) { |
| 145 |
make_unsupported_logical_volume_page(parent, vgroup, lvol, generic_card); |
| 146 |
} else { |
| 147 |
make_inactive_logical_volume_page(parent, vgroup, lvol, generic_card); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
function make_logical_volume_pages(parent, vgroup) { |
| 154 |
const isVDOPool = lvol => Object.keys(client.vdo_vols).some(v => client.vdo_vols[v].VDOPool == lvol.path); |
| 155 |
|
| 156 |
(client.vgroups_lvols[vgroup.path] || []).forEach(lvol => { |
| 157 |
// We ignore volumes in a thin pool; they appear as children |
| 158 |
// of their pool. |
| 159 |
// |
| 160 |
// We ignore old-style snapshots because Cockpit would need to |
| 161 |
// treat them specially, and we haven't bothered to write the |
| 162 |
// code for that. |
| 163 |
// |
| 164 |
// We ignore vdo pools; they appear as a card for their |
| 165 |
// single contained logical volume. |
| 166 |
// |
| 167 |
if (lvol.ThinPool == "/" && lvol.Origin == "/" && !isVDOPool(lvol)) |
| 168 |
make_lvm2_logical_volume_page(parent, vgroup, lvol); |
| 169 |
}); |
| 170 |
} |
| 171 |
|
| 172 |
function add_disk(vgroup) { |
| 173 |
function filter_inside_vgroup(spc) { |
| 174 |
let block = spc.block; |
| 175 |
if (client.blocks_part[block.path]) |
| 176 |
block = client.blocks[client.blocks_part[block.path].Table]; |
| 177 |
const lvol = (block && |
| 178 |
client.blocks_lvm2[block.path] && |
| 179 |
client.lvols[client.blocks_lvm2[block.path].LogicalVolume]); |
| 180 |
return !lvol || lvol.VolumeGroup != vgroup.path; |
| 181 |
} |
| 182 |
|
| 183 |
dialog_open({ |
| 184 |
Title: _("Add disks"), |
| 185 |
Fields: [ |
| 186 |
SelectSpaces("disks", _("Disks"), |
| 187 |
{ |
| 188 |
empty_warning: _("No disks are available."), |
| 189 |
validate: function(disks) { |
| 190 |
if (disks.length === 0) |
| 191 |
return _("At least one disk is needed."); |
| 192 |
}, |
| 193 |
spaces: get_available_spaces().filter(filter_inside_vgroup) |
| 194 |
}) |
| 195 |
], |
| 196 |
Action: { |
| 197 |
Title: _("Add"), |
| 198 |
action: function(vals) { |
| 199 |
return prepare_available_spaces(client, vals.disks).then(paths => |
| 200 |
Promise.all(paths.map(p => vgroup.AddDevice(p, {})))); |
| 201 |
} |
| 202 |
} |
| 203 |
}); |
| 204 |
} |
| 205 |
|
| 206 |
export function make_lvm2_volume_group_page(parent, vgroup) { |
| 207 |
const has_missing_pvs = vgroup.MissingPhysicalVolumes && vgroup.MissingPhysicalVolumes.length > 0; |
| 208 |
|
| 209 |
let lvol_excuse = null; |
| 210 |
if (has_missing_pvs) |
| 211 |
lvol_excuse = _("Volume group is missing physical volumes"); |
| 212 |
else if (vgroup.FreeSize == 0) |
| 213 |
lvol_excuse = _("No free space"); |
| 214 |
|
| 215 |
if (should_ignore(client, vgroup.path)) |
| 216 |
return; |
| 217 |
|
| 218 |
const vgroup_card = new_card({ |
| 219 |
title: _("LVM2 volume group"), |
| 220 |
next: null, |
| 221 |
page_location: ["vg", vgroup.Name], |
| 222 |
page_name: vgroup.Name, |
| 223 |
page_icon: VolumeIcon, |
| 224 |
page_category: PAGE_CATEGORY_VIRTUAL, |
| 225 |
page_size: vgroup.Size, |
| 226 |
job_path: vgroup.path, |
| 227 |
component: LVM2VolumeGroupCard, |
| 228 |
props: { vgroup }, |
| 229 |
actions: [ |
| 230 |
{ |
| 231 |
title: _("Add physical volume"), |
| 232 |
action: () => add_disk(vgroup), |
| 233 |
tag: "pvols", |
| 234 |
}, |
| 235 |
{ |
| 236 |
title: _("Delete group"), |
| 237 |
action: () => vgroup_delete(client, vgroup, vgroup_card), |
| 238 |
danger: true, |
| 239 |
tag: "group", |
| 240 |
}, |
| 241 |
], |
| 242 |
}); |
| 243 |
|
| 244 |
const lvols_card = new_card({ |
| 245 |
title: _("LVM2 logical volumes"), |
| 246 |
next: vgroup_card, |
| 247 |
has_warning: has_missing_pvs, |
| 248 |
component: LVM2LogicalVolumesCard, |
| 249 |
props: { vgroup }, |
| 250 |
actions: [ |
| 251 |
{ |
| 252 |
title: _("Create new logical volume"), |
| 253 |
action: () => create_logical_volume(client, vgroup), |
| 254 |
excuse: lvol_excuse, |
| 255 |
tag: "group", |
| 256 |
}, |
| 257 |
], |
| 258 |
}); |
| 259 |
|
| 260 |
const vgroup_page = new_page(parent, lvols_card); |
| 261 |
make_logical_volume_pages(vgroup_page, vgroup); |
| 262 |
} |
| 263 |
|
| 264 |
const LVM2LogicalVolumesCard = ({ card, vgroup }) => { |
| 265 |
return ( |
| 266 |
<StorageCard card={card}> |
| 267 |
<ChildrenTable |
| 268 |
emptyCaption={_("No logical volumes")} |
| 269 |
aria-label={_("LVM2 logical volumes")} |
| 270 |
page={card.page} /> |
| 271 |
</StorageCard> |
| 272 |
); |
| 273 |
}; |
| 274 |
|
| 275 |
const LVM2VolumeGroupCard = ({ card, vgroup }) => { |
| 276 |
const has_missing_pvs = vgroup.MissingPhysicalVolumes && vgroup.MissingPhysicalVolumes.length > 0; |
| 277 |
|
| 278 |
function is_partial_linear_lvol(block) { |
| 279 |
const lvm2 = client.blocks_lvm2[block.path]; |
| 280 |
const lvol = lvm2 && client.lvols[lvm2.LogicalVolume]; |
| 281 |
return lvol && lvol.Layout == "linear" && client.lvols_status[lvol.path] == "partial"; |
| 282 |
} |
| 283 |
|
| 284 |
function remove_missing() { |
| 285 |
/* Calling vgroup.RemoveMissingPhysicalVolumes will |
| 286 |
implicitly delete all partial, linear logical volumes. |
| 287 |
Instead of allowing this, we explicitly delete these |
| 288 |
volumes before calling RemoveMissingPhysicalVolumes. |
| 289 |
This allows us to kill processes that keep them busy |
| 290 |
and remove their fstab entries. |
| 291 |
|
| 292 |
RemoveMissingPhysicalVolumes leaves non-linear volumes |
| 293 |
alone, even if they can't be repaired anymore. This is |
| 294 |
a bit inconsistent, but *shrug*. |
| 295 |
*/ |
| 296 |
|
| 297 |
const all_usage = get_active_usage(client, vgroup.path, _("delete")); |
| 298 |
const usage = all_usage.filter(u => u.block && is_partial_linear_lvol(u.block)); |
| 299 |
usage.Blocking = all_usage.Blocking; |
| 300 |
usage.Teardown = all_usage.Teardown; |
| 301 |
|
| 302 |
if (usage.Blocking) { |
| 303 |
dialog_open({ |
| 304 |
Title: cockpit.format(_("$0 is in use"), |
| 305 |
vgroup.Name), |
| 306 |
Body: BlockingMessage(usage) |
| 307 |
}); |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
dialog_open({ |
| 312 |
Title: _("Remove missing physical volumes?"), |
| 313 |
Teardown: TeardownMessage(usage), |
| 314 |
Action: { |
| 315 |
Title: _("Remove"), |
| 316 |
action: function () { |
| 317 |
return teardown_active_usage(client, usage) |
| 318 |
.then(function () { |
| 319 |
return for_each_async(usage, |
| 320 |
u => { |
| 321 |
const lvm2 = client.blocks_lvm2[u.block.path]; |
| 322 |
const lvol = lvm2 && client.lvols[lvm2.LogicalVolume]; |
| 323 |
return lvol.Delete({ 'tear-down': { t: 'b', v: true } }); |
| 324 |
}) |
| 325 |
.then(() => vgroup.RemoveMissingPhysicalVolumes({})); |
| 326 |
}); |
| 327 |
} |
| 328 |
}, |
| 329 |
Inits: [ |
| 330 |
init_teardown_usage(client, usage) |
| 331 |
] |
| 332 |
}); |
| 333 |
} |
| 334 |
|
| 335 |
const alerts = []; |
| 336 |
if (has_missing_pvs) |
| 337 |
alerts.push( |
| 338 |
<Alert variant='warning' isInline key="missing" |
| 339 |
actionClose={<StorageButton onClick={remove_missing}>{_("Dismiss")}</StorageButton>} |
| 340 |
title={_("This volume group is missing some physical volumes.")}> |
| 341 |
{vgroup.MissingPhysicalVolumes.map(uuid => <div key={uuid}>{uuid}</div>)} |
| 342 |
</Alert>); |
| 343 |
|
| 344 |
return ( |
| 345 |
<StorageCard card={card} alerts={alerts}> |
| 346 |
<CardBody> |
| 347 |
<DescriptionList className="pf-m-horizontal-on-sm"> |
| 348 |
<StorageDescription title={_("Name")} |
| 349 |
value={vgroup.Name} |
| 350 |
action={<StorageLink onClick={() => vgroup_rename(client, vgroup, card)} |
| 351 |
excuse={has_missing_pvs && _("A volume group with missing physical volumes can not be renamed.")}> |
| 352 |
{_("edit")} |
| 353 |
</StorageLink>} /> |
| 354 |
<StorageDescription title={_("UUID")} value={vgroup.UUID} /> |
| 355 |
<StorageDescription title={_("Capacity")} value={fmt_size_long(vgroup.Size)} /> |
| 356 |
</DescriptionList> |
| 357 |
</CardBody> |
| 358 |
<CardHeader><strong>{_("Physical volumes")}</strong></CardHeader> |
| 359 |
<PageTable |
| 360 |
emptyCaption={_("No physical volumes found")} |
| 361 |
aria-label={_("LVM2 physical volumes")} |
| 362 |
crossrefs={get_crossrefs(vgroup)} /> |
| 363 |
</StorageCard> |
| 364 |
); |
| 365 |
}; |