ramdisk Creation and its benefits

Create ramdisk in Linux lets you directly store files in physical memory as if it was on your hard drive. RAM disks use the normal RAM in main memory as if it were a partition on a hard drive rather than actually accessing the data bus normally used for secondary storage such as hard disk.

 See below 20GB of the RAM memory has been used for RAM memory.

Output of df -h and free memory usage/availability #

[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 1016G 41G 925G 5% /
/dev/sda2 508M 37M 447M 8% /boot
/dev/sda5 5.0G 33M 5.0G 1% /tmp
/dev/sda6 1.8T 197M 1.7T 1% /home
/dev/ram0 20G 15G 3.7G 81% /opt/bglens/ramcache

[[email protected]~]# free -g total used free shared buffers cached

Mem: 62 51 10 0 0 29 -/+ buffers/

cache: 21 41 Swap: 7 0 7

Advantages of saving files in a ramdisk, rather than in a HD is accessing memory is faster than accessing a HD. (HD = HardDrive).

Major disadvantage is, since RAM memory is volatile, you will lose the data, when you reboot the system.

Below are the steps to create ramdisk of 20G in the above server.

Please make sure that there is a minimum of 24 GB memory in the server to give 20G as ramdisk. Here in above situation server had 64G.

Commands to create RAMDisk

mkfs -q -i 8192 /dev/ram0 20971520
mount /dev/ram0 /opt/bglens/ramcache
cp -rf /opt/bglens/data/posting.local/* /opt/bglens/ramcache

Here, there is a high chance that whenever you type the mkfs command as in above , you may get an output/error similar to below

Error for mkfs

mkfs.ext2: Filesystem larger than apparent device size
Proceed anyway? (y,n)

 If you give ‘n’, it will exit and you cant proceed with second step. But even if you say ‘y’, you won’t create it.

In such case, you would need to edit the boot loader configuration (usually grub.conf) and append the ‘ramdisk_size=20971520‘ to the kernel line. And reboot it. Here is a sample grub.conf

title CentOS (2.6.32-279.19.1.el6.x86_64)
root (hd0,1)
kernel /boot/vmlinuz-2.6.32-279.19.1.el6.x86_64 ro root=UUID=88244188-2787-468e-804a-b018c0d527eb rd_NO_LUKS rd_NO_DM nomodeset ramdisk_size=20971520
initrd /boot/initramfs-2.6.32-279.19.1.el6.x86_64.i

extra information #

Also, please note that if we unmount the ram disk, it will still reserve RAM and won’t release it for system’s normal use until you reboot the system. It will get flagged so that the Linux kernel will not try to reuse the memory again. If you remount the ramdisk, your data will still be there and that RAM space, cannot be used by other application/systems

To use memory as storage and still allow the kernel to reuse the memory, you can use ramfs or the newer tmpfs. This is another option to ramdisk. It allocate memory dynamically and by allowing less-used pages to be moved onto swap space.

Example of a tmpfs mounting a directory

mount -t tmpfs tmpfs /dir

if you don’t fill up the space in the tmpfs, the kernel gets to use the free RAM for buffers and cache. If you delete a file, the kernel will reclaim the ram, and if you umount the directory, all data is lost.

Powered by BetterDocs