ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I am writing a C program that auto mounts a flash drive when a certain button is pressed and unmounts it when another is pressed. Anyway, the main file system is vfat, but I also have a ext3 file system in a file contained in that partition. I can mount the vfat partition using the C based mount function, but I cannot figure out how to use the mount function to mount the file system in a file. It has to do with the fact that it must use a loop device to do it.
I can mount the ext3 file system in a file using the mount command that is spawned using an execvp call, but I do not get detailed errno information which is what I would like to have when it fails. I need to know why it failed - read only, busy, whatever. So does someone know how to mount a file system in a file via the mount C function, or can someone tell me how to get more detailed errno (not exit code) information from a spawned mount command?
errno is exactly what I want and I can get it for mounting the vfat partition since I call the mount function. But for mounting the file system in a file, I can not get the mount function to work because it must be mounted via a loop device. If anyone knows how to do this then my problem would be solved. But right now I have to spawn another process that executes the mount command. Because it is another process, I know of no way to get the errno value. Does anyone have a solution?
I changed my C code to explicitly call the losetup function outside of the mount instead of passing in /dev/loop0 as -o loop=/dev/loop0. Then I changed the exec spawned mount call to the mount function referenced in the sys/mount.h include file and it worked. Thanks for the idea of using losetup explicitly. I am not sure if my data=journal option is being passed correctly though. How can I verify this? In /proc/mounts I can see all the other options are correct except for the data=journal option.
Essentially I am mounting like this (X is one of the loopback device numbers):
losetup /dev/loopX /mnt/flashvfat/file
Status = mount (/dev/loopX, /mnt/flashext3, "ext3", MS_NODIRATIME | MS_NOATIME,
Is this the right way to specify data=journal with the mount function?
I found all the answers I needed in the util-linux source code. Hopefully this answer will save somebody else the need to look at source code.
Basically the mount command is a wrapper around the mount function and ends up eventually calling it. One thing that it automatically does is if you are trying to mount a file system in a file via a loopback device (like me) is that if something like /dev/loop is passed in, it will open the device as read only and check via an ioctl call to the loopback device (LOOP_GET_STATUS) if the device is in use. If the ioctl returns 0 then the code assumes it is being used. The code assumes the loopback device is not in use if the ioctl returns nonzero with an errno of ENXIO. If not then the code will use that device. If in use, it will poll the status of loop devices 0->7 until it finds a free one or fails. It then associates the loop device with a file with another ioctl call (LOOP_SET_FD). For the mount function call it then passes the loop device in as the first parameter. That is why it wasn't working when I tried to call the mount function with the loop device directly and why calling the external losetup command first worked.
In the umount command (another wrapper to the umount function) it will also call another ioctl (LOOP_CLR_FD) for the loop device after unmounting.
For my purposes it is easier to just call losetup externally until it passes on a loop device, and pass that loop device into the mount function. After I call the umount function, then I call the losetup call with the -d parameter to disassociate the device (although using the ioctl might actually be easier in that case but I'll just stay consistent in my code).
As far as the data=journal option, I was passing it in correctly as a string as the last parameter of the mount function.