mount system call in linux cannot display the mountpoint of file system by df command

谁说我不能喝 提交于 2019-12-13 14:17:41

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/32814782/mount-system-call-in-linux-cannot-display-the-mountpoint-of-file-system-by-df-co

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!