bright
bright

Reputation: 137

mount failed, errno is 20?

I'm newbie in linux program. why following code failed? its output is "failed 20". but in terminal the command: sudo mount /dev/sdb /home/abc/work/tmp works.

void main()
{
    int rtn;

    rtn=mount("/dev/sdb","/home/abc/work/tmp","vfat",MS_BIND,"");  
    if (rtn==-1)
        printf("failed %d.\n",errno);
    else
        printf("OK!\n");
}

Upvotes: 0

Views: 1746

Answers (3)

user25148
user25148

Reputation:

You should print out not just the errno value, but also the corresponding error message:

printf("failed %d: %s\n", errno, strerror(errno));

This should reveal the reason for the problem. ("Not a directory", so /home/abc/work/tmp does not seem to be a directory.)

(There are various other problems with your code, such as missing #include statements, and writing error messages to stdout and not stderr, but those are irrelevant to your problem at hand. You can fix them later.)

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799210

You can't bind-mount a device, only a directory. Try providing a useful value for mountflags.

Upvotes: 1

Joubarc
Joubarc

Reputation: 1216

Error 20 is ENOTDIR (http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html).

I think with MS_BIND, you would need the first argument to be an actual directory somewhere, not a device. See also the man page for mount

What you are trying to do would be equivalent to sudo mount --bind /dev/sdb /home/abc/work/temp which will give you an error too.

Upvotes: 0

Related Questions