问题
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