Skip to main content

DiskGeometry

Struct DiskGeometry 

Source
pub struct DiskGeometry {
    pub block_size: u32,
    pub total_blocks: u64,
    pub physical_block_size: Option<u32>,
}
Expand description

Disk geometry information.

Contains information about the disk’s logical and physical block sizes, total capacity, and provides utilities for calculating alignment.

Fields§

§block_size: u32

Logical block (sector) size in bytes.

§total_blocks: u64

Total number of blocks on the disk.

§physical_block_size: Option<u32>

Physical sector size (for alignment), if different from logical.

Modern Advanced Format drives have 4096-byte physical sectors but may present 512-byte logical sectors for compatibility.

Implementations§

Source§

impl DiskGeometry

Source

pub const STANDARD_BLOCK_SIZE: u32 = 512

Standard logical block size (512 bytes).

Source

pub const ADVANCED_FORMAT_BLOCK_SIZE: u32 = 4096

Advanced Format block size (4096 bytes).

Source

pub const DEFAULT_ALIGNMENT_BYTES: u64

Default partition alignment (1 MiB in bytes).

1 MiB alignment is the modern standard for optimal performance on both traditional and Advanced Format drives.

Source

pub const fn standard(total_blocks: u64) -> Self

Creates geometry for a standard 512-byte sector disk.

§Arguments
  • total_blocks - Total number of 512-byte sectors on the disk
Source

pub const fn advanced_format(total_blocks: u64) -> Self

Creates geometry for a 4K sector disk (Advanced Format).

§Arguments
  • total_blocks - Total number of 4096-byte sectors on the disk
Source

pub const fn emulated_512(total_blocks: u64) -> Self

Creates geometry for a 512e disk (512-byte logical, 4K physical).

These disks present 512-byte logical sectors for compatibility but have 4096-byte physical sectors internally.

§Arguments
  • total_blocks - Total number of 512-byte logical sectors
Source

pub const fn new( block_size: u32, total_blocks: u64, physical_block_size: Option<u32>, ) -> Self

Creates a new DiskGeometry with custom parameters.

Source

pub const fn total_bytes(&self) -> u64

Returns the total disk size in bytes.

Source

pub const fn alignment_boundary(&self) -> u32

Returns the effective alignment boundary in bytes.

This is the physical block size if set, otherwise the logical block size.

Source

pub const fn default_alignment(&self) -> u64

Returns the default partition alignment in sectors.

This is 1 MiB expressed in sectors for the current block size. For 512-byte sectors, this is 2048 sectors. For 4096-byte sectors, this is 256 sectors.

Source

pub const fn align_up(&self, lba: u64, alignment_sectors: u64) -> u64

Aligns an LBA up to the next alignment boundary.

§Arguments
  • lba - The LBA to align
  • alignment_sectors - The alignment boundary in sectors
§Returns

The LBA rounded up to the next multiple of alignment_sectors.

Source

pub const fn align_down(&self, lba: u64, alignment_sectors: u64) -> u64

Aligns an LBA down to the previous alignment boundary.

§Arguments
  • lba - The LBA to align
  • alignment_sectors - The alignment boundary in sectors
§Returns

The LBA rounded down to the previous multiple of alignment_sectors.

Source

pub const fn is_aligned(&self, lba: u64, alignment_sectors: u64) -> bool

Checks if an LBA is properly aligned.

§Arguments
  • lba - The LBA to check
  • alignment_sectors - The alignment boundary in sectors
Source

pub const fn gpt_first_usable_lba( &self, num_entries: u32, entry_size: u32, ) -> u64

Calculates the first usable LBA for GPT partitions.

This accounts for:

  • MBR (1 sector)
  • GPT header (1 sector)
  • Partition entry array
§Arguments
  • num_entries - Number of partition entries (typically 128)
  • entry_size - Size of each entry in bytes (typically 128)
Source

pub const fn gpt_first_usable_lba_aligned( &self, num_entries: u32, entry_size: u32, ) -> u64

Calculates the first usable LBA aligned to the default alignment.

§Arguments
  • num_entries - Number of partition entries (typically 128)
  • entry_size - Size of each entry in bytes (typically 128)
Source

pub const fn gpt_last_usable_lba( &self, num_entries: u32, entry_size: u32, ) -> u64

Calculates the last usable LBA for GPT partitions.

This accounts for:

  • Backup partition entry array
  • Backup GPT header (1 sector at the last LBA)
§Arguments
  • num_entries - Number of partition entries (typically 128)
  • entry_size - Size of each entry in bytes (typically 128)
Source

pub const fn gpt_last_usable_lba_aligned( &self, num_entries: u32, entry_size: u32, ) -> u64

Calculates the last usable LBA aligned down to the default alignment.

§Arguments
  • num_entries - Number of partition entries (typically 128)
  • entry_size - Size of each entry in bytes (typically 128)
Source

pub const fn gpt_usable_sectors(&self, num_entries: u32, entry_size: u32) -> u64

Calculates the usable space in sectors for GPT partitions.

§Arguments
  • num_entries - Number of partition entries (typically 128)
  • entry_size - Size of each entry in bytes (typically 128)

Trait Implementations§

Source§

impl Clone for DiskGeometry

Source§

fn clone(&self) -> DiskGeometry

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for DiskGeometry

Source§

impl Debug for DiskGeometry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for DiskGeometry

Source§

impl PartialEq for DiskGeometry

Source§

fn eq(&self, other: &DiskGeometry) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for DiskGeometry

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.