首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用sys/mount.h挂载ISO

问使用sys/mount.h挂载ISO
EN

Stack Overflow用户
提问于 2012-07-08 04:30:10
回答 2查看 6K关注 0票数 4

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

我知道linux命令可以实现这一点,比如挂载-o循环~/Test.iso /mnt/myISO

但mount(2)手册页说明了以下用于挂载的原型:

代码语言:javascript
复制
int mount(const char *source, const char *target,
const char *filesystemtype, unsigned long mountflags,
const void *data);

如何在此处指定循环选项?

--

另外,在linux编程中,使用来自C++的系统shell调用来完成这样的任务,通常是一种好的(/acceptable)实践吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-08 05:44:46

小示例

代码语言:javascript
复制
#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,"");
}

更新:卸载后需要空闲循环:

代码语言:javascript
复制
device_fd = open("/dev/loop0", O_RDWR);
...
if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
    perror("ioctl LOOP_CLR_FD failed");
    return 1;
}
票数 6
EN

Stack Overflow用户

发布于 2019-07-18 04:52:41

这是一个代码,它也为你创建了环路设备。注意不要在生产中使用这样的代码,因为没有对返回值、异常等进行单一检查:)。

代码语言:javascript
复制
#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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11378392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档