挂载失败,errno是20?
我是linux程序的新手。为什么以下代码失败?它的输出是“失败20”。 但在终端中,命令:sudo mount /dev/sdb /home/abc/work/tmp 有效。
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");
}
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能绑定挂载设备,只能绑定挂载目录。尝试为
mountflags
提供有用的值。You can't bind-mount a device, only a directory. Try providing a useful value for
mountflags
.错误 20 是 ENOTDIR (http://www-numi.fnal。 gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html)。
我认为使用 MS_BIND,您需要第一个参数是某处的实际目录,而不是设备。另请参阅 mount 的手册页
您尝试执行的操作相当于
sudo mount --bind /dev/sdb /home/abc/work/temp
这也会给你一个错误。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.您不仅应该打印 errno 值,还应该打印相应的错误消息:
这应该揭示问题的原因。 (“不是目录”,因此
/home/abc/work/tmp
似乎不是一个目录。)(您的代码还存在各种其他问题,例如缺少
#include
语句,并将错误消息写入 stdout 而不是 stderr,但这些与您当前的问题无关。您可以稍后修复它们。)You should print out not just the errno value, but also the corresponding error message:
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.)