Skip to content

Commit d1159a2

Browse files
committed
partitioning/writer: Clear first 1MiB of disk for old remnants
During testing gparted still found the old ISO remnants before GPT within the protective MBR at LBA0, so we now clear that out when formatting the disk. Signed-off-by: Ikey Doherty <[email protected]>
1 parent 69570f6 commit d1159a2

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

crates/partitioning/src/writer.rs

+10-1
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, io::Write};
6+
use std::{fs, io::{Write, Seek}};
77

88
use disks::BlockDevice;
99
use gpt::{mbr, partition_types, GptConfig};
@@ -100,6 +100,15 @@ impl<'a> DiskWriter<'a> {
100100
fn apply_changes(&self, device: &mut fs::File, writable: bool) -> Result<(), WriteError> {
101101
let mut gpt_table = if self.planner.wipe_disk() {
102102
if writable {
103+
// Zero out the first MiB to clear any old partition tables and boot sectors
104+
// Write 16*64 KiB = 1 MiB of zeros to the start of the disk
105+
let zeros = [0u8; 65_536];
106+
device.seek(std::io::SeekFrom::Start(0))?;
107+
for _ in 0..16 {
108+
device.write_all(&zeros)?;
109+
}
110+
device.flush()?;
111+
103112
let mbr = mbr::ProtectiveMBR::with_lb_size(
104113
u32::try_from((self.device.size() / 512) - 1).unwrap_or(0xFF_FF_FF_FF),
105114
);

0 commit comments

Comments
 (0)