MBR Partition Management in Rust
Library:
Cargo.toml:
toml
[dependencies]
mbrman = { version = "0.1.0", default-features = false }
A library that allows managing GUID partition tables.
Reading all the partitions of a disk: ```rust let mut f = std::fs::File::open("tests/fixtures/disk1.img") .expect("could not open disk"); let mbr = mbrman::MBR::read_from(&mut f, 512) .expect("could not find MBR");
println!("Disk signature: {:?}", mbr.header.disk_signature);
for (i, p) in mbr.iter() {
if p.isused() {
println!("Partition #{}: type = {:?}, size = {} bytes, starting lba = {}",
i,
p.sys,
p.sectors * mbr.sectorsize,
p.startinglba);
}
}
Creating new partitions:
rust
let mut f = std::fs::File::open("tests/fixtures/disk1.img")
.expect("could not open disk");
let mut mbr = mbrman::MBR::readfrom(&mut f, 512)
.expect("could not find MBR");
let freepartitionnumber = mbr.iter().find(|(i, p)| p.isunused()).map(|(i, _)| i) .expect("no more places available"); let sectors = mbr.getmaximumpartitionsize() .expect("no more space available"); let startinglba = mbr.findoptimal_place(sectors) .expect("could not find a place to put the partition");
mbr[freepartitionnumber] = mbrman::MBRPartitionEntry {
boot: false,
firstchs: mbrman::CHS::empty(),
sys: 0x83,
lastchs: mbrman::CHS::empty(),
startinglba,
sectors,
};
Creating a new partition table with one entry that fills the entire disk:
rust
let ss = 512;
let data = vec![0; 100 * ss as usize];
let mut cur = std::io::Cursor::new(data);
let mut mbr = mbrman::MBR::newfrom(&mut cur, ss as u32, [0xff; 4])
.expect("could not create partition table");
mbr[1] = mbrman::MBRPartitionEntry { boot: false, firstchs: mbrman::CHS::empty(), sys: 0x83, lastchs: mbrman::CHS::empty(), startinglba: 1, sectors: mbr.disksize - 1, }; ```