Linux Commands

TMPFS Virtual Memory File System in Linux

Tmpfs, formerly known as shmfs, is a temporary file system and refers to a file system that is located in memory and/or the swap partition(s). Tmpfs doesn’t store the file data on normal, non-volatile storage. Instead, the UNIX kernel manages the virtual memory, which is where Tmpfs files live.The main reason that Tmpfs was created was to improve the performance by letting the temporary files be written to and read from without using the disk or network I/O.

What Will We Talk About?

In this article, we will see an overview of the Tmpfs file system in Linux.

Advantages of Using the Tmpfs System

The former random access memory file system (ramfs) in Linux has been replaced with the new temporary file system (Tmpfs). This is because the old RAMFS did not address the situations properly when the system ran out of RAM.

Mounting directories as Tmpfs has several benefits such as:

1. To accelerate the access to the files inside them.

2. To make sure that every time the system restarts, the contents of the directories are automatically deleted.

3. Tmpfs increases the file manipulation rate while preserving the semantics of a UNIX file.

4. Tmpfs does not need the fixed or dedicated disk space for files and does not have adverse effects on performance.

Instead of utilizing the dedicated physical memory like a “RAM disk”, Tmpfs stores the file data in the page cache of an OS. It makes it easier to read and write the files, lets the file system grow and shrink on the fly without using any disk space, and doesn’t hurt the overall performance of the system.

Mounting Tmpfs

Use the mount command and choose “tmp” as the file system type to create a Tmpfs type file system. Since Tmpfs always utilizes the memory as the file system resource, the resource parameter to mount (e.g. raw device) is disregarded. Since Tmpfs always uses the memory as the file system resource, the resource parameter for mounting (such as raw device) is not taken into account.

Currently, Tmpfs has no mount options. Many regular mount options are not relevant to the context of Tmpfs. For instance, making a Tmpfs mount “read only” does not make any sense since they do not hold any data when they are mounted for the first time. Tmpfs supports all types of files, which also include the symlinks, block, and character special device files – UNIX file semantics. Although there can be many Tmpfs based file systems that are mounted on one system, all of them have to share the same pool of resources.

Since the contents of a memory-based file system are wiped after a system reboot or unmount operation, and because these files have very short lives, /tmp is the most suitable location for them (thus known as Tmpfs). This indicates that /usr/tmp is not an appropriate location for mounting a Tmpfs file system because its contents are retained between reboots.

Size of Tmpfs

The amount of free space that can be used by Tmpfs is directly proportional to the amount of unused swap space. The size of a Tmpfs file system expands to fit the files that are added to it, but the users who regularly use Tmpfs must be aware that this growth comes with some inherent costs. Tmpfs shares its resources with the data and stack segments of programs that are being executed.

If Tmpfs file systems are near their maximum permissible capacity, it is possible that the execution of very large programs are impacted. Tmpfs can assign all of the system’s swap space, with the exception of 4 MB. This is sufficient to make sure that most of the programs can execute. Nonetheless, it is possible that certain programs are unable to execute if the Tmpfs file systems are close to full capacity. Users who plan to run the large programs and extensively use Tmpfs should look for ways to increase the available swap space in the system.

Uses of Tmpfs

The uses of Tmpfs include:

1. There is always an internal mount in the kernel that cannot be seen. SYSV shared memory and shared anonymous mappings both utilize the Tmpfs. Tmpfs mount is independent of CONFIG_TMPFS. The user-visible portion of Tmpfs will not be created if CONFIG_TMPFS is not set. However, internal mechanisms exist at all times.

2. POSIX shared memory (shm_open, shm_unlink) requires that tmpfs be mounted at /dev/shm for glibc versions 2.2 and above. This may be resolved by including the following line in the /etc/fstab file:

tmpfs /dev/shm tmpfs defaults 0 0

If required, create the directory where you want to mount the Tmpfs.

The shared memory that is used by SYSV does not need the Tmpfs mount. This is done with the internal mount. To use the SYSV shared memory in the 2.3 kernel versions, shm fs (the predecessor to Tmpfs) had to be mounted.

3. It is really useful for some users to mount it on locations like /tmp and /var/tmp and have a sizable swap disk. Additionally, loop mounts of Tmpfs files are now functional. Thus, the majority of distributions’ mkinitrd should operate with a Tmpfs.

4. And there are too many to list.

Mount Options for Tmpfs

Let’s explore some of the mount options with Tmpfs:

size: This specifies the maximum number of bytes that can be used for a Tmpfs instance. By default, only half of the physical RAM can be used without swap space.

If Tmpfs instances are made larger than they need to be, the machine gets stuck in a deadlock because the OOM handler won’t be able to free up that memory.

nr blocks: It is the same as the size option. However, it is in blocks of PAGE_CACHE_SIZE.

nr inodes: It is the upper limit of inodes for this instance. The default value is half the number of physical RAM pages or the number of lowmem RAM pages (on a system with highmem), whichever is less.

Conclusion

Normally, Tmpfs files are deleted across the system reboots. But if you want to preserve them, you can use the systemd-tmpfiles. We recommend that you read the man pages if you want to learn more about Tmpfs.

About the author

Ali Imran Nagori

Ali imran is a technical writer and Linux enthusiast who loves to write about Linux system administration and related technologies. You can connect with him on LinkedIn
.