Crate rsfdisk

Source
Expand description

§Table of Contents

  1. Description
  2. API structure
  3. From libfdisk to rsfdisk
    1. Basic handlers and setting
      1. Context
      2. Ask
      3. Alignment
      4. Script
    2. Partitioning
      1. Label
      2. Partition
      3. Table
      4. Partition types
      5. Label item
      6. Field
    3. Label specific functions
      1. Dos
      2. UEFI GPT
      3. SUN
      4. SGI
      5. BSD
    4. Misc
      1. Iterator
      2. Utils
      3. Library Initialization
      4. Version functions

§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::core::partition_table::PartitionTableKind;
use rsfdisk::core::partition::Code;
use rsfdisk::core::partition::DOSFlag;
use rsfdisk::core::partition::Partition;
use rsfdisk::core::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 two parts:

  • fdisk: the main library module holding the Fdisk struct to create/edit/modify partition tables,
  • core: the module holding specialised objects used and/or returned by Fdisk.

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
libfdiskrsfdisk
struct fdisk_contextFdisk
fdisk_assign_deviceFdiskBuilder::assign_device
fdisk_assign_device_by_fdFdiskBuilder::assign_device_by_file
fdisk_deassign_deviceFdisk::close_device
Fdisk::close_device_async
fdisk_reassign_deviceFdisk::discard_changes
fdisk_device_is_usedFdisk::device_is_in_use
fdisk_enable_bootbits_protectionFdiskBuilder::erase_master_boot_record
fdisk_enable_detailsFdiskBuilder::display_partition_details
fdisk_enable_listonlyFdiskBuilder::display_partition_list_only
fdisk_enable_wipeFdiskBuilder::wipe_device_metadata
fdisk_disable_dialogsFdiskBuilder::enable_interactive
fdisk_get_alignment_offsetFdisk::device_alignment_offset
fdisk_get_collisionFdisk::device_describe_collisions
fdisk_get_devfdFdisk::device_borrow_fd
fdisk_get_devmodelFdisk::device_model
fdisk_get_devnameFdisk::device_name
fdisk_get_devnoFdisk::device_number
fdisk_get_disklabel_itemFdisk::partition_table_header_entry
fdisk_get_first_lbaFdisk::device_first_lba
fdisk_get_geom_cylindersFdisk::device_count_cylinders
fdisk_get_geom_headsFdisk::device_count_heads
fdisk_get_geom_sectorsFdisk::device_count_sectors
fdisk_get_grain_sizeFdisk::device_grain_size
fdisk_get_last_lbaFdisk::device_last_lba
fdisk_get_minimal_iosizeFdisk::device_minimum_io_size
fdisk_get_nsectorsFdisk::device_size_in_sectors
fdisk_get_optimal_iosizeFdisk::device_optimal_io_size
fdisk_get_parentFdisk::parent_partitioner
fdisk_get_physector_sizeFdisk::device_bytes_per_physical_sector
fdisk_get_sector_sizeFdisk::device_bytes_per_logical_sector
fdisk_get_size_unitFdisk::partition_size_format
fdisk_get_unitFdisk::displayed_unit_singular
Fdisk::displayed_unit_plural
fdisk_get_units_per_sectorFdisk::sectors_per_cylinder
fdisk_has_dialogsFdisk::is_partitioning_interactive
fdisk_has_labelFdisk::device_has_partition_table
fdisk_has_protected_bootbitsFdisk::protects_master_boot_record
fdisk_has_wipeFdisk::wipes_device_metadata
fdisk_is_detailsFdisk::displays_partition_details
fdisk_is_labeltypeFdisk::partition_table_is_of_type
fdisk_is_listonlyFdisk::displays_partition_list_only
fdisk_is_ptcollisionFdisk::device_has_collisions
fdisk_is_readonlyFdisk::device_is_read_only
fdisk_is_regfileFdisk::device_is_image_file
fdisk_new_contextFdisk::builder
fdisk_new_nested_contextFdisk::create_nested_partitioner
Fdisk::create_nested_partitioner_with_name
fdisk_ref_contextManaged automatically.
fdisk_reread_changesFdisk::reread_changed_partition_entries
fdisk_reread_partition_tableFdisk::reread_partition_entries
fdisk_set_first_lbaFdisk::device_set_first_lba
fdisk_set_last_lbaFdisk::device_set_last_lba
fdisk_set_size_unitFdiskBuilder::partition_size_format
fdisk_set_unitFdiskBuilder::device_addressing
fdisk_unref_contextFdisk is automatically deallocated when it goes out of scope.
fdisk_use_cylindersFdisk::displays_metadata_in_cylinders
§Ask
libfdiskrsfdisk
struct fdisk_askPrompt
enum fdisk_asktypePromptKind
fdisk_infoFdisk::log_info
fdisk_warnFdisk::log_warn_set_errno
fdisk_warnxFdisk::log_warn
fdisk_set_askTBD
fdisk_is_askPrompt::is_of_kind
fdisk_ask_get_queryPrompt::query
fdisk_ask_get_typePrompt::kind
fdisk_ask_menu_get_defaultPrompt::menu_default_key
fdisk_ask_menu_get_itemPrompt::menu_nth_item
fdisk_ask_menu_get_nitemsPrompt::menu_count_items
fdisk_ask_menu_get_resultPrompt::menu_selected_item
fdisk_ask_menu_set_resultPrompt::menu_item_select
fdisk_ask_numberFdisk::ask_number_in_range
fdisk_ask_number_get_corePrompt::number_reference_point
fdisk_ask_number_get_defaultPrompt::number_default
fdisk_ask_number_get_highPrompt::number_upper_bound
fdisk_ask_number_get_lowPrompt::number_lower_bound
fdisk_ask_number_get_rangePrompt::number_range
fdisk_ask_number_get_resultPrompt::number_answer
fdisk_ask_number_get_unitPrompt::number_bytes_per_unit
fdisk_ask_number_incharsPrompt::requires_lettered_partitions
fdisk_ask_number_is_wrap_negativePrompt::accepts_negative_numbers
fdisk_ask_number_set_relativePrompt::number_enable_relative
Prompt::number_disable_relative
fdisk_ask_number_set_resultPrompt::number_set_answer
fdisk_ask_partnumFdisk::ask_partition_number_used
Fdisk::ask_partition_number_unused
fdisk_ask_print_get_errnoPrompt::error_number
fdisk_ask_print_get_mesgPrompt::error_message
fdisk_ask_stringFdisk::ask_string_value
fdisk_ask_string_get_resultPrompt::string_answer
fdisk_ask_string_set_resultPrompt::string_set_answer
fdisk_ask_yesnoFdisk::ask_yes_no_question
fdisk_ask_yesno_get_resultPrompt::yes_no_answer
fdisk_ask_yesno_set_resultPrompt::yes_no_set_answer
fdisk_ref_askManaged automatically.
fdisk_unref_askPrompt is automatically deallocated when it goes out of scope.
§Alignment
§Script
libfdiskrsfdisk
struct fdisk_scriptScript
fdisk_set_scriptFdisk::script_dissociate
fdisk_get_scriptFdisk::script
Fdisk::script_mut
fdisk_apply_scriptFdisk::script_apply
fdisk_apply_script_headersFdisk::script_apply_headers
fdisk_new_scriptFdisk::script_new
fdisk_new_script_from_fileFdisk::script_new_from_file
fdisk_ref_scriptManaged automatically.
fdisk_script_enable_jsonScript::enable_json_output
Script::disable_json_output
fdisk_script_get_headerScript::header_value
fdisk_script_get_nlinesScript::count_lines
fdisk_script_set_tableScript::override_partition_table
fdisk_script_get_tableScript::partition_table_entries
fdisk_script_has_force_labelScript::has_header_label
fdisk_script_read_contextScript::compose_script
fdisk_script_read_fileScript::import_file
Script::import_stream
fdisk_script_read_lineScript::read_line
fdisk_script_set_headerScript::add_header
fdisk_script_set_fgetsScript::set_custom_read_line
fdisk_script_write_fileScript::export_to_file
Script::export_to_stream
fdisk_script_set_userdataManaged internally.
fdisk_script_get_userdataManaged internally.
fdisk_unref_scriptScript is automatically deallocated when it goes out of scope.

§Partitioning

§Label
libfdiskrsfdisk
struct fdisk_labelPartitionTable
enum fdisk_labeltypePartitionTableKind
fdisk_create_disklabelFdisk::partition_table_create_default
Fdisk::partition_table_create
fdisk_list_disklabelFdisk::partition_table_display_details
fdisk_locate_disklabelFdisk::partition_table_section
fdisk_reorder_partitionsFdisk::partition_table_sort_partitions
fdisk_set_disklabel_idFdisk::partition_table_set_id
fdisk_set_disklabel_id_from_stringFdisk::partition_table_set_string_id
fdisk_set_partition_typeFdisk::partition_table_set_partition_type
fdisk_toggle_partition_flagFdisk::partition_table_toggle_partition_flag
fdisk_verify_disklabelFdisk::partition_table_check
fdisk_write_disklabelFdisk::partition_table_write_to_disk
fdisk_get_disklabel_idFdisk::partition_table_id
fdisk_get_labelFdisk::partition_table_current
Fdisk::partition_table_current_mut
Fdisk::partition_table_by_type
Fdisk::partition_table_by_type_mut
fdisk_get_nlabelsFdisk::partition_table_count_types
fdisk_next_labelFdisk::iter
Fdisk::iter_mut
fdisk_get_npartitionsFdisk::partition_table_max_partitions
fdisk_is_label()
fdisk_label_advparse_parttypePartitionTable::partition_type_parse
fdisk_label_get_fieldPartitionTable::partition_field_format
fdisk_label_get_field_by_namePartitionTable::partition_field_format_by_name
fdisk_label_get_fields_idsFdisk::partition_table_collect_partition_fields
fdisk_label_get_fields_ids_allFdisk::partition_table_collect_all_partition_fields
fdisk_label_get_geomrange_cylindersPartitionTable::geometry_cylinders
fdisk_label_get_geomrange_headsPartitionTable::geometry_heads
fdisk_label_get_geomrange_sectorsPartitionTable::geometry_sectors
fdisk_label_get_namePartitionTable::name
fdisk_label_get_nparttypesPartitionTable::count_supported_partition_types
fdisk_label_get_parttypePartitionTable::supported_partition_types
fdisk_label_get_parttype_from_codePartitionTable::partition_type_from_code
fdisk_label_get_parttype_from_stringPartitionTable::partition_type_from_string
fdisk_label_get_parttype_shortcutPartitionTable::partition_type_shortcut
fdisk_label_get_typePartitionTable::kind
fdisk_label_has_code_parttypesPartitionTable::uses_partition_type_codes
fdisk_label_has_parttypes_shortcutsPartitionTable::supports_partition_type_shortcuts
fdisk_label_is_changedPartitionTable::has_changes
fdisk_label_is_disabledPartitionTable::is_disabled
fdisk_label_parse_parttypePartitionTable::partition_type_from_string_id
fdisk_label_require_geometryPartitionTable::requires_chs_addressing
fdisk_label_set_changedPartitionTable::mark_as_changed
PartitionTable::mark_as_unchanged
fdisk_label_set_disabledPartitionTable::disable
PartitionTable::enable
§Partition
libfdiskrsfdisk
struct fdisk_partitionPartition
fdisk_add_partitionFdisk::partition_add
Fdisk::partition_add_interactive
fdisk_delete_all_partitionsFdisk::partition_delete_all
fdisk_delete_partitionFdisk::partition_delete
fdisk_get_partitionFdisk::partition_by_number
Fdisk::partition_by_number_mut
fdisk_is_partition_usedFdisk::partition_is_number_in_use
fdisk_set_partitionFdisk::partition_override_settings
fdisk_wipe_partitionFdisk::partition_wipe_activate
Fdisk::partition_wipe_deactivate
fdisk_new_partitionPartition::builder
fdisk_partition_cmp_partnoPartition::compare_partition_numbers
fdisk_partition_cmp_startPartition::compare_starting_sectors
fdisk_partition_end_follow_defaultManaged internally by PartitionBuilder.
fdisk_partition_end_is_defaultPartition::uses_default_ending_sector
fdisk_partition_get_attrsPartition::attribute_bits
fdisk_partition_get_endPartition::ending_sector
fdisk_partition_get_namePartition::name
fdisk_partition_get_parentPartition::parent_partition_number
fdisk_partition_get_partnoPartition::number
fdisk_partition_get_sizePartition::size_in_sectors
fdisk_partition_get_startPartition::starting_sector
fdisk_partition_get_typePartition::partition_type
fdisk_partition_get_uuidPartition::uuid
fdisk_partition_has_endRedundant since we return Option::None when the value is not set.
fdisk_partition_has_partnoRedundant since we return Option::None when the value is not set.
fdisk_partition_has_sizeRedundant since we return Option::None when the value is not set.
fdisk_partition_has_startRedundant since we return Option::None when the value is not set.
fdisk_partition_has_wipeFdisk::is_partition_wipe_active
fdisk_partition_is_bootablePartition::is_bootable
fdisk_partition_is_containerPartition::is_container
fdisk_partition_is_freespacePartition::is_free_space
fdisk_partition_is_nestedPartition::is_nested
fdisk_partition_is_usedPartition::points_to_used_area
fdisk_partition_is_wholediskPartition::is_whole_disk
fdisk_partition_next_partnoFdisk::partition_ask_next_number
Fdisk::partition_next_number
fdisk_partition_partno_follow_defaultManaged internally by PartitionBuilder.
fdisk_partition_set_attrsPartitionBuilder::attribute_bits
fdisk_partition_set_namePartitionBuilder::name
fdisk_partition_set_partnoPartitionBuilder::number
Partition::set_partition_number
fdisk_partition_set_sizePartitionBuilder::size_in_sectors
Partition::set_size_in_sectors
fdisk_partition_set_startPartitionBuilder::starting_sector
Partition::set_starting_sector
fdisk_partition_set_typePartitionBuilder::partition_type
fdisk_partition_set_uuidPartitionBuilder::uuid
fdisk_partition_size_explicitPartitionBuilder::ask_size_interactive
fdisk_partition_start_follow_defaultManaged internally by PartitionBuilder.
fdisk_partition_start_is_defaultPartition::uses_default_starting_sector
fdisk_partition_to_stringFdisk::partition_field_to_string
fdisk_partition_unset_partnoPartition::unset_partition_number
fdisk_partition_unset_sizePartition::unset_size_in_sectors
fdisk_partition_unset_startPartition::unset_starting_sector
fdisk_ref_partitionManaged automatically.
fdisk_reset_partitionNot implemented.
fdisk_unref_partitionPartition is automatically deallocated when it goes out of scope.
§Table
libfdiskrsfdisk
struct fdisk_tablePartitionList
fdisk_get_freespacesFdisk::list_empty_spaces
fdisk_get_partitionsFdisk::list_partitions
fdisk_apply_tableFdisk::partitions_append
fdisk_new_tablePartitionList::new
fdisk_ref_tableManaged automatically.
fdisk_reset_tablePartitionList::clear
fdisk_table_add_partitionPartitionList::push
fdisk_table_get_nentsPartitionList::len
fdisk_table_get_partitionPartitionList::get
PartitionList::get_mut
fdisk_table_get_partition_by_partnoPartitionList::get_by_partition_number
PartitionList::get_by_partition_number_mut
fdisk_table_is_emptyPartitionList::is_empty
fdisk_table_next_partitionPartitionList::iter
PartitionList::iter_mut
fdisk_table_remove_partitionPartitionList::remove
fdisk_table_sort_partitionsCan not implement without a data pointer in the cmp function see Passing Rust closure to C
fdisk_table_wrong_orderPartitionList::is_not_in_increasing_order
fdisk_unref_tablePartitionList is automatically deallocated when it goes out of scope.
§Partition types
§Label item
libfdiskrsfdisk
struct fdisk_labelitemHeaderEntryContent
enum fdisk_labelitem_bsdHeaderEntry
enum fdisk_labelitem_genHeaderEntry
enum fdisk_labelitem_gptHeaderEntry
enum fdisk_labelitem_sgiHeaderEntry
enum fdisk_labelitem_sunHeaderEntry
fdisk_new_labelitemPrivate method.
fdisk_ref_labelitemManaged automatically.
fdisk_reset_labelitemNot implemented. HeaderEntry instances are immutable.
fdisk_unref_labelitemHeaderEntryContent is automatically deallocated when it goes out of scope.
fdisk_labelitem_get_nameHeaderEntryContent::name
fdisk_labelitem_get_idHeaderEntryContent::header_entry
fdisk_labelitem_get_data_u64HeaderEntryContent::data_u64
fdisk_labelitem_get_data_stringHeaderEntryContent::data_string
fdisk_labelitem_is_stringHeaderEntryContent::is_string
fdisk_labelitem_is_numberHeaderEntryContent::is_numeric
§Field

§Label specific functions

§DOS
§UEFI GPT
§SUN
§SGI
§BSD

§Misc

§Iterator
§Utils
§Library initialization
§Version functions

Modules§

core
Core objects and helper functions.
debug
Activate debug message output.
fdisk
rsfdisk’s main module.

Enums§

RsFdiskError
Library-level runtime errors.

Type Aliases§

Result
A specialized Result type for rsfdisk.