Expand description
A pure-Rust library to work with GPT partition tables.
It provides support for manipulating (R/W) GPT headers and partition tables. Raw disk devices as well as disk images are supported.
use gpt;
use std::convert::TryFrom;
use std::io::{Read, Seek};
fn inspect_disk() {
let diskpath = std::path::Path::new("/dev/sdz");
let disk = gpt::GptConfig::new()
.open(diskpath).expect("failed to open disk");
println!("Disk header: {:#?}", disk.primary_header());
println!("Partition layout: {:#?}", disk.partitions());
}
fn create_partition() {
let diskpath = std::path::Path::new("/tmp/chris.img");
let mut disk = gpt::GptConfig::new().writable(true)
.create(diskpath).expect("failed to open disk");
let result = disk.add_partition(
"rust_partition",
100,
gpt::partition_types::LINUX_FS,
0,
None
);
disk.write().unwrap();
}
/// Demonstrates how to create a new partition table without anything pre-existing
fn create_partition_in_ram() {
const TOTAL_BYTES: usize = 1024 * 64;
let mut mem_device = std::io::Cursor::new(vec![0u8; TOTAL_BYTES]);
// Create a protective MBR at LBA0
let mbr = gpt::mbr::ProtectiveMBR::with_lb_size(
u32::try_from((TOTAL_BYTES / 512) - 1).unwrap_or(0xFF_FF_FF_FF));
mbr.overwrite_lba0(&mut mem_device).expect("failed to write MBR");
let mut gdisk = gpt::GptConfig::default()
.writable(true)
.logical_block_size(gpt::disk::LogicalBlockSize::Lb512)
.create_from_device(mem_device, None)
.expect("failed to crate GptDisk");
// At this point, gdisk.primary_header() and gdisk.backup_header() are populated...
gdisk.add_partition("test1", 1024 * 12, gpt::partition_types::BASIC, 0, None)
.expect("failed to add test1 partition");
gdisk.add_partition("test2", 1024 * 18, gpt::partition_types::LINUX_FS, 0, None)
.expect("failed to add test2 partition");
// Write the partition table and take ownership of
// the underlying memory buffer-backed block device
let mut mem_device = gdisk.write().expect("failed to write partition table");
// Read the written bytes out of the memory buffer device
mem_device.seek(std::io::SeekFrom::Start(0)).expect("failed to seek");
let mut final_bytes = vec![0u8; TOTAL_BYTES];
mem_device.read_exact(&mut final_bytes)
.expect("failed to read contents of memory device");
}
create_partition_in_ram();
Modules§
- disk
- Disk-related types and helper functions.
- header
- GPT-header object and helper functions.
- mbr
- MBR-related types and helper functions.
- partition
- Partition-related types and helper functions.
- partition_
types - Parition type constants
Structs§
Enums§
- GptError
- Errors returned when interacting with a Gpt Disk.
Traits§
- Disk
Device - A generic device that we can read/write partitions from/to.
Type Aliases§
- Disk
Device Object - A dynamic trait object that is used by GptDisk for reading/writing/seeking.