Top |
struct | libmnt_context |
struct | libmnt_ns |
#define | MNT_ERR_AMBIFS |
#define | MNT_ERR_APPLYFLAGS |
#define | MNT_ERR_LOOPDEV |
#define | MNT_ERR_MOUNTOPT |
#define | MNT_ERR_NOFSTAB |
#define | MNT_ERR_NOFSTYPE |
#define | MNT_ERR_NOSOURCE |
#define | MNT_ERR_LOOPOVERLAP |
#define | MNT_ERR_LOCK |
#define | MNT_ERR_NAMESPACE |
#define | MNT_EX_SUCCESS |
#define | MNT_EX_USAGE |
#define | MNT_EX_SYSERR |
#define | MNT_EX_SOFTWARE |
#define | MNT_EX_USER |
#define | MNT_EX_FILEIO |
#define | MNT_EX_FAIL |
#define | MNT_EX_SOMEOK |
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
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.
int mnt_context_append_options (struct libmnt_context *cxt
,const char *optstr
);
int
mnt_context_apply_fstab (struct libmnt_context *cxt
);
This function is optional.
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).
int mnt_context_disable_helpers (struct libmnt_context *cxt
,int disable
);
Enable/disable /sbin/[u]mount.* helpers (see mount(8) man page, option -i).
int mnt_context_disable_mtab (struct libmnt_context *cxt
,int disable
);
Disable/enable userspace mount table update (see mount(8) man page, option -n). Originally /etc/mtab, now /run/mount/utab.
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.
int mnt_context_enable_fake (struct libmnt_context *cxt
,int enable
);
Enable/disable fake mounting (see mount(8) man page, option -f).
int mnt_context_enable_force (struct libmnt_context *cxt
,int enable
);
Enable/disable force umounting (see umount(8) man page, option -f).
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).
int mnt_context_enable_lazy (struct libmnt_context *cxt
,int enable
);
Enable/disable lazy umount (see umount(8) man page, option -l).
int mnt_context_enable_loopdel (struct libmnt_context *cxt
,int enable
);
Enable/disable the loop delete (destroy) after umount (see umount(8), option -d)
int mnt_context_enable_noautofs (struct libmnt_context *cxt
,int ignore
);
Enable/disable ignore autofs mount table entries on reading the mount table. Only effective if the "ignore" mount option is being used for autofs mounts (such as if automount(8) has been configured to do so).
int mnt_context_enable_onlyonce (struct libmnt_context *cxt
,int enable
);
Enable/disable only-once mount (check if FS is not already mounted).
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).
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).
Since: 2.30
int mnt_context_enable_sloppy (struct libmnt_context *cxt
,int enable
);
Set/unset sloppy mounting (see mount(8) man page, option -s).
int mnt_context_enable_verbose (struct libmnt_context *cxt
,int enable
);
Enable/disable verbose output (TODO: not implemented yet)
int
mnt_context_forced_rdonly (struct libmnt_context *cxt
);
See also mnt_context_enable_rwonly_mount()
.
Since: 2.30
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.
Since: 2.35
struct libmnt_cache *
mnt_context_get_cache (struct libmnt_context *cxt
);
See also mnt_context_set_cache()
.
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.
The current implementation does not read error message from external
helper into buf
.
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()
.
cxt |
context |
|
rc |
return code of the previous operation |
|
buf |
buffer to print error message (optional) |
|
bufsz |
size of the buffer |
Since: 2.30
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.
int mnt_context_get_fstab (struct libmnt_context *cxt
,struct libmnt_table **tb
);
See also mnt_table_parse_fstab()
for more details about fstab.
void *
mnt_context_get_fstab_userdata (struct libmnt_context *cxt
);
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.
struct libmnt_lock *
mnt_context_get_lock (struct libmnt_context *cxt
);
The libmount applications don't have to care about utab 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 utab 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.
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()
).
int mnt_context_get_mtab (struct libmnt_context *cxt
,struct libmnt_table **tb
);
Parse /proc/self/mountinfo mount table.
The file /etc/mtab is no more used, context
points always to mountinfo
(/proc/self/mountinfo). The function uses "mtab" in the name for backward
compatibility only.
See also mnt_table_parse_mtab()
for more details about mountinfo. The
result will be deallocated by mnt_free_context(cxt
).
void *
mnt_context_get_mtab_userdata (struct libmnt_context *cxt
);
The file /etc/mtab is no more used, context
points always to mountinfo
(/proc/self/mountinfo). The function uses "mtab" in the name for backward
compatibility only.
const char *
mnt_context_get_options (struct libmnt_context *cxt
);
This function returns mount options set by mnt_context_set_options()
,
mnt_context_append_options()
or mnt_context_set_mflags()
;
Before v2.39 this function ignored options specified by flags (see
mnt_context_set_mflags()
) before mnt_context_prepare_mount()
call. Now this
function always returns all mount options.
struct libmnt_ns *
mnt_context_get_origin_ns (struct libmnt_context *cxt
);
Since: 2.33
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.
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.
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 mountinfo 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
).
struct libmnt_ns *
mnt_context_get_target_ns (struct libmnt_context *cxt
);
Since: 2.33
const char *
mnt_context_get_target_prefix (struct libmnt_context *cxt
);
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()
).
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.
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);
int
mnt_context_is_child (struct libmnt_context *cxt
);
Return: 1 f the current context is child, or 0
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!
int
mnt_context_is_parent (struct libmnt_context *cxt
);
Return: 1 if mount -F enabled and the current context is parent, or 0
int
mnt_context_is_rdonly_umount (struct libmnt_context *cxt
);
See also mnt_context_enable_rdonly_umount()
and umount(8) man page,
option -r.
int
mnt_context_is_rwonly_mount (struct libmnt_context *cxt
);
See also mnt_context_enable_rwonly_mount()
and mount(8) man page,
option -w.
Since: 2.30
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.
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 mountinfo instances (if already
exists).
The old cache instance reference counter is de-incremented.
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.
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.
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()
.
int mnt_context_set_fstype_pattern (struct libmnt_context *cxt
,const char *pattern
);
See mount(8), option -t.
int mnt_context_set_mflags (struct libmnt_context *cxt
,unsigned long flags
);
Sets mount flags (see mount(2) man page).
Note that order of mount options (strings) and flags matter if you mix
mnt_context_append_options()
and mnt_context_set_mflags()
.
Be careful if use MS_REC flag -- this is flags is generic for all mask. In this case is better to use options string where mount options are independent and nothign is applied to all options.
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.
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.)
int mnt_context_set_options_pattern (struct libmnt_context *cxt
,const char *pattern
);
See mount(8), option -O.
int mnt_context_set_optsmode (struct libmnt_context *cxt
,int mode
);
Controls how to use mount optionssource and target paths from fstab/mountinfo.
MNT_OMODE_IGNORE
: ignore fstab options
MNT_OMODE_APPEND
: append fstab options to existing options
MNT_OMODE_PREPEND
: prepend fstab options to existing options
MNT_OMODE_REPLACE
: replace existing options with options from fstab
MNT_OMODE_FORCE
: always read fstab (although source and target are defined)
MNT_OMODE_FSTAB
: read from fstab
MNT_OMODE_MTAB
: read from mountinfo if fstab not enabled or failed
MNT_OMODE_NOTAB
: do not read fstab/mountinfoat 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/mountinfo are set according to MNT_OMODE_{IGNORE,APPEND,PREPEND,REPLACE}
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).
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).
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.
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. mountinfo, fstab) parsed within the context.
See also mnt_context_get_mtab()
,
mnt_context_get_fstab()
,
mnt_table_set_parser_errcb()
.
int mnt_context_set_target (struct libmnt_context *cxt
,const char *target
);
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.
Since: 2.33
int mnt_context_set_target_prefix (struct libmnt_context *cxt
,const char *path
);
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()
.
int mnt_context_strerror (struct libmnt_context *cxt
,char *buf
,size_t bufsiz
);
Not implemented, deprecated in favor or mnt_context_get_excode()
.
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); |
Since: 2.33
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)); |
Since: 2.33
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)); |
Since: 2.33
#define MNT_ERR_APPLYFLAGS 5005
failed to apply MS_PROPAGATION flags, and MOUNT_ATTR_* attributes for mount_setattr(2)
#define MNT_ERR_LOOPOVERLAP 5007
detected overlapping loop device that cannot be re-used
#define MNT_EX_SOFTWARE 4
[u]mount(8) exit code: internal mount bug or wrong version