LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Mounting file system in a file with mount (https://www.linuxquestions.org/questions/programming-9/mounting-file-system-in-a-file-with-mount-379755/)

Denes 11-03-2005 07:31 PM

Mounting file system in a file with mount
 
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?

Thanks

perfect_circle 11-03-2005 07:48 PM

Maybe by using the errno variable?
Code:

man errno

Denes 11-04-2005 10:21 AM

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?

ta0kira 11-04-2005 06:16 PM

Create the loopback first, then mount the loop:
Code:

> modprobe loop
> losetup /dev/loop0 yourfile.img
> mount /dev/loop0 /mnt/hd -t ext3

Or, if your mount supports it:
Code:

> mount yourfile.img /mnt/hd -t ext3 -o loop=/dev/loop0
ta0kira

Denes 11-07-2005 06:38 PM

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,
"data=journal");

Is this the right way to specify data=journal with the mount function?

ta0kira 11-08-2005 01:56 PM

Sorry, I've never used the headers for mount, etc. Have you tried using 'system("mount ...");'? You can type shell-like commands that way instead of using the headers.
ta0kira

Denes 11-11-2005 12:04 PM

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.


All times are GMT -5. The time now is 10:43 AM.