0

When I try to mount directory, 'test_mount' through command line, the operation succeeds:

 mount -t nfs4 remote_server_ip:/ local_dir

but am unable to mount the same directory programmatically:

int ret_val = mount("remote_server_ip:/", "local_dir", "nfs4", MS_SYNCHRONOUS, "");
if (ret_val < 0) {
    perror("Mount failed");
    return 1;
}

This C function fails with Mount failed: Invalid argument. How can I programmatically mount the target directory? I'm running the executable with super user permissions.

Platform:

 Linux ip-10-1-19-46 3.10.42-52.145.amzn1.x86_64 #1 SMP Tue Jun 10 23:46:43 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
user1071840
  • 3,522
  • 9
  • 48
  • 74
  • 2
    the solution is in this duplicate: http://stackoverflow.com/questions/28350912/nfs-mount-system-call-in-linux –  Oct 27 '16 at 13:05

2 Answers2

2

It should be:

int ret_val = mount("remote_server_ip:/", "local_dir", "nfs4", MS_SYNCHRONOUS, NULL);

Instead of:

int ret_val = mount("remote_server_ip:/", "local_dir", "nfs4", MS_SYNCHRONOUS, "");

NULL and "" is different. "" is just means empty string. An empty string is not NULL.

Also, check for a valid filesystemtype argument value in /proc/filesystems

1

If you run strace on that mount command (with no options), you will see that it runs:

mount("remote_server_ip:/", "local_dir", "nfs4", MS_MGC_VAL, NULL)

So the fifth argument of mount should be NULL if you do not want to pass any options.