TMPFS

From Wikipedia, the free encyclopedia

Jump to: navigation, search

tmpfs is a common name for a temporary file storage facility on many Unix-like operating systems. It is intended to appear as a mounted file system, but one which uses virtual memory instead of a persistent storage device.

Contents

[edit] Semantics

Everything stored in tmpfs is temporary in the sense that no files will be created on the hard drive; however, swap space is used as backing store in case of low memory situations. On reboot, everything in tmpfs will be lost.

The memory used by tmpfs grows and shrinks to accommodate the files it contains and can be swapped out to swap space.

Many Unix distributions enable and use tmpfs by default for the /tmp branch of the file system or for shared memory. This can be observed with df as in this example:

Filesystem            Size  Used Avail Use% Mounted on
tmpfs                 256M  688K  256M   1%  /tmp

[edit] Implementations

[edit] SunOS / Solaris

SunOS, and later Solaris, include some of the earliest implementations of tmpfs;[1] it first appeared in SunOS 4.1, released in March 1990.[2] As of Solaris 2.1 (November 1994), /tmp is by default a tmpfs file system. The `df` output however, looks a bit different, in that it does not show “tmpfs”, but “swap”, even if mounted with `mount -F tmpfs tmpfs /tmp/test`:

# df -k
Filesystem  kbytes  used   avail capacity  Mounted on
swap        601592     0  601592     0%    /tmp/test

[edit] Linux

tmpfs is supported by the Linux kernel from version 2.4 and up.[3] 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 virtual memory (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" which will allow up to 1 GiB in RAM/swap with 10240 inodes and only accessible by the owner of the directory /space. The filesystem's maximum size can also be changed on-the-fly, like "mount -o remount,size=2G /space".

Note that if tmpfs is used for /tmp then care must be taken to set the sticky bit for security reasons[4], and "mode=1777" should be typically used instead of "mode=0777" or "mode=777".

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

[edit] BSD

tmpfs was merged into the official NetBSD source tree on September 10, 2005[5], and is available in 4.0 and later versions. FreeBSD has ported NetBSD's implementation and is available in 7.0 and later versions[6].

[edit] Microsoft Windows

Windows systems have a rough analog to tmpfs in the form of "temporary files". Files created with both FILE_ATTRIBUTE_TEMPORARY and FILE_FLAG_DELETE_ON_CLOSE are held in memory and only written to disk if the system experiences low memory pressure. In this way they behave like tmpfs, except the files are written to the specified path during low memory situations rather than swap space. This technique is often used by servers along with TransmitFile to render content to a buffer before sending to the client.

[edit] References

  1. ^ Peter Snyder. "tmpfs: A Virtual Memory File System" (PDF). http://www.solarisinternals.com/si/reading/tmpfs.pdf. Retrieved on 2007-05-07. 
  2. ^ Hal L. Stern. "SunOS 4.1 Performance Tuning" (GZipped PostScript). http://www.sun3arc.org/papers/Perf/SunOS_4.1_performance_tuning.ps.gz. Retrieved on 2007-05-07. 
  3. ^ Daniel Robbins (September 1, 2001). "Advanced filesystem implementor's guide". http://www-128.ibm.com/developerworks/library/l-fs3.html. Retrieved on 2007-05-07.  Article describing the Linux implementation
  4. ^ CIAC (August 17, 1995). "F-27: Incorrect Permissions on /tmp". http://www.ciac.org/ciac/bulletins/f-27.shtml. Retrieved on 2008-11-17.  An example of a /tmp security vulnerability
  5. ^ Julio M. Merino Vidal (February 24, 2006). "NetBSD-SoC: Efficient memory file-system". http://netbsd-soc.sourceforge.net/projects/tmpfs/. Retrieved on 2007-05-07. 
  6. ^ Derek Morr (December 2, 2008). "FreeBSD tmpfs manpage". http://www.freebsd.org/cgi/man.cgi?query=tmpfs. Retrieved on 2008-12-02. 
Personal tools