3

I am trying to use mount() instead of mount command in my program, I use the following mount() successfully, the result returned success instead of Invalid Argument.

int rc = mount("172.16.74.20:/data/redun/snmp","/mnt/data/redun/snmp",
                    "nfs",0,"soft,timeo=2,addr=172.16.74.20");

if (rc != 0)
 {
     printf("mount failed, errCode=%d, reason=%s\n",errno, strerror(errno));
 }

But when I use df -h to check the mountpoint, there are nothing displayed. I found the related device was not mounted yet. What happened? Is it really mounted successfully? How can I display the mounted device by df command in Linux?

gavv
  • 4,649
  • 1
  • 23
  • 40
Leo Xu
  • 101
  • 6
  • Doesn't `df` read `/etc/mtab` which is updated by `mount` command, but not `mount()` syscall? Check you `/proc/mounts`. – gavv Sep 28 '15 at 16:32
  • Yes, I found the mountpoint info under /proc/mounts as well. but there is nothing displayed after running 'df -h' – Leo Xu Sep 30 '15 at 02:21
  • So the problem is about `/etc/mtab`, not about mount namespaces. I've posted an answer with more details. – gavv Sep 30 '15 at 12:40

1 Answers1

3

The problem is that mount() syscall, unlike mount command, doesn't update /etc/mtab file, while df command parses /etc/mtab to list mount points.

However, uptodate list of mount points is always available in /proc/mounts file. Unlike /etc/mtab, /proc/mounts is not a regular file, but instead a virtual file provided by kernel.


On some distributions, /etc/mtab is a symlink to /proc/mounts. If it's not, and you want df to work, you can do the following:

cat /proc/mounts > /etc/mtab

after every mount() or umount() call.

You can also make /etc/mtab a symlink to /proc/mounts or better /proc/self/mounts, but do it on your own risk (maybe some applications depend on it, but maybe it's just a bug in your distro).


See also this question.

Community
  • 1
  • 1
gavv
  • 4,649
  • 1
  • 23
  • 40