Technical article 100855

How can I use mount(S) to mount an NFS filesystem?

OpenServer 5.0.0OpenServer 5.0.2

Written · Last revised

Problem

How can I use the mount(S) API to mount an NFS filesystem?

Solution

The following code sample demonstrates how to NFS mount the /tmp filesystem from a machine called "beolain" onto the /mnt mount point on your local machine.

Compile the code using this command:
cc -o nfs_mount nfs_mount.c -lsocket -lrpcsvc
#include <sys/types.h>
#include <sys/errno.h>
#include <tiuser.h>
#include <sys/mount.h>
#include <sys/fs/nfs/types.h>
#include <sys/fs/nfs/time.h>
#include <sys/fs/nfs/nfs.h>
#include <sys/fs/nfs/nfs_clnt.h>
#include <sys/fs/nfs/mount.h>
#include <sys/fstyp.h>
#include <sys/fsid.h>
#include <rpc/rpc.h>
#include <rpcsvc/mount.h>
#include <netdb.h>
extern int errno;
main()
{
    int     fs_type;                    /* Filesystem type handle */
    char    *special = "beolain:/tmp";  /* Name of system and remote fsystem to mount */
    char    *directory = "/mnt";        /* Local mount point for the NFS filesystem */
    char    *path = "/tmp";             /* Name of remote filesystem to mount */
    struct  sockaddr_in sock_in;        /* File server address handle */
    struct  nfs_args margs;             /* NFS Data structure to pass to mount(S) */
    int     sz_mountargs;               /* Size of the NFS data structure */
    int     rflag = MS_DATA;            /* Flag to indicate to mount(S) to use */
    struct  hostent *host;              /* Pointer to return value from gethostbyname(SLIB) */
    struct  timeval timeout;
    int     s;                          /* Socket handle */
    int loopcount = 0;
    enum clnt_stat rpc_stat;
    ushort port;
    CLIENT  *client;                    /* RPC client handle */
    struct  fhstatus fhs;               /* Handle for /tmp on beolain */
/*  Make sure that the NFS filesystem is supported on your Unix system  */

if ( ( fs_type = sysfs ( GETFSIND, NFS ) ) == -1 ) { printf("The NFS filesystem is no supported on the running unix system.\n"); exit(2); }

sz_mountargs = sizeof(margs); sprintf( margs.hostname, "beolain"); margs.flags = NFSMNT_HOSTNAME; margs.trans = 1;

/* Example of adding further flags to the mount(S) system call */

margs.flags |= NFSMNT_RETRANS;
margs.retrans = 5;
/* Get the address of the server */

if ((host = gethostbyname(margs.hostname)) == NULL) { /* Try one more time */ if ((host = gethostbyname(margs.hostname)) == NULL) { printf("Host %s is not in the hosts database\n", special); exit(2); } }

/* We now need to get the file handle of the remote filesystem i.e. /tmp on beolain */

loopcount = 0; do { memset ((char *)&sock_in, '\0', sizeof(sock_in)); memcpy ((char *)&sock_in.sin_addr, host->h_addr, host->h_length); sock_in.sin_family = AF_INET; timeout.tv_usec = 0; timeout.tv_sec = 20; s = RPC_ANYSOCK; if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { printf(" Warning: cannot create socket\n"); exit (2); } if (bindresvport(s, (struct sockaddr_in *)0)) { printf(" Warning: cannot do local bind\n"); close(s); exit(2); } if ((client = clntudp_create(&sock_in, MOUNTPROG, MOUNTVERS, timeout, &s)) == NULL) { close(s); printf(" Server %s is not responding.\n", special); clnt_pcreateerror(""); if (rpc_createerr.cf_stat != RPC_TIMEDOUT && rpc_createerr.cf_stat != RPC_PMAPFAILURE && rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED) { printf(" Giving up on server with rpc_createerr to %s.\n", special); exit(2); } sleep(60); loopcount++; }

} while ((client == NULL) && (loopcount < 5));

if (loopcount >= 5) { printf("Giving up on server %s as loopcount > 5.\n", special); exit(2); }

auth_destroy(client->cl_auth);
client->cl_auth = authunix_create_default();

do { timeout.tv_usec=0; timeout.tv_sec=20; rpc_stat = clnt_call(client, MOUNTPROC_MNT, xdr_path, &path, xdr_fhstatus, &fhs, timeout); if (rpc_stat != RPC_SUCCESS) { printf(" Server %s is not responding.\n", special); clnt_perror(client, ""); if (rpc_stat != RPC_TIMEDOUT && rpc_stat != RPC_PMAPFAILURE && rpc_stat != RPC_PROGNOTREGISTERED) { printf (" Giving up on %s\n", special); auth_destroy(client->cl_auth); clnt_destroy(client); close(s); exit(2); } loopcount++; } } while ((rpc_stat != RPC_SUCCESS) && (loopcount < 5)); if (loopcount >= 5) { printf("Giving up on server %s.\n", special); auth_destroy(client->cl_auth); clnt_destroy(client); close(s); exit(2); }

/* Check to see if the file handle returned by the remote server is valid */

if (errno = fhs.fhs_status) { if (errno == EACCES) { printf(" Access denied to server %s.\n", special); } else { printf(" Physical error trying to access %s.\n", special); } auth_destroy(client->cl_auth); clnt_destroy(client); close(s); exit(2); }

/* Set up the rest of the arguments to pass to the mount(S) call */

port = (ushort) NFS_PORT; sock_in.sin_port = htons(port); sock_in.sin_family = htons (AF_INET); margs.fh = fhs.fhs_fh; margs.raddr.buf = (char *)&sock_in; margs.raddr.len = (unsigned int)sizeof(sock_in); margs.raddr.maxlen = margs.raddr.len;

auth_destroy(client->cl_auth);
clnt_destroy(client);
close(s);
special = "/";

if ( mount ( special, directory, rflag, fs_type, &margs, sz_mountargs) != 0) { printf("Mount failed with the error %d\n", errno); } }

From the Xinuos technical article database, record 100855. Reproduced with permission. The original is at www.sco.com/ta/100855, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.