How to create a ramdisk, or tmpfs instance

ram disk is a portion of memory which being used as if it is a disk drive.
ram disks have fixed size, and act like a partition disk.
The access to ram disk is much faster than a real physical disk, however all contenent
of the ram disk will be lost when the system is power off.

ram disk can serve:
* some type of web contenent
* mounting loopback filesystem

You can check ram disk already created on your system, on /dev/ram*,
by default debian create 16 ram disks.
$ ls -l /dev/ram*

We will try to create a 16MB ramdisk.

First start creating a block device with major and minor number equal to 1.
$ sudo mknod -m 660 /dev/ram b 1 1

Secondly, zero out the space you need on ramdisk device
$ sudo dd if=/dev/zero of=/dev/ram bs=1K count=16K

Later, make filesystem on ram disk, specifying the size.
$ sudo /sbin/mkfs -t ext2 -m 0 /dev/ram 16384

Lastly, mount the ram disk
$ sudo mkdir /mnt/ramdisk
$ sudo mount -t ext2 /dev/ram /mnt/ramdisk

You can even take a detailed look at the new ramdisk with the tune2fs command:
$ sudo tune2fs -l /dev/ram

If you need to automating creation of ramdisk on boot, add these lines above
to the local init file rc.local.

Inconvenient of ramdisks:
– simulation of a physical disk device with a fix size.
– needs to filesystem driver (ext2) to format and interpret this data
– multiple memory operation copy, fake block device into the page cache (and copying changes back out)
– ramdisks cannot swap and you do not have the possibility to resize them.

ramfs is a very simple filesystem that exports Linux’s disk caching
mechanisms (the page cache and dentry cache) as a dynamically resizable
RAM-based filesystem.

A ramfs derivative called tmpfs was created to add size limits, and the
ability to write the data to swap space.
Normal users can be allowed write access to tmpfs mounts.
Everything in tmpfs is temporary in the sense that no file will be created on
you hard drive.

See Documentation/filesystems/tmpfs.txt for more information.

For create a tmpfs instance accessible only by root, with 2M of size
$ mkdir tmpfs
$ sudo mount -t tmpfs -o size=2048K,mode=700 tmpfs tmpfs/
if you need to resize the tmpfs that is already mounted :
$ sudo mount -o remount -o size=4096k tmpfs/


Author: Ali MEZGANI

My name is MEZGANI Ali. I was born back in 1978 in Rabat Morocco. My interests are Debian Linux , programming , science and music.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s