Skip to content

Commit 8a40893

Browse files
committed
partitioning/blkpg: Explicitly control blkpg rescan lifetime
Signed-off-by: Ikey Doherty <[email protected]>
1 parent d1159a2 commit 8a40893

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

crates/partitioning/src/blkpg.rs

+42-12
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,17 @@ where
130130
Ok(())
131131
}
132132

133-
/// Updates kernel partition representations to match the GPT table
133+
/// Removes all kernel partitions for the specified block device
134134
///
135135
/// # Arguments
136136
/// * `path` - Path to the block device
137137
///
138138
/// # Returns
139139
/// `Result<(), Error>` indicating success or partition operation failure
140-
pub fn sync_gpt_partitions<P: AsRef<Path>>(path: P) -> Result<(), Error> {
141-
info!("Initiating GPT partition synchronization for {:?}", path.as_ref());
140+
pub fn remove_kernel_partitions<P: AsRef<Path>>(path: P) -> Result<(), Error> {
141+
debug!("Beginning partition cleanup process for {:?}", path.as_ref());
142142
let file = File::open(&path)?;
143143

144-
// Read GPT table
145-
debug!("Reading GPT partition table");
146-
let gpt = gpt::GptConfig::new().writable(false).open(&path)?;
147-
let partitions = gpt.partitions();
148-
let block_size = 512;
149-
info!("Located {} partitions (block size: {})", partitions.len(), block_size);
150-
151-
debug!("Beginning partition cleanup process");
152-
153144
// Find the disk for enumeration purposes
154145
let base_name = path
155146
.as_ref()
@@ -164,6 +155,28 @@ pub fn sync_gpt_partitions<P: AsRef<Path>>(path: P) -> Result<(), Error> {
164155
let _ = delete_partition(file.as_raw_fd(), partition.number as i32);
165156
}
166157

158+
info!("Successfully removed all kernel partitions");
159+
Ok(())
160+
}
161+
162+
/// Creates kernel partitions based on the current GPT table
163+
///
164+
/// # Arguments
165+
/// * `path` - Path to the block device
166+
///
167+
/// # Returns
168+
/// `Result<(), Error>` indicating success or partition operation failure
169+
pub fn create_kernel_partitions<P: AsRef<Path>>(path: P) -> Result<(), Error> {
170+
info!("Creating kernel partitions from GPT for {:?}", path.as_ref());
171+
let file = File::open(&path)?;
172+
173+
// Read GPT table
174+
debug!("Reading GPT partition table");
175+
let gpt = gpt::GptConfig::new().writable(false).open(&path)?;
176+
let partitions = gpt.partitions();
177+
let block_size = 512;
178+
info!("Located {} partitions (block size: {})", partitions.len(), block_size);
179+
167180
// Add partitions from GPT
168181
debug!("Beginning partition creation from GPT table");
169182
for (i, partition) in partitions.iter() {
@@ -175,6 +188,23 @@ pub fn sync_gpt_partitions<P: AsRef<Path>>(path: P) -> Result<(), Error> {
175188
)?;
176189
}
177190

191+
info!("GPT partition creation completed successfully");
192+
Ok(())
193+
}
194+
195+
/// Updates kernel partition representations to match the GPT table
196+
///
197+
/// # Arguments
198+
/// * `path` - Path to the block device
199+
///
200+
/// # Returns
201+
/// `Result<(), Error>` indicating success or partition operation failure
202+
pub fn sync_gpt_partitions<P: AsRef<Path>>(path: P) -> Result<(), Error> {
203+
info!("Initiating GPT partition synchronization for {:?}", path.as_ref());
204+
205+
remove_kernel_partitions(&path)?;
206+
create_kernel_partitions(&path)?;
207+
178208
info!("GPT partition synchronization completed successfully");
179209
Ok(())
180210
}

crates/partitioning/src/writer.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,28 @@
33
//
44
// SPDX-License-Identifier: MPL-2.0
55

6-
use std::{fs, io::{Write, Seek}};
6+
use std::{
7+
fs,
8+
io::{Seek, Write},
9+
};
710

811
use disks::BlockDevice;
912
use gpt::{mbr, partition_types, GptConfig};
1013
use thiserror::Error;
1114

1215
use crate::{
16+
blkpg,
1317
planner::{Change, Planner},
1418
GptAttributes,
1519
};
1620

1721
/// Errors that can occur when writing changes to disk
1822
#[derive(Debug, Error)]
1923
pub enum WriteError {
24+
// A blkpg error
25+
#[error("error syncing partitions: {0}")]
26+
Blkpg(#[from] blkpg::Error),
27+
2028
/// A partition ID was used multiple times
2129
#[error("Duplicate partition ID: {0}")]
2230
DuplicatePartitionId(u32),
@@ -98,6 +106,11 @@ impl<'a> DiskWriter<'a> {
98106
/// - Creating or opening the GPT table
99107
/// - Applying each change in sequence
100108
fn apply_changes(&self, device: &mut fs::File, writable: bool) -> Result<(), WriteError> {
109+
// Remove known partitions pre wipe
110+
if writable {
111+
blkpg::remove_kernel_partitions(self.device.device())?;
112+
}
113+
101114
let mut gpt_table = if self.planner.wipe_disk() {
102115
if writable {
103116
// Zero out the first MiB to clear any old partition tables and boot sectors
@@ -182,6 +195,7 @@ impl<'a> DiskWriter<'a> {
182195
if writable {
183196
let original = gpt_table.write()?;
184197
original.sync_all()?;
198+
blkpg::create_kernel_partitions(self.device.device())?;
185199
}
186200

187201
Ok(())

0 commit comments

Comments
 (0)