How to create a RAM drive in Linux

Contents

In case you were wondering, RAM drives and tmpfs instances are not the same. This post will explain the difference and show you how to create a RAM drive in Linux using the command line. Set up a fast RAM drive in minutes!

What is a RAM unit?

Your computer's RAM chips can be used to store a virtual drive. This drive is not stored on hard drive but in RAM. Not only will the speed of this virtual disk be significantly faster than that of a normal disk (and especially compared to an old type rotating disc, since the physical movement of parts within a rotating disk causes additional delays), RAM chips don't wear out as fast as disks, and again especially and even more so with older type physical drives.

It sounds too good to be true? Well, there is actually a caveat; If you accidentally restart your computer, or if it fails, all your data will disappear. RAM (random access memory), the memory chips in your computer, require constant energy to retain your information. RAM storage is considered volatile.

In other words, RAM drives lend themselves to temporary applications or specific optimizations. As an example, when we use test servers to test software, we set up a RAM drive to allow the many simultaneous tests to be faster. And even if the server lost power, you wouldn't miss much; we would just start another test run.

Another application is the preloading of data that is accessed many times on a RAM drive. As an example, if you have a server constantly accessing a certain read-only database (reading and writing can be more complex if data needs to be preserved), that regularly resides on disk, then you can copy the read-only database to RAM automatically (with some automated scripts on server startup, as an example, or with a cron job), and then let the database server use that data.

In other words, one can summarize two main use cases, one is caching (like our R database example / O), the other is data storage “invaluable” (as our test example). Later, can go one step further (a third use case if you want) and sync data to disk at specified intervals. As an example, with test example, which also includes writes to RAM storage, you could write the summary and / or test data on disk (permanent storage) at the end of each full test.

Another caveat with RAM drives is that they are limited to the size of memory on your system and probably less than that., since it needs other memory to run the operating system and other software.

Sizing a RAM drive larger than, let's say a 80-85% arbitrary system memory, may be looking for trouble. In any case, if you have 256 GB of RAM on your server, including the 90% allocated for a RAM unit would leave more than 25 GB for operating system and applications. With only 4 GB, an assignment of the 90% to RAM I would leave 0,4 GB (400 MB), which is likely to cause problems. Therefore, It depends, until a certain point, what is the total memory of the machine and how much will be needed for other software.

At the same time, a RAM drive does not function the same as the allocation / instancia tmpfs.

RAM drive vs tmpfs instance

A tmpfs can also be stored, but it doesn't have to be, on your computer's RAM chips. The usual /dev/shm The tmpfs mapping configured automatically with the installation of most, if not all linux operating systems, it is useful but does not work the same as a RAM drive.

The difference between the two is that a RAM drive (the term hard It comes to mind) is stored at 100% on real RAM chips, while tmpfs is stored in the Linux kernel memory pool, which can include things like swap space, found regularly on disk. Although the kernel would probably optimize all access to the group, still offers the opportunity for data to be written to physical RAM or physical DISK. Y, if you are going to disk, will be slower.

Creating a RAM unit

Creating a RAM drive is relatively straightforward. You can create a small script called ramdrive.sh, with the following code:

#!/bin/bash
if [ "$(mount | grep -o "/mnt/ram")" != "/mnt/ram" ]; then
  sudo mkdir -p /mnt/ram
  sudo mount -t ramfs -o size=1g ramfs /mnt/ram
  sudo chown -R $(whoami):$(whoami) /mnt/ram
fi
mount | grep ram

And another script umount_ram.sh, with the following code:

#!/bin/bash
sudo umount /mnt/ram

Let's take a look at the first script. First, we indicate that we want Bash as our command interpreter with the Shebang symbol (#!). For more information about Shebang, take a look at Bash Automation & Scripting Basics, our post of 3 parts about automation and bash scripting.

After that, we check if we already have a mount underneath /mnt/ram (the directory that we will use to mount our RAM memory unit), using only grep (grep -o) from /mnt/ram in the complete list of ‘mounted today’, as it is shown in mount. If the same is not found, we proceed with three sudo commands. All three require sudo, even when for various reasons.

The first command needs sudo, since it creates a directory possibly root and at least in /mnt, which are privileged directories / protected. The following command, our mounting and creation of real RAM disk, you need sudo since mount is a privileged operation. We set the size to 1GB using size=1g. We also indicate that we want a unit of type ramfs (-t ramfs) coming from the ramfs device (as indicated by the second ramfs), and in summary we list the mount point as /mnt/ram.

In the third command enabled for sudo, we change the owner of the /mnt/ram directory (now our RAM unit, our ramfs mount point) the current user and the current user's own group using the whoami command twice. You might want to change this to the specific group and / or specific to use ramdrive or to a broader group if more users will use ramdrive.

After that, we end our conditional if .. fi command and make a final call to mount with a grep for ram to make sure the script reports what was already mounted in terms of RAM, or what was mounted just now when the script was run. This is a practical check / quick that the script was successful when it was run.

Our sub script, umount_ram.sh, unmount RAM based drive with mount point /mnt/ram, In other words, the ramfs drive we just created. WARNING: run this removes / immediately erase all data stored in volatile memory and remounting the RAMFS drive will not bring this back; it will just create a new but empty RAM drive. Be careful!

Ending

In this post, we discuss RAM drives / ramfs (the same) and the instances / tmpfs assignments. Later we create a 1GB ramfs drive under /mnt/ram using a little script to mount or a RAM drive.

If you want to continue reading on Linux, take a look at our posts Screen Recording in Linux with SimpleScreenRecorder or Bits, Bytes y Binary o From 0 to F: Hexadecimal!

Enjoy!

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.