Ramdisk: How do you install and set up Ramdisk under Linux ? (CentOS 5) December 13, 2010Posted by tournasdimitrios1 in Linux.
What is a RAM disk? A RAM disk is a portion of RAM which is being used as if it were a disk drive. RAM disks have fixed sizes, and act like regular disk partitions. Access time is much faster for a RAM disk than for a real, physical disk. However, any data stored on a RAM disk is lost when the system is shut down or powered off. RAM disks can be a great place to store temporary data.
The Linux kernel version 2.4 and 2.6 has built-in support for ramdisks. Ramdisks are useful for a number of things, including:
- Working with the unencrypted data from encrypted documents
- Serving certain types of web content
- Mounting Loopback file systems (such as run-from-floppy/CD distributions)
In many embedded Linux systems, a Ramdisk is used to load initrd (initial Ram Disk ) , initrd is the final root file system. Don’t be surprised if you hear that your Linksys home router is operating on Linux .
Why did I write this document? Because I needed to setup a 80 MB ramdisk for viewing and creating encrypted documents. I did not want the unencrypted documents to be written to any physical media on my workstation. I also found it amazing that I could easily create a “virtual disk” in RAM that is much faster than my hard drive .
This document should take you step-by-step through the process of creating and using RAM disks.Red Hat based distros create 16 ramdisks by default, although they are not “active” or using any RAM. It lists devices ram0 – ram 15 . To check these block devices out, use the following command:
ls -l /dev/ram*
Now, grep through dmesg output to find out what size the ramdisks are:
dmesg | grep -i ramdisk
As you can see, the default ramdisk size is 16MB. I want a 80MB ramdisk, so the next step will be to configure Linux to use a larger ramdisk size during boot
- 1)Increase ramdisk size :
Ramdisk size is controlled by a command-line option that is passed to the kernel during boot. Since GRUB is the default bootloader for CentOs 5 , I will modify /etc/grub.conf with the new kernel option. The kernel option for ramdisk size is: ramdisk_size=xxxxx, where xxxxx is the size expressed in 1024-byte blocks. Here is what I will add to /etc/grub.conf to configure 80 MB ramdisks:
Once you save the file, you will need to reboot your system. After the reboot, a look at the dmesg output should confirm the change has taken effect:
dmesg | grep -i ramdisk
- 2)Format the ramdisk :
There is no need to format the ramdisk as a journaling file system, so we will simply use the ubiquitous ext2 file system. I only want to use one ramdisk, so I will only format /dev/ram0:
mke2fs -m 0 /dev/ram0
The -m 0 option keeps mke2fs from reserving any space on the file system for the root user, which is the default behavior. I want all of the ramdisk space available to a regular user for working with encrypted files.
- 3)Create a mount point and mount the ramdisk :
Now that you have formatted the ramdisk, you must create a mount point for it. Then you can mount your ramdisk and use it. We will use the directory /mnt/rd for this operation.
mount /dev/ram0 /mnt/ramdisc
Now verify the new ramdisk mount: mount |grep ram0 and df -h |grep ram0
You can even take a detailed look at the new ramdisk with the tune2fs command:
In my case, I need the user “john” to be able to read and write to the ramdisk, so I must change the ownership and permissions of the /mnt/rd directory:
chown john.john /mnt/ramdisc and chmod 770 /mnt/ramdisc
The ownership and permissions on the ramdisk filesystem/directory should be tailored to your particular needs.
Use the ramdisk :
Now that it has been created, you can copy, move, delete, edit, and list files on the ramdisk exactly as if they were on a physical disk partiton. This is a great place to view decrypted GPG or OpenSSL files, as well as a good place to create files that will be encrypted. After your host is powered down, all traces of files created on the ramdisk are gone.
To unmount the ramdisk, simply enter the following:
umount -v /mnt/ramdisc
If you remount the ramdisk, your data will still be there. Once memory has been allocated to the ramdisk, it is flagged so that the kernel will not try to reuse the memory later. Therefore, you cannot “reclaim” the RAM after you are done with using the ramdisk. For this reason, you will want to be careful not to allocate more memory to the ramdisk than is absolutely necessary. In my case, I am allocating < 10% of the physical RAM. You will have to tailor the ramdisk size to your needs. Of course, you can always free up the space with a reboot!
Automating ramdisk creation :
f you need to create and mount a ramdisk every time your system boots, you can automate the process by adding some commands to your /etc/rc.local init script. Here are the lines that I added:
# Formats, mounts, and sets permissions on my 80MB ramdisk /sbin/mke2fs -q -m 0 /dev/ram0 /bin/mount /dev/ram0 /mnt/rd /bin/chown john:john /mnt/rd /bin/chmod 0750 /mnt/rd