Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hi, I want to use the mount command from a c program running in a linux machine with 2.6.18 kernel. I want to use it to mount a file system ...
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    5

    using mount command within a c program

    Hi,

    I want to use the mount command from a c program running in a linux machine with 2.6.18 kernel. I want to use it to mount a file system via nfs.

    I have tried the umount2 command and it works but the mount command throws an error : Invalid argument.

    Sure i'm doing something wrong but i need help because i can't find any example of a c-programm using this function with nfs.

    mount( "server's ip address:/nameofdirectory", "/mnt", "nfs", 0, "");

    and /mnt is a directory existing in my local file system.

    Any advice??

    Thanks in advance

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Try info/man system, this should allow you to execute the shell commands "mount/umount" from your program...Gerard4143
    Make mine Arch Linux

  3. #3
    Just Joined!
    Join Date
    Mar 2009
    Posts
    2
    Try popen() call to execute the mount command.

  4. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Exclamation

    Hi diegos,
    Is nfs demaon running in your machine ?
    If so,first try to mount using mount command.

    If above works fine,try posting out of
    strace your_executable.
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  5. #5
    Just Joined!
    Join Date
    Mar 2009
    Posts
    5

    more details about the program

    Hi Lakshmipathi,

    Yes, nfs daemon is running and if I run

    mount -t nfs 172.20.41.173:/server /mnt

    it works without problem and i can see the remote directory.

    The result of strace ./pruebamount is:

    execve("./pruebamount", ["./pruebamount"], [/* 17 vars */]) = 0
    uname({sys="Linux", node="srvInstPreprod", ...}) = 0
    brk(0) = 0x804a000
    access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
    mmap2(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f18000
    access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
    open("/etc/ld.so.cache", O_RDONLY) = 3
    fstat64(3, {st_mode=S_IFREG|0644, st_size=21956, ...}) = 0
    mmap2(NULL, 21956, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7f12000
    close(3) = 0
    access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
    open("/lib/tls/libc.so.6", O_RDONLY) = 3
    read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\2 40O\1"..., 512) = 512
    fstat64(3, {st_mode=S_IFREG|0644, st_size=1245488, ...}) = 0
    mmap2(NULL, 1251484, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7de0000
    mmap2(0xb7f08000, 28672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x12 = 0xb7f08000
    mmap2(0xb7f0f000, 10396, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb7f0f000
    close(3) = 0
    mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7ddf000
    mprotect(0xb7f08000, 20480, PROT_READ) = 0
    set_thread_area({entry_number:-1 -> 6, base_addr:0xb7ddf8e0, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0
    munmap(0xb7f12000, 21956) = 0
    mount("172.20.41.173:/server", "/mnt", "nfs", 0, "") = -1 EINVAL (Invalid argument)
    fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
    mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f17000
    write(1, "mensaje_error 22 Invalid argumen"..., 34mensaje_error 22 Invalid argument
    ) = 34
    munmap(0xb7f17000, 4096) = 0
    exit_group(34) = ?
    Process 14103 detached
    I would prefer to use this function instead of the popen or system call because i think (i'm not sure if it's true) that i could be able to have a better control of the execution.

    Thanks in advance for your help and attention.

  6. #6
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Exclamation

    Try to fill the last argument as rw or ro and check again.

    mount( "server's ip address:/nameofdirectory", "/mnt", "nfs", 0, "rw");
    EINVAL denotes invalid parameter is passed.So it can be fixed.
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  7. #7
    Just Joined!
    Join Date
    Mar 2009
    Posts
    5

    Changing the options

    I've tried changing the last argument to "rw", "ro" but it remains with the same error.

    it could be the syntax of ip address:/nameofdirectory that it's wrong??

    I can't find any example of a c program making a call to mount() as i'm trying to do.

    Thanks for your replies

  8. #8
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Post

    one suggestion ,from another forum:

    mount(need to be mounted,"point mount path,flag,"MNT_RDONLY");

    code mount in c language - Page 2 - High Level Programming - The UNIX and Linux Forums


    and this another - Seems like you need to fill struct and pass it

    Re: [gentoo-user] mount nfs thru mount system call

    I'm not sure which is correct-just try them-Though i used mount() sys.call but never tried it with NFS.So curious about
    the solution.

    Let me know the results
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  9. #9
    Just Joined!
    Join Date
    Mar 2009
    Posts
    5

    still trying

    i'm still with more or less the same error.

    Now, following my code is:

    #include <stdio.h>
    #include <errno.h>
    #include <sys/socket.h>
    #include <linux/mount.h>
    #include <linux/nfs.h>
    #include <linux/nfs_mount.h>
    #include <sys/param.h>

    main (int argc, char *argv[]) {


    /*struct nfs_mount_data data;*/
    struct nfs2_fh estruc1;
    struct sockaddr_in estruc2;
    struct nfs3_fh estruc3;
    struct nfs_mount_data data={4,
    1,
    estruc1,
    1,
    4096,
    4096,
    1000,
    3,
    1,
    1,
    1,
    1,
    estruc2,
    "172.20.41.173",
    17,
    1,
    estruc3,
    4,
    ""};


    mount("172.20.41.173:/server", "/mnt", "nfs", 0 , data);
    /*
    mount("172.20.41.173:/server", "/mnt", "nfs", 0 , "rsize=4096,wsize=4096");
    */
    printf("mensaje_error %d %s\n",errno, strerror(errno));
    }
    But the strace ....

    execve("./kk", ["./kk"], [/* 17 vars */]) = 0
    uname({sys="Linux", node="srvInstPreprod", ...}) = 0
    brk(0) = 0x804a000
    access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
    mmap2(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7fab000
    access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
    open("/etc/ld.so.cache", O_RDONLY) = 3
    fstat64(3, {st_mode=S_IFREG|0644, st_size=21956, ...}) = 0
    mmap2(NULL, 21956, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7fa5000
    close(3) = 0
    access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
    open("/lib/tls/libc.so.6", O_RDONLY) = 3
    read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\2 40O\1"..., 512) = 512
    fstat64(3, {st_mode=S_IFREG|0644, st_size=1245488, ...}) = 0
    mmap2(NULL, 1251484, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7e73000
    mmap2(0xb7f9b000, 28672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x12 = 0xb7f9b000
    mmap2(0xb7fa2000, 10396, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb7fa2000
    close(3) = 0
    mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7e72000
    mprotect(0xb7f9b000, 20480, PROT_READ) = 0
    set_thread_area({entry_number:-1 -> 6, base_addr:0xb7e728e0, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0
    munmap(0xb7fa5000, 21956) = 0
    mount("172.20.41.173:/server", "/mnt", "nfs", 0, umovestr: Input/output error
    0x4) = -1 EFAULT (Bad address)
    fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
    mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7faa000
    write(1, "mensaje_error 14 Bad address\n", 29mensaje_error 14 Bad address
    ) = 29
    munmap(0xb7faa000, 4096) = 0
    exit_group(29) = ?
    Process 13592 detached
    With the string "rsize=4096,wsize=4096" instead of data, it does the same.

    I'm begining to be run out of ideas...

    Any other idea??

    thanks

  10. #10
    Just Joined!
    Join Date
    Mar 2009
    Posts
    5
    The last thing I've tried was:

    struct nfs2_fh estruc1={""};
    struct sockaddr_in estruc2; //={AF_INET,0};
    struct nfs3_fh estruc3={0,""};
    struct nfs_mount_data data={3, /* version */
    1, /* fd */
    estruc1,
    1, /* flags */
    4096, /* rsize */
    4096, /* wsize */
    1000, /* timeo */
    3, /* retrans */
    3, /* acregmin */
    60, /* acregmax */
    30, /* acdirmin */
    60, /* acdirmax */
    estruc2,
    "172.20.41.173", /* hostname */
    14, /* namelen */
    1, /* bsize */
    estruc3,
    4,
    ""};

    const char *remoto = "172.20.41.173:/server";
    const char *puntomontaje = "/mnt";
    const char *tipomontaje = "nfs";
    unsigned long int flags = MS_RDONLY;
    int flags_umount = 0;

    int error;

    if (mount(remoto, puntomontaje, tipomontaje, flags, &data) == 0) {
    printf("Se ha montado correctamente\n");
    } else {
    printf("No se ha podido montar.");
    switch(errno){
    case EBUSY: printf("Ya esta montado\n"); break;
    case EINVAL: printf("einval\n"); break;
    default: break;
    }
    }
    In certain moment, it worked! but i've tried to repeat it without success. Really, i don't known the detailed meaning of the nfs_mount_data structure... Any help??

    it would be very helpful if i find an example of c program using this.

    thanks in advance

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...