The
tmpfs filesystem, formerly known as
shmfs, is a filesystem
keeping all files in virtual memory.
Everything in tmpfs is temporary in the sense that no files will be
created on any device. If you unmount a tmpfs instance,
everything stored therein is lost.
tmpfs puts everything into the kernel internal caches and grows and
shrinks to accommodate the files it contains and is able to swap
unneeded pages out to swap space. It has maximum size limits which can
be adjusted on the fly via
'mount -o remount ...'
If you compare it to
ramfs (which was the template to create
tmpfs)
you gain swapping and limit checking. Another similar thing is the RAM
disk (
/dev/ram*), which simulates a fixed size hard disk in physical
RAM, where you have to create an ordinary filesystem on top. Ramdisks
cannot swap and you do not have the possibility to resize them.
tmpfs has a couple of mount options:
-
size: The limit of allocated bytes for this tmpfs instance. The
default is half of your physical RAM without swap. If you
oversize your tmpfs instances the machine will deadlock
since the OOM handler will not be able to free that memory.
-
nr_blocks: The same as size, but in blocks of PAGECACHE_SIZE.
-
nr_inodes: The maximum number of inodes for this instance. The default
is half of the number of your physical RAM pages.
These parameters accept a suffix k, m or g for kilo, mega and giga and
can be changed on remount.
To specify the initial root directory you can use the following mount
options:
-
mode: The permissions as an octal number
-
uid: The user id
-
gid: The group id
These options do not have any effect on remount. You can change these
parameters with
chmod(1),
chown(1) and
chgrp(1) on a mounted
filesystem.
So the following mount command will give you a tmpfs instance on
/mytmpfs which can allocate 12MB of RAM/SWAP and it is only accessible
by root.
mount -t tmpfs -o size=12M,mode=700 tmpfs /mytmpfs
In order to use a
tmpfs filesystem, the
CONFIG_TMPFS option has to be
enabled for your kernel configuration. It can be found in the
Filesystems configuration group. You can simply check if a running
kernel supports
tmpfs by searching the contents of
/proc/fileysystems:
bash# grep tmpfs /proc/filesystems
nodev tmpfs
bash#
In embedded systems
tmpfs is very well suited to provide read and write
space (e.g.
/tmp and
/var) for a read-only root file system such
as
CramFs described in section
9.1.4. Compressed ROM Filesystem.
One way to achieve this is to use symbolic links. The following code
could be part of the startup file
/etc/rc.sh of the read-only ramdisk:
#!/bin/sh
...
# Won't work on read-only root: mkdir /tmpfs
mount -t tmpfs tmpfs /tmpfs
mkdir /tmpfs/tmp /tmpfs/var
# Won't work on read-only root: ln -sf /tmpfs/tmp /tmpfs/var /
...
The commented out sections will of course fail on a read-only root filesystem,
so you have to create the
/tmpfs mount-point and the symbolic links in your root
filesystem beforehand in order to successfully use this setup.