(un)mount ramfs from C code
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
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/>
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
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/mountpointconfirms 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/zerountil 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]
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:
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:
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:
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.