c++linuxmount

Should I use mount() or /usr/bin/mount


I want to mount a filesystem using C++. Should I use the mount() system call, or just execute the mount binary?

Obviously a system call is going to be faster, and I'm going to waste less time building command-line arguments and parsing error messages and stuff. However, having read mount(2), it is unclear to me what restrictions (if any) there are on the arguments.

I particularly want to be able to mount disk image files as well as actual physical disks. And I also want to be able to mount individual filesystems by specifying an offset from the start of the device / image. I don't know whether you can do that with a single call to mount(), or whether you would need to manually create loop devices first. (I also don't know how hard it is to create a loop device — I presume it's fairly easy...)

If setting this stuff up right is fairly complicated, then it's probably simpler and easier to call the mount binary. But if it's just one system call, calling mount() directly seems cleaner. So which one is likely to give me the fewest problems?


Solution

  • Well, Nayden suggested running strace mount, which isn't a bad idea.

    Having done this, it appears that mount is doing a hell of a lot of work. It looks like it does create a loop device and mount that. (Which means it needs to figure out what the next unused loop device number is, etc.) It sounds like you may also need to manually figure out the filesystem type.

    In short, it looks to me like one simple call to the mount binary is probably going to be far less work than trying to recreate everything that program does. I had thought the intelligence is in the kernel, but apparently not.