Short tutorial explaining what is RAM disk and how to create RAM disk in Linux. It also includes differences between ramfs and tmpfs.
Recently one of our reader asked “how to create RAM disk in Linux?”. So I though of writing this small tutorial which will help you to understand what is RAM disk, what is the use of it and how to create RAM disk in Linux.
What is RAM disk?
Roghuly RAM disk can be termed as potion of your RAM mounted as a directory. It uses tmpfs or ramfs. Refer below table for difference between ramfs and tmpfs.
RAM disk is very high speed, high performance and almost zero latency area to store application files. Due to its performance oriented nature, its mostly used for temporary data like caching application files.
How to create RAM disk?
RAM disk can be created in simple two steps. One is to create directory on which it should be mounted and second step is to mount it on that directory using specific FS type. Make sure you have enough free RAM on system so that portion of it can be used in RAM disk. You can check it using free command.
Lets create directory /mnt/ram_disk and mount RAM disk on it.
# mkdir /mnt/ram_disk
# mount -t tmpfs -o size=1024m new_ram_disk /mnt/ram_disk
In above mount command, -t should be followed by tmpfs or ramfs type. For ramfs, size is starting size of RAM disk since ramfs has limitless size. Size followed by name of the disk (of your choice ex. new_ram_disk). You can verify if its mounted properly using df command.
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 5.8G 2.9G 2.7G 53% /
tmpfs 498M 0 498M 0% /dev/shm
You can see newly created tmpfs of 1GB size is mounted on /mnt/ram_disk (highlighted above).
You can add below entry in /etc/fstab as well to persist it over reboots as well. But keep in mind that data within RAM disk flushes for each reboot since its backed memory is volatile.
new_ram_disk /mnt/ram_disk tmpfs nodev,nosuid,noexec,nodiratime,size=1024M 0 0