#partition #wrapper #fdisk #util-linux #libfdisk #api-bindings

rsfdisk

Safe Rust wrapper around the util-linux/libfdisk C library

1 unstable release

new 0.1.0 Oct 15, 2024

#216 in Algorithms

Download history 165/week @ 2024-10-14

165 downloads per month

Apache-2.0 OR MIT

1MB
12K SLoC

rsfdisk


Crates.io Version docs.rs Crates.io MSRV Crates.io License

⚠️ WARNING: This library is still in development, thus not yet suitable for use in production.

The rsfdisk library is a safe Rust wrapper around util-linux/libfdisk.

rsfdisk can create and/or manipulate partition tables on block devices. It understands GPT, MBR, Sun, SGI, and BSD partition tables.

Usage

This crate requires libfdisk version 2.39.2 or later.

Add the following to your Cargo.toml:

[dependencies]
rsfdisk = "0.1.0"

Then install the system packages below before running cargo build:

  • util-linux: to generate Rust bindings from libfdisk's header files.
  • libclang: to satisfy the dependency of bindgen on libclang.
  • pkg-config: to detect system libraries.

Read the installation instructions below to install the required dependencies on your system.

Documentation (docs.rs)

Example

In this example we create a GPT partition table on /dev/vda, then divide it into three partitions:

  • a 16 GiB root partition to keep system files,
  • and two 64 GiB data partitions.
use rsfdisk::fdisk::Fdisk;
use rsfdisk::core::partition_table::PartitionTableKind;
use rsfdisk::core::partition::Guid;
use rsfdisk::core::partition::Partition;
use rsfdisk::core::partition::PartitionKind;
use rsfdisk::core::partition::PartitionList;

fn main() -> rsfdisk::Result<()> {
    let mut disk = Fdisk::builder()
        // Operate on `/dev/vda`.
        .assign_device("/dev/vda")
        // Allow Fdisk to persist changes to disk.
        .enable_read_write()
        // Remove all existing partition tables, file systems, and RAID
        // signatures on the assigned device before writing a new partition
        // table.
        .wipe_device_metadata()
        .build()?;

    // Create a `GPT` partition table.
    disk.partition_table_create(PartitionTableKind::GPT)?;

    // Configure a 16 GiB System partition
    let partition_type = PartitionKind::builder()
       // Set the partition type identifier for a GUID/GPT partition table.
       .guid(Guid::LinuxRootx86_64)
       .build()?;

    let root = Partition::builder()
       .partition_type(partition_type)
       .name("System")
       //Assuming 512 bytes per sector, 33,554,432 sectors <=> 16 GiB.
       .size_in_sectors(33_554_432)
       .build()?;

    // Create the root partition.
    let _ = disk.partition_add(root)?;

    // Configure two 64 GiB data partitions.
    let mut data_partitions = PartitionList::new()?;

    // Assuming 512 bytes per sector, 68,719,476,736 sectors <=> 64 GiB.
    let size = 68_719_476_736;

    for i in 0..2 {
        let partition_type = PartitionKind::builder()
           .guid(Guid::LinuxData)
           .build()?;

        let name = format!("Data Part {}", i + 1);

        let partition = Partition::builder()
           .partition_type(partition_type)
           .name(name)
           .size_in_sectors(size)
           .build()?;

        data_partitions.push(partition)?;
    }

    // Create the data partitions.
    disk.partitions_append(data_partitions)?;

    // Write the new partition table on `/dev/vda`.
    disk.partition_table_write_to_disk()?;

    Ok(())
}

Install required dependencies

Alpine Linux

As root, issue the following command:

apk add util-linux-dev clang-libclang pkgconfig

NixOS

Install the packages in a temporary environment with:

nix-shell -p util-linux.dev libclang.lib pkg-config

or permanently with:

nix-env -iA nixos.util-linux.dev nixos.libclang.lib nixos.pkg-config

Ubuntu

sudo apt-get install libfdisk-dev libclang-dev pkg-config

License

This project is licensed under either of the following:

at your discretion.

Files in the third-party/ and web-snapshots/ directories are subject to their own licenses and/or copyrights.

SPDX-License-Identifier: Apache-2.0 OR MIT

Copyright (c) 2023 Nick Piaddo

Dependencies

~0.6–3MB
~63K SLoC