2 unstable releases
| 0.2.0 | May 2, 2025 |
|---|---|
| 0.1.0 | Oct 15, 2024 |
#2112 in Filesystem
207 downloads per month
1MB
13K
SLoC
rsfdisk
⚠️ 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.2.0"
Then install the system packages below before running cargo build:
util-linux: to generate Rust bindings fromlibfdisk's header files.libclang: to satisfy the dependency ofbindgenonlibclang.pkg-config: to detect system libraries.
Read the installation instructions below to install the required dependencies on your system.
Example
In this example we create a GPT partition table on /dev/vda, then divide it
into three partitions:
- a 16 GiB
rootpartition to keep system files, - and two 64 GiB data partitions.
use rsfdisk::fdisk::Fdisk;
use rsfdisk::partition_table::PartitionTableKind;
use rsfdisk::partition::Guid;
use rsfdisk::partition::Partition;
use rsfdisk::partition::PartitionKind;
use rsfdisk::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
~62K SLoC