Table of filesystems

Table of filesystems — container for entries from fstab, mtab or mountinfo

Synopsis

struct              libmnt_table;
void                mnt_free_table                      (struct libmnt_table *tb);
struct libmnt_table * mnt_new_table                     (void);
int                 mnt_reset_table                     (struct libmnt_table *tb);
struct libmnt_table * mnt_new_table_from_dir            (const char *dirname);
struct libmnt_table * mnt_new_table_from_file           (const char *filename);
int                 mnt_table_add_fs                    (struct libmnt_table *tb,
                                                         struct libmnt_fs *fs);
struct libmnt_fs *  mnt_table_find_devno                (struct libmnt_table *tb,
                                                         dev_t devno,
                                                         int direction);
int                 mnt_table_find_next_fs              (struct libmnt_table *tb,
                                                         struct libmnt_iter *itr,
                                                         int (*match_func) (struct libmnt_fs *, void *),
                                                         void *userdata,
                                                         struct libmnt_fs **fs);
struct libmnt_fs *  mnt_table_find_pair                 (struct libmnt_table *tb,
                                                         const char *source,
                                                         const char *target,
                                                         int direction);
struct libmnt_fs *  mnt_table_find_source               (struct libmnt_table *tb,
                                                         const char *source,
                                                         int direction);
struct libmnt_fs *  mnt_table_find_srcpath              (struct libmnt_table *tb,
                                                         const char *path,
                                                         int direction);
struct libmnt_fs *  mnt_table_find_tag                  (struct libmnt_table *tb,
                                                         const char *tag,
                                                         const char *val,
                                                         int direction);
struct libmnt_fs *  mnt_table_find_target               (struct libmnt_table *tb,
                                                         const char *path,
                                                         int direction);
struct libmnt_cache * mnt_table_get_cache               (struct libmnt_table *tb);
int                 mnt_table_get_nents                 (struct libmnt_table *tb);
int                 mnt_table_get_root_fs               (struct libmnt_table *tb,
                                                         struct libmnt_fs **root);
int                 mnt_table_is_fs_mounted             (struct libmnt_table *tb,
                                                         struct libmnt_fs *fstab_fs);
int                 mnt_table_next_child_fs             (struct libmnt_table *tb,
                                                         struct libmnt_iter *itr,
                                                         struct libmnt_fs *parent,
                                                         struct libmnt_fs **chld);
int                 mnt_table_next_fs                   (struct libmnt_table *tb,
                                                         struct libmnt_iter *itr,
                                                         struct libmnt_fs **fs);
int                 mnt_table_parse_dir                 (struct libmnt_table *tb,
                                                         const char *dirname);
int                 mnt_table_parse_file                (struct libmnt_table *tb,
                                                         const char *filename);
int                 mnt_table_parse_fstab               (struct libmnt_table *tb,
                                                         const char *filename);
int                 mnt_table_parse_mtab                (struct libmnt_table *tb,
                                                         const char *filename);
int                 mnt_table_parse_stream              (struct libmnt_table *tb,
                                                         FILE *f,
                                                         const char *filename);
int                 mnt_table_parse_swaps               (struct libmnt_table *tb,
                                                         const char *filename);
int                 mnt_table_remove_fs                 (struct libmnt_table *tb,
                                                         struct libmnt_fs *fs);
int                 mnt_table_set_cache                 (struct libmnt_table *tb,
                                                         struct libmnt_cache *mpc);
int                 mnt_table_set_iter                  (struct libmnt_table *tb,
                                                         struct libmnt_iter *itr,
                                                         struct libmnt_fs *fs);
int                 mnt_table_set_parser_errcb          (struct libmnt_table *tb,
                                                         int (*cb) (struct libmnt_table *tb, const char *filename, int line));

Description

Note that mnt_table_find_* functions are mount(8) compatible. These functions try to find an entry in more iterations where the first attempt is always based on comparison with unmodified (non-canonicalized or un-evaluated) paths or tags. For example fstab with two entries:

1
2
LABEL=foo   /foo    auto   rw
/dev/foo    /foo    auto   rw

where both lines are used for the *same* device, then

1
mnt_table_find_source(tb, "/dev/foo", &fs);

will returns the second line, and

1
mnt_table_find_source(tb, "LABEL=foo", &fs);

will returns the first entry, and

1
mnt_table_find_source(tb, "UUID=anyuuid", &fs);

will returns the first entry (if UUID matches with the device).

Details

struct libmnt_table

struct libmnt_table;

List of struct libmnt_fs entries (parsed fstab/mtab/mountinfo)


mnt_free_table ()

void                mnt_free_table                      (struct libmnt_table *tb);

Deallocates tab struct and all entries.

tb :

tab pointer

mnt_new_table ()

struct libmnt_table * mnt_new_table                     (void);

The tab is a container for struct libmnt_fs entries that usually represents a fstab, mtab or mountinfo file from your system.

See also mnt_table_parse_file().

Returns :

newly allocated tab struct.

mnt_reset_table ()

int                 mnt_reset_table                     (struct libmnt_table *tb);

Dealocates all entries (filesystems) from the table

tb :

tab pointer

Returns :

0 on success or negative number in case of error.

mnt_new_table_from_dir ()

struct libmnt_table * mnt_new_table_from_dir            (const char *dirname);

dirname :

directory with *.fstab files

Returns :

newly allocated tab on success and NULL in case of error.

mnt_new_table_from_file ()

struct libmnt_table * mnt_new_table_from_file           (const char *filename);

Same as mnt_new_table() + mnt_table_parse_file(). Use this function for private files only. This function does not allow to use error callback, so you cannot provide any feedback to end-users about broken records in files (e.g. fstab).

filename :

/etc/{m,fs}tab or /proc/self/mountinfo path

Returns :

newly allocated tab on success and NULL in case of error.

mnt_table_add_fs ()

int                 mnt_table_add_fs                    (struct libmnt_table *tb,
                                                         struct libmnt_fs *fs);

Adds a new entry to tab.

tb :

tab pointer

fs :

new entry

Returns :

0 on success or negative number in case of error.

mnt_table_find_devno ()

struct libmnt_fs *  mnt_table_find_devno                (struct libmnt_table *tb,
                                                         dev_t devno,
                                                         int direction);

Note that zero could be valid device number for root pseudo filesystem (e.g. tmpfs).

tb :

/proc/self/mountinfo

devno :

device number

direction :

MNT_ITER_{FORWARD,BACKWARD}

Returns :

a tab entry or NULL.

mnt_table_find_next_fs ()

int                 mnt_table_find_next_fs              (struct libmnt_table *tb,
                                                         struct libmnt_iter *itr,
                                                         int (*match_func) (struct libmnt_fs *, void *),
                                                         void *userdata,
                                                         struct libmnt_fs **fs);

This function allows search in tb.

tb :

table

itr :

iterator

match_func :

function returns 1 or 0

userdata :

extra data for match_func

fs :

returns pointer to the next matching table entry

Returns :

negative number in case of error, 1 at end of table or 0 o success.

mnt_table_find_pair ()

struct libmnt_fs *  mnt_table_find_pair                 (struct libmnt_table *tb,
                                                         const char *source,
                                                         const char *target,
                                                         int direction);

This function is implemented by mnt_fs_match_source() and mnt_fs_match_target() functions. It means that this is more expensive that others mnt_table_find_* function, because every tab entry is fully evaluated.

tb :

tab pointer

source :

TAG or path

target :

mountpoint

direction :

MNT_ITER_{FORWARD,BACKWARD}

Returns :

a tab entry or NULL.

mnt_table_find_source ()

struct libmnt_fs *  mnt_table_find_source               (struct libmnt_table *tb,
                                                         const char *source,
                                                         int direction);

This is high-level API for mnt_table_find_{srcpath,tag}. You needn't to care about source format (device, LABEL, UUID, ...). This function parses source and calls mnt_table_find_tag() or mnt_table_find_srcpath().

tb :

tab pointer

source :

TAG or path

direction :

MNT_ITER_{FORWARD,BACKWARD}

Returns :

a tab entry or NULL.

mnt_table_find_srcpath ()

struct libmnt_fs *  mnt_table_find_srcpath              (struct libmnt_table *tb,
                                                         const char *path,
                                                         int direction);

Try to lookup an entry in given tab, possible are four iterations, first with path, second with realpath(path), third with tags (LABEL, UUID, ..) from path and fourth with realpath(path) against realpath(entry->srcpath).

The 2nd, 3rd and 4th iterations are not performed when tb cache is not set (see mnt_table_set_cache()).

Note that NULL is a valid source path; it will be replaced with "none". The "none" is used in /proc/{mounts,self/mountinfo} for pseudo filesystems.

tb :

tab pointer

path :

source path (devname or dirname) or NULL

direction :

MNT_ITER_{FORWARD,BACKWARD}

Returns :

a tab entry or NULL.

mnt_table_find_tag ()

struct libmnt_fs *  mnt_table_find_tag                  (struct libmnt_table *tb,
                                                         const char *tag,
                                                         const char *val,
                                                         int direction);

Try to lookup an entry in given tab, first attempt is lookup by tag and val, for the second attempt the tag is evaluated (converted to the device name) and mnt_table_find_srcpath() is preformed. The second attempt is not performed when tb cache is not set (see mnt_table_set_cache()).

tb :

tab pointer

tag :

tag name (e.g "LABEL", "UUID", ...)

val :

tag value

direction :

MNT_ITER_{FORWARD,BACKWARD}

Returns :

a tab entry or NULL.

mnt_table_find_target ()

struct libmnt_fs *  mnt_table_find_target               (struct libmnt_table *tb,
                                                         const char *path,
                                                         int direction);

Try to lookup an entry in given tab, possible are three iterations, first with path, second with realpath(path) and third with realpath(path) against realpath(fs->target). The 2nd and 3rd iterations are not performed when tb cache is not set (see mnt_table_set_cache()).

tb :

tab pointer

path :

mountpoint directory

direction :

MNT_ITER_{FORWARD,BACKWARD}

Returns :

a tab entry or NULL.

mnt_table_get_cache ()

struct libmnt_cache * mnt_table_get_cache               (struct libmnt_table *tb);

tb :

pointer to tab

Returns :

pointer to struct libmnt_cache instance or NULL.

mnt_table_get_nents ()

int                 mnt_table_get_nents                 (struct libmnt_table *tb);

tb :

pointer to tab

Returns :

number of valid entries in tab.

mnt_table_get_root_fs ()

int                 mnt_table_get_root_fs               (struct libmnt_table *tb,
                                                         struct libmnt_fs **root);

The function uses parent ID from mountinfo file to determine root filesystem (the filesystem with the smallest ID). The function is designed mostly for applications where is necessary to sort mountpoints by IDs to get the tree of the mountpoints (e.g. findmnt default output).

If you're not sure than use

mnt_table_find_target(tb, "/", MNT_ITER_BACKWARD);

this is more robust and usable for arbitrary tab file (including fstab).

tb :

mountinfo file (/proc/self/mountinfo)

root :

returns pointer to the root filesystem (/)

Returns :

0 on success or less then zero case of error.

mnt_table_is_fs_mounted ()

int                 mnt_table_is_fs_mounted             (struct libmnt_table *tb,
                                                         struct libmnt_fs *fstab_fs);

mnt_table_next_child_fs ()

int                 mnt_table_next_child_fs             (struct libmnt_table *tb,
                                                         struct libmnt_iter *itr,
                                                         struct libmnt_fs *parent,
                                                         struct libmnt_fs **chld);

Note that filesystems are returned in the order how was mounted (according to IDs in /proc/self/mountinfo).

tb :

mountinfo file (/proc/self/mountinfo)

itr :

iterator

parent :

parental FS

chld :

returns the next child filesystem

Returns :

0 on success, negative number in case of error or 1 at end of list.

mnt_table_next_fs ()

int                 mnt_table_next_fs                   (struct libmnt_table *tb,
                                                         struct libmnt_iter *itr,
                                                         struct libmnt_fs **fs);

tb :

tab pointer

itr :

iterator

fs :

returns the next tab entry

Returns :

0 on success, negative number in case of error or 1 at end of list. Example:
1
2
3
4
5
while(mnt_table_next_fs(tb, itr, &fs) == 0) {
const char *dir = mnt_fs_get_target(fs);
printf("mount point: %s\n", dir);
}
mnt_free_table(fi);
lists all mountpoints from fstab in backward order.

mnt_table_parse_dir ()

int                 mnt_table_parse_dir                 (struct libmnt_table *tb,
                                                         const char *dirname);

The directory:

  • files are sorted by strverscmp(3)

  • files that starts with "." are ignored (e.g. ".10foo.fstab")

  • files without the ".fstab" extension are ignored

tb :

mount table

dirname :

directory

Returns :

0 on success or negative number in case of error.

mnt_table_parse_file ()

int                 mnt_table_parse_file                (struct libmnt_table *tb,
                                                         const char *filename);

Parses whole table (e.g. /etc/mtab) and appends new records to the tab.

The libmount parser ignores broken (syntax error) lines, these lines are reported to caller by errcb() function (see mnt_table_set_parser_errcb()).

tb :

tab pointer

filename :

file

Returns :

0 on success, negative number in case of error.

mnt_table_parse_fstab ()

int                 mnt_table_parse_fstab               (struct libmnt_table *tb,
                                                         const char *filename);

This function parses /etc/fstab and appends new lines to the tab. If the filename is a directory then mnt_table_parse_dir() is called.

See also mnt_table_set_parser_errcb().

tb :

table

filename :

overwrites default (/etc/fstab or $LIBMOUNT_FSTAB) or NULL

Returns :

0 on success or negative number in case of error.

mnt_table_parse_mtab ()

int                 mnt_table_parse_mtab                (struct libmnt_table *tb,
                                                         const char *filename);

This function parses /etc/mtab or /proc/self/mountinfo + /run/mount/utabs or /proc/mounts.

See also mnt_table_set_parser_errcb().

tb :

table

filename :

overwrites default (/etc/mtab or $LIBMOUNT_MTAB) or NULL

Returns :

0 on success or negative number in case of error.

mnt_table_parse_stream ()

int                 mnt_table_parse_stream              (struct libmnt_table *tb,
                                                         FILE *f,
                                                         const char *filename);

tb :

tab pointer

f :

file stream

filename :

filename used for debug and error messages

Returns :

0 on success, negative number in case of error.

mnt_table_parse_swaps ()

int                 mnt_table_parse_swaps               (struct libmnt_table *tb,
                                                         const char *filename);

This function parses /proc/swaps and appends new lines to the tab.

See also mnt_table_set_parser_errcb().

tb :

table

filename :

overwrites default (/proc/swaps or $LIBMOUNT_SWAPS) or NULL

Returns :

0 on success or negative number in case of error.

mnt_table_remove_fs ()

int                 mnt_table_remove_fs                 (struct libmnt_table *tb,
                                                         struct libmnt_fs *fs);

tb :

tab pointer

fs :

new entry

Returns :

0 on success or negative number in case of error.

mnt_table_set_cache ()

int                 mnt_table_set_cache                 (struct libmnt_table *tb,
                                                         struct libmnt_cache *mpc);

Setups a cache for canonicalized paths and evaluated tags (LABEL/UUID). The cache is recommended for mnt_table_find_*() functions.

The cache could be shared between more tabs. Be careful when you share the same cache between more threads -- currently the cache does not provide any locking method.

See also mnt_new_cache().

tb :

pointer to tab

mpc :

pointer to struct libmnt_cache instance

Returns :

0 on success or negative number in case of error.

mnt_table_set_iter ()

int                 mnt_table_set_iter                  (struct libmnt_table *tb,
                                                         struct libmnt_iter *itr,
                                                         struct libmnt_fs *fs);

Sets iter to the position of fs in the file tb.

tb :

tab pointer

itr :

iterator

fs :

tab entry

Returns :

0 on success, negative number in case of error.

mnt_table_set_parser_errcb ()

int                 mnt_table_set_parser_errcb          (struct libmnt_table *tb,
                                                         int (*cb) (struct libmnt_table *tb, const char *filename, int line));

The error callback function is called by table parser (mnt_table_parse_file()) in case of syntax error. The callback function could be used for errors evaluation, libmount will continue/stop parsing according to callback return codes:

<0 : fatal error (abort parsing) 0 : success (parsing continue) >0 : recoverable error (the line is ignored, parsing continue).

tb :

pointer to table

cb :

pointer to callback function

Returns :

0 on success or negative number in case of error.