Skip to content

Commit c00a751

Browse files
committed
partitioning: Only use the BlockDevice APIs now
No more direct / concrete disk details, any block device should be usable from partition and planning API as long as it has an actual size and we're able to find some partitions on it if they exist. Note in future we'll also expose partition table specifics such as an existing table, the first+last LBAs, actual block size rather than the assumed 512, etc. Signed-off-by: Ikey Doherty <[email protected]>
1 parent 1022d5b commit c00a751

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

crates/partitioning/src/planner.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! - Track and undo changes
1313
//! - Validate that changes won't conflict with existing partitions
1414
15-
use disks::Disk;
15+
use disks::BlockDevice;
1616
use log::{debug, warn};
1717
use std::collections::VecDeque;
1818
use thiserror::Error;
@@ -46,8 +46,8 @@ pub enum Change {
4646

4747
/// A disk partitioning planner.
4848
pub struct Planner {
49-
/// The original disk state that we're planning changes for
50-
disk: Disk,
49+
/// The original block device state that we're planning changes for
50+
device: BlockDevice,
5151
/// Stack of changes that can be undone
5252
changes: VecDeque<Change>,
5353
/// Original partition layout for reference
@@ -190,12 +190,16 @@ impl Change {
190190

191191
impl Planner {
192192
/// Creates a new partitioning planner for the given disk.
193-
pub fn new(disk: Disk) -> Self {
194-
debug!("Creating new partition planner for disk of size {}", disk.size());
195-
// Extract original regions from disk
196-
let original_regions = disk.partitions().iter().map(|p| Region::new(p.start, p.end)).collect();
193+
pub fn new(device: BlockDevice) -> Self {
194+
debug!("Creating new partition planner for device of size {}", device.size());
195+
// Extract original regions from device
196+
let original_regions = device
197+
.partitions()
198+
.iter()
199+
.map(|p| Region::new(p.start, p.end))
200+
.collect();
197201
Self {
198-
disk,
202+
device,
199203
changes: VecDeque::new(),
200204
original_regions,
201205
}
@@ -207,7 +211,7 @@ impl Planner {
207211
return "No pending changes".to_string();
208212
}
209213

210-
let disk_size = self.disk.size();
214+
let disk_size = self.device.size();
211215
let mut description = "Pending changes:\n".to_string();
212216

213217
for (i, change) in self.changes.iter().enumerate() {
@@ -269,7 +273,7 @@ impl Planner {
269273
debug!("Aligned positions: {}..{}", aligned_start, aligned_end);
270274

271275
// Validate bounds
272-
if aligned_end > self.disk.size() {
276+
if aligned_end > self.device.size() {
273277
warn!("Partition would exceed disk bounds");
274278
return Err(PlanError::RegionOutOfBounds {
275279
start: aligned_start,
@@ -318,7 +322,7 @@ impl Planner {
318322
warn!("Invalid partition index {}", index);
319323
return Err(PlanError::RegionOutOfBounds {
320324
start: 0,
321-
end: self.disk.size(),
325+
end: self.device.size(),
322326
});
323327
}
324328

@@ -354,9 +358,9 @@ impl Planner {
354358
&self.changes
355359
}
356360

357-
/// Get the original disk state
358-
pub fn original_disk(&self) -> &Disk {
359-
&self.disk
361+
/// Get the original block device state
362+
pub fn original_device(&self) -> &BlockDevice {
363+
&self.device
360364
}
361365
/// Plan to initialize a clean partition layout
362366
pub fn plan_initialize_disk(&mut self) -> Result<(), PlanError> {
@@ -399,7 +403,7 @@ mod tests {
399403
#[test]
400404
fn test_fresh_installation() {
401405
let disk = create_mock_disk();
402-
let mut planner = Planner::new(Disk::Mock(disk));
406+
let mut planner = Planner::new(BlockDevice::mock_device(disk));
403407

404408
// Create typical Linux partition layout with absolute positions
405409
// - 0 -> 512MB: EFI System Partition
@@ -421,7 +425,7 @@ mod tests {
421425
#[test]
422426
fn test_dual_boot_with_windows() {
423427
let disk = create_windows_disk();
424-
let mut planner = Planner::new(Disk::Mock(disk));
428+
let mut planner = Planner::new(BlockDevice::mock_device(disk));
425429

426430
// Available space starts after Windows partitions (~ 200.6GB)
427431
let start = 200 * GB + 616 * MB;
@@ -448,7 +452,7 @@ mod tests {
448452
disk.add_partition(512 * MB, 4 * GB + 512 * MB); // Swap: 512MB -> 4.5GB
449453
disk.add_partition(4 * GB + 512 * MB, 500 * GB); // Root: 4.5GB -> 500GB
450454

451-
let mut planner = Planner::new(Disk::Mock(disk));
455+
let mut planner = Planner::new(BlockDevice::mock_device(disk));
452456

453457
// Delete old Linux partitions
454458
assert!(planner.plan_delete_partition(1).is_ok()); // Delete swap
@@ -471,7 +475,7 @@ mod tests {
471475
#[test]
472476
fn test_region_validation() {
473477
let disk = create_mock_disk();
474-
let mut planner = Planner::new(Disk::Mock(disk));
478+
let mut planner = Planner::new(BlockDevice::mock_device(disk));
475479

476480
// Test out of bounds
477481
assert!(matches!(
@@ -490,7 +494,7 @@ mod tests {
490494
#[test]
491495
fn test_undo_operations() {
492496
let disk = create_mock_disk();
493-
let mut planner = Planner::new(Disk::Mock(disk));
497+
let mut planner = Planner::new(BlockDevice::mock_device(disk));
494498

495499
// Add some partitions
496500
assert!(planner.plan_add_partition(0, 100 * GB).is_ok());
@@ -512,7 +516,7 @@ mod tests {
512516
#[test]
513517
fn test_partition_boundaries() {
514518
let disk = create_mock_disk();
515-
let mut planner = Planner::new(Disk::Mock(disk));
519+
let mut planner = Planner::new(BlockDevice::mock_device(disk));
516520

517521
// Add first partition from 0 to 100GB
518522
assert!(planner.plan_add_partition(0, 100 * GB).is_ok());

crates/partitioning/src/strategy.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl Strategy {
8888

8989
/// Find available free regions on the disk
9090
fn find_free_regions(&self, planner: &Planner) -> Vec<Region> {
91-
let disk_size = planner.original_disk().size();
91+
let disk_size = planner.original_device().size();
9292
let mut regions = Vec::new();
9393
let mut current = 0;
9494

@@ -150,7 +150,7 @@ impl Strategy {
150150
AllocationStrategy::InitializeWholeDisk => {
151151
// Clear existing partitions and start fresh
152152
planner.plan_initialize_disk()?;
153-
Region::new(0, planner.original_disk().size())
153+
Region::new(0, planner.original_device().size())
154154
}
155155
AllocationStrategy::LargestFree => {
156156
let free_regions = self.find_free_regions(planner);
@@ -247,7 +247,7 @@ impl Strategy {
247247
mod tests {
248248
use super::*;
249249
use crate::planner::Planner;
250-
use disks::{mock::MockDisk, Disk};
250+
use disks::{mock::MockDisk, BlockDevice};
251251
use test_log::test;
252252

253253
const MB: u64 = 1024 * 1024;
@@ -322,7 +322,7 @@ mod tests {
322322
fn test_uefi_clean_install() {
323323
// Test case: Clean UEFI installation with separate /home
324324
let disk = create_test_disk();
325-
let mut planner = Planner::new(Disk::Mock(disk));
325+
let mut planner = Planner::new(BlockDevice::mock_device(disk));
326326
let mut strategy = Strategy::new(AllocationStrategy::InitializeWholeDisk);
327327

328328
// Standard UEFI layout with separate /home
@@ -356,7 +356,7 @@ mod tests {
356356
disk.add_partition(100 * MB, 116 * MB); // MSR
357357
disk.add_partition(116 * MB, 200 * GB); // Windows
358358

359-
let mut planner = Planner::new(Disk::Mock(disk));
359+
let mut planner = Planner::new(BlockDevice::mock_device(disk));
360360
let mut strategy = Strategy::new(AllocationStrategy::LargestFree);
361361

362362
// Standard Linux layout using remaining space
@@ -375,7 +375,7 @@ mod tests {
375375
fn test_minimal_server_install() {
376376
// Test case: Minimal server installation with single root partition
377377
let disk = create_test_disk();
378-
let mut planner = Planner::new(Disk::Mock(disk));
378+
let mut planner = Planner::new(BlockDevice::mock_device(disk));
379379
let mut strategy = Strategy::new(AllocationStrategy::InitializeWholeDisk);
380380

381381
// Simple layout - just boot and root

0 commit comments

Comments
 (0)