Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Btrfs snapshot support #20234

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 24 additions & 2 deletions pkg/storaged/btrfs/subvolume.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
import cockpit from "cockpit";
import React from "react";

import { CardBody } from "@patternfly/react-core/dist/esm/components/Card/index.js";
import { Card, CardBody, CardHeader, CardTitle } from "@patternfly/react-core/dist/esm/components/Card/index.js";
import { DescriptionList } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js";

import {
StorageCard, StorageDescription, ChildrenTable, new_card, new_page, navigate_away_from_card
PageTable, StorageCard, StorageDescription, ChildrenTable,
new_card, new_page, navigate_away_from_card, register_crossref, get_crossrefs,
} from "../pages.jsx";
import { StorageUsageBar } from "../storage-controls.jsx";
import {
Expand Down Expand Up @@ -395,6 +396,14 @@ function make_btrfs_subvolume_page(parent, volume, subvol, path_prefix, subvols)
props: { subvol, mount_point, mismount_warning, block, fstab_config, forced_options },
actions,
});

if (subvol.id !== 5 && subvol.parent_uuid !== null)
register_crossref({
key: subvol.parent_uuid,
card,
size: mounted && <StorageUsageBar stats={use} short />,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This added line is not executed by any test.

});

const page = new_page(parent, card);
for (const sv of subvols) {
if (sv.parent && (sv.parent === subvol.id || sv.parent === subvol.fake_id)) {
Expand All @@ -404,6 +413,7 @@ function make_btrfs_subvolume_page(parent, volume, subvol, path_prefix, subvols)
}

const BtrfsSubvolumeCard = ({ card, subvol, mismount_warning, block, fstab_config, forced_options }) => {
const crossrefs = get_crossrefs(subvol.uuid);
return (
<StorageCard card={card} alert={mismount_warning &&
<MismountAlert warning={mismount_warning}
Expand All @@ -425,5 +435,17 @@ const BtrfsSubvolumeCard = ({ card, subvol, mismount_warning, block, fstab_confi
aria-label={_("btrfs subvolumes")}
page={card.page} />
</CardBody>
{crossrefs &&
<Card data-test-card-title="Snapshots">
<CardHeader>
<CardTitle component="h2">{_("Snapshots")}</CardTitle>
</CardHeader>
<CardBody className="contains-list">
<PageTable emptyCaption={_("No snapshots found")}
aria-label={_("snapshot")}
crossrefs={crossrefs} />
</CardBody>
</Card>
}
</StorageCard>);
};
14 changes: 10 additions & 4 deletions pkg/storaged/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,18 @@ export async function btrfs_poll() {
// ID 256 gen 7 parent 5 top level 5 path <FS_TREE>/one
// ID 257 gen 7 parent 256 top level 256 path one/two
// ID 258 gen 7 parent 257 top level 257 path <FS_TREE>/one/two/three/four
const output = await cockpit.spawn(["btrfs", "subvolume", "list", "-ap", mount_point], { superuser: "require", err: "message" });
const output = await cockpit.spawn(["btrfs", "subvolume", "list", "-apuq", mount_point], { superuser: "require", err: "message" });
const subvols = [{ pathname: "/", id: 5, parent: null }];
for (const line of output.split("\n")) {
const m = line.match(/ID (\d+).*parent (\d+).*path (<FS_TREE>\/)?(.*)/);
if (m)
subvols.push({ pathname: m[4], id: Number(m[1]), parent: Number(m[2]) });
const m = line.match(/ID (\d+).*parent (\d+).*parent_uuid (.*)uuid (.*) path (<FS_TREE>\/)?(.*)/);
if (m) {
// The parent uuid is the uuid of which this subvolume is a snapshot.
// https://github.com/torvalds/linux/blob/8d025e2092e29bfd13e56c78e22af25fac83c8ec/include/uapi/linux/btrfs.h#L885
let parent_uuid = m[3].trim();
// BTRFS_UUID_SIZE is 16
parent_uuid = parent_uuid.length < 16 ? null : parent_uuid;
subvols.push({ pathname: m[6], id: Number(m[1]), parent: Number(m[2]), uuid: m[4], parent_uuid });
}
}
uuids_subvols[uuid] = subvols;
} catch (err) {
Expand Down
29 changes: 29 additions & 0 deletions test/verify/check-storage-btrfs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,35 @@ class TestStorageBtrfs(storagelib.StorageCase):
b.wait_text(self.card_row_col("btrfs filesystem", row_name="home", col_index=3), "/mnt/home (not mounted)")
b.wait_text(self.card_row_col("btrfs filesystem", row_name="backups", col_index=3), "/mnt/backups (not mounted)")

def testSnapshot(self):
m = self.machine
b = self.browser

disk = self.add_ram_disk(size=128)
mount_point = "/run/butter"

snapshot_dir = f"{mount_point}/snapshots"
subdir = f"{mount_point}/subdir"

m.execute(f"""
mkfs.btrfs -L butter {disk}
mkdir -p {mount_point}
mount {disk} {mount_point}
echo '{disk} {mount_point} auto defaults 0 0' >> /etc/fstab
btrfs subvolume create {subdir} {snapshot_dir} {subdir}/foo
btrfs subvolume snapshot {subdir} {snapshot_dir}/snap-1
btrfs subvolume snapshot {mount_point} {snapshot_dir}/snap-2
""")

self.login_and_go("/storage")

self.click_card_row("Storage", name=os.path.basename(subdir))
b.wait_text(self.card_desc("btrfs subvolume", "Name"), "subdir")
b.wait_visible(self.card_row("Snapshots", name="snap-1"))
# Normal subvolume does not show under snapshots
b.wait_visible(self.card_row("btrfs subvolume", name="foo"))
b.wait_not_present(self.card_row("Snapshots", name="foo"))


if __name__ == '__main__':
testlib.test_main()