Terry : In Memory Filesystems

tmpfs in Linux

tmpfs is supported by the Linux kernel from version 2.4 and up. tmpfs (previously known as shmfs) distinguishes itself from the Linux ramdisk device by allocating memory dynamically and by allowing less-used pages to be moved onto swap space. ramfs, in contrast, does not make use of swap (which can be an advantage or disadvantage). In addition, MFS and some older versions of ramfs did not grow and shrink dynamically and instead used a fixed amount of memory at all times.

Usage of tmpfs for example is:

mount -t tmpfs -o size=1G,nr_inodes=10k,mode=0700 tmpfs /space

unmount

umount /space

which will allow up to 1 GiB in RAM/swap with 10240 inodes and only accessible by the owner of /space. Owner can be overridden with the uid/gid mount options, and will otherwise default to root. The filesystem's maximum size can also be changed on-the-fly, like below:

mount -o remount,size=2G /space

/var/run and /var/lock can be tmpfs filesystems, to alleviate having to clean them up at each reboot.

tmpfs VS ramfs

Primarily both ramfs and tmpfs does the same thing with few minor differences.

  • ramfs will grow dynamically.  So, you need control the process that writes the data to make sure ramfs doesn't go above the available RAM size in the system. Let us say you have 2GB of RAM on your system and created a 1 GB ramfs and mounted as /tmp/ram. When the total size of the /tmp/ram crosses 1GB, you can still write data to it.  System will not stop you from writing data more than 1GB. However, when it goes above total RAM size of 2GB, the system may hang, as there is no place in the RAM to keep the data.
  • tmpfs will not grow dynamically. It would not allow you to write more than the size you’ve specified while mounting the tmpfs. So, you don’t need to worry about controlling the process that writes the data to make sure tmpfs doesn’t go above the specified limit. It may give errors similar to “No space left on device”.
  • tmpfs uses swap
  • ramfs does not use swap

Disadvantages of ramfs and tmpfs

Since both ramfs and tmpfs is writing to the system RAM, it would get deleted once the system gets rebooted, or crashed. So, you should write a process to pick up the data from ramfs/tmpfs to disk in periodic intervals. You can also write a process to write down the data from ramfs/tmpfs to disk while the system is shutting down. But, this will not help you in the time of system crash.
Table: Comparison of ramfs and tmpfs

Experimentation

tmpfs

ramfs

Fill maximum space and continue writing

Will display error

Will continue writing

Fixed Size

Yes

No

Uses Swap

Yes

No

Volatile Storage

Yes

Yes

If you want your process to write faster, opting for tmpfs is a better choice with precautions about the system crash.

More examples

Use of ramfs

mount -t ramfs ramfs /mnt/ramfs

unmount

umount /mnt/ramfs

Kernel parameters

On Ubuntu 11.04 natty dev

root@natty:/home/terry# sysctl -a | grep shm
kernel.shmmax = 33554432
kernel.shmall = 2097152
kernel.shmmni = 4096
vm.hugetlb_shm_group = 0

Use sysctl

sysctl -w kernel.shmmax="65536"
sysctl -p

Change on the fly (pseudo file system)

echo number > /proc/sys/kernel/shmmax

Make the change permanent /etc/security/limits.conf
kernel.shmmax = 65536