Skip to content

Commit b868ad8

Browse files
committed
Multiple fies to make partitioning now *mostly* work
Signed-off-by: Ikey Doherty <[email protected]>
1 parent 4c2b7c0 commit b868ad8

File tree

4 files changed

+29
-40
lines changed

4 files changed

+29
-40
lines changed

crates/partitioning/src/planner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ impl Planner {
445445

446446
/// Clear all planned changes
447447
pub fn reset(&mut self) {
448-
debug!("Resetting all planned changes");
448+
eprintln!("Resetting all planned changes");
449449
self.changes.clear();
450450
}
451451

crates/partitioning/src/writer.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44
// SPDX-License-Identifier: MPL-2.0
55

6-
use std::fs;
6+
use std::{fs, io::Write};
77

88
use disks::BlockDevice;
99
use gpt::{mbr, partition_types, GptConfig};
@@ -14,10 +14,6 @@ use crate::planner::{Change, Planner};
1414
/// Errors that can occur when writing changes to disk
1515
#[derive(Debug, Error)]
1616
pub enum WriteError {
17-
/// Device size has changed since the plan was created
18-
#[error("Device size changed since planning")]
19-
DeviceSizeChanged,
20-
2117
/// A partition ID was used multiple times
2218
#[error("Duplicate partition ID: {0}")]
2319
DuplicatePartitionId(u32),
@@ -55,7 +51,7 @@ impl<'a> DiskWriter<'a> {
5551
.read(true)
5652
.write(false)
5753
.open(self.device.device())?;
58-
self.validate_changes(&device)?;
54+
self.validate_changes()?;
5955
self.apply_changes(&mut device, false)?;
6056
Ok(())
6157
}
@@ -67,21 +63,16 @@ impl<'a> DiskWriter<'a> {
6763
.write(true)
6864
.open(self.device.device())?;
6965

70-
self.validate_changes(&device)?;
66+
self.validate_changes()?;
7167
self.apply_changes(&mut device, true)?;
68+
device.flush()?;
7269
Ok(())
7370
}
7471

7572
/// Validate all planned changes before applying them by checking:
7673
/// - Device size matches the planned size
7774
/// - No duplicate partition IDs exist
78-
fn validate_changes(&self, device: &fs::File) -> Result<(), WriteError> {
79-
// Verify device size matches what we planned for
80-
let metadata = device.metadata()?;
81-
if metadata.len() != self.device.size() {
82-
return Err(WriteError::DeviceSizeChanged);
83-
}
84-
75+
fn validate_changes(&self) -> Result<(), WriteError> {
8576
// Verify partition IDs don't conflict
8677
let mut used_ids = std::collections::HashSet::new();
8778
for change in self.planner.changes() {
@@ -109,20 +100,28 @@ impl<'a> DiskWriter<'a> {
109100
let mbr = mbr::ProtectiveMBR::with_lb_size(
110101
u32::try_from((self.device.size() / 512) - 1).unwrap_or(0xFF_FF_FF_FF),
111102
);
103+
eprintln!("size is {}", self.device.size());
112104
mbr.overwrite_lba0(device)?;
113105
}
114106

115-
GptConfig::default()
107+
let mut c = GptConfig::default()
116108
.writable(writable)
117109
.logical_block_size(gpt::disk::LogicalBlockSize::Lb512)
118-
.create_from_device(device, None)?
110+
.create_from_device(device, None)?;
111+
112+
if writable {
113+
c.write_inplace()?;
114+
}
115+
c
119116
} else {
120117
GptConfig::default().writable(writable).open_from_device(device)?
121118
};
122119

123120
let layout = self.planner.current_layout();
124121
let changes = self.planner.changes();
125122

123+
eprintln!("Changes: {:?}", changes);
124+
126125
for change in changes {
127126
match change {
128127
Change::DeletePartition {
@@ -151,7 +150,7 @@ impl<'a> DiskWriter<'a> {
151150
}
152151
}
153152

154-
eprintln!("GPT is now: {gpt_table:?}");
153+
eprintln!("### GPT is now: {gpt_table:?}");
155154

156155
for region in layout.iter() {
157156
eprintln!(
@@ -160,6 +159,10 @@ impl<'a> DiskWriter<'a> {
160159
);
161160
}
162161

162+
if writable {
163+
gpt_table.write_inplace()?;
164+
}
165+
163166
Ok(())
164167
}
165168
}

crates/provisioning/src/provisioner.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::collections::HashMap;
88
use disks::BlockDevice;
99
use log::{debug, info, trace, warn};
1010
use partitioning::{
11-
planner::Planner,
11+
planner::{Planner, PARTITION_ALIGNMENT},
1212
strategy::{AllocationStrategy, PartitionRequest, SizeRequirement, Strategy},
1313
};
1414

@@ -31,9 +31,9 @@ pub struct Plan<'a> {
3131

3232
#[derive(Debug, Clone)]
3333
pub struct DevicePlan<'a> {
34-
device: &'a BlockDevice,
35-
planner: Planner,
36-
strategy: Strategy,
34+
pub device: &'a BlockDevice,
35+
pub planner: Planner,
36+
pub strategy: Strategy,
3737
}
3838

3939
impl Default for Provisioner {
@@ -134,7 +134,9 @@ impl Provisioner {
134134
command.name.clone(),
135135
DevicePlan {
136136
device,
137-
planner: Planner::new(device),
137+
planner: Planner::new(device)
138+
.with_start_offset(PARTITION_ALIGNMENT)
139+
.with_end_offset(device.size() - PARTITION_ALIGNMENT),
138140
strategy: Strategy::new(AllocationStrategy::LargestFree),
139141
},
140142
);

crates/provisioning/tests/use_whole_disk.kdl

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,12 @@ strategy name="whole_disk" summary="Wipe and use an entire disk" {
4040
// Create a partition for rootfs
4141
create-partition disk="root_disk" id="root" role="root" {
4242
constraints {
43-
min (GiB)30
43+
min (GiB)25
4444
max (GiB)120
4545
}
4646
filesystem {
4747
type "any"
4848
label "ROOT"
4949
}
5050
}
51-
52-
// find a partition (bound to root_disk here)
53-
// find-partition guid="$ESP"
54-
}
55-
56-
strategy name="whole_disk_with_swap" inherits="whole_disk" \
57-
summary="Wipe disk, include a swap" \
58-
{
59-
// Create a swap partition in addition to the base strategy
60-
create-partition disk="root_disk" id="swap" role="swap" {
61-
constraints {
62-
min (GiB)4
63-
max (GiB)8
64-
}
65-
type (GUID)"linux-swap"
66-
}
6751
}

0 commit comments

Comments
 (0)