Linux mount函数问题

晓敬 2011-05-26 03:09:30

#include <iostream>
#include <stdio.h>
#include <sys/mount.h>

int main(){
int mountfd = mount("/dev/sda10","/mnt/","vfat",MS_SYNCHRONOUS,"iocharse t=utf-8");
std::cout << mountfd << std::endl;
std::cout << MS_SYNCHRONOUS << std::endl;
perror("mount");
return 0;
}

程序运行之后返回的结果是:
mountfd = -1
MS_SYNCHRONOUS = 16
mount: Invalid argument
...全文
1575 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2011-05-30
  • 打赏
  • 举报
回复
int mountfd = mount("/dev/sda10","/mnt","vfat",MS_MGC_VAL,"iocharset=cp936,utf8");

试试
晓敬 2011-05-27
  • 打赏
  • 举报
回复
再顶一下 求解!
晓敬 2011-05-26
  • 打赏
  • 举报
回复
谢谢两位大哥的热情帮忙,问题已经解决了,不是mountflags的问题,是我的第四参数,设置文件系统参数出问题了,编码应该是utf8。
以上错误查找办法是查看系统日志,在syslog中会提示:
inger-laptop kernel: [30169.678758] FAT: IO charset utf-8 not found
不过我在用mount加载镜像文件的时候又出现了新的错误,提示为:
mount: Block device required
其中程序代码改为:

18 #include <iostream>
19 #include <stdio.h>
20 #include <sys/mount.h>
21
22 int main(){
23 const char* device = "/home/jinger/Fedora-15-i686-Live-Desktop.iso";
24 //const char* device = "/dev/sda10";
25 const char* target = "/mnt";
26 const char* ftype = "iso9660";
27 //const char* ftype = "vfat";
28 int mountfd = mount(device,target,ftype,MS_RDONLY,"loop") ;
29 std::cout << mountfd << std::endl;
30 std::cout << MS_RDONLY << std::endl;
31 perror("mount");
32 return 0;
33 }

最后参数部分我尝试过"loop=/dev/loop3",因为在man 8 mount手册中,我查看关于loop设备的介绍,以为提示需要块设备是需要在参数中手动指定loop设备,就进行了以上尝试,但是均提示上述错误。

还有一个值得注意的地方就是mount函数第二参数就相当于命令行中的-t filetype[iso9660,vfat etc.],第四参数相当于-o options[loop,iocharset=utf8 etc.]

我已经在命令行中使用如下命令【测试成功】:
sudo mount -o loop -t iso9660 /home/jinger/Fedora-15-i686-Live-Desktop.iso /mnt
justkk 2011-05-26
  • 打赏
  • 举报
回复
mountflags 填0试试看
晓敬 2011-05-26
  • 打赏
  • 举报
回复
1、ioscharset=8是用复制错误,源程序中没有换行或者空格
2、mount -t vfat /dev/sda10 /mnt -o iocharset=utf8 执行之后正确加载了
3、对于上面的程序我测试了"/dev/sda10","/mnt/"参数如果出错的话会显示没有该文件夹
4、如果我使用mount命令加载了文件系统的话,使用以上程序加载会提示错误
mount:Device or resource busy
我怀疑是第三个参数出的问题,然后我查看man手册,mount的函数原型是这样的
int mount(const char *source, const char *target,
const char *filesystemtype, unsigned long mountflags,
const void *data);
其中mountflags在2.4以前版本的内核中需要在高16位添加一个MS_MGC_VAL的字节,我的内核是Linux 2.6.32-31-generic 我尝试了一下,用(MS_MGC_VAL|MS_SYNCHRONOUS)做mountflags还是提示参数出错。
zhaokai3000 2011-05-26
  • 打赏
  • 举报
回复
最后一个参数:"iocharse t=utf-8",复制错误?还是程序里面手误?
"iocharset=utf8"

还有,在命令行里面执行
mount -t vfat /dev/sda10 /mnt -o iocharset=utf8
能成功吗

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧