LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-03-2005, 08:31 PM   #1
Denes
Member
 
Registered: Mar 2004
Distribution: CentOS 4.3/4.5
Posts: 72

Rep: Reputation: 15
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
 
Old 11-03-2005, 08:48 PM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
Maybe by using the errno variable?
Code:
man errno
 
Old 11-04-2005, 11:21 AM   #3
Denes
Member
 
Registered: Mar 2004
Distribution: CentOS 4.3/4.5
Posts: 72

Original Poster
Rep: Reputation: 15
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?
 
Old 11-04-2005, 07:16 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 11-07-2005, 07:38 PM   #5
Denes
Member
 
Registered: Mar 2004
Distribution: CentOS 4.3/4.5
Posts: 72

Original Poster
Rep: Reputation: 15
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?
 
Old 11-08-2005, 02:56 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 11-11-2005, 01:04 PM   #7
Denes
Member
 
Registered: Mar 2004
Distribution: CentOS 4.3/4.5
Posts: 72

Original Poster
Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read .chm file in fedora, can't mount ntfs file system ishti_du Linux - Newbie 12 03-06-2007 04:27 AM
can't see file system after mounting in Debian subjazz Linux - Newbie 2 06-27-2005 07:49 PM
mounting a binary file thats not on any file system!?!!? aXoneX Linux - General 7 10-26-2004 03:21 AM
Cannot Mount File System slappycakes Linux - Hardware 18 10-13-2003 11:33 AM
mounting problem in local file system svyshna Linux - Newbie 0 08-03-2003 02:00 PM


All times are GMT -5. The time now is 11:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration