Debian Codegraph @

pkg/storaged/partitions/partition-table.jsx

raw
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
13 import { get_partitions, register_available_free_space } from "../utils.js";
14 import { StorageCard, ChildrenTable, new_page, new_card } from "../pages.jsx";
15 import { format_dialog } from "../block/format-dialog.jsx";
16 import { make_block_page } from "../block/create-pages.jsx";
17
18 import { make_partition_card, delete_partition } from "./partition.jsx";
19
20 const _ = cockpit.gettext;
21
22 function make_partition_pages(parent, block) {
23 const block_ptable = client.blocks_ptable[block.path];
24 let counter = 0;
25
26 function make_free_space_page(parent, start, size, enable_dos_extended) {
27 counter++;
28 const card = new_card({
29 page_name: _("Free space"),
30 page_key: "free-space-" + counter,
31 page_size: size,
32 actions: [
33 {
34 title: _("Create partition"),
35 action: () => format_dialog(client, block.path, start, size,
36 enable_dos_extended),
37 }
38 ],
39 });
40 new_page(parent, card);
41 }
42
43 function make_extended_partition_page(parent, partition) {
44 const card = new_card({
45 page_name: _("Extended partition"),
46 page_size: partition.size,
47 actions: [
48 { title: _("Delete"), action: () => delete_partition(partition.block, card), danger: true },
49 ]
50 });
51 const page = new_page(parent, card);
52 process_partitions(page, partition.partitions, false);
53 }
54
55 function process_partitions(parent, partitions, enable_dos_extended) {
56 let i;
57 let p;
58 for (i = 0; i < partitions.length; i++) {
59 p = partitions[i];
60 if (p.type == 'free') {
61 register_available_free_space(client, block, p);
62 make_free_space_page(parent, p.start, p.size, enable_dos_extended);
63 } else if (p.type == 'container')
64 make_extended_partition_page(parent, p);
65 else {
66 const card = make_partition_card(null, p.block);
67 make_block_page(parent, p.block, card);
68 }
69 }
70 }
71
72 process_partitions(parent, get_partitions(client, block),
73 block_ptable.Type == 'dos');
74 }
75
76 export function make_partition_table_page(parent, block, next_card, partitionable) {
77 const block_ptable = client.blocks_ptable[block.path];
78
79 const parts_card = new_card({
80 title: (block_ptable.Type
81 ? cockpit.format(_("$0 partitions"), block_ptable.Type.toLocaleUpperCase())
82 : _("Partitions")),
83 next: next_card,
84 component: partitionable ? PartitionsCard : UnexpectedPartitionsCard,
85 props: { },
86 });
87
88 const p = new_page(parent, parts_card, { sorted: false });
89 if (partitionable)
90 make_partition_pages(p, block);
91 }
92
93 const PartitionsCard = ({ card }) => {
94 return (
95 <StorageCard card={card}>
96 <ChildrenTable
97 emptyCaption={_("No partitions found")}
98 aria-label={_("Partitions")}
99 page={card.page} />
100 </StorageCard>
101 );
102 };
103
104 const UnexpectedPartitionsCard = ({ card }) => {
105 return (
106 <StorageCard card={card}>
107 <CardBody>
108 <Alert isInline isPlain variant="info" title={_("Unexpected partitions")}>
109 <p>{_("Partitions are not supported on this block device. If it is used as a disk for a virtual machine, the partitions must be managed by the operating system inside the virtual machine.")}</p>
110 </Alert>
111 </CardBody>
112 </StorageCard>
113 );
114 };