Skip to content

Commit fa9ef75

Browse files
committed
storage: Offer global "Create partition" action
1 parent 20530fc commit fa9ef75

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed

pkg/storaged/block/actions.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import cockpit from "cockpit";
2121
import client from "../client";
2222

2323
import { format_disk } from "./format-disk-dialog.jsx";
24-
import { format_dialog, add_encryption_dialog, encrypted_format_dialog } from "./format-dialog.jsx";
24+
import { format_dialog } from "./format-dialog.jsx";
2525
import { erase_dialog } from "./erase-dialog.jsx";
2626

2727
const _ = cockpit.gettext;
@@ -37,7 +37,7 @@ export function block_actions(block, kind) {
3737
if (kind != "crypto") {
3838
actions.push({
3939
title: _("Add encryption"),
40-
action: () => add_encryption_dialog(client, block),
40+
action: () => format_dialog(block, { add_encryption: true }),
4141
excuse,
4242
});
4343
}
@@ -54,14 +54,14 @@ export function block_actions(block, kind) {
5454
if (kind == "crypto") {
5555
actions.push({
5656
title: _("Format cleartext device"),
57-
action: () => encrypted_format_dialog(client, block),
57+
action: () => format_dialog(block, { is_encrypted: true }),
5858
primary: true,
5959
excuse,
6060
});
6161
} else {
6262
actions.push({
6363
title: _("Format as filesystem"),
64-
action: () => format_dialog(client, block.path),
64+
action: () => format_dialog(block),
6565
primary: true,
6666
excuse,
6767
});

pkg/storaged/block/format-dialog.jsx

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,6 @@ export function initial_mount_options(client, block) {
8181
return initial_tab_options(client, block, true);
8282
}
8383

84-
export function format_dialog(client, path, start, size, enable_dos_extended) {
85-
return format_dialog_internal(client, client.blocks[path], { start, size, enable_dos_extended });
86-
}
87-
88-
export function add_encryption_dialog(client, block) {
89-
return format_dialog_internal(client, block, { add_encryption: true });
90-
}
91-
92-
export function encrypted_format_dialog(client, block) {
93-
return format_dialog_internal(client, block, { is_encrypted: true });
94-
}
95-
9684
function find_root_fsys_block() {
9785
const root = client.anaconda?.mount_point_prefix || "/";
9886
for (const p in client.blocks) {
@@ -104,14 +92,14 @@ function find_root_fsys_block() {
10492
return null;
10593
}
10694

107-
function format_dialog_internal(client, block, options) {
108-
const { start, size, enable_dos_extended, add_encryption } = options;
95+
export function format_dialog(block, options) {
96+
const { free_spaces, enable_dos_extended, add_encryption } = options || { };
10997
const is_already_encrypted = options.is_encrypted;
11098
const block_part = client.blocks_part[block.path];
11199
const block_ptable = client.blocks_ptable[block.path] || client.blocks_ptable[block_part?.Table];
112100
const content_block = block.IdUsage == "crypto" ? client.blocks_cleartext[block.path] : block;
113101

114-
const create_partition = (start !== undefined);
102+
const create_partition = (free_spaces !== undefined);
115103

116104
let title;
117105
if (add_encryption)
@@ -282,18 +270,20 @@ function format_dialog_internal(client, block, options) {
282270
];
283271
}
284272

273+
let max_size = 0;
274+
if (create_partition)
275+
max_size = Math.max(...free_spaces.map(f => f.size));
276+
285277
dialog_open({
286278
Title: title,
287279
Teardown: TeardownMessage(usage),
288280
Fields: [
289281
SizeSlider("size", _("Size"),
290282
{
291-
value: size,
292-
max: size,
283+
value: max_size,
284+
max: max_size,
293285
round: 1024 * 1024,
294-
visible: function () {
295-
return create_partition;
296-
}
286+
visible: () => create_partition,
297287
}),
298288
SelectOne("type", _("Type"),
299289
{
@@ -497,6 +487,13 @@ function format_dialog_internal(client, block, options) {
497487

498488
function format() {
499489
if (create_partition) {
490+
let start = free_spaces[0].start;
491+
for (const fs of free_spaces) {
492+
if (fs.size >= vals.size) {
493+
start = fs.start;
494+
break;
495+
}
496+
}
500497
if (type == "dos-extended")
501498
return block_ptable.CreatePartition(start, vals.size, "0x05", "", { });
502499
else

pkg/storaged/partitions/partition-table.jsx

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { make_partition_card, delete_partition } from "./partition.jsx";
3232

3333
const _ = cockpit.gettext;
3434

35-
function make_partition_pages(parent, block) {
35+
function make_partition_pages(parent, block, partitions) {
3636
const block_ptable = client.blocks_ptable[block.path];
3737
let counter = 0;
3838

@@ -45,8 +45,7 @@ function make_partition_pages(parent, block) {
4545
actions: [
4646
{
4747
title: _("Create partition"),
48-
action: () => format_dialog(client, block.path, start, size,
49-
enable_dos_extended),
48+
action: () => format_dialog(block, { free_spaces: [{ start, size }], enable_dos_extended }),
5049
}
5150
],
5251
});
@@ -80,12 +79,24 @@ function make_partition_pages(parent, block) {
8079
}
8180
}
8281

83-
process_partitions(parent, get_partitions(client, block),
84-
block_ptable.Type == 'dos');
82+
process_partitions(parent, partitions, block_ptable.Type == 'dos');
83+
}
84+
85+
function get_free_spaces(partitions) {
86+
let result = [];
87+
for (const p of partitions) {
88+
if (p.type == 'free')
89+
result.push({ start: p.start, size: p.size });
90+
else if (p.type == 'container')
91+
result = result.concat(get_free_spaces(p.partitions));
92+
}
93+
return result;
8594
}
8695

8796
export function make_partition_table_page(parent, block, next_card) {
8897
const block_ptable = client.blocks_ptable[block.path];
98+
const partitions = get_partitions(client, block);
99+
const free_spaces = get_free_spaces(partitions);
89100

90101
const parts_card = new_card({
91102
title: (block_ptable.Type
@@ -94,10 +105,18 @@ export function make_partition_table_page(parent, block, next_card) {
94105
next: next_card,
95106
component: PartitionsCard,
96107
props: { },
108+
actions: [
109+
{
110+
title: _("Create partition"),
111+
action: () => format_dialog(block, { free_spaces, enable_dos_extended: false }),
112+
primary: true,
113+
excuse: free_spaces.length == 0 ? _("No free space") : null,
114+
},
115+
],
97116
});
98117

99118
const p = new_page(parent, parts_card, { sorted: false });
100-
make_partition_pages(p, block);
119+
make_partition_pages(p, block, partitions);
101120
}
102121

103122
const PartitionsCard = ({ card }) => {

0 commit comments

Comments
 (0)