Part

Part — plugin for operations with partition tables

Synopsis

#include <part.h>

gboolean            bd_part_check_deps                  ();
gboolean            bd_part_init                        ();
void                bd_part_close                       ();
#define             BD_PART_ERROR
#define             BD_PART_TYPE_SPEC
enum                BDPartAlign;
enum                BDPartFlag;
                    BDPartSpec;
enum                BDPartType;
enum                BDPartTypeReq;
enum                BDPartError;
enum                BDPartTableType;
enum                BDPartDiskFlag;
                    BDPartDiskSpec;
gboolean            bd_part_create_table                (const gchar *disk,
                                                         BDPartTableType type,
                                                         gboolean ignore_existing,
                                                         GError **error);
BDPartSpec *        bd_part_create_part                 (const gchar *disk,
                                                         BDPartTypeReq type,
                                                         guint64 start,
                                                         guint64 size,
                                                         BDPartAlign align,
                                                         GError **error);
gboolean            bd_part_delete_part                 (const gchar *disk,
                                                         const gchar *part,
                                                         GError **error);
gboolean            bd_part_resize_part                 (const gchar *disk,
                                                         const gchar *part,
                                                         guint64 size,
                                                         BDPartAlign align,
                                                         GError **error);
BDPartSpec **       bd_part_get_disk_parts              (const gchar *disk,
                                                         GError **error);
BDPartSpec *        bd_part_get_part_spec               (const gchar *disk,
                                                         const gchar *part,
                                                         GError **error);
gboolean            bd_part_set_part_flag               (const gchar *disk,
                                                         const gchar *part,
                                                         BDPartFlag flag,
                                                         gboolean state,
                                                         GError **error);
BDPartSpec *        bd_part_spec_copy                   (BDPartSpec *data);
void                bd_part_spec_free                   (BDPartSpec *data);
GType               bd_part_spec_get_type               ();
const gchar *       bd_part_get_flag_str                (BDPartFlag flag,
                                                         GError **error);
const gchar *       bd_part_get_part_table_type_str     (BDPartTableType type,
                                                         GError **error);
const gchar *       bd_part_get_type_str                (BDPartType type,
                                                         GError **error);
BDPartSpec *        bd_part_get_best_free_region        (const gchar *disk,
                                                         BDPartType type,
                                                         guint64 size,
                                                         GError **error);
BDPartSpec **       bd_part_get_disk_free_regions       (const gchar *disk,
                                                         GError **error);
BDPartDiskSpec *    bd_part_get_disk_spec               (const gchar *disk,
                                                         GError **error);
BDPartSpec *        bd_part_get_part_by_pos             (const gchar *disk,
                                                         guint64 position,
                                                         GError **error);
gboolean            bd_part_set_disk_flag               (const gchar *disk,
                                                         BDPartDiskFlag flag,
                                                         gboolean state,
                                                         GError **error);
gboolean            bd_part_set_part_flags              (const gchar *disk,
                                                         const gchar *part,
                                                         guint64 flags,
                                                         GError **error);
gboolean            bd_part_set_part_name               (const gchar *disk,
                                                         const gchar *part,
                                                         const gchar *name,
                                                         GError **error);
gboolean            bd_part_set_part_type               (const gchar *disk,
                                                         const gchar *part,
                                                         const gchar *type_guid,
                                                         GError **error);
gboolean            bd_part_set_part_id                 (const gchar *disk,
                                                         const gchar *part,
                                                         const gchar *part_id,
                                                         GError **error);
gchar *             bd_part_get_part_id                 (const gchar *disk,
                                                         const gchar *part,
                                                         GError **error);
GQuark              bd_part_error_quark                 (void);

Description

A plugin for operations with partition tables. Currently supported table (disk label) types are MBR and GPT. See the functions below to get an overview of which operations are supported. If there's anything missing, please don't hesitate to report it as this plugin (just like all the others) is subject to future development and enhancements.

This particular implementation of the part plugin uses libparted for manipulations of both the MBR and GPT disk label types together with the sgdisk utility for some extra GPT-specific features libparted doesn't support. In the future, there's likely to be another implementation of this plugin based on libfdisk which provides full support for both MBR and GPT tables (and possibly some others).

Details

bd_part_check_deps ()

gboolean            bd_part_check_deps                  ();

Returns :

whether the plugin's runtime dependencies are satisfied or not Function checking plugin's runtime dependencies.

bd_part_init ()

gboolean            bd_part_init                        ();

Initializes the plugin. **This function is called automatically by the library's initialization functions.**


bd_part_close ()

void                bd_part_close                       ();

Cleans up after the plugin. **This function is called automatically by the library's functions that unload it.**


BD_PART_ERROR

#define             BD_PART_ERROR

BD_PART_TYPE_SPEC

#define BD_PART_TYPE_SPEC (bd_part_spec_get_type ())

enum BDPartAlign

typedef enum {
    BD_PART_ALIGN_MINIMAL,
    BD_PART_ALIGN_OPTIMAL,
    BD_PART_ALIGN_NONE
} BDPartAlign;

BD_PART_ALIGN_MINIMAL

BD_PART_ALIGN_OPTIMAL

BD_PART_ALIGN_NONE


enum BDPartFlag

typedef enum {
    BD_PART_FLAG_BOOT = 1 << 1,
    BD_PART_FLAG_ROOT = 1 << 2,
    BD_PART_FLAG_SWAP = 1 << 3,
    BD_PART_FLAG_HIDDEN = 1 << 4,
    BD_PART_FLAG_RAID = 1 << 5,
    BD_PART_FLAG_LVM = 1 << 6,
    BD_PART_FLAG_LBA = 1 << 7,
    BD_PART_FLAG_HPSERVICE = 1 << 8,
    BD_PART_FLAG_CPALO = 1 << 9,
    BD_PART_FLAG_PREP = 1 << 10,
    BD_PART_FLAG_MSFT_RESERVED = 1 << 11,
    BD_PART_FLAG_BIOS_GRUB = 1 << 12,
    BD_PART_FLAG_APPLE_TV_RECOVERY = 1 << 13,
    BD_PART_FLAG_DIAG = 1 << 14,
    BD_PART_FLAG_LEGACY_BOOT = 1 << 15,
    BD_PART_FLAG_MSFT_DATA = 1 << 16,
    BD_PART_FLAG_IRST = 1 << 17,
    BD_PART_FLAG_ESP = 1 << 18,
    BD_PART_FLAG_BASIC_LAST = 1 << 19,
    BD_PART_FLAG_GPT_SYSTEM_PART = 1 << 25,
    BD_PART_FLAG_GPT_READ_ONLY = 1 << 26,
    BD_PART_FLAG_GPT_HIDDEN = 1 << 27,
    BD_PART_FLAG_GPT_NO_AUTOMOUNT = 1 << 28
} BDPartFlag;

BD_PART_FLAG_BOOT

BD_PART_FLAG_ROOT

BD_PART_FLAG_SWAP

BD_PART_FLAG_HIDDEN

BD_PART_FLAG_RAID

BD_PART_FLAG_LVM

BD_PART_FLAG_LBA

BD_PART_FLAG_HPSERVICE

BD_PART_FLAG_CPALO

BD_PART_FLAG_PREP

BD_PART_FLAG_MSFT_RESERVED

BD_PART_FLAG_BIOS_GRUB

BD_PART_FLAG_APPLE_TV_RECOVERY

BD_PART_FLAG_DIAG

BD_PART_FLAG_LEGACY_BOOT

BD_PART_FLAG_MSFT_DATA

BD_PART_FLAG_IRST

BD_PART_FLAG_ESP

BD_PART_FLAG_BASIC_LAST

BD_PART_FLAG_GPT_SYSTEM_PART

BD_PART_FLAG_GPT_READ_ONLY

BD_PART_FLAG_GPT_HIDDEN

BD_PART_FLAG_GPT_NO_AUTOMOUNT


BDPartSpec

typedef struct {
    gchar *path;
    gchar *name;
    gchar *type_guid;
    guint64 type;
    guint64 start;
    guint64 size;
    guint64 flags;
} BDPartSpec;

gchar *path;

path of the partition (block device)

gchar *name;

name of the partition (for GPT partitions)

gchar *type_guid;

GUID of the partition's type (GPT)

guint64 type;

bit combination of partition's types (BDPartType)

guint64 start;

start of the partition

guint64 size;

size of the partition

guint64 flags;

bit combination of partition's flags (BDPartFlag)

enum BDPartType

typedef enum {
    BD_PART_TYPE_NORMAL    = 0x00,
    BD_PART_TYPE_LOGICAL   = 0x01,
    BD_PART_TYPE_EXTENDED  = 0x02,
    BD_PART_TYPE_FREESPACE = 0x04,
    BD_PART_TYPE_METADATA  = 0x08,
    BD_PART_TYPE_PROTECTED = 0x10
} BDPartType;

BD_PART_TYPE_NORMAL

BD_PART_TYPE_LOGICAL

BD_PART_TYPE_EXTENDED

BD_PART_TYPE_FREESPACE

BD_PART_TYPE_METADATA

BD_PART_TYPE_PROTECTED


enum BDPartTypeReq

typedef enum {
    BD_PART_TYPE_REQ_NORMAL   = 0x00,
    BD_PART_TYPE_REQ_LOGICAL  = 0x01,
    BD_PART_TYPE_REQ_EXTENDED = 0x02,
    BD_PART_TYPE_REQ_NEXT     = 0x04
} BDPartTypeReq;

BD_PART_TYPE_REQ_NORMAL

BD_PART_TYPE_REQ_LOGICAL

BD_PART_TYPE_REQ_EXTENDED

BD_PART_TYPE_REQ_NEXT


enum BDPartError

typedef enum {
    BD_PART_ERROR_EXISTS,
    BD_PART_ERROR_INVAL,
    BD_PART_ERROR_FAIL,
} BDPartError;

BD_PART_ERROR_EXISTS

BD_PART_ERROR_INVAL

BD_PART_ERROR_FAIL


enum BDPartTableType

typedef enum {
    BD_PART_TABLE_MSDOS = 0,
    BD_PART_TABLE_GPT,
    BD_PART_TABLE_UNDEF
} BDPartTableType;

BD_PART_TABLE_MSDOS

BD_PART_TABLE_GPT

BD_PART_TABLE_UNDEF


enum BDPartDiskFlag

typedef enum {
    BD_PART_DISK_FLAG_GPT_PMBR_BOOT = 1
} BDPartDiskFlag;

BD_PART_DISK_FLAG_GPT_PMBR_BOOT


BDPartDiskSpec

typedef struct {
    gchar *path;
    BDPartTableType table_type;
    guint64 size;
    guint64 sector_size;
    guint64 flags;
} BDPartDiskSpec;

gchar *path;

path of the disk (block device)

BDPartTableType table_type;

type of the disk's partition table

guint64 size;

size of the disk

guint64 sector_size;

disk's sector size

guint64 flags;

bit combination of the disk's flags (BDPartDiskFlag)

bd_part_create_table ()

gboolean            bd_part_create_table                (const gchar *disk,
                                                         BDPartTableType type,
                                                         gboolean ignore_existing,
                                                         GError **error);

disk :

path of the disk block device to create partition table on

type :

type of the partition table to create

ignore_existing :

whether to ignore/overwrite the existing table or not (reports an error if FALSE and there's some table on disk)

error :

place to store error (if any). [out]

Returns :

whether the partition table was successfully created or not

bd_part_create_part ()

BDPartSpec *        bd_part_create_part                 (const gchar *disk,
                                                         BDPartTypeReq type,
                                                         guint64 start,
                                                         guint64 size,
                                                         BDPartAlign align,
                                                         GError **error);

disk :

disk to create partition on

type :

type of the partition to create (if BD_PART_TYPE_REQ_NEXT, the partition type will be determined automatically based on the existing partitions)

start :

where the partition should start (i.e. offset from the disk start)

size :

desired size of the partition (if 0, a max-sized partition is created)

align :

alignment to use for the partition

error :

place to store error (if any). [out]

Returns :

specification of the created partition or NULL in case of error NOTE: The resulting partition may start at a different position than given by start and can have different size than size due to alignment. [transfer full]

bd_part_delete_part ()

gboolean            bd_part_delete_part                 (const gchar *disk,
                                                         const gchar *part,
                                                         GError **error);

disk :

disk to remove the partition from

part :

partition to remove

error :

place to store error (if any). [out]

Returns :

whether the part partition was successfully deleted from disk

bd_part_resize_part ()

gboolean            bd_part_resize_part                 (const gchar *disk,
                                                         const gchar *part,
                                                         guint64 size,
                                                         BDPartAlign align,
                                                         GError **error);

disk :

disk containing the paritition

part :

partition to resize

size :

new partition size, 0 for maximal size

align :

alignment to use for the partition end

error :

place to store error (if any). [out]

Returns :

whether the part partition was successfully resized on disk to size NOTE: The resulting partition may be slightly bigger than requested due to alignment.

bd_part_get_disk_parts ()

BDPartSpec **       bd_part_get_disk_parts              (const gchar *disk,
                                                         GError **error);

disk :

disk to get information about partitions for

error :

place to store error (if any). [out]

Returns :

specs of the partitions from disk or NULL in case of error. [transfer full][array zero-terminated=1]

bd_part_get_part_spec ()

BDPartSpec *        bd_part_get_part_spec               (const gchar *disk,
                                                         const gchar *part,
                                                         GError **error);

disk :

disk to remove the partition from

part :

partition to get spec for

error :

place to store error (if any). [out]

Returns :

spec of the part partition from disk or NULL in case of error. [transfer full]

bd_part_set_part_flag ()

gboolean            bd_part_set_part_flag               (const gchar *disk,
                                                         const gchar *part,
                                                         BDPartFlag flag,
                                                         gboolean state,
                                                         GError **error);

disk :

disk the partition belongs to

part :

partition to set the flag on

flag :

flag to set

state :

state to set for the flag (TRUE = enabled)

error :

place to store error (if any). [out]

Returns :

whether the flag flag was successfully set on the part partition or not.

bd_part_spec_copy ()

BDPartSpec *        bd_part_spec_copy                   (BDPartSpec *data);

bd_part_spec_free ()

void                bd_part_spec_free                   (BDPartSpec *data);

bd_part_spec_get_type ()

GType               bd_part_spec_get_type               ();

bd_part_get_flag_str ()

const gchar *       bd_part_get_flag_str                (BDPartFlag flag,
                                                         GError **error);

flag :

flag to get string representation for

error :

place to store error (if any). [out]

Returns :

string representation of flag. [transfer none]

bd_part_get_part_table_type_str ()

const gchar *       bd_part_get_part_table_type_str     (BDPartTableType type,
                                                         GError **error);

type :

table type to get string representation for

error :

place to store error (if any). [out]

Returns :

string representation of table_type. [transfer none]

bd_part_get_type_str ()

const gchar *       bd_part_get_type_str                (BDPartType type,
                                                         GError **error);

type :

type to get string representation for

error :

place to store error (if any). [out]

Returns :

string representation of type. [transfer none]

bd_part_get_best_free_region ()

BDPartSpec *        bd_part_get_best_free_region        (const gchar *disk,
                                                         BDPartType type,
                                                         guint64 size,
                                                         GError **error);

disk :

disk to get the best free region for

type :

type of the partition that is planned to be added

size :

size of the partition to be added

error :

place to store error (if any). [out]

Returns :

spec of the best free region on disk for a new partition of type type with the size of size or NULL if there is none such region or if there was an error (error gets populated) Note: For the type BD_PART_TYPE_NORMAL, the smallest possible space that *is not* in an extended partition is found. For the type BD_PART_TYPE_LOGICAL, the smallest possible space that *is* in an extended partition is found. For BD_PART_TYPE_EXTENDED, the biggest possible space is found as long as there is no other extended partition (there can only be one). [transfer full]

bd_part_get_disk_free_regions ()

BDPartSpec **       bd_part_get_disk_free_regions       (const gchar *disk,
                                                         GError **error);

disk :

disk to get free regions for

error :

place to store error (if any). [out]

Returns :

specs of the free regions from disk or NULL in case of error. [transfer full][array zero-terminated=1]

bd_part_get_disk_spec ()

BDPartDiskSpec *    bd_part_get_disk_spec               (const gchar *disk,
                                                         GError **error);

disk :

disk to get information about

error :

place to store error (if any). [out]

Returns :

information about the given disk or NULL (in case of error). [transfer full]

bd_part_get_part_by_pos ()

BDPartSpec *        bd_part_get_part_by_pos             (const gchar *disk,
                                                         guint64 position,
                                                         GError **error);

disk :

disk to remove the partition from

position :

position (in bytes) determining the partition

error :

place to store error (if any). [out]

Returns :

spec of the partition from disk spanning over the position or NULL if no such partition exists or in case of error (error is set). [transfer full]

bd_part_set_disk_flag ()

gboolean            bd_part_set_disk_flag               (const gchar *disk,
                                                         BDPartDiskFlag flag,
                                                         gboolean state,
                                                         GError **error);

disk :

disk the partition belongs to

flag :

flag to set

state :

state to set for the flag (TRUE = enabled)

error :

place to store error (if any). [out]

Returns :

whether the flag flag was successfully set on the disk or not

bd_part_set_part_flags ()

gboolean            bd_part_set_part_flags              (const gchar *disk,
                                                         const gchar *part,
                                                         guint64 flags,
                                                         GError **error);

disk :

disk the partition belongs to

part :

partition to set the flag on

flags :

flags to set (mask combined from BDPartFlag numbers)

error :

place to store error (if any). [out]

Returns :

whether the flags were successfully set on the part partition or not Note: Unsets all the other flags on the partition.

bd_part_set_part_name ()

gboolean            bd_part_set_part_name               (const gchar *disk,
                                                         const gchar *part,
                                                         const gchar *name,
                                                         GError **error);

disk :

device the partition belongs to

part :

partition the should be set for

name :

name to set

error :

place to store error (if any). [out]

Returns :

whether the name was successfully set or not

bd_part_set_part_type ()

gboolean            bd_part_set_part_type               (const gchar *disk,
                                                         const gchar *part,
                                                         const gchar *type_guid,
                                                         GError **error);

disk :

device the partition belongs to

part :

partition the should be set for

type_guid :

GUID of the type

error :

place to store error (if any). [out]

Returns :

whether the type_guid type was successfully set for part or not

bd_part_set_part_id ()

gboolean            bd_part_set_part_id                 (const gchar *disk,
                                                         const gchar *part,
                                                         const gchar *part_id,
                                                         GError **error);

disk :

device the partition belongs to

part :

partition the should be set for

part_id :

partition Id

error :

place to store error (if any). [out]

Returns :

whether the part_id type was successfully set for part or not

bd_part_get_part_id ()

gchar *             bd_part_get_part_id                 (const gchar *disk,
                                                         const gchar *part,
                                                         GError **error);

disk :

device the partition belongs to

part :

partition the should be set for

error :

place to store error (if any). [out]

Returns :

partition id type or NULL in case of error. [transfer full]

bd_part_error_quark ()

GQuark              bd_part_error_quark                 (void);