使用sys / mount.h安装ISO

我正在尝试在Linux中的C ++程序中安装ISO文件

我知道的Linux命令来实现这一点,即安装-o循环〜/ Test.iso / mnt / myISO

但是挂载(2)手册页指出了下面的安装原型:

int mount(const char *source, const char *target, const char *filesystemtype, unsigned long mountflags, const void *data); 

我如何在这里指定循环选项?

另外,一般来说,在Linux编程中使用来自C ++的系统shell调用来实现这些任务是好的(/可接受的)练习吗?

小例子

 #include <sys/mount.h> #include <linux/loop.h> #include <fcntl.h> int main() { int file_fd, device_fd; file_fd = open("./TVM_TOMI1.iso", O_RDWR); if (file_fd < -1) { perror("open backing file failed"); return 1; } device_fd = open("/dev/loop0", O_RDWR); if (device_fd < -1) { perror("open loop device failed"); close(file_fd); return 1; } if (ioctl(device_fd, LOOP_SET_FD, file_fd) < 0) { perror("ioctl LOOP_SET_FD failed"); close(file_fd); close(device_fd); return 1; } close(file_fd); close(device_fd); mount("/dev/loop0","/mnt/iso","iso9660",MS_RDONLY,""); } 

upd:在卸载之后你需要自由循环:

 device_fd = open("/dev/loop0", O_RDWR); ... if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) { perror("ioctl LOOP_CLR_FD failed"); return 1; }