Top |
int | fdisk_set_script () |
struct fdisk_script * | fdisk_get_script () |
int | fdisk_apply_script () |
int | fdisk_apply_script_headers () |
struct fdisk_script * | fdisk_new_script () |
struct fdisk_script * | fdisk_new_script_from_file () |
void | fdisk_ref_script () |
int | fdisk_script_enable_json () |
const char * | fdisk_script_get_header () |
int | fdisk_script_get_nlines () |
int | fdisk_script_set_table () |
struct fdisk_table * | fdisk_script_get_table () |
int | fdisk_script_has_force_label () |
int | fdisk_script_read_context () |
int | fdisk_script_read_file () |
int | fdisk_script_read_line () |
int | fdisk_script_set_header () |
int | fdisk_script_set_fgets () |
int | fdisk_script_write_file () |
int | fdisk_script_set_userdata () |
void * | fdisk_script_get_userdata () |
void | fdisk_unref_script () |
This interface can be used to compose in-memory partition table with all details, write all partition table description to human readable text file, read it from the file, and apply the script to on-disk label.
The libfdisk scripts are based on original sfdisk script (dumps). Each script has two parts: script headers and partition table entries (partitions). The script is possible to dump in JSON too (read JSON is not implemented yet).
For more details about script format see sfdisk man page.
There are four ways how to build the script:
read the current on-disk partition table by fdisk_script_read_context()
)
read it from text file by fdisk_script_read_file()
read it interactively from user by fdisk_script_read_line()
and fdisk_script_set_fgets()
manually in code by fdisk_script_set_header()
and fdisk_script_set_table()
The read functions fdisk_script_read_context()
and fdisk_script_read_file()
creates always a new script partition table. The table (see
fdisk_script_get_table()
) is possible to modify by standard
fdisk_table_...() functions and then apply by fdisk_apply_script()
.
Note that script API is fully non-interactive and forces libfdisk to not use standard dialog driven partitioning as we have in fdisk(8).
int fdisk_set_script (struct fdisk_context *cxt
,struct fdisk_script *dp
);
Sets reference to the dp
script and remove reference to the previously used
script.
The script headers might be used by label drivers to overwrite built-in defaults (for example disk label Id) and label driver might optimize the default semantic to be more usable for scripts (for example to not ask for primary/logical/extended partition type).
Note that script also contains reference to the fdisk context (see
fdisk_new_script()
). This context may be completely independent on
context used for fdisk_set_script()
.
Don't forget to call fdisk_set_script(cxt, NULL); to remove this reference if no more necessary!
int fdisk_apply_script (struct fdisk_context *cxt
,struct fdisk_script *dp
);
This function creates a new disklabel and partition within context cxt
. You
have to call fdisk_write_disklabel()
to apply changes to the device.
int fdisk_apply_script_headers (struct fdisk_context *cxt
,struct fdisk_script *dp
);
Associate context cxt
with script dp
and creates a new empty disklabel.
The script may be later unreference by fdisk_set_script()
with NULL as script.
struct fdisk_script *
fdisk_new_script (struct fdisk_context *cxt
);
The script hold fdisk_table and additional information to read/write script to the file.
struct fdisk_script * fdisk_new_script_from_file (struct fdisk_context *cxt
,const char *filename
);
Allocates a new script and reads script from filename
.
int fdisk_script_enable_json (struct fdisk_script *dp
,int json
);
Disable/Enable JSON output format.
const char * fdisk_script_get_header (struct fdisk_script *dp
,const char *name
);
int fdisk_script_set_table (struct fdisk_script *dp
,struct fdisk_table *tb
);
Replaces table used by script and creates a new reference to tb
. This
function can be used to generate a new script table independently on the current
context and without any file reading.
This is useful for example to create partition table with the same basic
settings (e.g. label-id, ...) but with different partitions -- just call
fdisk_script_read_context()
to get current settings and then
fdisk_script_set_table()
to set a different layout.
If tb
is NULL then the current script table is unreferenced.
Note that script read_ functions (e.g. fdisk_script_read_context()
) create
always a new script table.
Since: 2.35
struct fdisk_table *
fdisk_script_get_table (struct fdisk_script *dp
);
The table represents partitions holded by the script. The table is possible to
fill by fdisk_script_read_context()
or fdisk_script_read_file()
. All the "read"
functions remove old partitions from the table. See also fdisk_script_set_table()
.
int
fdisk_script_has_force_label (struct fdisk_script *dp
);
Label has been explicitly specified in the script.
Since: 2.30
int fdisk_script_read_context (struct fdisk_script *dp
,struct fdisk_context *cxt
);
Reads data from the cxt
context (on disk partition table) into the script.
If the context is not specified then defaults to context used for fdisk_new_script()
.
Return: 0 on success, <0 on error.
int fdisk_script_read_file (struct fdisk_script *dp
,FILE *f
);
Reads file f
into script dp
.
int fdisk_script_read_line (struct fdisk_script *dp
,FILE *f
,char *buf
,size_t bufsz
);
Reads next line into dump.
int fdisk_script_set_header (struct fdisk_script *dp
,const char *name
,const char *data
);
The headers are used as global options for whole partition table, always one header per line.
If no data
is specified then the header is removed. If header does not exist
and data
is specified then a new header is added.
Note that libfdisk can be used to specify arbitrary custom header, the default built-in headers are "unit" and "label", and some label specific headers (for example "uuid" and "name" for GPT).
int fdisk_script_set_fgets (struct fdisk_script *dp
,char* (*fn_fgets) (struct fdisk_script *, char *, size_t, FILE *)
);
The library uses fgets()
function to read the next line from the script.
This default maybe overridden by another function. Note that the function has
to return the line terminated by \n (for example readline(3) removes \n).
Return: 0 on success, <0 on error
int fdisk_script_write_file (struct fdisk_script *dp
,FILE *f
);
Writes script dp
to the file f
.
int fdisk_script_set_userdata (struct fdisk_script *dp
,void *data
);
Sets data usable for example in callbacks (e.g fdisk_script_set_fgets()
).