Welcome to NYCU CSIT Mirror site

Partitions probing: libblkid Reference Manual

Partitions probing

Partitions probing — partitions tables detection and parsing

Types and Values

Description

This chain supports binary and NAME=value interfaces, but complete PT description is provided by binary interface only. The libblkid prober is compatible with kernel partition tables parser. The parser does not return empty (size=0) partitions or special hidden partitions.

NAME=value interface, supported tags:

PTTYPE : partition table type (dos, gpt, etc.).

PTUUID : partition table id (uuid for gpt, hex for dos).

PART_ENTRY_SCHEME : partition table type

PART_ENTRY_NAME : partition name (gpt and mac only)

PART_ENTRY_UUID : partition UUID (gpt, or pseudo IDs for MBR)

PART_ENTRY_TYPE : partition type, 0xNN (e.g 0x82) or type UUID (gpt only) or type string (mac)

PART_ENTRY_FLAGS : partition flags (e.g. boot_ind) or attributes (e.g. gpt attributes)

PART_ENTRY_NUMBER : partition number

PART_ENTRY_OFFSET : the begin of the partition

PART_ENTRY_SIZE : size of the partition

PART_ENTRY_DISK : whole-disk maj:min

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
blkid_probe pr;
const char *ptname;

pr = blkid_new_probe_from_filename(devname);
if (!pr)
err("%s: failed to open device", devname);

blkid_probe_enable_partitions(pr, TRUE);
blkid_do_fullprobe(pr);

blkid_probe_lookup_value(pr, "PTTYPE", &ptname, NULL);
printf("%s partition type detected\n", pttype);

blkid_free_probe(pr);

// don't forget to check return codes in your code!

Binary interface:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
blkid_probe pr;
blkid_partlist ls;
int nparts, i;

pr = blkid_new_probe_from_filename(devname);
if (!pr)
err("%s: failed to open device", devname);

ls = blkid_probe_get_partitions(pr);
nparts = blkid_partlist_numof_partitions(ls);

for (i = 0; i < nparts; i++) {
     blkid_partition par = blkid_partlist_get_partition(ls, i);
     printf("#%d: %llu %llu  0x%x",
              blkid_partition_get_partno(par),
              blkid_partition_get_start(par),
              blkid_partition_get_size(par),
              blkid_partition_get_type(par));
}

blkid_free_probe(pr);

// don't forget to check return codes in your code!

Functions

blkid_probe_enable_partitions ()

int
blkid_probe_enable_partitions (blkid_probe pr,
                               int enable);

Enables/disables the partitions probing for non-binary interface.

Parameters

pr

probe

 

enable

TRUE/FALSE

 

Returns

0 on success, or -1 in case of error.


blkid_probe_set_partitions_flags ()

int
blkid_probe_set_partitions_flags (blkid_probe pr,
                                  int flags);

Sets probing flags to the partitions prober. This function is optional.

Parameters

pr

prober

 

flags

BLKID_PARTS_* flags

 

Returns

0 on success, or -1 in case of error.


blkid_probe_filter_partitions_type ()

int
blkid_probe_filter_partitions_type (blkid_probe pr,
                                    int flag,
                                    char *names[]);

BLKID_FLTR_NOTIN - probe for all items which are NOT IN names

BLKID_FLTR_ONLYIN - probe for items which are IN names

Parameters

pr

prober

 

flag

filter BLKID_FLTR_{NOTIN,ONLYIN} flag

 

names

NULL terminated array of probing function names (e.g. "vfat").

 

Returns

0 on success, or -1 in case of error.


blkid_probe_invert_partitions_filter ()

int
blkid_probe_invert_partitions_filter (blkid_probe pr);

Inverts partitions probing filter

Parameters

pr

prober

 

Returns

0 on success, or -1 in case of error.


blkid_probe_reset_partitions_filter ()

int
blkid_probe_reset_partitions_filter (blkid_probe pr);

Resets partitions probing filter

Parameters

pr

prober

 

Returns

0 on success, or -1 in case of error.


blkid_known_pttype ()

int
blkid_known_pttype (const char *pttype);

Parameters

pttype

partition name

 

Returns

1 for known or 0 for unknown partition type.


blkid_partitions_get_name ()

int
blkid_partitions_get_name (const size_t idx,
                           const char **name);

Parameters

idx

number >= 0

 

name

returns name of a supported partition

 

Returns

-1 if idx is out of range, or 0 on success.

Since: 2.30


blkid_partition_get_name ()

const char *
blkid_partition_get_name (blkid_partition par);

Parameters

par

partition

 

Returns

partition name string if supported by PT (e.g. Mac) or NULL.


blkid_partition_get_flags ()

unsigned long long
blkid_partition_get_flags (blkid_partition par);

Parameters

par

partition

 

Returns

partition flags (or attributes for gpt).


blkid_partition_get_partno ()

int
blkid_partition_get_partno (blkid_partition par);

Parameters

par

partition

 

Returns

proposed partition number (e.g. 'N' from sda'N') or -1 in case of error. Note that the number is generate by library independently on your OS.


blkid_partition_get_size ()

blkid_loff_t
blkid_partition_get_size (blkid_partition par);

WARNING: be very careful when you work with MS-DOS extended partitions. The library always returns full size of the partition. If you want add the partition to the Linux system (BLKPG_ADD_PARTITION ioctl) you need to reduce the size of the partition to 1 or 2 blocks. The rest of the partition has to be inaccessible for mkfs or mkswap programs, we need a small space for boot loaders only.

For some unknown reason this (safe) practice is not to used for nested BSD, Solaris, ..., partition tables in Linux kernel.

Parameters

par

partition

 

Returns

size of the partition (in 512-sectors).


blkid_partition_get_start ()

blkid_loff_t
blkid_partition_get_start (blkid_partition par);

Be careful if you _not_ probe whole disk:

1) the offset is usually relative to begin of the disk -- but if you probe a fragment of the disk only -- then the offset could be still relative to the begin of the disk rather that relative to the fragment.

2) the offset for nested partitions could be relative to parent (e.g. Solaris) _or_ relative to the begin of the whole disk (e.g. bsd).

You don't have to care about such details if you probe whole disk. In such a case libblkid always returns the offset relative to the begin of the disk.

Parameters

par

partition

 

Returns

start of the partition (in 512-sectors).


blkid_partition_get_table ()

blkid_parttable
blkid_partition_get_table (blkid_partition par);

The "parttable" describes partition table. The table is usually the same for all partitions -- except nested partition tables.

For example bsd, solaris, etc. use a nested partition table within standard primary dos partition:

1
2
3
4
5
6
-- dos partition table
0: sda1     dos primary partition
1: sda2     dos primary partition
   -- bsd partition table (with in sda2)
2:    sda5  bds partition
3:    sda6  bds partition

The library does not to use a separate partition table object for dos logical partitions (partitions within extended partition). It's possible to differentiate between logical, extended and primary partitions by

blkid_partition_is_{extended,primary,logical}().

Parameters

par

partition

 

Returns

partition table object or NULL in case of error.


blkid_partition_get_type ()

int
blkid_partition_get_type (blkid_partition par);

Parameters

par

partition

 

Returns

partition type.


blkid_partition_get_type_string ()

const char *
blkid_partition_get_type_string (blkid_partition par);

The type string is supported by a small subset of partition tables (e.g Mac and EFI GPT). Note that GPT uses type UUID and this function returns this UUID as string.

Parameters

par

partition

 

Returns

partition type string or NULL.


blkid_partition_get_uuid ()

const char *
blkid_partition_get_uuid (blkid_partition par);

Parameters

par

partition

 

Returns

partition UUID string if supported by PT (e.g. GPT) or NULL.


blkid_partition_is_extended ()

int
blkid_partition_is_extended (blkid_partition par);

Parameters

par

partition

 

Returns

1 if the partitions is extended (dos, windows or linux) partition or 0 if not.


blkid_partition_is_logical ()

int
blkid_partition_is_logical (blkid_partition par);

Note that this function returns TRUE for all partitions in all nested partition tables (e.g. BSD labels).

Parameters

par

partition

 

Returns

1 if the partitions is logical partition or 0 if not.


blkid_partition_is_primary ()

int
blkid_partition_is_primary (blkid_partition par);

Note, this function returns FALSE for DOS extended partitions and all partitions in nested partition tables.

Parameters

par

partition

 

Returns

1 if the partitions is primary partition or 0 if not.


blkid_partlist_get_partition ()

blkid_partition
blkid_partlist_get_partition (blkid_partlist ls,
                              int n);

It's possible that the list of partitions is *empty*, but there is a valid partition table on the disk. This happen when on-disk details about partitions are unknown or the partition table is empty.

See also blkid_partlist_get_table().

Parameters

ls

partitions list

 

n

partition number in range 0..N, where 'N' is blkid_partlist_numof_partitions().

 

Returns

partition object or NULL in case or error.


blkid_partlist_get_partition_by_partno ()

blkid_partition
blkid_partlist_get_partition_by_partno
                               (blkid_partlist ls,
                                int n);

This does not assume any order of the input blkid_partlist. And correctly handles "out of order" partition tables. partition N is located after partition N+1 on the disk.

Parameters

ls

partitions list

 

n

the partition number (e.g. 'N' from sda'N')

 

Returns

partition object or NULL in case or error.


blkid_partlist_numof_partitions ()

int
blkid_partlist_numof_partitions (blkid_partlist ls);

Parameters

ls

partitions list

 

Returns

number of partitions in the list or -1 in case of error.


blkid_partlist_devno_to_partition ()

blkid_partition
blkid_partlist_devno_to_partition (blkid_partlist ls,
                                   dev_t devno);

This function tries to get start and size for devno from sysfs and returns a partition from ls which matches with the values from sysfs.

This function is necessary when you want to make a relation between an entry in the partition table (ls ) and block devices in your system.

Parameters

ls

partitions list

 

devno

requested partition

 

Returns

partition object or NULL in case or error.


blkid_partlist_get_table ()

blkid_parttable
blkid_partlist_get_table (blkid_partlist ls);

Parameters

ls

partitions list

 

Returns

top-level partition table or NULL of there is not a partition table on the device.


blkid_parttable_get_id ()

const char *
blkid_parttable_get_id (blkid_parttable tab);

The ID is GPT disk UUID or DOS disk ID (in hex format).

Parameters

tab

partition table

 

Returns

partition table ID (for example GPT disk UUID) or NULL


blkid_parttable_get_offset ()

blkid_loff_t
blkid_parttable_get_offset (blkid_parttable tab);

Note the position is relative to begin of the device as defined by blkid_probe_set_device() for primary partition table, and relative to parental partition for nested partition tables.

1
2
3
4
5
6
7
8
off_t offset;
blkid_partition parent = blkid_parttable_get_parent(tab);

offset = blkid_parttable_get_offset(tab);

if (parent)
     / * 'tab' is nested partition table * /
offset += blkid_partition_get_start(parent);

Parameters

tab

partition table

 

Returns

position (in bytes) of the partition table or -1 in case of error.


blkid_parttable_get_parent ()

blkid_partition
blkid_parttable_get_parent (blkid_parttable tab);

Parameters

tab

partition table

 

Returns

parent for nested partition tables or NULL.


blkid_parttable_get_type ()

const char *
blkid_parttable_get_type (blkid_parttable tab);

Parameters

tab

partition table

 

Returns

partition table type (type name, e.g. "dos", "gpt", ...)


blkid_probe_get_partitions ()

blkid_partlist
blkid_probe_get_partitions (blkid_probe pr);

This is a binary interface for partitions. See also blkid_partlist_* functions.

This function is independent on blkid_do_[safe,full]probe() and blkid_probe_enable_partitions() calls.

WARNING: the returned object will be overwritten by the next blkid_probe_get_partitions() call for the same pr . If you want to use more blkid_partlist objects in the same time you have to create more blkid_probe handlers (see blkid_new_probe()).

Parameters

pr

probe

 

Returns

list of partitions, or NULL in case of error.

Types and Values

blkid_partlist

typedef struct blkid_struct_partlist *blkid_partlist;

list of all detected partitions and partitions tables


blkid_partition

typedef struct blkid_struct_partition *blkid_partition;

information about a partition


blkid_parttable

typedef struct blkid_struct_parttable *blkid_parttable;

information about a partition table