All files are in filesystems, and before you can access any file, its filesystem must be mounted. Because of Unix's concept of Everything is a file, mounting of filesystems is central to doing almost anything. This section explains how to find out what filesystems are currently mounted and what filesystems are available for mounting, and how to change what is mounted.
The classic filesystem is the contents of a disk drive. The concept is considerably more abstract, though, and lots of things other than disk drives can be mounted.
Some block devices don't correspond to traditional devices like disk drives. For example, a loop device is a block device whose driver uses a regular file in another filesystem as its medium. So if that regular file contains appropriate data for a filesystem, you can by mounting the loop device essentially mount a regular file.
Some filesystems aren't based on a device of any kind. The "proc" filesystem, for example, contains files whose data is made up by the filesystem driver on the fly whenever you ask for it. And when you write to it, the data you write causes changes in the system. No data gets stored.
For some programs it is desirable and necessary to access information about whether a certain filesystem is mounted and, if it is, where, or simply to get lists of all the available filesystems. The GNU libc provides some functions to retrieve this information portably.
Traditionally Unix systems have a file named /etc/fstab which describes all possibly mounted filesystems. The mount program uses this file to mount at startup time of the system all the necessary filesystems. The information about all the filesystems actually mounted is normally kept in a file named /etc/mtab. Both files share the same syntax and it is crucial that this syntax is followed all the time. Therefore it is best to never directly write the files. The functions described in this section can do this and they also provide the functionality to convert the external textual representation to the internal representation.
Note that the fstab and mtab files are maintained on a system by convention. It is possible for the files not to exist or not to be consistent with what is really mounted or available to mount, if the system's administration policy allows it. But programs that mount and unmount filesystems typically maintain and use these files as described herein.
The filenames given above should never be used directly. The portable way to handle these file is to use the macros _PATH_FSTAB, defined in fstab.h and _PATH_MNTTAB, defined in mntent.h, respectively. There are also two alternate macro names FSTAB and _PATH_MOUNTED defined but both names are deprecated and kept only for backward compatibility. The two former names should always be used.
The internal representation for entries of the file is struct fstab, defined in fstab.h.
function>struct fstab/function> This structure is used with the getfsent, getfsspec, and getfsfile functions.
This element describes the device from which the filesystem is mounted. Normally this is the name of a special device, such as a hard disk partition, but it could also be a more or less generic string. For NFS it would be a hostname and directory name combination.
Even though the element is not declared const it shouldn't be modified. The missing const has historic reasons, since this function predates ISO C. The same is true for the other string elements of this structure.
This describes the mount point on the local system. I.e., accessing any file in this filesystem has implicitly or explicitly this string as a prefix.
This is the type of the filesystem. Depending on what the underlying kernel understands it can be any string.
This is a string containing options passed to the kernel with the mount call. Again, this can be almost anything. There can be more than one option, separated from the others by a comma. Each option consists of a name and an optional value part, introduced by an = character.
If the value of this element must be processed it should ideally be done using the getsubopt function; see the section called “Parsing of Suboptions”.
This name is poorly chosen. This element points to a string (possibly in the fs_mntops string) which describes the modes with which the filesystem is mounted. fstab defines five macros to describe the possible values:
The filesystems gets mounted with read and write enabled.
The filesystems gets mounted with read and write enabled. Write access is restricted by quotas.
The filesystem gets mounted read-only.
This is not a real filesystem, it is a swap device.
This entry from the fstab file is totally ignored.
Testing for equality with these value must happen using strcmp since these are all strings. Comparing the pointer will probably always fail.
This element describes the dump frequency in days.
This element describes the pass number on parallel dumps. It is closely related to the dump utility used on Unix systems.
To read the entire content of the of the fstab file the GNU libc contains a set of three functions which are designed in the usual way.
int function>setfsent/function> (void) This function makes sure that the internal read pointer for the fstab file is at the beginning of the file. This is done by either opening the file or resetting the read pointer.
Since the file handle is internal to the libc this function is not thread-safe.
This function returns a non-zero value if the operation was successful and the getfs* functions can be used to read the entries of the file.
void function>endfsent/function> (void) This function makes sure that all resources acquired by a prior call to setfsent (explicitly or implicitly by calling getfsent) are freed.
struct fstab * function>getfsent/function> (void) This function returns the next entry of the fstab file. If this is the first call to any of the functions handling fstab since program start or the last call of endfsent, the file will be opened.
The function returns a pointer to a variable of type struct fstab. This variable is shared by all threads and therefore this function is not thread-safe. If an error occurred getfsent returns a NULL pointer.
struct fstab * function>getfsspec/function> (const char *name) This function returns the next entry of the fstab file which has a string equal to name pointed to by the fs_spec element. Since there is normally exactly one entry for each special device it makes no sense to call this function more than once for the same argument. If this is the first call to any of the functions handling fstab since program start or the last call of endfsent, the file will be opened.
The function returns a pointer to a variable of type struct fstab. This variable is shared by all threads and therefore this function is not thread-safe. If an error occurred getfsent returns a NULL pointer.
struct fstab * function>getfsfile/function> (const char *name) This function returns the next entry of the fstab file which has a string equal to name pointed to by the fs_file element. Since there is normally exactly one entry for each mount point it makes no sense to call this function more than once for the same argument. If this is the first call to any of the functions handling fstab since program start or the last call of endfsent, the file will be opened.
The function returns a pointer to a variable of type struct fstab. This variable is shared by all threads and therefore this function is not thread-safe. If an error occurred getfsent returns a NULL pointer.
The following functions and data structure access the mtab file.
function>struct mntent/function> This structure is used with the getmntent, getmntent_t, addmntent, and hasmntopt functions.
This element contains a pointer to a string describing the name of the special device from which the filesystem is mounted. It corresponds to the fs_spec element in struct fstab.
This element points to a string describing the mount point of the filesystem. It corresponds to the fs_file element in struct fstab.
mnt_type describes the filesystem type and is therefore equivalent to fs_vfstype in struct fstab. mntent.h defines a few symbolic names for some of the values this string can have. But since the kernel can support arbitrary filesystems it does not make much sense to give them symbolic names. If one knows the symbol name one also knows the filesystem name. Nevertheless here follows the list of the symbols provided in mntent.h.
This symbol expands to "ignore". The value is sometime used in fstab files to make sure entries are not used without removing them.
Expands to "nfs". Using this macro sometimes could make sense since it names the default NFS implementation, in case both version 2 and 3 are supported.
This symbol expands to "swap". It names the special fstab entry which names one of the possibly multiple swap partitions.
The element contains a string describing the options used while mounting the filesystem. As for the equivalent element fs_mntops of struct fstab it is best to use the function getsubopt (the section called “Parsing of Suboptions”) to access the parts of this string.
The mntent.h file defines a number of macros with string values which correspond to some of the options understood by the kernel. There might be many more options which are possible so it doesn't make much sense to rely on these macros but to be consistent here is the list:
Expands to "defaults". This option should be used alone since it indicates all values for the customizable values are chosen to be the default.
Expands to "ro". See the FSTAB_RO value, it means the filesystem is mounted read-only.
Expand to "rw". See the FSTAB_RW value, it means the filesystem is mounted with read and write permissions.
Expands to "suid". This means that the SUID bit (the section called “How an Application Can Change Persona”) is respected when a program from the filesystem is started.
Expands to "nosuid". This is the opposite of MNTOPT_SUID, the SUID bit for all files from the filesystem is ignored.
Expands to "noauto". At startup time the mount program will ignore this entry if it is started with the -a option to mount all filesystems mentioned in the fstab file.
As for the FSTAB_* entries introduced above it is important to use strcmp to check for equality.
This elements corresponds to fs_freq and also specifies the frequency in days in which dumps are made.
This element is equivalent to fs_passno with the same meaning which is uninteresting for all programs beside dump.
For accessing the mtab file there is again a set of three functions to access all entries in a row. Unlike the functions to handle fstab these functions do not access a fixed file and there is even a thread safe variant of the get function. Beside this the GNU libc contains functions to alter the file and test for specific options.
FILE * function>setmntent/function> (const char *file, const char *mode) The setmntent function prepares the file named FILE which must be in the format of a fstab and mtab file for the upcoming processing through the other functions of the family. The mode parameter can be chosen in the way the opentype parameter for fopen (the section called “Opening Streams”) can be chosen. If the file is opened for writing the file is also allowed to be empty.
If the file was successfully opened setmntent returns a file descriptor for future use. Otherwise the return value is NULL and errno is set accordingly.
int function>endmntent/function> (FILE *stream) This function takes for the stream parameter a file handle which previously was returned from the setmntent call. endmntent closes the stream and frees all resources.
The return value is 1 unless an error occurred in which case it is 0.
struct mntent * function>getmntent/function> (FILE *stream) The getmntent function takes as the parameter a file handle previously returned by successful call to setmntent. It returns a pointer to a static variable of type struct mntent which is filled with the information from the next entry from the file currently read.
The file format used prescribes the use of spaces or tab characters to separate the fields. This makes it harder to use name containing one of these characters (e.g., mount points using spaces). Therefore these characters are encoded in the files and the getmntent function takes care of the decoding while reading the entries back in. '\040' is used to encode a space character, '\012' to encode a tab character and '\\' to encode a backslash.
If there was an error or the end of the file is reached the return value is NULL.
This function is not thread-safe since all calls to this function return a pointer to the same static variable. getmntent_r should be used in situations where multiple threads access the file.
struct mntent * function>getmntent_r/function> (FILE *stream, struct mentent *result, char *buffer, int bufsize) The getmntent_r function is the reentrant variant of getmntent. It also returns the next entry from the file and returns a pointer. The actual variable the values are stored in is not static, though. Instead the function stores the values in the variable pointed to by the result parameter. Additional information (e.g., the strings pointed to by the elements of the result) are kept in the buffer of size bufsize pointed to by buffer.
Escaped characters (space, tab, backslash) are converted back in the same way as it happens for getmentent.
The function returns a NULL pointer in error cases. Errors could be:
error while reading the file,
end of file reached,
bufsize is too small for reading a complete new entry.
int function>addmntent/function> (FILE *stream, const struct mntent *mnt) The addmntent function allows adding a new entry to the file previously opened with setmntent. The new entries are always appended. I.e., even if the position of the file descriptor is not at the end of the file this function does not overwrite an existing entry following the current position.
The implication of this is that to remove an entry from a file one has to create a new file while leaving out the entry to be removed and after closing the file remove the old one and rename the new file to the chosen name.
This function takes care of spaces and tab characters in the names to be written to the file. It converts them and the backslash character into the format describe in the getmntent description above.
This function returns 0 in case the operation was successful. Otherwise the return value is 1 and errno is set appropriately.
char * function>hasmntopt/function> (const struct mntent *mnt, const char *opt) This function can be used to check whether the string pointed to by the mnt_opts element of the variable pointed to by mnt contains the option opt. If this is true a pointer to the beginning of the option in the mnt_opts element is returned. If no such option exists the function returns NULL.
This function is useful to test whether a specific option is present but when all options have to be processed one is better off with using the getsubopt function to iterate over all options in the string.
On a system with a Linux kernel and the proc filesystem, you can get information on currently mounted filesystems from the file mounts in the proc filesystem. Its format is similar to that of the mtab file, but represents what is truly mounted without relying on facilities outside the kernel to keep mtab up to date.
This section describes the functions for mounting, unmounting, and remounting filesystems.
Only the superuser can mount, unmount, or remount a filesystem.
These functions do not access the fstab and mtab files. You should maintain and use these separately. the section called “Mount Information”.
The symbols in this section are declared in sys/mount.h.
int function>mount/function> (const char *special_file, const char *dir, const char *fstype, unsigned long int options, const void *data) mount mounts or remounts a filesystem. The two operations are quite different and are merged rather unnaturally into this one function. The MS_REMOUNT option, explained below, determines whether mount mounts or remounts.
For a mount, the filesystem on the block device represented by the device special file named special_file gets mounted over the mount point dir. This means that the directory dir (along with any files in it) is no longer visible; in its place (and still with the name dir) is the root directory of the filesystem on the device.
As an exception, if the filesystem type (see below) is one which is not based on a device (e.g. "proc"), mount instantiates a filesystem and mounts it over dir and ignores special_file.
For a remount, dir specifies the mount point where the filesystem to be remounted is (and remains) mounted and special_file is ignored. Remounting a filesystem means changing the options that control operations on the filesystem while it is mounted. It does not mean unmounting and mounting again.
For a mount, you must identify the type of the filesystem as fstype. This type tells the kernel how to access the filesystem and can be thought of as the name of a filesystem driver. The acceptable values are system dependent. On a system with a Linux kernel and the proc filesystem, the list of possible values is in the file filesystems in the proc filesystem (e.g. type cat /proc/filesystems to see the list). With a Linux kernel, the types of filesystems that mount can mount, and their type names, depends on what filesystem drivers are configured into the kernel or loaded as loadable kernel modules. An example of a common value for fstype is ext2.
For a remount, mount ignores fstype.
options specifies a variety of options that apply until the filesystem is unmounted or remounted. The precise meaning of an option depends on the filesystem and with some filesystems, an option may have no effect at all. Furthermore, for some filesystems, some of these options (but never MS_RDONLY) can be overridden for individual file accesses via ioctl.
options is a bit string with bit fields defined using the following mask and masked value macros:
This multibit field contains a magic number. If it does not have the value MS_MGC_VAL, mount assumes all the following bits are zero and the data argument is a null string, regardless of their actual values.
This bit on means to remount the filesystem. Off means to mount it.
This bit on specifies that no writing to the filesystem shall be allowed while it is mounted. This cannot be overridden by ioctl. This option is available on nearly all filesystems.
This bit on specifies that no writing to the files in the filesystem shall be allowed while it is mounted. This can be overridden for a particular file access by a properly privileged call to ioctl. This option is a relatively new invention and is not available on many filesystems.
This bit on specifies that the only file writing that shall be allowed while the filesystem is mounted is appending. Some filesystems allow this to be overridden for a particular process by a properly privileged call to ioctl. This is a relatively new invention and is not available on many filesystems.
This bit on specifies that Setuid and Setgid permissions on files in the filesystem shall be ignored while it is mounted.
This bit on specifies that no files in the filesystem shall be executed while the filesystem is mounted.
This bit on specifies that no device special files in the filesystem shall be accessible while the filesystem is mounted.
This bit on specifies that all writes to the filesystem while it is mounted shall be synchronous; i.e. data shall be synced before each write completes rather than held in the buffer cache.
This bit on specifies that mandatory locks on files shall be permitted while the filesystem is mounted.
This bit on specifies that access times of files shall not be updated when the files are accessed while the filesystem is mounted.
This bit on specifies that access times of directories shall not be updated when the directories are accessed while the filesystem in mounted.
Any bits not covered by the above masks should be set off; otherwise, results are undefined.
The meaning of data depends on the filesystem type and is controlled entirely by the filesystem driver in the kernel.
Example:
#include sys/mount.h mount("/dev/hdb", "/cdrom", MS_MGC_VAL | MS_RDONLY | MS_NOSUID, ""); mount("/dev/hda2", "/mnt", MS_MGC_VAL | MS_REMOUNT, "");
Appropriate arguments for mount are conventionally recorded in the fstab table. the section called “Mount Information”.
The return value is zero if the mount or remount is successful. Otherwise, it is -1 and errno is set appropriately. The values of errno are filesystem dependent, but here is a general list:
The process is not superuser.
The file system type fstype is not known to the kernel.
The file dev is not a block device special file.
The device is already mounted.
The mount point is busy. (E.g. it is some process' working directory or has a filesystem mounted on it already).
The request is to remount read-only, but there are files open for write.
A remount was attempted, but there is no filesystem mounted over the specified mount point.
The supposed filesystem has an invalid superblock.
The filesystem is inherently read-only (possibly due to a switch on the device) and the process attempted to mount it read/write (by setting the MS_RDONLY bit off).
special_file or dir is not accessible due to file permissions.
special_file is not accessible because it is in a filesystem that is mounted with the MS_NODEV option.
The table of dummy devices is full. mount needs to create a dummy device (aka "unnamed" device) if the filesystem being mounted is not one that uses a device.
int function>umount2/function> (const char *file, int flags) umount2 unmounts a filesystem.
You can identify the filesystem to unmount either by the device special file that contains the filesystem or by the mount point. The effect is the same. Specify either as the string file.
flags contains the one-bit field identified by the following mask macro:
This bit on means to force the unmounting even if the filesystem is busy, by making it unbusy first. If the bit is off and the filesystem is busy, umount2 fails with errno = EBUSY. Depending on the filesystem, this may override all, some, or no busy conditions.
All other bits in flags should be set to zero; otherwise, the result is undefined.
Example:
#include sys/mount.h umount2("/mnt", MNT_FORCE); umount2("/dev/hdd1", 0);
After the filesystem is unmounted, the directory that was the mount point is visible, as are any files in it.
As part of unmounting, umount2 syncs the filesystem.
If the unmounting is successful, the return value is zero. Otherwise, it is -1 and errno is set accordingly:
The process is not superuser.
The filesystem cannot be unmounted because it is busy. E.g. it contains a directory that is some process's working directory or a file that some process has open. With some filesystems in some cases, you can avoid this failure with the MNT_FORCE option.
file validly refers to a file, but that file is neither a mount point nor a device special file of a currently mounted filesystem.
This function is not available on all systems.
int function>umount/function> (const char *file) umount does the same thing as umount2 with flags set to zeroes. It is more widely available than umount2 but since it lacks the possibility to forcefully unmount a filesystem is deprecated when umount2 is also available.