Welcome to NYCU CSIT Mirror site

Library high-level context: libmount Reference Manual

Library high-level context

Library high-level context — high-level API to mount/umount devices.

Functions

void mnt_free_context ()
struct libmnt_context * mnt_new_context ()
int mnt_reset_context ()
int mnt_context_append_options ()
int mnt_context_apply_fstab ()
int mnt_context_disable_canonicalize ()
int mnt_context_disable_helpers ()
int mnt_context_disable_mtab ()
int mnt_context_disable_swapmatch ()
int mnt_context_enable_fake ()
int mnt_context_enable_force ()
int mnt_context_enable_fork ()
int mnt_context_enable_lazy ()
int mnt_context_enable_loopdel ()
int mnt_context_enable_rdonly_umount ()
int mnt_context_enable_rwonly_mount ()
int mnt_context_enable_sloppy ()
int mnt_context_enable_verbose ()
int mnt_context_forced_rdonly ()
int mnt_context_force_unrestricted ()
struct libmnt_cache * mnt_context_get_cache ()
int mnt_context_get_excode ()
struct libmnt_fs * mnt_context_get_fs ()
int mnt_context_get_fstab ()
void * mnt_context_get_fstab_userdata ()
const char * mnt_context_get_fstype ()
void * mnt_context_get_fs_userdata ()
int mnt_context_get_helper_status ()
struct libmnt_lock * mnt_context_get_lock ()
int mnt_context_get_mflags ()
int mnt_context_get_mtab ()
void * mnt_context_get_mtab_userdata ()
const char * mnt_context_get_options ()
int mnt_context_get_optsmode ()
struct libmnt_ns * mnt_context_get_origin_ns ()
const char * mnt_context_get_source ()
int mnt_context_get_status ()
int mnt_context_get_syscall_errno ()
int mnt_context_get_table ()
const char * mnt_context_get_target ()
struct libmnt_ns * mnt_context_get_target_ns ()
const char * mnt_context_get_target_prefix ()
int mnt_context_get_user_mflags ()
int mnt_context_helper_executed ()
int mnt_context_helper_setopt ()
int mnt_context_init_helper ()
int mnt_context_is_child ()
int mnt_context_is_fake ()
int mnt_context_is_force ()
int mnt_context_is_fork ()
int mnt_context_is_fs_mounted ()
int mnt_context_is_lazy ()
int mnt_context_is_loopdel ()
int mnt_context_is_nocanonicalize ()
int mnt_context_is_nohelpers ()
int mnt_context_is_nomtab ()
int mnt_context_is_parent ()
int mnt_context_is_rdonly_umount ()
int mnt_context_is_restricted ()
int mnt_context_is_rwonly_mount ()
int mnt_context_is_sloppy ()
int mnt_context_is_swapmatch ()
int mnt_context_is_verbose ()
int mnt_context_reset_status ()
int mnt_context_set_cache ()
int mnt_context_set_fs ()
int mnt_context_set_fstab ()
int mnt_context_set_fstype ()
int mnt_context_set_fstype_pattern ()
int mnt_context_set_mflags ()
int mnt_context_set_mountdata ()
int mnt_context_set_options ()
int mnt_context_set_options_pattern ()
int mnt_context_set_optsmode ()
int mnt_context_set_passwd_cb ()
int mnt_context_set_source ()
int mnt_context_set_syscall_status ()
int mnt_context_set_tables_errcb ()
int mnt_context_set_target ()
int mnt_context_set_target_ns ()
int mnt_context_set_target_prefix ()
int mnt_context_set_user_mflags ()
int mnt_context_strerror ()
struct libmnt_ns * mnt_context_switch_ns ()
struct libmnt_ns * mnt_context_switch_origin_ns ()
struct libmnt_ns * mnt_context_switch_target_ns ()
int mnt_context_syscall_called ()
int mnt_context_tab_applied ()
int mnt_context_wait_for_children ()

Types and Values

Description

1
2
3
4
5
6
7
8
9
struct libmnt_context *cxt = mnt_new_context();

mnt_context_set_options(cxt, "aaa,bbb,ccc=CCC");
mnt_context_set_mflags(cxt, MS_NOATIME|MS_NOEXEC);
mnt_context_set_target(cxt, "/mnt/foo");

if (!mnt_context_mount(cxt))
	printf("successfully mounted\n");
mnt_free_context(cxt);

This code is similar to:

mount -o aaa,bbb,ccc=CCC,noatime,noexec /mnt/foo

Functions

mnt_free_context ()

void
mnt_free_context (struct libmnt_context *cxt);

Deallocates context struct.

Parameters

cxt

mount context

 

mnt_new_context ()

struct libmnt_context *
mnt_new_context (void);

Returns

newly allocated mount context


mnt_reset_context ()

int
mnt_reset_context (struct libmnt_context *cxt);

Resets all information in the context that is directly related to the latest mount (spec, source, target, mount options, ...).

The match patterns, target namespace, prefix, cached fstab, cached canonicalized paths and tags and [e]uid are not reset. You have to use

mnt_context_set_fstab(cxt, NULL); mnt_context_set_cache(cxt, NULL); mnt_context_set_fstype_pattern(cxt, NULL); mnt_context_set_options_pattern(cxt, NULL); mnt_context_set_target_ns(cxt, NULL);

to reset this stuff.

Parameters

cxt

mount context

 

Returns

0 on success, negative number in case of error.


mnt_context_append_options ()

int
mnt_context_append_options (struct libmnt_context *cxt,
                            const char *optstr);

Parameters

cxt

mount context

 

optstr

comma delimited mount options

 

Returns

0 on success, negative number in case of error.


mnt_context_apply_fstab ()

int
mnt_context_apply_fstab (struct libmnt_context *cxt);

This function is optional.

Parameters

cxt

mount context

 

Returns

0 on success, negative number in case of error.


mnt_context_disable_canonicalize ()

int
mnt_context_disable_canonicalize (struct libmnt_context *cxt,
                                  int disable);

Enable/disable paths canonicalization and tags evaluation. The libmount context canonicalizes paths when searching in fstab and when preparing source and target paths for mount(2) syscall.

This function has an effect on the private (within context) fstab instance only (see mnt_context_set_fstab()). If you want to use an external fstab then you need to manage your private struct libmnt_cache (see mnt_table_set_cache(fstab, NULL).

Parameters

cxt

mount context

 

disable

TRUE or FALSE

 

Returns

0 on success, negative number in case of error.


mnt_context_disable_helpers ()

int
mnt_context_disable_helpers (struct libmnt_context *cxt,
                             int disable);

Enable/disable /sbin/[u]mount.* helpers (see mount(8) man page, option -i).

Parameters

cxt

mount context

 

disable

TRUE or FALSE

 

Returns

0 on success, negative number in case of error.


mnt_context_disable_mtab ()

int
mnt_context_disable_mtab (struct libmnt_context *cxt,
                          int disable);

Disable/enable mtab update (see mount(8) man page, option -n).

Parameters

cxt

mount context

 

disable

TRUE or FALSE

 

Returns

0 on success, negative number in case of error.


mnt_context_disable_swapmatch ()

int
mnt_context_disable_swapmatch (struct libmnt_context *cxt,
                               int disable);

Disable/enable swap between source and target for mount(8) if only one path is specified.

Parameters

cxt

mount context

 

disable

TRUE or FALSE

 

Returns

0 on success, negative number in case of error.


mnt_context_enable_fake ()

int
mnt_context_enable_fake (struct libmnt_context *cxt,
                         int enable);

Enable/disable fake mounting (see mount(8) man page, option -f).

Parameters

cxt

mount context

 

enable

TRUE or FALSE

 

Returns

0 on success, negative number in case of error.


mnt_context_enable_force ()

int
mnt_context_enable_force (struct libmnt_context *cxt,
                          int enable);

Enable/disable force umounting (see umount(8) man page, option -f).

Parameters

cxt

mount context

 

enable

TRUE or FALSE

 

Returns

0 on success, negative number in case of error.


mnt_context_enable_fork ()

int
mnt_context_enable_fork (struct libmnt_context *cxt,
                         int enable);

Enable/disable fork(2) call in mnt_context_next_mount() (see mount(8) man page, option -F).

Parameters

cxt

mount context

 

enable

TRUE or FALSE

 

Returns

0 on success, negative number in case of error.


mnt_context_enable_lazy ()

int
mnt_context_enable_lazy (struct libmnt_context *cxt,
                         int enable);

Enable/disable lazy umount (see umount(8) man page, option -l).

Parameters

cxt

mount context

 

enable

TRUE or FALSE

 

Returns

0 on success, negative number in case of error.


mnt_context_enable_loopdel ()

int
mnt_context_enable_loopdel (struct libmnt_context *cxt,
                            int enable);

Enable/disable the loop delete (destroy) after umount (see umount(8), option -d)

Parameters

cxt

mount context

 

enable

TRUE or FALSE

 

Returns

0 on success, negative number in case of error.


mnt_context_enable_rdonly_umount ()

int
mnt_context_enable_rdonly_umount (struct libmnt_context *cxt,
                                  int enable);

Enable/disable read-only remount on failed umount(2) (see umount(8) man page, option -r).

Parameters

cxt

mount context

 

enable

TRUE or FALSE

 

Returns

0 on success, negative number in case of error.


mnt_context_enable_rwonly_mount ()

int
mnt_context_enable_rwonly_mount (struct libmnt_context *cxt,
                                 int enable);

Force read-write mount; if enabled libmount will never try MS_RDONLY after failed mount(2) EROFS. (See mount(8) man page, option -w).

Parameters

cxt

mount context

 

enable

TRUE or FALSE

 

Returns

0 on success, negative number in case of error.

Since: 2.30


mnt_context_enable_sloppy ()

int
mnt_context_enable_sloppy (struct libmnt_context *cxt,
                           int enable);

Set/unset sloppy mounting (see mount(8) man page, option -s).

Parameters

cxt

mount context

 

enable

TRUE or FALSE

 

Returns

0 on success, negative number in case of error.


mnt_context_enable_verbose ()

int
mnt_context_enable_verbose (struct libmnt_context *cxt,
                            int enable);

Enable/disable verbose output (TODO: not implemented yet)

Parameters

cxt

mount context

 

enable

TRUE or FALSE

 

Returns

0 on success, negative number in case of error.


mnt_context_forced_rdonly ()

int
mnt_context_forced_rdonly (struct libmnt_context *cxt);

See also mnt_context_enable_rwonly_mount().

Parameters

cxt

mount context

 

Returns

1 if mounted read-only on write-protected device.

Since: 2.30


mnt_context_force_unrestricted ()

int
mnt_context_force_unrestricted (struct libmnt_context *cxt);

This function is DANGEROURS as it disables all security policies in libmount. Don't use if not sure. It removes "restricted" flag from the context, so libmount will use the current context as for root user.

This function is designed for case you have no any suid permissions, so you can depend on kernel.

Parameters

cxt

mount context

 

Returns

0 on success, negative number in case of error.

Since: 2.35


mnt_context_get_cache ()

struct libmnt_cache *
mnt_context_get_cache (struct libmnt_context *cxt);

See also mnt_context_set_cache().

Parameters

cxt

mount context

 

Returns

pointer to cache or NULL if canonicalization is disabled.


mnt_context_get_excode ()

int
mnt_context_get_excode (struct libmnt_context *cxt,
                        int rc,
                        char *buf,
                        size_t bufsz);

This function analyzes context, [u]mount syscall and external helper status and mntrc and generates unified return code (see MNT_EX_*) as expected from mount(8) or umount(8).

If the external helper (e.g. /sbin/mount.type) has been executed than it returns status from wait() of the helper. It's not libmount fail if helper returns some crazy undocumented codes... See mnt_context_helper_executed() and mnt_context_get_helper_status(). Note that mount(8) and umount(8) utils always return code from helper without extra care about it.

If the argument buf is not NULL then error message is generated (if anything failed).

The mntrc is usually return code from mnt_context_mount(), mnt_context_umount(), or 'mntrc' as returned by mnt_context_next_mount().

Parameters

cxt

context

 

rc

return code of the previous operation

 

buf

buffer to print error message (optional)

 

bufsz

size of the buffer

 

Returns

MNT_EX_* codes.

Since: 2.30


mnt_context_get_fs ()

struct libmnt_fs *
mnt_context_get_fs (struct libmnt_context *cxt);

The FS contains the basic description of mountpoint, fs type and so on. Note that the FS is modified by mnt_context_set_{source,target,options,fstype} functions.

Parameters

cxt

mount context

 

Returns

pointer to FS description or NULL in case of a calloc() error.


mnt_context_get_fstab ()

int
mnt_context_get_fstab (struct libmnt_context *cxt,
                       struct libmnt_table **tb);

See also mnt_table_parse_fstab() for more details about fstab.

Parameters

cxt

mount context

 

tb

returns fstab

 

Returns

0 on success, negative number in case of error.


mnt_context_get_fstab_userdata ()

void *
mnt_context_get_fstab_userdata (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

pointer to userdata or NULL.


mnt_context_get_fstype ()

const char *
mnt_context_get_fstype (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

pointer or NULL in case of error or if not set.


mnt_context_get_fs_userdata ()

void *
mnt_context_get_fs_userdata (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

pointer to userdata or NULL.


mnt_context_get_helper_status ()

int
mnt_context_get_helper_status (struct libmnt_context *cxt);

Return: mount.type helper exit status, result is reliable only if mnt_context_helper_executed() returns 1.

Parameters

cxt

mount context

 

mnt_context_get_lock ()

struct libmnt_lock *
mnt_context_get_lock (struct libmnt_context *cxt);

The libmount applications don't have to care about mtab locking, but with a small exception: the application has to be able to remove the lock file when interrupted by signal or signals have to be ignored when the lock is locked.

The default behavior is to ignore all signals (except SIGALRM and SIGTRAP for mtab update) when the lock is locked. If this behavior is unacceptable, then use:

lc = mnt_context_get_lock(cxt); if (lc) mnt_lock_block_signals(lc, FALSE);

and don't forget to call mnt_unlock_file(lc) before exit.

Parameters

cxt

mount context

 

Returns

pointer to lock struct or NULL.


mnt_context_get_mflags ()

int
mnt_context_get_mflags (struct libmnt_context *cxt,
                        unsigned long *flags);

Converts mount options string to MS_* flags and bitwise-OR the result with the already defined flags (see mnt_context_set_mflags()).

Parameters

cxt

mount context

 

flags

returns MS_* mount flags

 

Returns

0 on success, negative number in case of error.


mnt_context_get_mtab ()

int
mnt_context_get_mtab (struct libmnt_context *cxt,
                      struct libmnt_table **tb);

See also mnt_table_parse_mtab() for more details about mtab/mountinfo. The result will be deallocated by mnt_free_context(cxt ).

Parameters

cxt

mount context

 

tb

returns mtab

 

Returns

0 on success, negative number in case of error.


mnt_context_get_mtab_userdata ()

void *
mnt_context_get_mtab_userdata (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

pointer to userdata or NULL.


mnt_context_get_options ()

const char *
mnt_context_get_options (struct libmnt_context *cxt);

This function returns mount options set by mnt_context_set_options() or mnt_context_append_options().

Note that *after* mnt_context_prepare_mount(), the mount options string may also include options set by mnt_context_set_mflags() or other options generated by this library.

Parameters

cxt

mount context

 

Returns

pointer or NULL


mnt_context_get_optsmode ()

int
mnt_context_get_optsmode (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

MNT_OMODE_* mask or zero.


mnt_context_get_origin_ns ()

struct libmnt_ns *
mnt_context_get_origin_ns (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

pointer to original namespace

Since: 2.33


mnt_context_get_source ()

const char *
mnt_context_get_source (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

returns pointer or NULL in case of error or if not set.


mnt_context_get_status ()

int
mnt_context_get_status (struct libmnt_context *cxt);

Global libmount status.

The real exit code of the mount.type helper has to be tested by mnt_context_get_helper_status(). The mnt_context_get_status() only informs that exec() has been successful.

Parameters

cxt

mount context

 

Returns

1 if mount.type or mount(2) syscall has been successfully called.


mnt_context_get_syscall_errno ()

int
mnt_context_get_syscall_errno (struct libmnt_context *cxt);

The result from this function is reliable only if mnt_context_syscall_called() returns 1.

Parameters

cxt

mount context

 

Returns

mount(2) errno if the syscall failed or 0.


mnt_context_get_table ()

int
mnt_context_get_table (struct libmnt_context *cxt,
                       const char *filename,
                       struct libmnt_table **tb);

This function allocates a new table and parses the file . The parser error callback and cache for tags and paths is set according to the cxt setting. See also mnt_table_parse_file().

It's strongly recommended to use the mnt_context_get_mtab() and mnt_context_get_fstab() functions for mtab and fstab files. This function does not care about LIBMOUNT_* env.variables and does not merge userspace options.

The result will NOT be deallocated by mnt_free_context(cxt ).

Parameters

cxt

mount context

 

filename

e.g. /proc/self/mountinfo

 

tb

returns the table

 

Returns

0 on success, negative number in case of error.


mnt_context_get_target ()

const char *
mnt_context_get_target (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

returns pointer or NULL in case of error or if not set.


mnt_context_get_target_ns ()

struct libmnt_ns *
mnt_context_get_target_ns (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

pointer to target namespace

Since: 2.33


mnt_context_get_target_prefix ()

const char *
mnt_context_get_target_prefix (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

returns pointer or NULL in case of error or if not set.


mnt_context_get_user_mflags ()

int
mnt_context_get_user_mflags (struct libmnt_context *cxt,
                             unsigned long *flags);

Converts mount options string to MNT_MS_* flags and bitwise-OR the result with the already defined flags (see mnt_context_set_user_mflags()).

Parameters

cxt

mount context

 

flags

returns mount flags

 

Returns

0 on success, negative number in case of error.


mnt_context_helper_executed ()

int
mnt_context_helper_executed (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

1 if mount.type helper has been executed, or 0.


mnt_context_helper_setopt ()

int
mnt_context_helper_setopt (struct libmnt_context *cxt,
                           int c,
                           char *arg);

This function applies the [u]mount.type command line option (for example parsed by getopt or getopt_long) to cxt . All unknown options are ignored and then 1 is returned.

Parameters

cxt

context

 

c

getopt() result

 

arg

getopt() optarg

 

Returns

negative number on error, 1 if c is unknown option, 0 on success.


mnt_context_init_helper ()

int
mnt_context_init_helper (struct libmnt_context *cxt,
                         int action,
                         int flags);

This function informs libmount that used from [u]mount.type helper.

The function also calls mnt_context_disable_helpers() to avoid recursive mount.type helpers calling. It you really want to call another mount.type helper from your helper, then you have to explicitly enable this feature by:

mnt_context_disable_helpers(cxt, FALSE);

Parameters

cxt

mount context

 

action

MNT_ACT_{UMOUNT,MOUNT}

 

flags

not used now

 

Returns

0 on success, negative number in case of error.


mnt_context_is_child ()

int
mnt_context_is_child (struct libmnt_context *cxt);

Return: 1 f the current context is child, or 0

Parameters

cxt

mount context

 

mnt_context_is_fake ()

int
mnt_context_is_fake (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

1 if fake flag is enabled or 0


mnt_context_is_force ()

int
mnt_context_is_force (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

1 if force umounting flag is enabled or 0


mnt_context_is_fork ()

int
mnt_context_is_fork (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

1 if fork (mount -F) is enabled or 0


mnt_context_is_fs_mounted ()

int
mnt_context_is_fs_mounted (struct libmnt_context *cxt,
                           struct libmnt_fs *fs,
                           int *mounted);

Please, read the mnt_table_is_fs_mounted() description!

Parameters

cxt

context

 

fs

filesystem

 

mounted

returns 1 for mounted and 0 for non-mounted filesystems

 

Returns

0 on success and negative number in case of error.


mnt_context_is_lazy ()

int
mnt_context_is_lazy (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

1 if lazy umount is enabled or 0


mnt_context_is_loopdel ()

int
mnt_context_is_loopdel (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

1 if loop device should be deleted after umount (umount -d) or 0.


mnt_context_is_nocanonicalize ()

int
mnt_context_is_nocanonicalize (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

1 if no-canonicalize mode is enabled or 0.


mnt_context_is_nohelpers ()

int
mnt_context_is_nohelpers (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

1 if helpers are disabled (mount -i) or 0


mnt_context_is_nomtab ()

int
mnt_context_is_nomtab (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

1 if no-mtab is enabled or 0


mnt_context_is_parent ()

int
mnt_context_is_parent (struct libmnt_context *cxt);

Return: 1 if mount -F enabled and the current context is parent, or 0

Parameters

cxt

mount context

 

mnt_context_is_rdonly_umount ()

int
mnt_context_is_rdonly_umount (struct libmnt_context *cxt);

See also mnt_context_enable_rdonly_umount() and umount(8) man page, option -r.

Parameters

cxt

mount context

 

Returns

1 if read-only remount failed umount(2) is enables or 0


mnt_context_is_restricted ()

int
mnt_context_is_restricted (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

0 for an unrestricted mount (user is root), or 1 for non-root mounts


mnt_context_is_rwonly_mount ()

int
mnt_context_is_rwonly_mount (struct libmnt_context *cxt);

See also mnt_context_enable_rwonly_mount() and mount(8) man page, option -w.

Parameters

cxt

mount context

 

Returns

1 if only read-write mount is allowed.

Since: 2.30


mnt_context_is_sloppy ()

int
mnt_context_is_sloppy (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

1 if sloppy flag is enabled or 0


mnt_context_is_swapmatch ()

int
mnt_context_is_swapmatch (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

1 if swap between source and target is allowed (default is 1) or 0.


mnt_context_is_verbose ()

int
mnt_context_is_verbose (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

1 if verbose flag is enabled or 0


mnt_context_reset_status ()

int
mnt_context_reset_status (struct libmnt_context *cxt);

Resets mount(2) and mount.type statuses, so mnt_context_do_mount() or mnt_context_do_umount() could be again called with the same settings.

BE CAREFUL -- after this soft reset the libmount will NOT parse mount options, evaluate permissions or apply stuff from fstab.

Parameters

cxt

context

 

Returns

0 on success, negative number in case of error.


mnt_context_set_cache ()

int
mnt_context_set_cache (struct libmnt_context *cxt,
                       struct libmnt_cache *cache);

The mount context maintains a private struct libmnt_cache by default. This function can be used to overwrite the private cache with an external instance. This function increments cache reference counter.

If the cache argument is NULL, then the current cache instance is reset. This function apply the cache to fstab and mtab instances (if already exists).

The old cache instance reference counter is de-incremented.

Parameters

cxt

mount context

 

cache

cache instance or NULL

 

Returns

0 on success, negative number in case of error.


mnt_context_set_fs ()

int
mnt_context_set_fs (struct libmnt_context *cxt,
                    struct libmnt_fs *fs);

The mount context uses private fs by default. This function can be used to overwrite the private fs with an external instance. This function increments fs reference counter (and decrement reference counter of the old fs).

The fs will be modified by mnt_context_set_{source,target,options,fstype} functions, If the fs is NULL, then all current FS specific settings (source, target, etc., exclude spec) are reset.

Parameters

cxt

mount context

 

fs

filesystem description

 

Returns

0 on success, negative number in case of error.


mnt_context_set_fstab ()

int
mnt_context_set_fstab (struct libmnt_context *cxt,
                       struct libmnt_table *tb);

The mount context reads /etc/fstab to the private struct libmnt_table by default. This function can be used to overwrite the private fstab with an external instance.

This function modify the tb reference counter. This function does not set the cache for the tb . You have to explicitly call mnt_table_set_cache(tb, mnt_context_get_cache(cxt));

The fstab is used read-only and is not modified, it should be possible to share the fstab between more mount contexts (TODO: test it.)

If the tb argument is NULL, then the current private fstab instance is reset.

Parameters

cxt

mount context

 

tb

fstab

 

Returns

0 on success, negative number in case of error.


mnt_context_set_fstype ()

int
mnt_context_set_fstype (struct libmnt_context *cxt,
                        const char *fstype);

Note that the fstype has to be a FS type. For patterns with comma-separated list of filesystems or for the "nofs" notation, use mnt_context_set_fstype_pattern().

Parameters

cxt

mount context

 

fstype

filesystem type

 

Returns

0 on success, negative number in case of error.


mnt_context_set_fstype_pattern ()

int
mnt_context_set_fstype_pattern (struct libmnt_context *cxt,
                                const char *pattern);

See mount(8), option -t.

Parameters

cxt

mount context

 

pattern

FS name pattern (or NULL to reset the current setting)

 

Returns

0 on success, negative number in case of error.


mnt_context_set_mflags ()

int
mnt_context_set_mflags (struct libmnt_context *cxt,
                        unsigned long  flags);

Sets mount flags (see mount(2) man page).

Note that mount context can be used to define mount options by mount flags. It means you can for example use

mnt_context_set_mflags(cxt, MS_NOEXEC | MS_NOSUID);

rather than

mnt_context_set_options(cxt, "noexec,nosuid");

both of these calls have the same effect.

Be careful if you want to use MS_REC flag -- in this case the bit is applied to all bind/slave/etc. options. If you want to mix more propadation flags and/or bind operations than it's better to specify mount options by strings.

Parameters

cxt

mount context

 

flags

mount(2) flags (MS_* flags)

 

Returns

0 on success, negative number in case of error.


mnt_context_set_mountdata ()

int
mnt_context_set_mountdata (struct libmnt_context *cxt,
                           void *data);

The mount context generates mountdata from mount options by default. This function can be used to overwrite this behavior, and data will be used instead of mount options.

The libmount does not deallocate the data by mnt_free_context(). Note that NULL is also valid mount data.

Parameters

cxt

mount context

 

data

mount(2) data

 

Returns

0 on success, negative number in case of error.


mnt_context_set_options ()

int
mnt_context_set_options (struct libmnt_context *cxt,
                         const char *optstr);

Note that MS_MOVE cannot be specified as "string". It's operation that is no supported in fstab (etc.)

Parameters

cxt

mount context

 

optstr

comma delimited mount options

 

Returns

0 on success, negative number in case of error.


mnt_context_set_options_pattern ()

int
mnt_context_set_options_pattern (struct libmnt_context *cxt,
                                 const char *pattern);

See mount(8), option -O.

Parameters

cxt

mount context

 

pattern

options pattern (or NULL to reset the current setting)

 

Returns

0 on success, negative number in case of error.


mnt_context_set_optsmode ()

int
mnt_context_set_optsmode (struct libmnt_context *cxt,
                          int mode);

Controls how to use mount optionssource and target paths from fstab/mtab.

MNT_OMODE_IGNORE : ignore mtab/fstab options

MNT_OMODE_APPEND : append mtab/fstab options to existing options

MNT_OMODE_PREPEND : prepend mtab/fstab options to existing options

MNT_OMODE_REPLACE : replace existing options with options from mtab/fstab

MNT_OMODE_FORCE : always read mtab/fstab (although source and target are defined)

MNT_OMODE_FSTAB : read from fstab

MNT_OMODE_MTAB : read from mtab if fstab not enabled or failed

MNT_OMODE_NOTAB : do not read fstab/mtab at all

MNT_OMODE_AUTO : default mode (MNT_OMODE_PREPEND | MNT_OMODE_FSTAB | MNT_OMODE_MTAB)

MNT_OMODE_USER : default for non-root users (MNT_OMODE_REPLACE | MNT_OMODE_FORCE | MNT_OMODE_FSTAB)

Notes:

  • MNT_OMODE_USER is always used if mount context is in restricted mode

  • MNT_OMODE_AUTO is used if nothing else is defined

  • the flags are evaluated in this order: MNT_OMODE_NOTAB, MNT_OMODE_FORCE, MNT_OMODE_FSTAB, MNT_OMODE_MTAB and then the mount options from fstab/mtab are set according to MNT_OMODE_{IGNORE,APPEND,PREPEND,REPLACE}

Parameters

cxt

mount context

 

mode

MNT_OMODE_* flags

 

Returns

0 on success, negative number in case of error.


mnt_context_set_passwd_cb ()

int
mnt_context_set_passwd_cb (struct libmnt_context *cxt,
                           char* (*get) (struct libmnt_context *),
                           void (*release) (struct libmnt_context *, char *));

Sets callbacks for encryption password (e.g encrypted loopdev). This function is deprecated (encrypted loops are no longer supported).

Parameters

cxt

mount context

 

get

callback to get password

 

release

callback to release (deallocate) password

 

Returns

0 on success, negative number in case of error.


mnt_context_set_source ()

int
mnt_context_set_source (struct libmnt_context *cxt,
                        const char *source);

Note that libmount does not interpret "nofail" (MNT_MS_NOFAIL) mount option. The real return code is always returned, when the device does not exist then it's usually MNT_ERR_NOSOURCE from libmount or ENOENT, ENOTDIR, ENOTBLK, ENXIO from mount(2).

Parameters

cxt

mount context

 

source

mount source (device, directory, UUID, LABEL, ...)

 

Returns

0 on success, negative number in case of error.


mnt_context_set_syscall_status ()

int
mnt_context_set_syscall_status (struct libmnt_context *cxt,
                                int status);

The status should be 0 on success, or negative number on error (-errno).

This function should only be used if the [u]mount(2) syscall is NOT called by libmount code.

Parameters

cxt

mount context

 

status

mount(2) status

 

Returns

0 or negative number in case of error.


mnt_context_set_tables_errcb ()

int
mnt_context_set_tables_errcb (struct libmnt_context *cxt,
                              int (*cb) (struct libmnt_table *tb, const char *filename, int line));

The error callback is used for all tab files (e.g. mtab, fstab) parsed within the context.

See also mnt_context_get_mtab(), mnt_context_get_fstab(), mnt_table_set_parser_errcb().

Parameters

cxt

mount context

 

cb

pointer to callback function

 

Returns

0 on success, negative number in case of error.


mnt_context_set_target ()

int
mnt_context_set_target (struct libmnt_context *cxt,
                        const char *target);

Parameters

cxt

mount context

 

target

mountpoint

 

Returns

0 on success, negative number in case of error.


mnt_context_set_target_ns ()

int
mnt_context_set_target_ns (struct libmnt_context *cxt,
                           const char *path);

Sets target namespace to namespace represented by path . If path is NULL, target namespace is cleared.

This function sets errno to ENOSYS and returns error if libmount is compiled without namespaces support.

Parameters

cxt

mount context

 

path

path to target namespace or NULL

 

Returns

0 on success, negative number in case of error.

Since: 2.33


mnt_context_set_target_prefix ()

int
mnt_context_set_target_prefix (struct libmnt_context *cxt,
                               const char *path);

Parameters

cxt

mount context

 

path

mountpoint prefix

 

Returns

0 on success, negative number in case of error.


mnt_context_set_user_mflags ()

int
mnt_context_set_user_mflags (struct libmnt_context *cxt,
                             unsigned long  flags);

Sets userspace mount flags.

See also notes for mnt_context_set_mflags().

Parameters

cxt

mount context

 

flags

mount(2) flags (MNT_MS_* flags, e.g. MNT_MS_LOOP)

 

Returns

0 on success, negative number in case of error.


mnt_context_strerror ()

int
mnt_context_strerror (struct libmnt_context *cxt,
                      char *buf,
                      size_t bufsiz);

Not implemented, deprecated in favor or mnt_context_get_excode().

Parameters

cxt

context

 

buf

buffer

 

bufsiz

size of the buffer

 

Returns

0 or negative number in case of error.


mnt_context_switch_ns ()

struct libmnt_ns *
mnt_context_switch_ns (struct libmnt_context *cxt,
                       struct libmnt_ns *ns);

Switch to namespace specified by ns

Typical usage:

1
2
3
4
struct libmnt_ns *ns_old;
ns_old = mnt_context_switch_ns(cxt, mnt_context_get_target_ns(cxt));
... code ...
mnt_context_switch_ns(cxt, ns_old);

Parameters

cxt

mount context

 

ns

namespace to switch to

 

Returns

pointer to previous namespace or NULL on error

Since: 2.33


mnt_context_switch_origin_ns ()

struct libmnt_ns *
mnt_context_switch_origin_ns (struct libmnt_context *cxt);

Switch to original namespace

This is shorthand for

1
mnt_context_switch_ns(cxt, mnt_context_get_origin_ns(cxt));

Parameters

cxt

mount context

 

Returns

pointer to previous namespace or NULL on error

Since: 2.33


mnt_context_switch_target_ns ()

struct libmnt_ns *
mnt_context_switch_target_ns (struct libmnt_context *cxt);

Switch to target namespace

This is shorthand for

1
mnt_context_switch_ns(cxt, mnt_context_get_target_ns(cxt));

Parameters

cxt

mount context

 

Returns

pointer to previous namespace or NULL on error

Since: 2.33


mnt_context_syscall_called ()

int
mnt_context_syscall_called (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

1 if mount(2) syscall has been called, or 0.


mnt_context_tab_applied ()

int
mnt_context_tab_applied (struct libmnt_context *cxt);

Parameters

cxt

mount context

 

Returns

1 if fstab (or mtab) has been applied to the context, or 0.


mnt_context_wait_for_children ()

int
mnt_context_wait_for_children (struct libmnt_context *cxt,
                               int *nchildren,
                               int *nerrs);

Types and Values

struct libmnt_context

struct libmnt_context;

Mount/umount status


struct libmnt_ns

struct libmnt_ns;

Describes mount namespace


MNT_ERR_AMBIFS

#define MNT_ERR_AMBIFS       5006

libblkid detected more filesystems on the device


MNT_ERR_APPLYFLAGS

#define MNT_ERR_APPLYFLAGS   5005

failed to apply MS_PROPAGATION flags


MNT_ERR_LOOPDEV

#define MNT_ERR_LOOPDEV      5003

loopdev setup failed, errno set by libc


MNT_ERR_MOUNTOPT

#define MNT_ERR_MOUNTOPT     5004

failed to parse/use userspace mount options


MNT_ERR_NOFSTAB

#define MNT_ERR_NOFSTAB      5000

not found required entry in fstab


MNT_ERR_NOFSTYPE

#define MNT_ERR_NOFSTYPE     5001

failed to detect filesystem type


MNT_ERR_NOSOURCE

#define MNT_ERR_NOSOURCE     5002

required mount source undefined


MNT_ERR_LOOPOVERLAP

#define MNT_ERR_LOOPOVERLAP 5007

detected overlapping loop device that cannot be re-used


MNT_ERR_LOCK

#define MNT_ERR_LOCK         5008

failed to lock mtab/utab or so.


MNT_ERR_NAMESPACE

#define MNT_ERR_NAMESPACE    5009

failed to switch namespace


MNT_EX_SUCCESS

#define MNT_EX_SUCCESS 0

[u]mount(8) exit code: no errors


MNT_EX_USAGE

#define MNT_EX_USAGE 1

[u]mount(8) exit code: incorrect invocation or permission


MNT_EX_SYSERR

#define MNT_EX_SYSERR 2

[u]mount(8) exit code: out of memory, cannot fork, ...


MNT_EX_SOFTWARE

#define MNT_EX_SOFTWARE 4

[u]mount(8) exit code: internal mount bug or wrong version


MNT_EX_USER

#define MNT_EX_USER 8

[u]mount(8) exit code: user interrupt


MNT_EX_FILEIO

#define MNT_EX_FILEIO 16

[u]mount(8) exit code: problems writing, locking, ... mtab/utab


MNT_EX_FAIL

#define MNT_EX_FAIL 32

[u]mount(8) exit code: mount failure


MNT_EX_SOMEOK

#define MNT_EX_SOMEOK 64

[u]mount(8) exit code: some mount succeeded; usually when executed with --all options. Never returned by libmount.