6

I need to make a ramfs an mount it to an directory in linux using c++. I want to make it like a user (no sudo).

I need to call an application on a file that i created and it will be often. Writing it to HDD is very slow.

I found just:

system("mkdir /mnt/ram");
system("mount -t ramfs -o size=20m ramfs /mnt/ram");

but that is not good. I want to be a regular user, and command mount can be called just as root. what can i do?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
microo8
  • 3,568
  • 5
  • 37
  • 67
  • 1
    You can read the file once to memory and manipulate just the in-memory representation. Once you're done, write it back to disk. – MadScientist Sep 27 '12 at 10:54
  • why not add a tmpfs entry to the `fstab` file? Add the `user` flag so it can be mounted by a user on demand. Or just use memory mapped files. – Some programmer dude Sep 27 '12 at 10:55
  • I guess gain superuser privileges is the only way. This can be done with some C++ code or giving your executable root privileges with setuid bit. – Paolo Sep 27 '12 at 10:55
  • my app will write a file, and call an other app on it. the other app will write some magic to this file and my app will read it and so on. This is very offten. This app will by distributed, so I cant want from all the users that will use it, to edit the fstab. – microo8 Sep 27 '12 at 11:01
  • How about shared memory, then? http://www.boost.org/doc/libs/1_51_0/doc/html/interprocess/sharedmemorybetweenprocesses.html – enobayram Sep 27 '12 at 11:03
  • 2
    You might also want to consider benchmarking this in the real world since a mere 20 MB will anyways be cached. And how about /tmp, which is a ramdisk on any reasonable system nowadays? – themel Sep 27 '12 at 11:55
  • yes thank you. `/tmp` will be the solution. but anyway, it cannot make a ramfs from c++? – microo8 Sep 27 '12 at 16:50
  • C++ looks a bit extraneous since you are using `system` :-) – Ciro Santilli OurBigBook.com Oct 25 '15 at 22:44

2 Answers2

1

For a userspace ramfs solution, you can use python-fuse-ramfs.

Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
0

I checket if /tmp is a ramfs, but it is not. It creates files on the HDD. but when i run df -h it outputs:

rootfs                       25G  9,4G   15G  40% /
devtmpfs                    1,9G     0  1,9G   0% /dev
tmpfs                       1,9G  1,6G  347M  83% /dev/shm
tmpfs                       1,9G  1,3M  1,9G   1% /run
/dev/mapper/vg_micro-root    25G  9,4G   15G  40% /
tmpfs                       1,9G     0  1,9G   0% /sys/fs/cgroup
tmpfs                       1,9G     0  1,9G   0% /media
/dev/mapper/vg_micro-stack  289G  191M  274G   1% /stack
/dev/mapper/vg_micro-home   322G   40G  266G  14% /home
/dev/sda2                   485M   89M  371M  20% /boot
/dev/sda1                   200M   19M  182M  10% /boot/efi

This means that tmpfs (ramdisks) are: /dev/shm, /run, /sys/fs/cgroup and /media. But only one of this is meant to be a temporary ramdisk for comunication between processes, using files. Here is the /dev/shm description and usage. The only thing is that tmpfs will not grow dynamically, but for my purposes it will be enough (20MB - 1GB).

microo8
  • 3,568
  • 5
  • 37
  • 67
  • As themel noted, files in `/tmp` will be in RAM. Not because it's a tmpfs, but because the file cache is lazy. – MSalters Sep 28 '12 at 09:42