Script

Script — complex way to create and dump partition table

Functions

Types and Values

struct fdisk_script

Description

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:

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).

Functions

fdisk_set_script ()

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!

Parameters

cxt

context

 

dp

script (or NULL to remove previous reference)

 

Returns

<0 on error, 0 on success.


fdisk_get_script ()

struct fdisk_script *
fdisk_get_script (struct fdisk_context *cxt);

Parameters

cxt

context

 

Returns

the current script or NULL.


fdisk_apply_script ()

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.

Parameters

cxt

context

 

dp

script

 

Returns

0 on error, <0 on error.


fdisk_apply_script_headers ()

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.

Parameters

cxt

context

 

dp

script

 

Returns

0 on success, <0 on error.


fdisk_new_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.

Parameters

cxt

context

 

Returns

newly allocated script struct.


fdisk_new_script_from_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 .

Parameters

cxt

context

 

filename

path to the script file

 

Returns

new script instance or NULL in case of error (check errno for more details).


fdisk_ref_script ()

void
fdisk_ref_script (struct fdisk_script *dp);

Increments reference counter.

Parameters

dp

script pointer

 

fdisk_script_enable_json ()

int
fdisk_script_enable_json (struct fdisk_script *dp,
                          int json);

Disable/Enable JSON output format.

Parameters

dp

script

 

json

0 or 1

 

Returns

0 on success, <0 on error.


fdisk_script_get_header ()

const char *
fdisk_script_get_header (struct fdisk_script *dp,
                         const char *name);

Parameters

dp

script instance

 

name

header name

 

Returns

pointer to header data or NULL.


fdisk_script_get_nlines ()

int
fdisk_script_get_nlines (struct fdisk_script *dp);

Parameters

dp

script

 

Returns

number of parsed lines or <0 on error.


fdisk_script_set_table ()

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.

Parameters

dp

script

 

tb

table

 

Returns

0 on success, <0 on error

Since: 2.35


fdisk_script_get_table ()

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().

Parameters

dp

script

 

Returns

NULL or script table.


fdisk_script_has_force_label ()

int
fdisk_script_has_force_label (struct fdisk_script *dp);

Label has been explicitly specified in the script.

Parameters

dp

script

 

Returns

true if "label: name" has been parsed.

Since: 2.30


fdisk_script_read_context ()

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.

Parameters

dp

script

 

cxt

context

 

fdisk_script_read_file ()

int
fdisk_script_read_file (struct fdisk_script *dp,
                        FILE *f);

Reads file f into script dp .

Parameters

dp

script

 

f

input file

 

Returns

0 on success, <0 on error.


fdisk_script_read_line ()

int
fdisk_script_read_line (struct fdisk_script *dp,
                        FILE *f,
                        char *buf,
                        size_t bufsz);

Reads next line into dump.

Parameters

dp

script

 

f

file

 

buf

buffer to store one line of the file

 

bufsz

buffer size

 

Returns

0 on success, <0 on error, 1 when nothing to read. For unknown headers returns -ENOTSUP, it's usually safe to ignore this error.


fdisk_script_set_header ()

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).

Parameters

dp

script instance

 

name

header name

 

data

header data (or NULL)

 

Returns

0 on success, <0 on error


fdisk_script_set_fgets ()

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

Parameters

dp

script

 

fn_fgets

callback function

 

fdisk_script_write_file ()

int
fdisk_script_write_file (struct fdisk_script *dp,
                         FILE *f);

Writes script dp to the file f .

Parameters

dp

script

 

f

output file

 

Returns

0 on success, <0 on error.


fdisk_script_set_userdata ()

int
fdisk_script_set_userdata (struct fdisk_script *dp,
                           void *data);

Sets data usable for example in callbacks (e.g fdisk_script_set_fgets()).

Parameters

dp

script

 

data

your data

 

Returns

0 on success, <0 on error.


fdisk_script_get_userdata ()

void *
fdisk_script_get_userdata (struct fdisk_script *dp);

Parameters

dp

script

 

Returns

user data or NULL.


fdisk_unref_script ()

void
fdisk_unref_script (struct fdisk_script *dp);

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

Parameters

dp

script pointer

 

Types and Values

struct fdisk_script

struct fdisk_script;

library handler for sfdisk compatible scripts and dumps