Introduction

TmpFS (Temporary File System) is the generic name given to any temporary Unix filesystem. Any file created in such a filesystem disappears when the system shuts down.

The default implementation of tmpfs in Linux 2.6.x kernels is based on ramfs which uses the caching mechanism to optimize memory management.
It is also available on Solaris 10.

However, for security reasons, tmpfs additionally offers a memory size limit set at mount time that can be changed on-the-fly with the “remount” option. Tmpfs also allows the system to use swap when necessary, which provides an additional guarantee.

Unlike a RAM Disk, it dynamically allocates memory so as not to use it excessively, and offers better performance thanks to its extreme simplicity.

Usage

Prerequisites

We will create a mount point on /media/montmpfs.

First, we need to create the directory:

  mkdir -p /media/montmpfs
  

Then, if necessary, change the permissions on this directory so that everyone can read/write/execute:

  chmod 777 /media/montmpfs
  

Mounting

Finally, a tmpfs is mounted like all mount points in Linux, with the mount command.

  mount -t tmpfs -o size=256M tmpfs /media/montmpfs
  

The options are:

  • -t: to specify the file type
  • -o: for options, including size (if not specified, the default size is equal to half of the RAM)
  • then the device, here tmpfs or none (personally I use tmpfs, because with the df command it’s listed as tmpfs as the Filesystem)
  • then the mount point.

To mount it automatically at startup, you need to edit the /etc/fstab file.

Example of a line to add:

  tmpfs /tmp tmpfs defaults,size=1g 0 0
  

Resources

Last updated 18 Feb 2012, 18:47 +0200. history