| 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 { CardBody } 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 { |
| 14 |
dialog_open, TextInput, CheckBoxes, SizeSlider, BlockingMessage, TeardownMessage, |
| 15 |
init_teardown_usage, |
| 16 |
} from "../dialog.jsx"; |
| 17 |
import { StorageUsageBar, StorageLink } from "../storage-controls.jsx"; |
| 18 |
import { |
| 19 |
StorageCard, StorageDescription, |
| 20 |
new_page, new_card, |
| 21 |
navigate_away_from_card, navigate_to_new_card_location, |
| 22 |
} from "../pages.jsx"; |
| 23 |
import { |
| 24 |
MountPoint, edit_mount_point, |
| 25 |
is_valid_mount_point, is_mounted, |
| 26 |
get_fstab_config, mount_point_text, |
| 27 |
} from "../filesystem/utils.jsx"; |
| 28 |
import { MismountAlert, check_mismounted_fsys } from "../filesystem/mismounting.jsx"; |
| 29 |
import { mounting_dialog, at_boot_input, update_at_boot_input, mount_options } from "../filesystem/mounting-dialog.jsx"; |
| 30 |
import { fmt_size, get_active_usage, teardown_active_usage } from "../utils.js"; |
| 31 |
import { std_reply, validate_fs_name, set_mount_options, destroy_filesystem } from "./utils.jsx"; |
| 32 |
|
| 33 |
const _ = cockpit.gettext; |
| 34 |
|
| 35 |
export function make_stratis_filesystem_page(parent, pool, fsys, |
| 36 |
offset, forced_options) { |
| 37 |
const filesystems = client.stratis_pool_filesystems[pool.path]; |
| 38 |
const stats = client.stratis_pool_stats[pool.path]; |
| 39 |
const block = client.slashdevs_block[fsys.Devnode]; |
| 40 |
|
| 41 |
if (!block) |
| 42 |
return; |
| 43 |
|
| 44 |
const fstab_config = get_fstab_config(block); |
| 45 |
const [, mount_point] = fstab_config; |
| 46 |
const fs_is_mounted = is_mounted(client, block); |
| 47 |
|
| 48 |
const mismount_warning = check_mismounted_fsys(block, block, fstab_config); |
| 49 |
|
| 50 |
function mount() { |
| 51 |
return mounting_dialog(client, block, "mount", forced_options); |
| 52 |
} |
| 53 |
|
| 54 |
function unmount() { |
| 55 |
return mounting_dialog(client, block, "unmount", forced_options); |
| 56 |
} |
| 57 |
|
| 58 |
function snapshot_fsys() { |
| 59 |
dialog_open({ |
| 60 |
Title: cockpit.format(_("Create a snapshot of filesystem $0"), fsys.Name), |
| 61 |
Fields: [ |
| 62 |
TextInput("name", _("Name"), |
| 63 |
{ |
| 64 |
value: "", |
| 65 |
validate: name => validate_fs_name(null, name, filesystems) |
| 66 |
}), |
| 67 |
TextInput("mount_point", _("Mount point"), |
| 68 |
{ |
| 69 |
validate: (val, values, variant) => { |
| 70 |
return is_valid_mount_point(client, |
| 71 |
null, |
| 72 |
client.add_mount_point_prefix(val), |
| 73 |
variant == "nomount"); |
| 74 |
} |
| 75 |
}), |
| 76 |
mount_options(false, false), |
| 77 |
at_boot_input(), |
| 78 |
], |
| 79 |
update: update_at_boot_input, |
| 80 |
Action: { |
| 81 |
Title: _("Create snapshot and mount"), |
| 82 |
Variants: [{ tag: "nomount", Title: _("Create snapshot only") }], |
| 83 |
action: function (vals) { |
| 84 |
return pool.SnapshotFilesystem(fsys.path, vals.name) |
| 85 |
.then(std_reply) |
| 86 |
.then(result => { |
| 87 |
if (result[0]) |
| 88 |
return set_mount_options(result[1], vals, forced_options); |
| 89 |
else |
| 90 |
return Promise.resolve(); |
| 91 |
}); |
| 92 |
} |
| 93 |
} |
| 94 |
}); |
| 95 |
} |
| 96 |
|
| 97 |
function delete_fsys() { |
| 98 |
const usage = get_active_usage(client, block.path, _("delete")); |
| 99 |
|
| 100 |
if (usage.Blocking) { |
| 101 |
dialog_open({ |
| 102 |
Title: cockpit.format(_("$0 is in use"), |
| 103 |
fsys.Name), |
| 104 |
Body: BlockingMessage(usage) |
| 105 |
}); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
dialog_open({ |
| 110 |
Title: cockpit.format(_("Confirm deletion of $0"), fsys.Name), |
| 111 |
Teardown: TeardownMessage(usage), |
| 112 |
Action: { |
| 113 |
Danger: _("Deleting a filesystem will delete all data in it."), |
| 114 |
Title: _("Delete"), |
| 115 |
action: async function () { |
| 116 |
await teardown_active_usage(client, usage); |
| 117 |
await destroy_filesystem(fsys); |
| 118 |
navigate_away_from_card(fsys_card); |
| 119 |
} |
| 120 |
}, |
| 121 |
Inits: [ |
| 122 |
init_teardown_usage(client, usage) |
| 123 |
] |
| 124 |
}); |
| 125 |
} |
| 126 |
|
| 127 |
const mp_text = mount_point_text(mount_point, fs_is_mounted); |
| 128 |
if (mp_text == null) |
| 129 |
return null; |
| 130 |
|
| 131 |
const fsys_card = new_card({ |
| 132 |
title: _("Stratis filesystem"), |
| 133 |
location: mp_text, |
| 134 |
next: null, |
| 135 |
page_location: ["pool", pool.Name, fsys.Name], |
| 136 |
page_name: fsys.Name, |
| 137 |
page_size: <StorageUsageBar stats={[Number(fsys.Used[0] && Number(fsys.Used[1])), stats.pool_total]} |
| 138 |
critical={1} total={stats.fsys_total_used} offset={offset} short />, |
| 139 |
has_warning: !!mismount_warning, |
| 140 |
component: StratisFilesystemCard, |
| 141 |
props: { pool, fsys, fstab_config, forced_options, mismount_warning, offset }, |
| 142 |
actions: [ |
| 143 |
client.in_anaconda_mode() && |
| 144 |
{ title: _("Edit mount point"), action: () => edit_mount_point(block, forced_options) }, |
| 145 |
(fs_is_mounted |
| 146 |
? { title: _("Unmount"), action: unmount } |
| 147 |
: null), |
| 148 |
(!fs_is_mounted && !client.in_anaconda_mode() |
| 149 |
? { title: _("Mount"), action: mount } |
| 150 |
: null), |
| 151 |
{ |
| 152 |
title: _("Snapshot"), |
| 153 |
action: snapshot_fsys, |
| 154 |
excuse: ((!pool.Overprovisioning && stats.pool_free < Number(fsys.Size)) |
| 155 |
? _("Not enough free space") |
| 156 |
: null), |
| 157 |
}, |
| 158 |
{ title: _("Delete"), action: delete_fsys, danger: true }, |
| 159 |
] |
| 160 |
}); |
| 161 |
|
| 162 |
new_page(parent, fsys_card); |
| 163 |
} |
| 164 |
|
| 165 |
const StratisFilesystemCard = ({ |
| 166 |
card, pool, fsys, fstab_config, forced_options, mismount_warning, offset, |
| 167 |
}) => { |
| 168 |
const filesystems = client.stratis_pool_filesystems[pool.path]; |
| 169 |
const stats = client.stratis_pool_stats[pool.path]; |
| 170 |
const block = client.slashdevs_block[fsys.Devnode]; |
| 171 |
|
| 172 |
function rename_fsys() { |
| 173 |
dialog_open({ |
| 174 |
Title: _("Rename filesystem"), |
| 175 |
Fields: [ |
| 176 |
TextInput("name", _("Name"), |
| 177 |
{ |
| 178 |
value: fsys.Name, |
| 179 |
validate: name => validate_fs_name(fsys, name, filesystems) |
| 180 |
}) |
| 181 |
], |
| 182 |
Action: { |
| 183 |
Title: _("Rename"), |
| 184 |
action: async function (vals) { |
| 185 |
await fsys.SetName(vals.name).then(std_reply); |
| 186 |
navigate_to_new_card_location(card, ["pool", pool.Name, vals.name]); |
| 187 |
} |
| 188 |
} |
| 189 |
}); |
| 190 |
} |
| 191 |
|
| 192 |
function set_limit() { |
| 193 |
dialog_open({ |
| 194 |
Title: _("Set limit of virtual filesystem size"), |
| 195 |
Fields: [ |
| 196 |
CheckBoxes("size_options", _("Options"), |
| 197 |
{ |
| 198 |
value: { |
| 199 |
custom_limit: fsys.SizeLimit[0], |
| 200 |
}, |
| 201 |
fields: [ |
| 202 |
{ tag: "custom_limit", title: _("Limit virtual filesystem size") }, |
| 203 |
] |
| 204 |
}), |
| 205 |
SizeSlider("limit", _("Virtual size limit"), |
| 206 |
{ |
| 207 |
visible: vals => vals.size_options.custom_limit, |
| 208 |
value: fsys.SizeLimit[0] && Number(fsys.SizeLimit[1]), |
| 209 |
min: Number(fsys.Size), |
| 210 |
max: pool.Overprovisioning ? stats.pool_total : stats.pool_free + Number(fsys.Size), |
| 211 |
allow_infinite: true, |
| 212 |
round: 512 |
| 213 |
}), |
| 214 |
], |
| 215 |
Action: { |
| 216 |
Title: _("Set"), |
| 217 |
action: async function (vals) { |
| 218 |
await client.stratis_set_property(fsys, |
| 219 |
"SizeLimit", |
| 220 |
"(bs)", (vals.size_options.custom_limit |
| 221 |
? [true, vals.limit.toString()] |
| 222 |
: [false, ""])); |
| 223 |
} |
| 224 |
} |
| 225 |
}); |
| 226 |
} |
| 227 |
|
| 228 |
return ( |
| 229 |
<StorageCard card={card} |
| 230 |
alert={mismount_warning && |
| 231 |
<MismountAlert warning={mismount_warning} |
| 232 |
fstab_config={fstab_config} forced_options={forced_options} |
| 233 |
backing_block={block} content_block={block} />}> |
| 234 |
<CardBody> |
| 235 |
<DescriptionList className="pf-m-horizontal-on-sm"> |
| 236 |
<StorageDescription title={_("Name")} |
| 237 |
value={fsys.Name} |
| 238 |
action={<StorageLink onClick={rename_fsys}> |
| 239 |
{_("edit")} |
| 240 |
</StorageLink>} /> |
| 241 |
<StorageDescription title={_("Mount point")}> |
| 242 |
<MountPoint fstab_config={fstab_config} forced_options={forced_options} |
| 243 |
backing_block={block} content_block={block} /> |
| 244 |
</StorageDescription> |
| 245 |
<StorageDescription title={_("Usage")}> |
| 246 |
<StorageUsageBar stats={[Number(fsys.Used[0] && Number(fsys.Used[1])), stats.pool_total]} |
| 247 |
critical={1} total={stats.fsys_total_used} offset={offset} /> |
| 248 |
</StorageDescription> |
| 249 |
<StorageDescription title={_("Virtual size")} |
| 250 |
value={fmt_size(Number(fsys.Size))} /> |
| 251 |
<StorageDescription title={_("Virtual size limit")} |
| 252 |
value={fsys.SizeLimit[0] ? fmt_size(Number(fsys.SizeLimit[1])) : _("none")} |
| 253 |
action={<StorageLink onClick={set_limit}> |
| 254 |
{_("edit")} |
| 255 |
</StorageLink>} /> |
| 256 |
</DescriptionList> |
| 257 |
</CardBody> |
| 258 |
</StorageCard> |
| 259 |
); |
| 260 |
}; |