Partition

Partition — generic label independent partition abstraction

Functions

int fdisk_add_partition ()
int fdisk_delete_all_partitions ()
int fdisk_delete_partition ()
int fdisk_get_partition ()
int fdisk_is_partition_used ()
int fdisk_set_partition ()
int fdisk_wipe_partition ()
struct fdisk_partition * fdisk_new_partition ()
int fdisk_partition_cmp_partno ()
int fdisk_partition_cmp_start ()
int fdisk_partition_end_follow_default ()
int fdisk_partition_end_is_default ()
const char * fdisk_partition_get_attrs ()
fdisk_sector_t fdisk_partition_get_end ()
const char * fdisk_partition_get_name ()
int fdisk_partition_get_parent ()
size_t fdisk_partition_get_partno ()
fdisk_sector_t fdisk_partition_get_size ()
fdisk_sector_t fdisk_partition_get_start ()
struct fdisk_parttype * fdisk_partition_get_type ()
const char * fdisk_partition_get_uuid ()
int fdisk_partition_get_max_size ()
int fdisk_partition_has_end ()
int fdisk_partition_has_partno ()
int fdisk_partition_has_size ()
int fdisk_partition_has_start ()
int fdisk_partition_has_wipe ()
int fdisk_partition_is_bootable ()
int fdisk_partition_is_container ()
int fdisk_partition_is_freespace ()
int fdisk_partition_is_nested ()
int fdisk_partition_is_used ()
int fdisk_partition_is_wholedisk ()
int fdisk_partition_next_partno ()
int fdisk_partition_partno_follow_default ()
int fdisk_partition_set_attrs ()
int fdisk_partition_set_name ()
int fdisk_partition_set_partno ()
int fdisk_partition_set_size ()
int fdisk_partition_set_start ()
int fdisk_partition_set_type ()
int fdisk_partition_set_uuid ()
int fdisk_partition_size_explicit ()
int fdisk_partition_start_follow_default ()
int fdisk_partition_start_is_default ()
int fdisk_partition_to_string ()
int fdisk_partition_unset_partno ()
int fdisk_partition_unset_size ()
int fdisk_partition_unset_start ()
void fdisk_ref_partition ()
void fdisk_reset_partition ()
void fdisk_unref_partition ()

Types and Values

Description

The fdisk_partition provides label independent abstraction. The partitions are not directly connected with partition table (label) data. Any change to fdisk_partition does not affects in-memory or on-disk label data.

The fdisk_partition is possible to use as a template for fdisk_add_partition() or fdisk_set_partition() operations.

Functions

fdisk_add_partition ()

int
fdisk_add_partition (struct fdisk_context *cxt,
                     struct fdisk_partition *pa,
                     size_t *partno);

If pa is not specified or any pa item is missing the libfdisk will ask by fdisk_ask_ API.

The pa template is important for non-interactive partitioning, especially for MBR where is necessary to differentiate between primary/logical; this is done by start offset or/and partno. The rules for MBR:

A) template specifies start within extended partition: add logical B) template specifies start out of extended partition: add primary C) template specifies start (or default), partno < 4: add primary D) template specifies default start, partno >= 4: add logical

otherwise MBR driver uses Ask-API to get missing information.

Adds a new partition to disklabel.

Parameters

cxt

fdisk context

 

pa

template for the partition (or NULL)

 

partno

NULL or returns new partition number

 

Returns

0 on success, <0 on error.


fdisk_delete_all_partitions ()

int
fdisk_delete_all_partitions (struct fdisk_context *cxt);

Delete all used partitions from disklabel.

Parameters

cxt

fdisk context

 

Returns

0 on success, otherwise, a corresponding error.


fdisk_delete_partition ()

int
fdisk_delete_partition (struct fdisk_context *cxt,
                        size_t partno);

Deletes a partno partition from disklabel.

Parameters

cxt

fdisk context

 

partno

partition number to delete (0 is the first partition)

 

Returns

0 on success, <0 on error


fdisk_get_partition ()

int
fdisk_get_partition (struct fdisk_context *cxt,
                     size_t partno,
                     struct fdisk_partition **pa);

Reads disklabel and fills in pa with data about partition n .

Note that partno may address unused partition and then this function does not fill anything to pa . See fdisk_is_partition_used(). If pa points to NULL then the function allocates a newly allocated fdisk_partition struct, use fdisk_unref_partition() to deallocate.

Parameters

cxt

context

 

partno

partition number (0 is the first partition)

 

pa

returns data about partition

 

Returns

0 on success, otherwise, a corresponding error.


fdisk_is_partition_used ()

int
fdisk_is_partition_used (struct fdisk_context *cxt,
                         size_t n);

Check if the partition number n is used by partition table. This function does not check if the device is used (e.g. mounted) by system!

This is faster than fdisk_get_partition() + fdisk_partition_is_used().

Parameters

cxt

context

 

n

partition number (0 is the first partition)

 

Returns

0 or 1


fdisk_set_partition ()

int
fdisk_set_partition (struct fdisk_context *cxt,
                     size_t partno,
                     struct fdisk_partition *pa);

Modifies disklabel according to setting with in pa .

Parameters

cxt

context

 

partno

partition number (0 is the first partition)

 

pa

new partition setting

 

Returns

0 on success, <0 on error.


fdisk_wipe_partition ()

int
fdisk_wipe_partition (struct fdisk_context *cxt,
                      size_t partno,
                      int enable);

Enable/disable filesystems/RAIDs wiping in area defined by partition start and size.

Parameters

cxt

fdisk context

 

partno

partition number

 

enable

0 or 1

 

Returns

<0 in case of error, 0 on success

Since: 2.29


fdisk_new_partition ()

struct fdisk_partition *
fdisk_new_partition (void);

Returns

new instance.


fdisk_partition_cmp_partno ()

int
fdisk_partition_cmp_partno (struct fdisk_partition *a,
                            struct fdisk_partition *b);

Compares partitions according to partition number See fdisk_table_sort_partitions().

Return: 0 if the same, <0 if b greater, >0 if a greater.

Parameters

a

partition

 

b

partition

 

fdisk_partition_cmp_start ()

int
fdisk_partition_cmp_start (struct fdisk_partition *a,
                           struct fdisk_partition *b);

Compares partitions according to start offset, See fdisk_table_sort_partitions().

Return: 0 if the same, <0 if b greater, >0 if a greater.

Parameters

a

partition

 

b

partition

 

fdisk_partition_end_follow_default ()

int
fdisk_partition_end_follow_default (struct fdisk_partition *pa,
                                    int enable);

When pa used as a template for fdisk_add_partition() when force label driver to use all the possible space for the new partition.

Parameters

pa

partition

 

enable

0|1

 

Returns

0 on success, <0 on error.


fdisk_partition_end_is_default ()

int
fdisk_partition_end_is_default (struct fdisk_partition *pa);

Parameters

pa

partition

 

Returns

1 if the partition follows default


fdisk_partition_get_attrs ()

const char *
fdisk_partition_get_attrs (struct fdisk_partition *pa);

Parameters

pa

partition

 

Returns

partition attributes in string format


fdisk_partition_get_end ()

fdisk_sector_t
fdisk_partition_get_end (struct fdisk_partition *pa);

This function may returns absolute non-sense, always check fdisk_partition_has_end().

Note that partition end is defined by fdisk_partition_set_start() and fdisk_partition_set_size().

Parameters

pa

partition

 

Returns

last partition sector LBA.


fdisk_partition_get_name ()

const char *
fdisk_partition_get_name (struct fdisk_partition *pa);

Parameters

pa

partition

 

Returns

partition name


fdisk_partition_get_parent ()

int
fdisk_partition_get_parent (struct fdisk_partition *pa,
                            size_t *parent);

Parameters

pa

partition

 

parent

parent parno

 

Returns

returns devno of the parent


fdisk_partition_get_partno ()

size_t
fdisk_partition_get_partno (struct fdisk_partition *pa);

The zero is also valid partition number. The function may return random value when partno is undefined (for example after fdisk_partition_unset_partno()). Always use fdisk_partition_has_partno() to be sure that you work with valid numbers.

Parameters

pa

partition

 

Returns

partition number (0 is the first partition)


fdisk_partition_get_size ()

fdisk_sector_t
fdisk_partition_get_size (struct fdisk_partition *pa);

The zero is also valid size. The function may return random undefined value when size is undefined (for example after fdisk_partition_unset_size()). Always use fdisk_partition_has_size() to be sure that you work with valid numbers.

Parameters

pa

partition

 

Returns

size offset in sectors


fdisk_partition_get_start ()

fdisk_sector_t
fdisk_partition_get_start (struct fdisk_partition *pa);

The zero is also valid offset. The function may return random undefined value when start offset is undefined (for example after fdisk_partition_unset_start()). Always use fdisk_partition_has_start() to be sure that you work with valid numbers.

Parameters

pa

partition

 

Returns

start offset in sectors


fdisk_partition_get_type ()

struct fdisk_parttype *
fdisk_partition_get_type (struct fdisk_partition *pa);

Parameters

pa

partition

 

Returns

pointer to partition type.


fdisk_partition_get_uuid ()

const char *
fdisk_partition_get_uuid (struct fdisk_partition *pa);

Parameters

pa

partition

 

Returns

partition UUID as string


fdisk_partition_get_max_size ()

int
fdisk_partition_get_max_size (struct fdisk_context *cxt,
                              size_t n,
                              fdisk_sector_t *maxsz);

fdisk_partition_has_end ()

int
fdisk_partition_has_end (struct fdisk_partition *pa);

Parameters

pa

partition

 

Returns

1 if the partition has defined last sector


fdisk_partition_has_partno ()

int
fdisk_partition_has_partno (struct fdisk_partition *pa);

Parameters

pa

partition

 

Returns

1 or 0


fdisk_partition_has_size ()

int
fdisk_partition_has_size (struct fdisk_partition *pa);

Parameters

pa

partition

 

Returns

1 or 0


fdisk_partition_has_start ()

int
fdisk_partition_has_start (struct fdisk_partition *pa);

Parameters

pa

partition

 

Returns

1 or 0


fdisk_partition_has_wipe ()

int
fdisk_partition_has_wipe (struct fdisk_context *cxt,
                          struct fdisk_partition *pa);

Parameters

cxt

fdisk context

 

pa

partition

 

Returns

1 if the area specified by pa will be wiped by write command, or 0.

Since: 2.30


fdisk_partition_is_bootable ()

int
fdisk_partition_is_bootable (struct fdisk_partition *pa);

Parameters

pa

partition

 

Returns

1 if the partition has enabled boot flag


fdisk_partition_is_container ()

int
fdisk_partition_is_container (struct fdisk_partition *pa);

Parameters

pa

partition

 

Returns

1 if the partition is container (e.g. MBR extended partition)


fdisk_partition_is_freespace ()

int
fdisk_partition_is_freespace (struct fdisk_partition *pa);

Parameters

pa

partition

 

Returns

1 if pa points to freespace


fdisk_partition_is_nested ()

int
fdisk_partition_is_nested (struct fdisk_partition *pa);

Parameters

pa

partition

 

Returns

1 if the partition is nested (e.g. MBR logical partition)


fdisk_partition_is_used ()

int
fdisk_partition_is_used (struct fdisk_partition *pa);

Parameters

pa

partition

 

Returns

1 if the partition points to some area


fdisk_partition_is_wholedisk ()

int
fdisk_partition_is_wholedisk (struct fdisk_partition *pa);

Parameters

pa

partition

 

Returns

1 if the partition is special whole-disk (e.g. SUN) partition


fdisk_partition_next_partno ()

int
fdisk_partition_next_partno (struct fdisk_partition *pa,
                             struct fdisk_context *cxt,
                             size_t *n);

If pa specified and partno-follow-default (see fdisk_partition_partno_follow_default()) enabled then returns next expected partno or -ERANGE on error.

If pa is NULL, or pa does not specify any semantic for the next partno then use Ask API to ask user for the next partno. In this case returns 1 if no free partition available. If fdisk dialogs are disabled then returns -EINVAL.

Parameters

pa

partition

 

cxt

context

 

n

returns partition number

 

Returns

0 on success, <0 on error, or 1 for non-free partno by Ask API.


fdisk_partition_partno_follow_default ()

int
fdisk_partition_partno_follow_default (struct fdisk_partition *pa,
                                       int enable);

When pa used as a template for fdisk_add_partition() when force label driver to add a new partition to the default (next) position.

Parameters

pa

partition

 

enable

0|1

 

Returns

0 on success, <0 on error.


fdisk_partition_set_attrs ()

int
fdisk_partition_set_attrs (struct fdisk_partition *pa,
                           const char *attrs);

Sets attrs to pa .

Return: 0 on success, <0 on error.

Parameters

pa

partition

 

attrs

attributes

 

fdisk_partition_set_name ()

int
fdisk_partition_set_name (struct fdisk_partition *pa,
                          const char *name);

Parameters

pa

partition

 

name

partition name

 

Returns

0 on success, <0 on error.


fdisk_partition_set_partno ()

int
fdisk_partition_set_partno (struct fdisk_partition *pa,
                            size_t num);

Note that zero is valid partno too. Use fdisk_partition_unset_partno() to undefine the partno.

Parameters

pa

partition

 

num

partition number (0 is the first partition, maximal is SIZE_MAX-1)

 

Returns

0 on success, <0 on error.


fdisk_partition_set_size ()

int
fdisk_partition_set_size (struct fdisk_partition *pa,
                          uint64_t sz);

Note that zero is valid size too. Use fdisk_partition_unset_size() to undefine the size.

Parameters

pa

partition

 

sz

size in sectors, maximal is UIN64_MAX-1

 

Returns

0 on success, <0 on error.


fdisk_partition_set_start ()

int
fdisk_partition_set_start (struct fdisk_partition *pa,
                           uint64_t off);

Note that zero is valid offset too. Use fdisk_partition_unset_start() to undefine the offset.

Parameters

pa

partition

 

off

offset in sectors, maximal is UINT64_MAX-1

 

Returns

0 on success, <0 on error.


fdisk_partition_set_type ()

int
fdisk_partition_set_type (struct fdisk_partition *pa,
                          struct fdisk_parttype *type);

Sets partition type.

Parameters

pa

partition

 

type

partition type

 

Returns

0 on success, <0 on error.


fdisk_partition_set_uuid ()

int
fdisk_partition_set_uuid (struct fdisk_partition *pa,
                          const char *uuid);

Parameters

pa

partition

 

uuid

UUID of the partition

 

Returns

0 on success, <0 on error.


fdisk_partition_size_explicit ()

int
fdisk_partition_size_explicit (struct fdisk_partition *pa,
                               int enable);

By default libfdisk aligns the size when add the new partition (by fdisk_add_partition()). If you want to disable this functionality use enable = 1.

Parameters

pa

partition

 

enable

0|1

 

Returns

0 on success, <0 on error.


fdisk_partition_start_follow_default ()

int
fdisk_partition_start_follow_default (struct fdisk_partition *pa,
                                      int enable);

When pa used as a template for fdisk_add_partition() when force label driver to use the first possible space for the new partition.

Parameters

pa

partition

 

enable

0|1

 

Returns

0 on success, <0 on error.


fdisk_partition_start_is_default ()

int
fdisk_partition_start_is_default (struct fdisk_partition *pa);

See fdisk_partition_start_follow_default().

Parameters

pa

partition

 

Returns

1 if the partition follows default


fdisk_partition_to_string ()

int
fdisk_partition_to_string (struct fdisk_partition *pa,
                           struct fdisk_context *cxt,
                           int id,
                           char **data);

Returns info about partition converted to printable string.

For example

1
2
3
4
5
6
7
struct fdisk_partition *pa;

fdisk_get_partition(cxt, 0, &pa);
fdisk_partition_to_string(pa, FDISK_FIELD_UUID, &data);
printf("first partition uuid: %s\n", data);
free(data);
fdisk_unref_partition(pa);

returns UUID for the first partition.

Parameters

pa

partition

 

cxt

context

 

id

field (FDISK_FIELD_*)

 

data

returns string with allocated data

 

Returns

0 on success, otherwise, a corresponding error.


fdisk_partition_unset_partno ()

int
fdisk_partition_unset_partno (struct fdisk_partition *pa);

Sets the partno as undefined. See fdisk_partition_has_partno().

Parameters

pa

partition

 

Returns

0 on success, <0 on error.


fdisk_partition_unset_size ()

int
fdisk_partition_unset_size (struct fdisk_partition *pa);

Sets the size as undefined. See fdisk_partition_has_size().

Parameters

pa

partition

 

Returns

0 on success, <0 on error.


fdisk_partition_unset_start ()

int
fdisk_partition_unset_start (struct fdisk_partition *pa);

Sets the size as undefined. See fdisk_partition_has_start().

Parameters

pa

partition

 

Returns

0 on success, <0 on error.


fdisk_ref_partition ()

void
fdisk_ref_partition (struct fdisk_partition *pa);

Increments reference counter.

Parameters

pa

partition pointer

 

fdisk_reset_partition ()

void
fdisk_reset_partition (struct fdisk_partition *pa);

Resets partition content.

Parameters

pa

partition

 

fdisk_unref_partition ()

void
fdisk_unref_partition (struct fdisk_partition *pa);

Decrements reference counter, on zero the pa is automatically deallocated.

Parameters

pa

partition pointer

 

Types and Values

struct fdisk_partition

struct fdisk_partition;

Partition abstraction (and template).