| 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 |
|
| 14 |
import { StorageButton, StorageLink } from "../storage-controls.jsx"; |
| 15 |
import { |
| 16 |
dialog_open, |
| 17 |
SelectOne, TextInput, |
| 18 |
init_teardown_usage, BlockingMessage, TeardownMessage |
| 19 |
} from "../dialog.jsx"; |
| 20 |
import { block_name, fmt_size, get_active_usage, teardown_active_usage, reload_systemd } from "../utils.js"; |
| 21 |
import { check_unused_space, get_resize_info, free_space_after_part, grow_dialog, shrink_dialog } from "../block/resize.jsx"; |
| 22 |
import { StorageCard, StorageDescription, new_card, navigate_away_from_card } from "../pages.jsx"; |
| 23 |
|
| 24 |
const _ = cockpit.gettext; |
| 25 |
|
| 26 |
export function delete_partition(block, card) { |
| 27 |
const block_part = client.blocks_part[block.path]; |
| 28 |
const name = block_name(block); |
| 29 |
const usage = get_active_usage(client, block.path, _("delete")); |
| 30 |
|
| 31 |
if (usage.Blocking) { |
| 32 |
dialog_open({ |
| 33 |
Title: cockpit.format(_("$0 is in use"), name), |
| 34 |
Body: BlockingMessage(usage) |
| 35 |
}); |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
dialog_open({ |
| 40 |
Title: cockpit.format(_("Permanently delete $0?"), name), |
| 41 |
Teardown: TeardownMessage(usage), |
| 42 |
Action: { |
| 43 |
Danger: _("Deleting a partition will delete all data in it."), |
| 44 |
Title: _("Delete"), |
| 45 |
action: async function () { |
| 46 |
await teardown_active_usage(client, usage); |
| 47 |
await block_part.Delete({ 'tear-down': { t: 'b', v: true } }); |
| 48 |
await reload_systemd(); |
| 49 |
navigate_away_from_card(card); |
| 50 |
} |
| 51 |
}, |
| 52 |
Inits: [ |
| 53 |
init_teardown_usage(client, usage) |
| 54 |
] |
| 55 |
}); |
| 56 |
} |
| 57 |
|
| 58 |
export function make_partition_card(next, block) { |
| 59 |
const block_part = client.blocks_part[block.path]; |
| 60 |
const unused_space_warning = check_unused_space(block.path); |
| 61 |
const unused_space = !!unused_space_warning; |
| 62 |
let { info, shrink_excuse, grow_excuse } = get_resize_info(client, block, unused_space); |
| 63 |
|
| 64 |
if (!unused_space_warning && !grow_excuse && free_space_after_part(client, block_part) == 0) { |
| 65 |
grow_excuse = _("No free space after this partition"); |
| 66 |
} |
| 67 |
|
| 68 |
const type = block_part.Type.replace(/^0x/, "").toLowerCase(); |
| 69 |
let type_extra; |
| 70 |
if (type == "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" || type == "ef") |
| 71 |
type_extra = _("EFI system partition"); |
| 72 |
else if (type == "21686148-6449-6e6f-744e-656564454649") |
| 73 |
type_extra = _("BIOS boot partition"); |
| 74 |
|
| 75 |
const card = new_card({ |
| 76 |
title: _("Partition"), |
| 77 |
type_extra, |
| 78 |
next, |
| 79 |
page_block: block, |
| 80 |
for_summary: true, |
| 81 |
has_warning: !!unused_space_warning, |
| 82 |
component: PartitionCard, |
| 83 |
props: { block, unused_space_warning, resize_info: info }, |
| 84 |
actions: [ |
| 85 |
(!unused_space && |
| 86 |
{ |
| 87 |
title: _("Shrink"), |
| 88 |
action: () => shrink_dialog(client, block_part, info), |
| 89 |
excuse: shrink_excuse, |
| 90 |
}), |
| 91 |
(!unused_space && |
| 92 |
{ |
| 93 |
title: _("Grow"), |
| 94 |
action: () => grow_dialog(client, block_part, info), |
| 95 |
excuse: grow_excuse, |
| 96 |
}), |
| 97 |
{ |
| 98 |
title: _("Delete"), |
| 99 |
action: () => delete_partition(block, card), |
| 100 |
danger: true, |
| 101 |
}, |
| 102 |
], |
| 103 |
}); |
| 104 |
return card; |
| 105 |
} |
| 106 |
|
| 107 |
const gpt_type_names = { |
| 108 |
"21686148-6449-6e6f-744e-656564454649": _("BIOS boot partition"), |
| 109 |
"c12a7328-f81f-11d2-ba4b-00a0c93ec93b": _("EFI system partition"), |
| 110 |
"9e1a2d38-c612-4316-aa26-8b49521e5a8b": _("PowerPC PReP boot partition"), |
| 111 |
"0657fd6d-a4ab-43c4-84e5-0933c84b4f4f": _("Linux swap space"), |
| 112 |
"0fc63daf-8483-4772-8e79-3d69d8477de4": _("Linux filesystem data"), |
| 113 |
"e6d6d379-f507-44c2-a23c-238f2a3df928": _("Logical Volume Manager partition"), |
| 114 |
}; |
| 115 |
|
| 116 |
const mbr_type_names = { |
| 117 |
ef: _("EFI system partition"), |
| 118 |
82: _("Linux swap space"), |
| 119 |
83: _("Linux filesystem data"), |
| 120 |
"8e": _("Logical Volume Manager partition"), |
| 121 |
}; |
| 122 |
|
| 123 |
const PartitionCard = ({ card, block, unused_space_warning, resize_info }) => { |
| 124 |
const block_part = client.blocks_part[block.path]; |
| 125 |
const unused_space = !!unused_space_warning; |
| 126 |
const block_ptable = client.blocks_ptable[block_part.Table]; |
| 127 |
const type_names = block_ptable?.Type == "dos" ? mbr_type_names : gpt_type_names; |
| 128 |
const type = block_part.Type.replace(/^0x/, "").toLowerCase(); |
| 129 |
|
| 130 |
function shrink_to_fit() { |
| 131 |
return shrink_dialog(client, block_part, resize_info, true); |
| 132 |
} |
| 133 |
|
| 134 |
function grow_to_fit() { |
| 135 |
return grow_dialog(client, block_part, resize_info, true); |
| 136 |
} |
| 137 |
|
| 138 |
function set_type_dialog() { |
| 139 |
const choices = Object.keys(type_names).map(k => ({ value: k, title: type_names[k] })); |
| 140 |
choices.push({ title: _("Custom"), value: "custom" }); |
| 141 |
|
| 142 |
function validate_type(val) { |
| 143 |
if (block_ptable.Type == "dos") { |
| 144 |
const hex_rx = /^[a-fA-F0-9]{2}$/; |
| 145 |
if (!hex_rx.test(val)) |
| 146 |
return _("Type must contain exactly two hexadecimal characters (0 to 9, A to F)."); |
| 147 |
} else { |
| 148 |
/* We let people use any 128 bit value as a UUID, even |
| 149 |
those that are not defined by RFC 4122. This is |
| 150 |
what the rest of the storage stack does as well, |
| 151 |
and the "BIOS boot" UUID is in fact invalid |
| 152 |
according to RFC 4122. |
| 153 |
|
| 154 |
But we do insist that the dashes are in the right |
| 155 |
place, because UDisks2 does as well. |
| 156 |
*/ |
| 157 |
const uuid_rx_1 = /^[a-fA-F0-9-]*$/; |
| 158 |
if (!uuid_rx_1.test(val)) |
| 159 |
return _("Type can only contain the characters 0 to 9, A to F, and \"-\"."); |
| 160 |
const uuid_rx_2 = /^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$/; |
| 161 |
if (!uuid_rx_2.test(val)) |
| 162 |
return _("Type must be of the form NNNNNNNN-NNNN-NNNN-NNNN-NNNNNNNNNNNN."); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
dialog_open({ |
| 167 |
Title: cockpit.format(_("Set partition type of $0"), block_name(block)), |
| 168 |
Fields: [ |
| 169 |
SelectOne("type", _("Type"), |
| 170 |
{ |
| 171 |
value: (type_names[type] ? type : "custom"), |
| 172 |
choices, |
| 173 |
}), |
| 174 |
TextInput("custom", _("Custom type"), |
| 175 |
{ |
| 176 |
value: type, |
| 177 |
validate: validate_type, |
| 178 |
visible: vals => vals.type == "custom", |
| 179 |
}), |
| 180 |
], |
| 181 |
Action: { |
| 182 |
Danger: !client.in_anaconda_mode() && _("Changing partition types might prevent the system from booting."), |
| 183 |
Title: _("Save"), |
| 184 |
action: async function (vals) { |
| 185 |
let t = vals.type == "custom" ? vals.custom : vals.type; |
| 186 |
if (block_ptable?.Type == "dos") |
| 187 |
t = "0x" + t; |
| 188 |
await block_part.SetType(t, { }); |
| 189 |
} |
| 190 |
}, |
| 191 |
}); |
| 192 |
} |
| 193 |
|
| 194 |
return ( |
| 195 |
<StorageCard card={card} |
| 196 |
alert={unused_space && |
| 197 |
<Alert variant="warning" isInline |
| 198 |
title={_("This partition is not completely used by its content.")}> |
| 199 |
{cockpit.format(_("Partition size is $0. Content size is $1."), |
| 200 |
fmt_size(unused_space_warning.volume_size), |
| 201 |
fmt_size(unused_space_warning.content_size))} |
| 202 |
<div className='storage-alert-actions'> |
| 203 |
<StorageButton onClick={shrink_to_fit}> |
| 204 |
{_("Shrink partition")} |
| 205 |
</StorageButton> |
| 206 |
<StorageButton onClick={grow_to_fit}> |
| 207 |
{_("Grow content")} |
| 208 |
</StorageButton> |
| 209 |
</div> |
| 210 |
</Alert>}> |
| 211 |
<CardBody> |
| 212 |
<DescriptionList className="pf-m-horizontal-on-sm"> |
| 213 |
<StorageDescription title={_("Name")} value={block_part.Name || "-"} /> |
| 214 |
<StorageDescription title={_("UUID")} value={block_part.UUID} /> |
| 215 |
<StorageDescription title={_("Type")} |
| 216 |
value={type_names[type] || type} |
| 217 |
action={<StorageLink onClick={set_type_dialog}> |
| 218 |
{_("edit")} |
| 219 |
</StorageLink>} /> |
| 220 |
{ !unused_space && |
| 221 |
<StorageDescription title={_("Size")} value={fmt_size(block_part.Size)} /> |
| 222 |
} |
| 223 |
</DescriptionList> |
| 224 |
</CardBody> |
| 225 |
</StorageCard>); |
| 226 |
}; |