Top |
The fdisk_new_context()
initializes all label drivers, and allocate
per-label specific data struct. This concept allows to store label specific
settings to the label driver independently on the currently active label
driver. Note that label struct cannot be deallocated, so there is no
reference counting for fdisk_label objects. All is destroyed by
fdisk_unref_context()
only.
Anyway, all label drives share in-memory first sector. The function
fdisk_create_disklabel()
overwrites thi in-memory sector. But it's possible that
label driver also uses another buffers, for example GPT reads more sectors
from the device.
All label operations are in-memory only, except fdisk_write_disklabel()
.
All functions that use "struct fdisk_context" rather than "struct fdisk_label" use the currently active label driver.
int fdisk_create_disklabel (struct fdisk_context *cxt
,const char *name
);
Creates a new disk label of type name
. If name
is NULL, then it will
create a default system label type, either SUN or DOS. The function
automaticaly switches the current label driver to name
. The function
fdisk_get_label()
returns the current label driver.
The function modifies in-memory data only.
int
fdisk_list_disklabel (struct fdisk_context *cxt
);
Lists details about disklabel, but no partitions.
This function is based on fdisk_get_disklabel_item()
and prints all label
specific information by ASK interface (FDISK_ASKTYPE_INFO, aka fdisk_info()
).
The function requires enabled "details" by fdisk_enable_details()
.
It's recommended to use fdisk_get_disklabel_item()
if you need better
control on output and formmatting.
int fdisk_locate_disklabel (struct fdisk_context *cxt
,int n
,const char **name
,uint64_t *offset
,size_t *size
);
Locate disklabel and returns info about n
item of the label. For example
GPT is composed from two items, PMBR and GPT, n=0 return offset to PMBR and n=1
return offset to GPT. For more details see 'D' expert fdisk command.
int
fdisk_reorder_partitions (struct fdisk_context *cxt
);
Sort partitions according to the partition start sector.
int fdisk_set_partition_type (struct fdisk_context *cxt
,size_t partnum
,struct fdisk_parttype *t
);
int fdisk_toggle_partition_flag (struct fdisk_context *cxt
,size_t partnum
,unsigned long flag
);
int
fdisk_verify_disklabel (struct fdisk_context *cxt
);
Verifies the partition table.
int
fdisk_write_disklabel (struct fdisk_context *cxt
);
Write in-memory changes to disk. Be careful!
struct fdisk_label * fdisk_get_label (struct fdisk_context *cxt
,const char *name
);
If no name
specified then returns the current context label.
The label is allocated and maintained within the context cxt. There is nothing like reference counting for labels, you cannot delallocate the label.
int fdisk_next_label (struct fdisk_context *cxt
,struct fdisk_label **lb
);
1 2 3 4 5 6 7 |
// print all supported labels struct fdisk_context *cxt = fdisk_new_context(); struct fdisk_label *lb = NULL; while (fdisk_next_label(cxt, &lb) == 0) print("label name: %s\n", fdisk_label_get_name(lb)); fdisk_unref_context(cxt); |
size_t
fdisk_get_npartitions (struct fdisk_context *cxt
);
The maximal number of the partitions depends on disklabel and does not have to describe the real limit of PT.
For example the limit for MBR without extend partition is 4, with extended partition it's unlimited (so the function returns the current number of all partitions in this case).
And for example for GPT it depends on space allocated on disk for array of entry records (usually 128).
It's fine to use fdisk_get_npartitions()
in loops, but don't forget that
partition may be unused (see fdisk_is_partition_used()
).
1 2 3 4 5 6 7 8 |
struct fdisk_partition *pa = NULL; size_t i, nmax = fdisk_get_npartitions(cxt); for (i = 0; i < nmax; i++) { if (!fdisk_is_partition_used(cxt, i)) continue; ... do something ... } |
Note that the recommended way to list partitions is to use
fdisk_get_partitions()
and struct fdisk_table than ask disk driver for each
individual partitions.
const struct fdisk_field * fdisk_label_get_field (const struct fdisk_label *lb
,int id
);
The field struct describes data stored in struct fdisk_partition. The info
about data is usable for example to generate human readable output (e.g.
fdisk 'p'rint command). See fdisk_partition_to_string()
and fdisk code.
const struct fdisk_field * fdisk_label_get_field_by_name (const struct fdisk_label *lb
,const char *name
);
int fdisk_label_get_fields_ids (const struct fdisk_label *lb
,struct fdisk_context *cxt
,int **ids
,size_t *nids
);
This function returns the default fields for the label.
Note that the set of the default fields depends on fdisk_enable_details()
function. If the details are enabled then this function usually returns more
fields.
int fdisk_label_get_fields_ids_all (const struct fdisk_label *lb
,struct fdisk_context *cxt
,int **ids
,size_t *nids
);
This function returns all fields for the label.
struct fdisk_parttype * fdisk_label_get_parttype (const struct fdisk_label *lb
,size_t n
);
struct fdisk_parttype * fdisk_label_get_parttype_from_code (const struct fdisk_label *lb
,unsigned int code
);
Search for partition type in label-specific table. The result is pointer to static array of label types.
struct fdisk_parttype * fdisk_label_get_parttype_from_string (const struct fdisk_label *lb
,const char *str
);
Search for partition type in label-specific table. The result is pointer to static array of label types.
int
fdisk_label_has_code_parttypes (const struct fdisk_label *lb
);
struct fdisk_parttype * fdisk_label_parse_parttype (const struct fdisk_label *lb
,const char *str
);
Parses partition type from str
according to the label. Thefunction returns
a pointer to static table of the partition types, or newly allocated
partition type for unknown types (see fdisk_parttype_is_unknown()
. It's
safe to call fdisk_unref_parttype()
for all results.
void fdisk_label_set_changed (struct fdisk_label *lb
,int changed
);
Marks in-memory data as changed, to force fdisk_write_disklabel()
to write
to device. This should be unnecessar by default, the library keeps track
about changes.