Expand description
§Description
The rsfdisk
library is a safe Rust wrapper around util-linux/libfdisk
.
Like libfdisk
, rsfdisk
is a library for the creation and manipulation of partition tables
on block devices. It understands GPT
, MBR
, Sun
, SGI
, and BSD
partition tables. It
provides the tools necessary for dividing block devices into partitions, also known as logical
disks. All metadata about partitions are recorded in a partition table, usually written on
the first sector of a device.
use rsfdisk::fdisk::Fdisk;
use rsfdisk::partition_table::PartitionTableKind;
use rsfdisk::partition::Code;
use rsfdisk::partition::DOSFlag;
use rsfdisk::partition::Partition;
use rsfdisk::partition::PartitionKind;
fn main() -> rsfdisk::Result<()> {
// Creating a 16GiB Linux partition on /dev/vda
let mut partitioner = 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 `DOS` partition table.
partitioner.partition_table_create(PartitionTableKind::DOS)?;
// Configure a 16GiB Linux partition
let partition_type = PartitionKind::builder()
// Set the partition type identifier for an MBR partition table.
.code(Code::Linux)
.build()?;
let partition = Partition::builder()
.partition_type(partition_type)
.name("System")
.starting_sector(64)
//Assuming 512 bytes per sector, 33,554,432 sectors <=> 16GiB.
.size_in_sectors(33_554_432)
.build()?;
// Create the partition at the first entry available in the partition table.
let partition_number = partitioner.partition_add(partition)?;
// Make it bootable.
partitioner.partition_table_toggle_partition_flag(partition_number, DOSFlag::Boot.into())?;
// Write the new partition table on `/dev/vda`.
partitioner.partition_table_write_to_disk()?;
Ok(())
}
§API structure
rsfdisk
’s API is roughly divided into the following major modules:
fdisk
: the main library module holding theFdisk
struct to create/edit/modify partition tables,partition_table
: for working with partition tables.partition
: for working with partitions in a partition table.
Finally, look to the debug
module if you need diagnostics during development.
§From libfdisk
to rsfdisk
This section maps libfdisk
functions to rsfdisk
methods. It follows the same layout as
libfdisk
’s documentation. You can use it as a reference to ease the transition from one API
to the other.
§Basic handlers and setting
§Context
§Ask
§Alignment
§Script
§Partitioning
§Label
§Partition
§Table
§Partition types
§Label item
libfdisk | rsfdisk |
---|---|
struct fdisk_labelitem | HeaderEntryContent |
enum fdisk_labelitem_bsd | HeaderEntry |
enum fdisk_labelitem_gen | HeaderEntry |
enum fdisk_labelitem_gpt | HeaderEntry |
enum fdisk_labelitem_sgi | HeaderEntry |
enum fdisk_labelitem_sun | HeaderEntry |
fdisk_new_labelitem | Private method. |
fdisk_ref_labelitem | Managed automatically. |
fdisk_reset_labelitem | Not implemented. HeaderEntry instances are immutable. |
fdisk_unref_labelitem | HeaderEntryContent is automatically deallocated when it goes out of scope. |
fdisk_labelitem_get_name | HeaderEntryContent::name |
fdisk_labelitem_get_id | HeaderEntryContent::header_entry |
fdisk_labelitem_get_data_u64 | HeaderEntryContent::data_u64 |
fdisk_labelitem_get_data_string | HeaderEntryContent::data_string |
fdisk_labelitem_is_string | HeaderEntryContent::is_string |
fdisk_labelitem_is_number | HeaderEntryContent::is_numeric |
§Field
§Label specific functions
§DOS
§UEFI GPT
§SUN
§SGI
libfdisk | rsfdisk |
---|---|
SGI_FLAG_BOOT | SGIFlag::Boot |
SGI_FLAG_SWAP | SGIFlag::Swap |
fdisk_sgi_create_info | FdiskSGIExt::sgi_add_hint |
fdisk_sgi_set_bootfile | FdiskSGIExt::sgi_set_boot_file |
§BSD
§Misc
§Iterator
libfdisk | rsfdisk |
---|---|
struct fdisk_iter | GenIterator |
fdisk_free_iter | GenIterator is automatically deallocated when it goes out of scope. |
fdisk_iter_get_direction | GenIterator::direction |
fdisk_new_iter | GenIterator::new |
fdisk_reset_iter | GenIterator::reset GenIterator::reset_forward GenIterator::reset_backward |
§Utils
libfdisk | rsfdisk |
---|---|
fdisk_partname | core::utils::misc::partition_name |
§Library initialization
libfdisk | rsfdisk |
---|---|
fdisk_init_debug | debug::init_default_debug debug::init_full_debug |
§Version functions
Modules§
- debug
- Activate debug message output.
- errors
- Runtime errors.
- fdisk
rsfdisk
’s main module.- iter
libfdisk
generic iterator.- partition
- Module for working with partitions in a partition table.
- partition_
table - Module for working with partition tables.
- prompt
- Module for dialog-driven partitioning.
- script
- Module for composing
sfdisk
-compatible scripts. - utils
- Utility objects and helper functions.
Enums§
- RsFdisk
Error - Library-level runtime errors.