tmpfs in Linux

  • by

tmpfs

1.0 tmpfs

tmpfs is a filesystem that resides in the main memory of a Linux system. In the case of disk files, a file is first fetched into the kernel pagecache and the delivered to the requesting process. The fetch part is not there in the case of tmpfs as the files are created and updated directly in kernel caches. The downside is that the files are lost when the system is shutdown. But that's OK if the files are temporary and are not required once the concerned software is not running. The advantage is that, if the software is heavily using those temporary files, it will run a lot faster if those files are a part of a tmpfs filesystem.

2.0 tmpfs size

At the start, a tmpfs filesystem occupies zero bytes. As files grow in it, the size of the tmpfs increases. There is a size attribute for any tmpfs filesystem, the maximum size to which it can grow in physical memory. If it grows beyond that, it is swapped to the swap partition on disk. If that happens, the advantage of using a tmpfs filesystem is lost. So, a tmpfs filesystem should be used only when there is adequate amount of RAM in the system. You can specify the size of a tmpfs filesystem. However, if you don't specify, a default size equal to half of the size of physical RAM is used.

3.0 Creating a tmpfs filesystem

First, we need a directory where the tmpfs would be mounted. Let's say it is /var/mycache,

$ sudo mkdir /var/mycache

We can create the tmpfs by simply mounting it using the mount command. We assume that the login id of the user is alice and user id and group id for alice, both, have a value of 1000. Also, we go with the default size of tmpfs, which is half the size of the physical RAM.

$ sudo mount -t tmpfs -o rw,nosuid,nodev,noexec,mode=0755,uid=1000,gid=1000 tmpfs /var/mycache
$ # Checking whether it has been mounted
$ mount
...
tmpfs on /var/mycache type tmpfs (rw,nosuid,nodev,noexec,relatime,mode=755,uid=1000,gid=1000)
$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
...
tmpfs            1982720        0   1982720   0% /var/mycache
$ ls -ls /var
total 44
...
0 drwxr-xr-x  2 alice alice     40 May 20 16:55 mycache
...

4.0 Mounting tmpfs at the boot time

Ultimately, we want the tmpfs to be mounted at /var/mycache automatically at the boot time. For that we need to add the following line in the /etc/fstab file.

tmpfs  /var/mycache  tmpfs   rw,nosuid,nodev,noexec,mode=0755,uid=1000,gid=1000  0  0
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You may like these, also

  • POSIX message queues in LinuxPOSIX message queues in Linux 1.0 POSIX Message queues POSIX interprocess comunication (IPC) was introduced in the POSIX.1b standard (IEEE Std 1003.1b-1993) for real time…
  • POSIX Shared Memory in LinuxPOSIX Shared Memory in Linux 1.0 Shared Memory Shared memory is the fastest method of interprocess communication (IPC) under Linux and other Unix-like systems. The…
  • POSIX Semaphores in LinuxPOSIX Semaphores in Linux 1.0 Semaphores Semaphores are used for process and thread synchronization. Semaphores are clubbed with message queues and shared memory under…
  • fork and exec system calls in Linuxfork and exec system calls in Linux 1.0 fork and exec system calls Suppose we wish to write a "shell program" which would execute another program. Now,…
  • Connecting two computers with Ethernet LAN cableConnecting two computers with Ethernet LAN cable Quite often, we wish to connect two computers back to back using an Ethernet LAN cable. It may be because…
  • D-Bus TutorialD-Bus Tutorial 1.0 D-Bus D-Bus is a mechanism for interprocess communication under Linux and other Unix-like systems. D-Bus has a layered architecture.…
  • Socket programming using the select system callSocket programming using the select system call 1.0 Client-Server Paradigm The Client-Server paradigm divides the software architecture of a system in two parts, the server and its…
  • System V message queues in LinuxSystem V message queues in Linux 1.0 Message queues Message queues are one of the interprocess communication mechanisms available under Linux. Message queues, shared memory and…
  • POSIX Threads Synchronization in CPOSIX Threads Synchronization in C 1.0 POSIX Threads Synchronization POSIX Threads provide multiple flows of execution within a process. The threads have their own stacks…
  • System V Shared Memory in LinuxSystem V Shared Memory in Linux 1.0 Shared Memory Shared memory is one of the three interprocess communication (IPC) mechanisms available under Linux and other Unix-like…