gpt4 book ai didi

c++ - 使用 sys/mount.h 挂载 ISO

转载 作者:太空狗 更新时间:2023-10-29 20:45:10 26 4
gpt4 key购买 nike

我正在尝试在 linux 中的 C++ 程序中挂载 ISO 文件

我知道实现此目的的 linux 命令,即 mount -o loop ~/Test.iso/mnt/myISO

但是 mount(2) 手册页声明了以下安装原型(prototype):

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,"");
}

更新:卸载后你需要自由循环:

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

关于c++ - 使用 sys/mount.h 挂载 ISO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11378392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com