使用sys/mount.h挂载ISO [英] Mounting an ISO with sys/mount.h

查看:487
本文介绍了使用sys/mount.h挂载ISO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to mount an ISO file in a C++ program in linux

我知道linux命令可以实现这一点,即mount -o loop〜/Test.iso/mnt/myISO

I'm aware of the linux command to achieve this, i.e mount -o loop ~/Test.iso /mnt/myISO

但是mount(2)手册页指出了以下用于安装的原型:

But the mount(2) man page states the following prototype for mounting :

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

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

How do I specify the loop option here ?

-

此外,在linux编程中,使用来自C ++的系统shell调用来完成诸如此类的任务,这通常是好的(/可接受的)做法吗?

Also, is it good (/acceptable) practice in general, in linux programming to use system shell calls from C++ to achieve tasks such as these ?

推荐答案

小例子

#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: 卸载后,您需要自由循环:

upd: after unmount you need free loop:

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

这篇关于使用sys/mount.h挂载ISO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆