ubuntu ramdisk

A RAMDisk is a block of memory that’s formatted with a filesystem and mounted to a directory. You can use this disk to test filesystem performance, store temporary data such as cache files, or use it as a location to unpack or compile programs.

You can use the Block RAM Disk (brd) module to use a specified amount of RAM as a block device. Alternatively, you can mount a tmpfs instance and use that as a RAMDisk. We’ll cover the steps for both of these methods in this article.

Using brd to Create A RAMDisk

Loading the brd module will designate a part of the RAM as a block device. You’ll be able to build file systems on this device, read/write to it, and so on. When loading it, you should define three parameters

  • rd_size: Size of RAMDisk (kb)
  • max_part: Max no. of partitions per RAMDisk
  • rd_nr: Max no. of brd devices

For instance, to create a 1GB RAMDisk with a single partition, you could use

sudo modprobe brd rd_size=1048576 max_part=1 rd_nr=1

This’ll create the RAMDisk at /dev/ram0. In your case, you may want to check your RAM usage first to determine the appropriate size for the RAMDisk.

free -h

Then, you can build a filesystem on the created partition using mkfs.

sudo mkfs -t ext4 /dev/ram0

Now, create a temporary partition, change its ownership to your current user, and mount the RAMDisk there.

sudo mkdir /mnt/ramdisk
sudo chown -R $USER:$GROUP /mnt/ramdisk
sudo mount /dev/ram0 /mnt/ramdisk

When you’re done using the RAMDisk, you can free up the space by unmounting the partition and unloading the brd module.

sudo umount /mnt/ramdisk
sudo rmmod brd

Creating a tmpfs RAMDisk

A Temporary File System (tmpfs) looks like a disk-mounted file system, but it actually stores all data in the RAM. To use it as a RAMDisk, first, create the mount point with mkdir.

sudo mkdir /mnt/ramdisk 

Then, all you need to do is mount the disk.

sudo mount -t tmpfs -o rw,size=1G newramdisk /mnt/ramdisk

You can verify that the RAMdisk is successfully created using

df -h

Optionally, you can configure the /etc/fstab file to automatically mount the RAMDisk at boot. To do this, backup the original fstab config first.

sudo cp /etc/fstab /etc/fstab.original

Once that’s done, open the fstab file with a text editor. We’ll use nano.

sudo nano /etc/fstab

Add the following line at the end:

newramdisk  /mnt/ramdisk  tmpfs  rw,size=1G  0   0

Adjust the values like mount point and disk size if required. Then, save the changes and exit the editor.

You can verify that the RAMDisk automounts by restarting your machine.

sudo shutdown -r now

If you want to stop auto-mounting the disk in the future, edit the fstab file to remove the line we added earlier. Additionally, you can manually unmount the disk like so

sudo umount /mnt/ramdisk
Anup Thapa

Senior Writer

Anup Thapa is a Linux enthusiast with an extensive background in computer hardware and networking. His goal is to effectively communicate technical concepts in a simplified form understandable by new Linux users. To this end, he mainly writes beginner-friendly tutorials and troubleshooting guides. Outside of work, he enjoys reading up on a range of topics, traveling, working out, and MOBAs.