| 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 |
|
| 9 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 10 |
import { CardBody, CardHeader } from "@patternfly/react-core/dist/esm/components/Card/index.js"; |
| 11 |
import { DescriptionList } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js"; |
| 12 |
|
| 13 |
import { dirname } from "cockpit-path"; |
| 14 |
|
| 15 |
import { |
| 16 |
PageTable, StorageCard, StorageDescription, ChildrenTable, |
| 17 |
new_card, new_page, navigate_away_from_card, register_crossref, get_crossrefs, |
| 18 |
} from "../pages.jsx"; |
| 19 |
import { StorageUsageBar } from "../storage-controls.jsx"; |
| 20 |
import { |
| 21 |
encode_filename, decode_filename, |
| 22 |
get_fstab_config_with_client, reload_systemd, |
| 23 |
flatten, teardown_active_usage, |
| 24 |
} from "../utils.js"; |
| 25 |
import { btrfs_usage, validate_subvolume_name, parse_subvol_from_options } from "./utils.jsx"; |
| 26 |
import { at_boot_input, update_at_boot_input, mounting_dialog, mount_options } from "../filesystem/mounting-dialog.jsx"; |
| 27 |
import { |
| 28 |
dialog_open, TextInput, |
| 29 |
TeardownMessage, init_teardown_usage, |
| 30 |
} from "../dialog.jsx"; |
| 31 |
import { check_mismounted_fsys, MismountAlert } from "../filesystem/mismounting.jsx"; |
| 32 |
import { |
| 33 |
is_mounted, is_valid_mount_point, mount_point_text, MountPoint, edit_mount_point |
| 34 |
} from "../filesystem/utils.jsx"; |
| 35 |
import client, { btrfs_poll, btrfs_tool } from "../client.js"; |
| 36 |
|
| 37 |
const _ = cockpit.gettext; |
| 38 |
|
| 39 |
function subvolume_unmount(volume, subvol, forced_options) { |
| 40 |
const block = client.blocks[volume.path]; |
| 41 |
mounting_dialog(client, block, "unmount", forced_options, subvol); |
| 42 |
} |
| 43 |
|
| 44 |
function subvolume_mount(volume, subvol, forced_options) { |
| 45 |
const block = client.blocks[volume.path]; |
| 46 |
mounting_dialog(client, block, "mount", forced_options, subvol); |
| 47 |
} |
| 48 |
|
| 49 |
function get_rw_mount_point(volume, subvol) { |
| 50 |
const mount_points = client.btrfs_mounts[volume.data.uuid]; |
| 51 |
return mount_points?.[subvol.id]?.rw_mount_points?.[0]; |
| 52 |
} |
| 53 |
|
| 54 |
function get_rw_mount_point_in_parent(volume, subvol) { |
| 55 |
const subvols = client.uuids_btrfs_subvols[volume.data.uuid]; |
| 56 |
|
| 57 |
if (!subvols) |
| 58 |
return null; |
| 59 |
|
| 60 |
for (const p of subvols) { |
| 61 |
const has_parent_subvol = (p.pathname == "/" && subvol.pathname !== "/") || |
| 62 |
(subvol.pathname.substring(0, p.pathname.length) == p.pathname && |
| 63 |
subvol.pathname[p.pathname.length] == "/"); |
| 64 |
const parent_rw_mp = get_rw_mount_point(volume, p); |
| 65 |
if (has_parent_subvol && parent_rw_mp) { |
| 66 |
if (p.pathname == "/") { |
| 67 |
return parent_rw_mp + "/" + subvol.pathname; |
| 68 |
} else { |
| 69 |
return parent_rw_mp + subvol.pathname.substring(p.pathname.length); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
return null; |
| 74 |
} |
| 75 |
|
| 76 |
function set_mount_options(subvol, block, vals) { |
| 77 |
const mount_options = []; |
| 78 |
const mount_now = vals.variant != "nomount"; |
| 79 |
|
| 80 |
if (!mount_now || vals.at_boot == "never") { |
| 81 |
mount_options.push("noauto"); |
| 82 |
} |
| 83 |
if (vals.mount_options?.ro) |
| 84 |
mount_options.push("ro"); |
| 85 |
if (vals.at_boot == "never") |
| 86 |
mount_options.push("x-cockpit-never-auto"); |
| 87 |
if (vals.at_boot == "nofail") |
| 88 |
mount_options.push("nofail"); |
| 89 |
if (vals.at_boot == "netdev") |
| 90 |
mount_options.push("_netdev"); |
| 91 |
|
| 92 |
const name = (subvol.pathname == "/" ? vals.name : subvol.pathname + "/" + vals.name); |
| 93 |
mount_options.push("subvol=" + name); |
| 94 |
if (vals.mount_options?.extra) |
| 95 |
mount_options.push(vals.mount_options.extra); |
| 96 |
|
| 97 |
let mount_point = vals.mount_point; |
| 98 |
if (mount_point[0] != "/") |
| 99 |
mount_point = "/" + mount_point; |
| 100 |
mount_point = client.add_mount_point_prefix(mount_point); |
| 101 |
|
| 102 |
const config = |
| 103 |
["fstab", |
| 104 |
{ |
| 105 |
dir: { t: 'ay', v: encode_filename(mount_point) }, |
| 106 |
type: { t: 'ay', v: encode_filename("btrfs") }, |
| 107 |
opts: { t: 'ay', v: encode_filename(mount_options.join(",") || "defaults") }, |
| 108 |
freq: { t: 'i', v: 0 }, |
| 109 |
passno: { t: 'i', v: 0 }, |
| 110 |
"track-parents": { t: 'b', v: true } |
| 111 |
} |
| 112 |
]; |
| 113 |
|
| 114 |
return block.AddConfigurationItem(config, {}) |
| 115 |
.then(reload_systemd) |
| 116 |
.then(() => { |
| 117 |
if (mount_now) { |
| 118 |
return client.mount_at(block, mount_point); |
| 119 |
} else |
| 120 |
return Promise.resolve(); |
| 121 |
}); |
| 122 |
} |
| 123 |
|
| 124 |
function subvolume_create(volume, subvol) { |
| 125 |
const block = client.blocks[volume.path]; |
| 126 |
const parent_dir = (get_rw_mount_point(volume, subvol) || |
| 127 |
get_rw_mount_point_in_parent(volume, subvol)); |
| 128 |
|
| 129 |
let action_variants = [ |
| 130 |
{ tag: null, Title: _("Create and mount") }, |
| 131 |
{ tag: "nomount", Title: _("Create only") } |
| 132 |
]; |
| 133 |
|
| 134 |
if (client.in_anaconda_mode()) { |
| 135 |
action_variants = [ |
| 136 |
{ tag: "nomount", Title: _("Create") } |
| 137 |
]; |
| 138 |
} |
| 139 |
|
| 140 |
dialog_open({ |
| 141 |
Title: _("Create subvolume"), |
| 142 |
Fields: [ |
| 143 |
TextInput("name", _("Name"), |
| 144 |
{ |
| 145 |
validate: name => validate_subvolume_name(name) |
| 146 |
}), |
| 147 |
TextInput("mount_point", _("Mount Point"), |
| 148 |
{ |
| 149 |
validate: (val, _values, variant) => { |
| 150 |
return is_valid_mount_point(client, |
| 151 |
block, |
| 152 |
client.add_mount_point_prefix(val), |
| 153 |
variant == "nomount"); |
| 154 |
} |
| 155 |
}), |
| 156 |
mount_options(false, false), |
| 157 |
at_boot_input(), |
| 158 |
], |
| 159 |
update: update_at_boot_input, |
| 160 |
Action: { |
| 161 |
Variants: action_variants, |
| 162 |
action: async function (vals) { |
| 163 |
// HACK: cannot use block_btrfs.CreateSubvolume as it always creates a subvolume relative to MountPoints[0] which |
| 164 |
// makes it impossible to handle a situation where we have multiple subvolumes mounted. |
| 165 |
// https://github.com/storaged-project/udisks/issues/1242 |
| 166 |
if (parent_dir) |
| 167 |
await cockpit.spawn(["btrfs", "subvolume", "create", `${parent_dir}/${vals.name}`], { superuser: "require", err: "message" }); |
| 168 |
else { |
| 169 |
await btrfs_tool(["do", volume.data.uuid, |
| 170 |
"btrfs", "subvolume", "create", |
| 171 |
subvol.pathname == "/" ? vals.name : subvol.pathname + "/" + vals.name |
| 172 |
]); |
| 173 |
} |
| 174 |
await btrfs_poll(); |
| 175 |
if (vals.mount_point !== "") { |
| 176 |
await set_mount_options(subvol, block, vals); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
}); |
| 181 |
} |
| 182 |
|
| 183 |
function subvolume_delete(volume, subvol, card) { |
| 184 |
const block = client.blocks[volume.path]; |
| 185 |
const subvols = client.uuids_btrfs_subvols[volume.data.uuid]; |
| 186 |
const mount_point_in_parent = get_rw_mount_point_in_parent(volume, subvol); |
| 187 |
|
| 188 |
function get_direct_subvol_children(subvol) { |
| 189 |
function is_direct_parent(sv) { |
| 190 |
return (sv.pathname.length > subvol.pathname.length && |
| 191 |
sv.pathname.substring(0, subvol.pathname.length) == subvol.pathname && |
| 192 |
sv.pathname[subvol.pathname.length] == "/" && |
| 193 |
sv.pathname.substring(subvol.pathname.length + 1).indexOf("/") == -1); |
| 194 |
} |
| 195 |
|
| 196 |
return subvols.filter(is_direct_parent); |
| 197 |
} |
| 198 |
|
| 199 |
function get_subvol_children(subvol) { |
| 200 |
// The deepest nested children must come first |
| 201 |
const direct_children = get_direct_subvol_children(subvol); |
| 202 |
return flatten(direct_children.map(get_subvol_children)).concat(direct_children); |
| 203 |
} |
| 204 |
|
| 205 |
const all_subvols = get_subvol_children(subvol).concat([subvol]); |
| 206 |
const configs_to_remove = []; |
| 207 |
const paths_to_delete = []; |
| 208 |
const usage = []; |
| 209 |
|
| 210 |
usage.Teardown = true; |
| 211 |
for (const sv of all_subvols) { |
| 212 |
const [config, mount_point] = get_fstab_config_with_client(client, block, false, sv); |
| 213 |
const fs_is_mounted = is_mounted(client, block, sv); |
| 214 |
|
| 215 |
usage.push({ |
| 216 |
level: 0, |
| 217 |
usage: fs_is_mounted ? 'mounted' : 'none', |
| 218 |
block, |
| 219 |
name: sv.pathname, |
| 220 |
location: mount_point, |
| 221 |
actions: fs_is_mounted ? [_("unmount"), _("delete")] : [_("delete")], |
| 222 |
blocking: false, |
| 223 |
}); |
| 224 |
|
| 225 |
if (config) |
| 226 |
configs_to_remove.push(config); |
| 227 |
|
| 228 |
paths_to_delete.push(sv.pathname); |
| 229 |
} |
| 230 |
|
| 231 |
function move_to_parent(pathname) { |
| 232 |
return mount_point_in_parent + pathname.substring(subvol.pathname.length); |
| 233 |
} |
| 234 |
|
| 235 |
dialog_open({ |
| 236 |
Title: cockpit.format(_("Permanently delete subvolume $0?"), subvol.pathname), |
| 237 |
Teardown: TeardownMessage(usage), |
| 238 |
Action: { |
| 239 |
Title: _("Delete"), |
| 240 |
Danger: _("Deleting erases all data on this subvolume and all its children."), |
| 241 |
action: async function () { |
| 242 |
await teardown_active_usage(client, usage); |
| 243 |
for (const c of configs_to_remove) |
| 244 |
await block.RemoveConfigurationItem(c, {}); |
| 245 |
if (mount_point_in_parent) { |
| 246 |
await cockpit.spawn(["btrfs", "subvolume", "delete", |
| 247 |
...paths_to_delete.map(move_to_parent) |
| 248 |
], |
| 249 |
{ superuser: "require", err: "message" }); |
| 250 |
} else { |
| 251 |
await btrfs_tool(["do", volume.data.uuid, |
| 252 |
"btrfs", "subvolume", "delete", ...paths_to_delete]); |
| 253 |
} |
| 254 |
await btrfs_poll(); |
| 255 |
navigate_away_from_card(card); |
| 256 |
} |
| 257 |
}, |
| 258 |
Inits: [ |
| 259 |
init_teardown_usage(client, usage) |
| 260 |
] |
| 261 |
}); |
| 262 |
} |
| 263 |
|
| 264 |
export function make_btrfs_subvolume_pages(parent, volume) { |
| 265 |
let subvols = client.uuids_btrfs_subvols[volume.data.uuid]; |
| 266 |
if (!subvols) { |
| 267 |
const block = client.blocks[volume.path]; |
| 268 |
/* |
| 269 |
* Try to show subvolumes based on fstab entries. We collect |
| 270 |
* all subvolumes that are mentioned in fstab entries so that |
| 271 |
* the user can at least mount those. |
| 272 |
* |
| 273 |
* The real subvolume data structure has "id" fields and |
| 274 |
* "parent" fields that refer to the ids to form a tree. We |
| 275 |
* want to do the same here, and we give fake ids to our fake |
| 276 |
* subvolumes for this reason. We don't store these fake ids |
| 277 |
* in the "id" field since we don't want them to be taken |
| 278 |
* seriously by the rest of the code. |
| 279 |
*/ |
| 280 |
let fake_id = 5; |
| 281 |
subvols = [{ pathname: "/", id: 5, fake_id: fake_id++ }]; |
| 282 |
const subvols_by_pathname = { }; |
| 283 |
for (const config of block.Configuration) { |
| 284 |
if (config[0] == "fstab") { |
| 285 |
const opts = config[1].opts; |
| 286 |
if (!opts) |
| 287 |
continue; |
| 288 |
|
| 289 |
const fstab_subvol = parse_subvol_from_options(decode_filename(opts.v)); |
| 290 |
|
| 291 |
if (fstab_subvol && fstab_subvol.pathname && fstab_subvol.pathname !== "/") { |
| 292 |
fstab_subvol.fake_id = fake_id++; |
| 293 |
subvols_by_pathname[fstab_subvol.pathname] = fstab_subvol; |
| 294 |
subvols.push(fstab_subvol); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
// Find parents |
| 300 |
for (const pn in subvols_by_pathname) { |
| 301 |
let dn = pn; |
| 302 |
while (true) { |
| 303 |
dn = dirname(dn); |
| 304 |
if (dn == "." || dn == "/") { |
| 305 |
subvols_by_pathname[pn].parent = 5; |
| 306 |
break; |
| 307 |
} else if (subvols_by_pathname[dn]) { |
| 308 |
subvols_by_pathname[pn].parent = subvols_by_pathname[dn].fake_id; |
| 309 |
break; |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
const root = subvols.find(s => s.id == 5); |
| 316 |
if (root) |
| 317 |
make_btrfs_subvolume_page(parent, volume, root, "", subvols); |
| 318 |
} |
| 319 |
|
| 320 |
function make_btrfs_subvolume_page(parent, volume, subvol, path_prefix, subvols) { |
| 321 |
const actions = []; |
| 322 |
|
| 323 |
const use = btrfs_usage(client, volume); |
| 324 |
const block = client.blocks[volume.path]; |
| 325 |
const block_fsys = client.blocks_fsys[volume.path]; |
| 326 |
const fstab_config = get_fstab_config_with_client(client, block, false, subvol); |
| 327 |
const [, mount_point] = fstab_config; |
| 328 |
const mismount_warning = check_mismounted_fsys(block, block, fstab_config, subvol); |
| 329 |
const mounted = is_mounted(client, block, subvol); |
| 330 |
const mp_text = mount_point_text(mount_point, mounted); |
| 331 |
if (mp_text == null) |
| 332 |
return null; |
| 333 |
const forced_options = [`subvol=${subvol.pathname}`]; |
| 334 |
|
| 335 |
if (client.in_anaconda_mode()) { |
| 336 |
actions.push({ |
| 337 |
title: _("Edit mount point"), |
| 338 |
action: () => edit_mount_point(block, forced_options, subvol), |
| 339 |
}); |
| 340 |
} |
| 341 |
|
| 342 |
if (mounted) { |
| 343 |
actions.push({ |
| 344 |
title: _("Unmount"), |
| 345 |
action: () => subvolume_unmount(volume, subvol, forced_options), |
| 346 |
}); |
| 347 |
} else if (!client.in_anaconda_mode()) { |
| 348 |
actions.push({ |
| 349 |
title: _("Mount"), |
| 350 |
action: () => subvolume_mount(volume, subvol, forced_options), |
| 351 |
}); |
| 352 |
} |
| 353 |
|
| 354 |
// If the filesystem is mounted anywhere, we know that we are |
| 355 |
// showing the real list of subvolumes. (Otherwise only those in |
| 356 |
// fstab are shown.) If so, we allow creating new ones and |
| 357 |
// deleting existing ones, because we know that those changes will |
| 358 |
// be reflected in the UI. However, we don't allow deleting the |
| 359 |
// last mounted subvolume, since that would also break the |
| 360 |
// subvolume listing. |
| 361 |
// |
| 362 |
// In Anaconda mode, however, we always have a proper list of |
| 363 |
// subvolumes, so we always allow creation and deletion. |
| 364 |
|
| 365 |
let create_excuse = ""; |
| 366 |
let delete_excuse = ""; |
| 367 |
if (!client.in_anaconda_mode()) { |
| 368 |
if (!block_fsys || block_fsys.MountPoints.length == 0) |
| 369 |
create_excuse = delete_excuse = _("At least one subvolume needs to be mounted"); |
| 370 |
else if (block_fsys && block_fsys.MountPoints.length == 1 && |
| 371 |
decode_filename(block_fsys.MountPoints[0]) == mount_point) { |
| 372 |
delete_excuse = _("The last mounted subvolume can not be deleted"); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
actions.push({ |
| 377 |
title: _("Create subvolume"), |
| 378 |
excuse: create_excuse, |
| 379 |
action: () => subvolume_create(volume, subvol), |
| 380 |
}); |
| 381 |
|
| 382 |
// Don't show deletion for the root subvolume as it can never be deleted. |
| 383 |
if (subvol.id !== 5 && subvol.pathname !== "/") |
| 384 |
actions.push({ |
| 385 |
danger: true, |
| 386 |
title: _("Delete"), |
| 387 |
excuse: delete_excuse, |
| 388 |
action: () => subvolume_delete(volume, subvol, card), |
| 389 |
}); |
| 390 |
|
| 391 |
function strip_prefix(str, prefix) { |
| 392 |
if (str.startsWith(prefix)) |
| 393 |
return str.slice(prefix.length); |
| 394 |
else |
| 395 |
return str; |
| 396 |
} |
| 397 |
|
| 398 |
// Show the hidden "root" of a btrfs filesystem as "top-level" as "/" can |
| 399 |
// be confused with the root filesystem. |
| 400 |
// https://btrfs.readthedocs.io/en/latest/Subvolumes.html |
| 401 |
function subvol_name(subvol, path_prefix) { |
| 402 |
if (subvol.id === 5) { |
| 403 |
return "top-level"; |
| 404 |
} |
| 405 |
return strip_prefix(subvol.pathname, path_prefix); |
| 406 |
} |
| 407 |
|
| 408 |
let snapshot_origin = null; |
| 409 |
if (subvol.id !== 5 && subvol.parent_uuid !== null) { |
| 410 |
for (const sv of subvols) { |
| 411 |
if (sv.uuid === subvol.parent_uuid) { |
| 412 |
snapshot_origin = sv; |
| 413 |
break; |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
const card = new_card({ |
| 419 |
title: _("btrfs subvolume"), |
| 420 |
next: null, |
| 421 |
page_location: ["btrfs", volume.data.uuid, subvol.pathname], |
| 422 |
page_name: subvol_name(subvol, path_prefix), |
| 423 |
page_size: mounted && <StorageUsageBar stats={use} short />, |
| 424 |
location: mp_text, |
| 425 |
component: BtrfsSubvolumeCard, |
| 426 |
has_warning: !!mismount_warning, |
| 427 |
props: { volume, subvol, snapshot_origin, mount_point, mismount_warning, block, fstab_config, forced_options }, |
| 428 |
actions, |
| 429 |
}); |
| 430 |
|
| 431 |
if (subvol.id !== 5 && subvol.parent_uuid !== null) |
| 432 |
register_crossref({ |
| 433 |
key: subvol.parent_uuid, |
| 434 |
card, |
| 435 |
size: mounted && <StorageUsageBar stats={use} short />, |
| 436 |
}); |
| 437 |
|
| 438 |
const page = new_page(parent, card); |
| 439 |
for (const sv of subvols) { |
| 440 |
if (sv.parent && (sv.parent === subvol.id || sv.parent === subvol.fake_id)) { |
| 441 |
make_btrfs_subvolume_page(page, volume, sv, subvol.pathname + "/", subvols); |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
const BtrfsSubvolumeCard = ({ card, volume, subvol, snapshot_origin, mismount_warning, block, fstab_config, forced_options }) => { |
| 447 |
const crossrefs = get_crossrefs(subvol.uuid); |
| 448 |
|
| 449 |
return ( |
| 450 |
<StorageCard card={card} alert={mismount_warning && |
| 451 |
<MismountAlert warning={mismount_warning} |
| 452 |
fstab_config={fstab_config} |
| 453 |
backing_block={block} content_block={block} subvol={subvol} />}> |
| 454 |
<CardBody> |
| 455 |
<DescriptionList className="pf-m-horizontal-on-sm"> |
| 456 |
<StorageDescription title={_("Name")} value={subvol.id === 5 ? "top-level" : subvol.pathname} /> |
| 457 |
<StorageDescription title={_("ID")} value={subvol.id} /> |
| 458 |
{snapshot_origin !== null && |
| 459 |
<StorageDescription title={_("Snapshot origin")}> |
| 460 |
<Button variant="link" isInline role="link" |
| 461 |
onClick={() => cockpit.location.go(["btrfs", volume.data.uuid, snapshot_origin.pathname])}> |
| 462 |
{snapshot_origin.pathname} |
| 463 |
</Button> |
| 464 |
</StorageDescription> |
| 465 |
} |
| 466 |
<StorageDescription title={_("Mount point")}> |
| 467 |
<MountPoint fstab_config={fstab_config} |
| 468 |
backing_block={block} content_block={block} |
| 469 |
forced_options={forced_options} subvol={subvol} /> |
| 470 |
</StorageDescription> |
| 471 |
</DescriptionList> |
| 472 |
</CardBody> |
| 473 |
<ChildrenTable |
| 474 |
emptyCaption={_("No subvolumes")} |
| 475 |
aria-label={_("btrfs subvolumes")} |
| 476 |
page={card.page} /> |
| 477 |
{crossrefs && |
| 478 |
<> |
| 479 |
<CardHeader><strong>{_("Snapshots")}</strong></CardHeader> |
| 480 |
<PageTable |
| 481 |
emptyCaption={_("No snapshots found")} |
| 482 |
aria-label={_("snapshot")} |
| 483 |
crossrefs={crossrefs} /> |
| 484 |
</> |
| 485 |
} |
| 486 |
</StorageCard>); |
| 487 |
}; |