Skip to contentSkip to conversation

(un)mount ramfs from C code

DISCUSSION Brian McGrew 5 participants 7 messages Sep 22, 2009

lore
Share & tools
lore
#0 lore raw

Good morning all!

So I'm using a ramfs for temporary files, thank you whoever designed that, it works great!

I can mkdir, mount, chmoud, readand write and then umount the thing from the command line just fine.

What I need now is some method from within my C/C++ code to determine if the ramfs is mounted, if not, then mount it so I can use it and unmount it when I'm done, without making a system call.

Can this be done? Is there any access to mount/unmount from C/C++?

-b

#1 lore raw
Quoted 11 lines · Brian McGrew

Good morning all!

So I'm using a ramfs for temporary files, thank you whoever designed that, it works great!

I can mkdir, mount, chmoud, readand write and then umount the thing from the command line just fine.

What I need now is some method from within my C/C++ code to determine if the ramfs is mounted, if not, then mount it so I can use it and unmount it when I'm done, without making a system call.

You mean without using system(3), right? System call is how your program interacts with the outside world.

The relevant system calls are:
- mount(2): mount("none", "/mnt", "ramfs", 0, NULL);
- umount(2): umount("/mnt");

The function(section) is the standard Unix way of specifying the manual section of the function. Use it like this:

  $ man 2 mount

About checking whether it is already mounted, you could parse the file /proc/mounts, or check the result of statfs(2).

Regards,
Luciano Rocha

Signature · 2 lines
Luciano Rocha <[email protected]>
Eurotux Informática, S.A. <http://www.eurotux.com/>
#2 lore raw
Quoted 7 lines

You mean without using system(3), right? System call is how your program interacts with the outside world.

The relevant system calls are:
- mount(2): mount("none", "/mnt", "ramfs", 0, NULL);
- umount(2): umount("/mnt");

Thanks for the help! I'm getting there. Considering the following:

If (mount("ramfs", rd_path, "ramfs", MS_NOEXEC | MS_NOSUID, "size=2000m") <

0) {
    strerror(errno);
} else {
    std::cout << "mounted";
}

Works great! Thank you. However, even though the filesystem is mounted and /bin/mountpoint confirms it's a mountpoint, I do not see the mountpoint listed in /etc/mtab.

Also, according to what I've read about ramfs, the size is supposed to be limited to size=, however, I can cat /dev/zero until the box it out of memory (and swap).

I think I'm just missing something and google's not helping a whole lot since ramfs isn't the most popular subject today.

Thanks,

-b

#3 lore raw

mount(2) (i.e. the system-call/library function) does not update /etc/mtab, IIRC... mount(8) (the command-line utility) does...

In either case, I would expect the mount to show up in /proc/mounts (assuming /proc is mounted).

Quoted 30 lines · Brian McGrew
Quoted 30 lines

> You mean without using system(3), right? System call is how your program > interacts with the outside world.
>
> The relevant system calls are:
> - mount(2): mount("none", "/mnt", "ramfs", 0, NULL);
> - umount(2): umount("/mnt");
>

Thanks for the help! I'm getting there. Considering the following:

If (mount("ramfs", rd_path, "ramfs", MS_NOEXEC | MS_NOSUID, "size=2000m") <
0) {
strerror(errno);
} else {
std::cout << "mounted";
}

Works great! Thank you. However, even though the filesystem is mounted and /bin/mountpoint confirms it's a mountpoint, I do not see the mountpoint listed in /etc/mtab.

Also, according to what I've read about ramfs, the size is supposed to be limited to size=, however, I can cat /dev/zero until the box it out of memory (and swap).

I think I'm just missing something and google's not helping a whole lot since ramfs isn't the most popular subject today.

Thanks,

Quoted 7 lines
Quoted 7 lines

-b

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

End of included message

Signature · 1 line
[email protected]
#4 lore raw
In reply to Brian McGrew
Quoted 5 lines · Brian McGrew

Good morning all!

So I'm using a ramfs for temporary files, thank you whoever designed that, it works great!

Quoted 8 lines

I can mkdir, mount, chmoud, readand write and then umount the thing from the command line just fine.

What I need now is some method from within my C/C++ code to determine if the ramfs is mounted, if not, then mount it so I can use it and unmount it when I'm done, without making a system call.

Can this be done? Is there any access to mount/unmount from C/C++?

I suppose looking at the source code of the 'mount' utility could prove very enlightening. You can find it at:

http://www.kernel.org/pub/linux/utils/util-linux-ng

#5 lore raw
Quoted 17 lines · Nikos Chantziaras
Quoted 16 lines · Brian McGrew

Good morning all!

So I'm using a ramfs for temporary files, thank you whoever designed that,
it works great!

I can mkdir, mount, chmoud, readand write and then umount the thing from the
command line just fine.

What I need now is some method from within my C/C++ code to determine if the
ramfs is mounted, if not, then mount it so I can use it and unmount it when
I'm done, without making a system call.

Quoted 6 lines
Quoted 1 line

Can this be done? Is there any access to mount/unmount from C/C++?

I suppose looking at the source code of the 'mount' utility could prove very enlightening. You can find it at:

http://www.kernel.org/pub/linux/utils/util-linux-ng

Also, "man 2 mount" will provide docs about the the mount() function in <sys/mount.h>. If for some reason that man page isn't provided in your system, you can find a copy at:

http://linux.die.net/man/2/mount

#6 lore raw
In reply to Brian McGrew
Quoted 5 lines · Brian McGrew

Good morning all!

So I'm using a ramfs for temporary files, thank you whoever designed that, it works great!

Quoted 8 lines

I can mkdir, mount, chmoud, readand write and then umount the thing from the command line just fine.

What I need now is some method from within my C/C++ code to determine if the ramfs is mounted, if not, then mount it so I can use it and unmount it when I'm done, without making a system call.

Can this be done? Is there any access to mount/unmount from C/C++?

The "/bin/mountpoint"-util appears to stat the mountpoint and it's parent directory and compares if they have the same device-no (st_dev). If they are different, then something is mounted.

Bis denn

Signature · 4 lines
Real Programmers consider "what you see is what you get" to be just as 
bad a concept in Text Editors as it is in women. No, the Real Programmer
wants a "you asked for it, you got it" text editor -- complicated, 
cryptic, powerful, unforgiving, dangerous.