55

I want to make a partition that is made of ram ...

Example

In windows 7 you can make a partition that is made of ram

enter image description here

I have created 1 GB of partition in ram . using Primo RamDisk

Is there any good Alternative in Ubuntu ?

  • 3
    Ubuntu comes with tmpfs. You need not create a RAMDISK. – mixdev Mar 30 '14 at 4:03
  • Depending on your intended use case you may not need a ramdisk for Ubuntu (or most Linux distros). The operating system caches reads and write activity to RAM while it is working with regular disks. If you read a small file several times, it will only be fetched from disk once, then retrieved from the RAM cache on the following times. If you have plenty of RAM, everything you do will get cached in this way so you get very little repeat disk activity. If you want non-persistent fast memory use instead of files, you need a RAMDISK still. See linuxatemyram.com for more details. – TafT Oct 6 '16 at 14:11
77

This will show you how to make a RAMDISK super fast and easily. With a RAMDISK you can use your memory for temporary space and it’s also a lot quicker than your hard drive.

Now lets start by using the next 2 commands to make your RAMDISK.

Put whatever you want your RAMDISK to be called where I wrote “nameme”.

mkdir -p /media/nameme

mount -t tmpfs -o size=2048M tmpfs /media/nameme/
The above commands would use 2GB of my RAM for the RAMDISK. If you don’t have as much ram as I do I would use 512MB or 1GB. So next were going to create a command for Terminal that will automatically create the RAMDISK for you.

Source: How To Create A RAMDISK In Linux

  • 3
    How can i make this persist between reboots? – chovy Nov 13 '16 at 4:53
  • 1
    Being it's RAM, it's volatile, when you restart, it's all lost, even if a warm boot as the BIOS/EFI will clear the RAM. Best you can do is make a shell script that runs the commands at login. – Brandon MacEachern Dec 2 '16 at 21:43
  • 1
    @chovy to persist you have couple of options i think about 2 at the moment. 1. write api to write to ram disk and asynchronous write to hdd disk. Always read from ram, only on boot warm up ram from disk. 2. time-to-time call some script which copies anything in ram disk to some hdd disk and copy back once after boot. – Lukas May 15 '17 at 5:05
  • another option would be to designate something like /tmp/cache as your cache dir, where that directory is actually the mountpoint of an overlayfs (using a tmpfs as the upperdir, and whatever else on your normal filesystem that you'd normally interact with as the lowerdir). this would let you have a fast, ephemeral layer, and something able to be trivially synchronized if you did occasionally want to make parts of it persistent. – mway Jul 17 '17 at 4:51
  • chovy add it to /etc/rc.local and it will automatically mount the partition... but the data will be lost. – 777Anon Jan 5 '18 at 18:40
49

The tmpfs filesystem is a RAMDISK. The following will create a 2G RAMDISK that will always be available.

sudo mkdir -p /media/ramdisk
sudo mount -t tmpfs -o size=2048M tmpfs /media/ramdisk

The ramdisk folder is owned by root as it is to be available on reboot. The ramdisk permissions should be writeable by everyone. The tmpfs default permissions (chmod 1777) are correct.

sudo chmod 1777 /media/ramdisk
drwxrwxrwt 2 root root 180 Apr 23 07:34 /media/ramdisk

To make the ramdisk permanently available, add it to /etc/fstab.

grep /media/ramdisk /etc/mtab | sudo tee -a /etc/fstab

You will see the line moved from mtab to fstab. It will look something like this.

tmpfs /media/ramdisk tmpfs rw,size=2048M 0 0

The RAMDISK won't consume memory until you use it. Double check your memory requirements during maximum system load. If the RAMDISK is too large, your system will consume swap storage to make up the difference.

To adjust the size of the RAMDISK, edit /etc/fstab and verify by remounting the ramdisk (you will lose your current RAMDISK content as you will on reboot). The following will change the size of the ramdisk to 512M

# Check the existing ramdisk size.
df /media/ramdisk
# change size=512M for a 512 megabyte ram drive.
sudo vi /etc/fstab
# Remount the ramdisk, you will lose any existing content.
sudo mount -a /media/ramdisk
# Verify the new ramdisk size.
df /media/ramdisk
  • 1
    Technically, tmpfs is a ramfs, not ramdisk. – heemayl Oct 5 '16 at 12:08
  • 2
    Agree. I chose to use RAMDISK as it's the common name for this on Windows. – rickfoosusa Oct 7 '16 at 14:35
  • How can one check if the ramdisk is really in use? I always see it as Used 0%. – BTR Naidu Jan 12 '17 at 9:41
9

Adding my 2-cent for the case you don't have root-privileges:

Quoting from this answer from unix.stackexchange

Linux provides a tmpfs device which any user can use, /dev/shm. It is not mounted to a specific directory by default, but you can still use it as one.

Simply create a directory in /dev/shm and then symlink it to wherever you want. You can give the created directory any permissions you choose, so that other users can't access it.

This is a RAM backed device, so what's there is in memory by default. You can create any directories you need inside /dev/shm

Naturally, files placed here will not survive a reboot, and if your machine starts swapping, /dev/shm won't help you.

Not the answer you're looking for? Browse other questions tagged or ask your own question.