Skip to content

Commit e1fbb41

Browse files
committed
provisioning: Flesh out create-partitition with role
Exporting the role allows tooling to determine the intended mountpoint for a strategy's partitions. Signed-off-by: Ikey Doherty <[email protected]>
1 parent b5e1ebf commit e1fbb41

File tree

6 files changed

+105
-10
lines changed

6 files changed

+105
-10
lines changed

crates/provisioning/src/commands.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers
2+
// SPDX-FileCopyrightText: Copyright © 2025 AerynOS Developers
23
//
34
// SPDX-License-Identifier: MPL-2.0
45

@@ -11,7 +12,7 @@ mod find_disk;
1112
/// A command
1213
#[derive(Debug)]
1314
pub enum Command {
14-
CreatePartition,
15+
CreatePartition(Box<create_partition::Command>),
1516
CreatePartitionTable(Box<create_partition_table::Command>),
1617
FindDisk(Box<find_disk::Command>),
1718
}
@@ -22,7 +23,7 @@ type CommandExec = for<'a> fn(Context<'a>) -> Result<Command, crate::Error>;
2223
/// Map of command names to functions
2324
static COMMANDS: phf::Map<&'static str, CommandExec> = phf::phf_map! {
2425
"find-disk" => find_disk::parse,
25-
//"create-partition" => create_partition::parse,
26+
"create-partition" => create_partition::parse,
2627
"create-partition-table" => create_partition_table::parse,
2728
};
2829

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers
2+
// SPDX-FileCopyrightText: Copyright © 2025 AerynOS Developers
23
//
34
// SPDX-License-Identifier: MPL-2.0
45

5-
use super::Command;
6-
use crate::Context;
6+
use crate::{get_kdl_property, get_property_str, Context, FromKdlProperty, PartitionRole};
7+
8+
/// Command to create a partition
9+
#[derive(Debug)]
10+
pub struct Command {
11+
/// The disk ID to create the partition on
12+
pub disk: String,
13+
14+
/// The reference ID of the partition
15+
pub id: String,
16+
17+
/// The role, if any, of the partition
18+
pub role: Option<PartitionRole>,
19+
}
720

821
/// Generate a command to create a partition
9-
pub(crate) fn parse(_context: Context<'_>) -> Result<Command, crate::Error> {
10-
unimplemented!("Command not implemented");
22+
pub(crate) fn parse(context: Context<'_>) -> Result<super::Command, crate::Error> {
23+
let disk = get_property_str(context.node, "disk")?;
24+
let id = get_property_str(context.node, "id")?;
25+
let role = if let Ok(role) = get_kdl_property(context.node, "role") {
26+
Some(PartitionRole::from_kdl_property(role)?)
27+
} else {
28+
None
29+
};
30+
31+
// TODO: Load constraints etc
32+
Ok(super::Command::CreatePartition(Box::new(Command { disk, id, role })))
1133
}

crates/provisioning/src/types.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers
2+
// SPDX-FileCopyrightText: Copyright © 2025 AerynOS Developers
23
//
34
// SPDX-License-Identifier: MPL-2.0
45

@@ -10,6 +11,8 @@ use crate::Error;
1011

1112
mod partition_table;
1213
pub use partition_table::*;
14+
mod partition_role;
15+
pub use partition_role::*;
1316

1417
mod units;
1518
pub use units::*;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers
2+
// SPDX-FileCopyrightText: Copyright © 2025 AerynOS Developers
3+
//
4+
// SPDX-License-Identifier: MPL-2.0
5+
6+
use std::{fmt, str::FromStr};
7+
8+
use crate::kdl_value_to_string;
9+
10+
use super::FromKdlProperty;
11+
12+
/// The role assigned to a partition
13+
#[derive(Debug, PartialEq)]
14+
pub enum PartitionRole {
15+
/// Boot partition (usually ESP)
16+
Boot,
17+
18+
/// Extended boot partition (e.g. XBOOTLDR)
19+
ExtendedBoot,
20+
21+
/// Root filesystem
22+
Root,
23+
24+
/// Home directory mount
25+
Home,
26+
27+
/// Swap partition
28+
Swap,
29+
}
30+
31+
impl fmt::Display for PartitionRole {
32+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33+
match self {
34+
Self::Boot => f.write_str("boot"),
35+
Self::ExtendedBoot => f.write_str("extended-boot"),
36+
Self::Root => f.write_str("root"),
37+
Self::Home => f.write_str("home"),
38+
Self::Swap => f.write_str("swap"),
39+
}
40+
}
41+
}
42+
43+
impl FromStr for PartitionRole {
44+
type Err = crate::Error;
45+
46+
/// Attempt to convert a string to a partition role
47+
fn from_str(value: &str) -> Result<Self, Self::Err> {
48+
match value {
49+
"boot" => Ok(Self::Boot),
50+
"extended-boot" => Ok(Self::ExtendedBoot),
51+
"root" => Ok(Self::Root),
52+
"home" => Ok(Self::Home),
53+
"swap" => Ok(Self::Swap),
54+
_ => Err(crate::Error::UnknownVariant),
55+
}
56+
}
57+
}
58+
59+
impl FromKdlProperty<'_> for PartitionRole {
60+
fn from_kdl_property(entry: &kdl::KdlEntry) -> Result<Self, crate::Error> {
61+
let value = kdl_value_to_string(entry)?;
62+
let v = value.parse().map_err(|_| crate::UnsupportedValue {
63+
at: entry.span(),
64+
advice: Some("'boot', 'extended-boot', 'root', 'home' and 'swap' are supported".into()),
65+
})?;
66+
Ok(v)
67+
}
68+
}

crates/provisioning/src/types/partition_table.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers
2+
// SPDX-FileCopyrightText: Copyright © 2025 AerynOS Developers
23
//
34
// SPDX-License-Identifier: MPL-2.0
45

crates/provisioning/tests/use_whole_disk.kdl

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ strategy name="whole_disk" summary="Wipe and use an entire disk" {
1313
create-partition-table type="gpt" disk="root_disk"
1414

1515
// Create the ESP
16-
create-partition disk="root_disk" id="esp" {
16+
create-partition disk="root_disk" role="boot" id="esp" {
1717
constraints {
1818
min (GB)1
1919
max (GB)2
2020
}
21-
type (GUID)"LinuxRoot"
21+
type (GUID)"ESP"
2222
}
2323

2424
// Create xbootldr
25-
create-partition disk="root_disk" id="xbootldr" {
25+
create-partition disk="root_disk" role="extended-boot" id="xbootldr" {
2626
constraints {
2727
min (GB)2
2828
max (GB)4
@@ -47,7 +47,7 @@ strategy name="whole_disk_with_swap" inherits="whole_disk" \
4747
summary="Wipe disk, include a swap" \
4848
{
4949
// Create a swap partition in addition to the base strategy
50-
create-partition disk="root_disk" id="swap" {
50+
create-partition disk="root_disk" id="swap" role="swap" {
5151
constraints {
5252
min (GB)4
5353
max (GB)8

0 commit comments

Comments
 (0)