I have the below simple code, but it fails with invalid argument. I am running this program as root. And the mount point does exist. In ":/vol/share", IP is IP address of NFS server and it is reachable.
package main
import (
"fmt"
"syscall"
)
func nfsMount() {
// err := syscall.Mount("<IP>:/vol/share", "/mnt/export1", "nfs", 0x0, "")
err := err := syscall.Mount("IP:/vol/share", "/mnt/export1", "nfs", 0, "")
if err != nil {
fmt.Printf("error : %v\n", err.Error())
}
}
func main() {
nfsMount()
}
root@~#
root@~# nfsmount
error : invalid argument
root@~#
I have tried the similar program in C.
#include <stdio.h>
#include <sys/mount.h>
#include <errno.h>
void
nfsmount(void)
{
const char *src = "IP:/vol/share";
const char *target = "/mnt/export1";
const char *fsType = "nfs";
unsigned long mountFlags = 0;
const void *data = NULL;
int rc;
rc = mount(src, target, fsType, mountFlags, "");
if (rc != 0) {
printf("mount failed, rc = %d, errno=%d\n", rc, errno);
}
}
int
main(void)
{
nfsmount();
}
root@~# cc nfsmount.c -o nfsmountc
root@~#./nfsmountc
mount failed, rc = -1, errno=22
root@~#
It seems to me, this error is some thing to do with data
(const void *data)`, the last arg to mount.
When I do the mount through shell, it works.
root@~# mount -t nfs IP:/vol/share /mnt/export1
root@~# echo $?
0
root@~# cat /proc/mounts | grep export1
IP:/vol/share /mnt/export1 nfs rw,relatime,vers=3,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=IP,mountvers=3,mountport=4046,mountproto=udp,local_lock=none,addr=172.22.14.107 0 0
root@~#
After some further read, NFS mount System Call in linux helped me. The fix seems:
err := syscall.Mount(":/vol/share", "/mnt/export1", "nfs", 0, "nolock,addr=NFSServer>")