13

I'm using Ubuntu 10.04(64bit) for my desktop.
The machine has a 5GB RAM.

I want to use RAM disk(1G or 2G) but I don't know how can I do this.

Is there any opensource product of RAM disk?

Benjamin
  • 2,239
  • 5
  • 26
  • 39

1 Answers1

18

The technology is built into the kernel, you don't need any extra tools. In fact, you already have a few RAM disks (which you shouldn't use, they're reserved for the system), which you can see by doing

grep -w tmpfs /proc/mounts

To set up a 2GB RAM disk mounted on /ramdisk, add the following line to /etc/fstab:

ramdisk  /ramdisk  tmpfs  mode=1777,size=2g

Then mount the disk with the command mount /ramdisk (this will be done automatically when you reboot).

The indicated size is a maximum, the disk only uses as much memory as the files that are on it.


You can change /tmp to be a RAM disk. In the /etc/fstab line above, put /tmp rather than /ramdisk, then reboot.

The first time you reboot after changing /tmp to be a RAM disk, the files that were in /tmp will be hidden. That's harmless, except that they're wasting a little disk space. You can clean them up (after you've rebooted with /tmp on the RAM disk) by doing

mount --bind / /mnt
rm -r /mnt/tmp/* /mnt/tmp/.*
umount /mnt

The mount --bind command makes /mnt a duplicate view of your root filesystem; but while the RAM disk now obscures /tmp on the root view, nothing is obscuring /mnt/tmp.

ADDED: You can switch /tmp to a RAM disk without rebooting, it's just a little more complicated. Add the line to /etc/fstab as above, then run the following commands:

mkdir /tmp.old
mount --bind /tmp /tmp.old
mount /tmp
cd /tmp
ln -s /tmp.old/* /tmp/.* .

Then delete /tmp.old after your next reboot.

The reason you can't just move files from /tmp.old to /tmp is that some critical programs have files open in /tmp, for example /tmp/.X11-unix/X0 which the X server listens on and every GUI program opens when it starts. Moving a file to a different filesystem means copying it and deleting the old one, so you would end up with the X server still listening on /tmp.old/.X11-unix/X0 but X clients contacting /tmp/.X11-unix/X0 in vain. On a server, you might get away with a move if you're careful.

Gilles 'SO- stop being evil'
  • 69,786
  • 21
  • 137
  • 178
  • @giles: Is there a way to do this without change to fstab and rebooting? I'm assuming there's a corresponding mkfs method, followed by a mount but "man mkfs" wasn't very helpful. – hotei Aug 14 '10 at 16:30
  • 1
    @hotei: The reboot is only to switch `/tmp` over to `tmpfs`. It's not necessary, and I've edited my answer to explain how to do it, but it's more complicated. There's no `mkfs` involved because there's no underlying storage to prepare, the kernel handles everything. – Gilles 'SO- stop being evil' Aug 14 '10 at 18:43
  • @giles: Thanks for update. I was missing the part where "magic happens" during mount's reading of the the fstab line. I believe I understand why it works now. – hotei Aug 15 '10 at 16:12
  • Can I ask if this results in a noticeable speedup? – Robert Massaioli Oct 28 '10 at 21:55
  • @Robert: I think it did cause a noticeable speed up on my netbook's very slow SSD (mainly because syncs can last several seconds and `/tmp` on the disk increases the frequency of syncs), but this is an unreliable subjective impression. I've never tried to benchmark. – Gilles 'SO- stop being evil' Oct 28 '10 at 22:21