使用 sys/mount.h 挂载 ISO
Mounting an ISO with sys/mount.h
我正在尝试在 Linux 的C++程序中挂载 ISO 文件
我知道 linux 命令来实现这一点,即挂载 -o 循环 ~/Test.iso/mnt/myISO
但是 mount(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,"");
}
更新:卸载后,您需要自由循环:
device_fd = open("/dev/loop0", O_RDWR);
...
if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
perror("ioctl LOOP_CLR_FD failed");
return 1;
}
这是一个代码,它也为你创建循环设备。请注意不要在生产中使用此类代码,因为没有对返回值、异常等:)进行单一检查。
#include <sys/mount.h> //mount
#include <sys/ioctl.h> //ioctl
#include <sys/stat.h> //open
#include <linux/loop.h> //LOOP_SET_FD
#include <fcntl.h> //open
#include <cstdio> // declaration of ::fileno
#include <cstdint> //int32_t
#include <sstream> //std::stringstream
#include <string>
constexpr char IMAGE_NAME[] = "image.iso"; //of course we need this file to be present in same folder as built tool
constexpr char MOUNT_POINT[] = "/tmp/image_mnt"; //of course we need this folder already created
constexpr char FILESYSTEM_TYPE[] = "iso9660";
constexpr char DEV_LOOP_CONTROL[] = "/dev/loop-control";
constexpr char DEV_LOOP_PREFIX[] = "/dev/loop";
constexpr int32_t MOUNT_FLAGS = MS_RDONLY;
int main()
{
const auto loop_control = std::fopen(DEV_LOOP_CONTROL, "r");
const auto loop_control_fd = fileno(loop_control);
const auto devnr = ioctl(loop_control_fd, LOOP_CTL_GET_FREE);
std::stringstream loopname;
loopname << DEV_LOOP_PREFIX << devnr;
const auto loop_device_name = loopname.str();
const auto loop_device = std::fopen(loop_device_name.c_str(), "r");
const auto loop_device_fd = fileno(loop_device);
const auto image = std::fopen(IMAGE_NAME, "r");
const auto image_fd = fileno(image);
//Associate the loop device with the open file whose file descriptor is passed as the (third) ioctl(2) argument.
ioctl(loop_device_fd, LOOP_SET_FD, image_fd);
const auto result = mount(loop_device_name.c_str(), MOUNT_POINT, FILESYSTEM_TYPE, MOUNT_FLAGS, NULL);
ioctl(loop_device_fd, LOOP_CLR_FD, 0);
return result;
}
基于:
http://man7.org/linux/man-pages/man4/loop.4.html
https://linux.die.net/man/2/mount
相关文章:
- 如何使用 C++将 ISO-2022-KR 编码转换为 UTF-8 编码?
- ISO C++禁止声明没有类型的"setInputNombre"
- Linux 挂载使用重新挂载以允许读写
- ISO C++禁止指针和整数 [-fpermissive] [c++] 之间的比较
- 为什么我会收到此警告:ISO c++ 禁止可变长度数组"v"[-Wvla]
- ISO 中禁止可变长度数组 C++崇高文本中的错误 3.
- 是否存在经过认证(ISO 26262或类似)C++标准库?
- 使用C++挂载功能挂载加密
- 张量流错误 此文件需要编译器和库支持 ISO C++ 2011 标准
- 错误:ISO C++禁止可变长度数组"subVec"[-Werror=vla]
- Clang-CL 警告 strnicmp 已弃用,请使用 ISO C 并C++符合标准的名称_strnicmp
- Qt 错误 iso c++ 禁止指针和整数之间的比较 -permissive
- DEV C++ 第 69 行 [错误] ISO "for" 范围更改了"i"的名称查找 [-允许]
- 错误:ISO C++禁止在指针和整数 [-fallowive] 之间进行比较
- 如何确保挂载命令在 qt 进程中实际完成
- 警告:ISO C++禁止可变长度数组
- 包括C++标题,"..."与<...>风格,ISO / IEC官方意见?
- 在 c++ 中将 ISO 日期时间转换为本地日期时间并提取时间
- 在 Boost 中解析 RFC3339/ISO 8601 时间戳
- 使用 sys/mount.h 挂载 ISO