Mounting an ISO with sys/mount.h

2020-07-18 03:24发布

问题:

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

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

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 ?

--

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

回答1:

small example

#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: 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;
}


回答2:

Here is a code, that also creates loop device for you. Be aware not to use such code in production, as there is no single check on return values, exceptions etc :).

#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;
}

based on:
http://man7.org/linux/man-pages/man4/loop.4.html
https://linux.die.net/man/2/mount