- Using the RAM disk block device with Linux¶
- 2) Parameters¶
- 3) Using «rdev»¶
- 4) An Example of Creating a Compressed RAM Disk¶
- Changelog:¶
- How to Create a Ramdisk in Linux
- Tmpfs and Ramfs:
- How to Create a Ramdisk in Linux Using Tmpfs:
- SSD vs. Tmpfs:
- Creating a Ramdisk in Linux Using Ramfs:
- Tmpfs vs. Ramfs:
- Conclusion
- About the author
- David Adams
- How do I make a RAM disk?
- 3 Answers 3
Using the RAM disk block device with Linux¶
The RAM disk driver is a way to use main system memory as a block device. It is required for initrd, an initial filesystem used if you need to load modules in order to access the root filesystem (see Using the initial RAM disk (initrd) ). It can also be used for a temporary filesystem for crypto work, since the contents are erased on reboot.
The RAM disk dynamically grows as more space is required. It does this by using RAM from the buffer cache. The driver marks the buffers it is using as dirty so that the VM subsystem does not try to reclaim them later.
The RAM disk supports up to 16 RAM disks by default, and can be reconfigured to support an unlimited number of RAM disks (at your own risk). Just change the configuration symbol BLK_DEV_RAM_COUNT in the Block drivers config menu and (re)build the kernel.
To use RAM disk support with your system, run ‘./MAKEDEV ram’ from the /dev directory. RAM disks are all major number 1, and start with minor number 0 for /dev/ram0, etc. If used, modern kernels use /dev/ram0 for an initrd.
The new RAM disk also has the ability to load compressed RAM disk images, allowing one to squeeze more programs onto an average installation or rescue floppy disk.
2) Parameters¶
2a) Kernel Command Line Parameters
ramdisk_size=N
Size of the ramdisk.
This parameter tells the RAM disk driver to set up RAM disks of N k size. The default is 4096 (4 MB).
rd_nr
/dev/ramX devices created.
max_part
Maximum partition number.
rd_size
See ramdisk_size.
3) Using «rdev»¶
«rdev» is an obsolete, deprecated, antiquated utility that could be used to set the boot device in a Linux kernel image.
Instead of using rdev, just place the boot device information on the kernel command line and pass it to the kernel from the bootloader.
You can also pass arguments to the kernel by setting FDARGS in arch/x86/boot/Makefile and specify in initrd image by setting FDINITRD in arch/x86/boot/Makefile.
Some of the kernel command line boot options that may apply here are:
ramdisk_start=N ramdisk_size=M
If you make a boot disk that has LILO, then for the above, you would use:
append = "ramdisk_start=N ramdisk_size=M"
4) An Example of Creating a Compressed RAM Disk¶
To create a RAM disk image, you will need a spare block device to construct it on. This can be the RAM disk device itself, or an unused disk partition (such as an unmounted swap partition). For this example, we will use the RAM disk device, «/dev/ram0».
Note: This technique should not be done on a machine with less than 8 MB of RAM. If using a spare disk partition instead of /dev/ram0, then this restriction does not apply.
- Decide on the RAM disk size that you want. Say 2 MB for this example. Create it by writing to the RAM disk device. (This step is not currently required, but may be in the future.) It is wise to zero out the area (esp. for disks) so that maximal compression is achieved for the unused blocks of the image that you are about to create:
dd if=/dev/zero of=/dev/ram0 bs=1k count=2048
dd if=/dev/ram0 bs=1k count=2048 | gzip -v9 > /tmp/ram_image.gz
dd if=zImage of=/dev/fd0 bs=1k
dd if=/tmp/ram_image.gz of=/dev/fd0 bs=1k seek=400
That is it. You now have your boot/root compressed RAM disk floppy. Some users may wish to combine steps (d) and (f) by using a pipe.
Changelog:¶
Updated to reflect changes in command line options, remove obsolete references, general cleanup. James Nelson (james4765 @ gmail . com)
How to Create a Ramdisk in Linux
A ramdisk is a volatile storage space defined in the RAM memory. Using this feature increases file processing performance ten times over the best SSD hard disks. Implementing a ramdisk is very advantageous for users whose tasks require significant amounts of hardware resources. In addition, media editors and gamers can enjoy this implementation.
A ramdisk is a volatile space, all information stored in it will be lost if the device is turned off or reboots.
In Linux, ramdisks can be created using the command mount and the filesystems tmpfs and ramfs. This tutorial shows how to create a ramdisk in Linux using both of them.
Tmpfs and Ramfs:
Tmpfs: Tmpfs is a temporary file system stored in the RAM memory (and/or swap memory). By specifying this file system with the argument -t of the command mount, you can assign limited memory resources to a temporary file system. As a result, applications stored in this filesystem will perform several times faster than they would on conventional storage devices, including cssd devices.
Ramfs: Ramfs is similar to Tmpfs, but the user can’t ensure a limit, and the allocated resource grows dynamically. If the user doesn’t control the ramfs consumption, ramfs will keep using all the memory until hanging or crashing the system.
Tmpfs vs. Ramfs: There is no notable difference between the performance of tmpfs and its predecessor ramfs. The reason behind ramfs being replaced by tmpfs is the unlimited RAM consumption risk by ramfs which may lead to a system crash.
Another advantage of tmpfs over ramfs is the ability to use the swap space while ramfs are limited to hardware memory.
How to Create a Ramdisk in Linux Using Tmpfs:
First, let’s see the free memory we can use before creating a tmpfs mount point. To check the available ram and swap, you can use the command free. To print the results in gigabytes, you can add the argument –giga, as shown in the example below:
As you can see in the output above, I have two physical GB and two on the swap space.
Now, create a mount point under the directory /mnt using the command mkdir as shown in the example below. The mount point name choice is arbitrary. If you are going to use the ramdisk for a specific application, you can name the mount point after it. In the example below I call it /mnt/tmp:
Now you can create the ramdisk using the mount command. The following example shows how to create a ramdisk using tmpfs on 2GB Ram/Swap, on the mount point /mnt/tmp.
The -t (type) argument allows to specify the file system (in this case, tmpfs). The -o (options) argument is used to define the space for the ramdisk.
The ramdisk was created under /mnt/tmp.
SSD vs. Tmpfs:
I copied an Ubuntu image from a user’s home directory to the root directory in the following screenshot.
Using the command time to display timing, you can see the copying process took 0:55.290s
In the following screenshot, you can see how copying the same Ubuntu iso image to the ramdisk takes 0:9.424s:
As you can see, the difference is titanic, and the ramdisk is very advantageous for tasks with large amounts of file writing.
To remove the ramdisk, just unmount it by running the following command and replacing tmp with your mount point:
Creating a Ramdisk in Linux Using Ramfs:
The procedure to create a ramdisk using ramfs is the same as with tmpfs. The following command will create a dynamic ramdisk on the mount point /mnt/tmp.
Tmpfs vs. Ramfs:
Now let’s test the ramfs performance against tmpfs, and let’s see what happens when each ramdisk type reaches the defined limit.
In the first example, I will create a 2GB ramdisk using tmpfs, and I will try to copy a bigger iso inside:
As you can see, the cp returned an error because the ramdisk space isn’t enough for the iso image. I only assigned 2GB for the ramdisk.
Now, see what happens when I do the same procedure using ramdisk:
As you can see, the ramfs kept writing into /mnt/tmp even though I have defined a 2GB limit. This is ramfs disadvantage because it may hang a system by consuming all its RAM memory. On the contrary, tmpfs is limited to the memory amount we define.
You can also see in the output that the copying task was done within 0:9.624s, almost the same performance shown by tmpfs in the test against SSD.
Note: The same iso image was used.
Conclusion
Creating a ramdisk is a one-minute process with significant benefits for any user who needs to process big files. The reading and writing speed increase exponentially over the best hard disks in the market. Portable software can be executed from a ramdisk, though changes won’t be persistent. This implementation is being highly appreciated by media editors whose tasks require long periods of media conversion.
Using ramfs may be risky if the system runs out of resources. That’s why tmpfs became the first method.
I hope this tutorial to create a ramdisk in Linux was useful. Keep following Linux Hint for more Linux tips and tutorials.
About the author
David Adams
David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.
How do I make a RAM disk?
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 I have created 1 GB of partition in ram . using Primo RamDisk Is there any good Alternative in Ubuntu ?
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.
3 Answers 3
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.
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.
@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.
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.
chovy add it to /etc/rc.local and it will automatically mount the partition. but the data will be lost.
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