Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux function to get mount points

Tags:

c

linux

mount

libc

Is there a function (or interface; ioctl, netlink etc) in the standard Linux libs that will return the current mounts directly from the kernel without parsing /proc? straceing the mount command, it looks like it parses files in /proc

like image 345
tMC Avatar asked Feb 14 '12 16:02

tMC


People also ask

How do I find mount points in Linux?

See mount points using the mount command: If followed by the -l flag, it will also show the mount point name; the output is similar to the mount command without flags. As you can see, in the last two lines, there is a mounted pen drive containing a Kali Linux distribution.

How do I get mount point?

To display only the mount point where the filesystem with label "/boot" or “/” is mounted, use the following command. # findmnt -n --raw --evaluate --output=target LABEL=/boot OR # findmnt -n --raw --evaluate --output=target LABEL=/

What is mount point command in Linux?

mount command is used to mount the filesystem found on a device to big tree structure(Linux filesystem) rooted at '/'. Conversely, another command umount can be used to detach these devices from the Tree. Syntax: mount -t type device dir.


1 Answers

Please see the clarification at the bottom of the answer for the reasoning being used in this answer.

Is there any reason that you would not use the getmntent libc library call? I do realize that it's not the same as an 'all in one' system call, but it should allow you to get the relevant information.

#include <stdio.h>
#include <stdlib.h>
#include <mntent.h>

int main(void)
{
  struct mntent *ent;
  FILE *aFile;

  aFile = setmntent("/proc/mounts", "r");
  if (aFile == NULL) {
    perror("setmntent");
    exit(1);
  }
  while (NULL != (ent = getmntent(aFile))) {
    printf("%s %s\n", ent->mnt_fsname, ent->mnt_dir);
  }
  endmntent(aFile);
}

Clarification

Considering that the OP clarified about trying to do this without having /proc mounted, I'm going to clarify:

There is no facility outside of /proc for getting the fully qualified list of mounted file systems from the linux kernel. There is no system call, there is no ioctl. The /proc interface is the agreed upon interface.

With that said, if you don't have /proc mounted, you will have to parse the /etc/mtab file - pass in /etc/mtab instead of /proc/mounts to the initial setmntent call.

It is an agreed upon protocol that the mount and unmount commands will maintain a list of currently mounted filesystems in the file /etc/mtab. This is detailed in almost all linux/unix/bsd manual pages for these commands. So if you don't have /proc you can sort of rely on the contents of this file. It's not guaranteed to be a source of truth, but conventions are conventions for these things.

So, if you don't have /proc, you would use /etc/mtab in the getmntent libc library call below to get the list of file systems; otherwise you could use one of /proc/mounts or /proc/self/mountinfo (which is recommended nowadays over /proc/mounts).

like image 187
Petesh Avatar answered Oct 28 '22 17:10

Petesh