Monday, January 21, 2008

HowTo: Create Vanishing Virtual Drive

You can work with your hard drives, network drives and other external or USB storage devices accomplishing critical linux hacks. Yes, technology nowadays have enabled us to make use of larger and more stable storage capacity drives and devices. Most firms and company services enjoy the benefits of having them as part of their IT infrastructure. What's good of having them are your data is still there between system reboots.

Here's a quick entry on how to create Read Access Memory (RAM) disk from Fedora.

What is RAM Disk
A RAM disk is a portion of Read Access Memory (RAM) which is temporarily used as if it were a disk drive. RAM disks have fixed sizes, and can be mounted like regular disk partitions. RAM disks can be a great place to store temporary data for temporary task.

Additionally, one advantage of having RAM disk is that access time with RAM disk is much faster than for physical disk. However, RAM disk is volatile type of disk. Any data stored on a RAM disk will be lost after shutting down the system or powering off. Another issue of having RAM disk is that RAM memory allocated as RAM disk would no longer be available for application's usage from system's overall memory capacity.

Creating RAM Disk

By default, kernel 2.4.x and kernel 2.6.x supports RAM disk. Fedora by default supports 16 RAM disks from ram0 to ram15 as shown below and assigns them with default 16MB RAM of disk size when mounted.

# ls -la /dev/ram*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lrwxrwxrwx 1 root root 4 2008-01-21 07:56 /dev/ram -> ram1
brw-r----- 1 root disk 1, 0 2008-01-21 12:02 /dev/ram0
brw-r----- 1 root disk 1, 1 2008-01-21 07:56 /dev/ram1
brw-r----- 1 root disk 1, 10 2008-01-21 07:56 /dev/ram10
brw-r----- 1 root disk 1, 11 2008-01-21 07:56 /dev/ram11
brw-r----- 1 root disk 1, 12 2008-01-21 07:56 /dev/ram12
brw-r----- 1 root disk 1, 13 2008-01-21 07:56 /dev/ram13
brw-r----- 1 root disk 1, 14 2008-01-21 07:56 /dev/ram14
brw-r----- 1 root disk 1, 15 2008-01-21 07:56 /dev/ram15
brw-r----- 1 root disk 1, 2 2008-01-21 07:56 /dev/ram2
brw-r----- 1 root disk 1, 3 2008-01-21 07:56 /dev/ram3
brw-r----- 1 root disk 1, 4 2008-01-21 07:56 /dev/ram4
brw-r----- 1 root disk 1, 5 2008-01-21 07:56 /dev/ram5
brw-r----- 1 root disk 1, 6 2008-01-21 07:56 /dev/ram6
brw-r----- 1 root disk 1, 7 2008-01-21 07:56 /dev/ram7
brw-r----- 1 root disk 1, 8 2008-01-21 07:56 /dev/ram8
brw-r----- 1 root disk 1, 9 2008-01-21 07:56 /dev/ram9
lrwxrwxrwx 1 root root 4 2008-01-21 07:56 /dev/ramdisk -> ram0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can check if your current linux kernel supports RAM by issuing

# dmesg |grep RAM

You should be seeing similar lines like this
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
BIOS-provided physical RAM map:
RAMDISK driver initialized: 16 RAM disks of 16384K size 4096 blocksize
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Format RAM Disk for Initialization
Now, let us start using a single RAM disk and format it.

# mkfs.ext3 /dev/ram0

Alternatively, RAM disk creation without a journalized file system would be

# mke2fs -m 0 /dev/ram0

Mounting RAM Disk
Create a temporary mounting point
# mkdir /mnt/ram

Mount RAM disk
# mount /dev/ram0 /mnt/ram

Operate on RAM disk such as copying file
# cd /mnt/ram
# cp /bin/ls .

Congratulations! You have just created a single 16MB RAM disk which is only available from your current CPU session. This RAM disk would vanish when you shutdown or reboot your system.

If you wish to take a look more of your RAM disk details, simply

# tune2fs -l /dev/ram0

Change Default RAM Size

If you wish to overwrite linux default RAM disk size, you can pass RAM size parameter to linux kernel during reboot. This can be accomplished by editing your /etc/grub.conf and append the below line to the kernel parameter

ramdisk_size=4000

Well, a linux tip to make your RAM disk allocation permanent is to include it on one of your startup script or to /etc/rc.local. This approach would mount RAM disk permanently between reboots though the old data would not be there.

Hope you find this interesting, enjoy.

Related Article:
Mounting and Burning ISO Images, Burning DVD/CD Images
HowTo: Add New HardDisk to Linux
Optimize Your HardDisk Read Performance
HowTo: NTFS Drive to Linux
TestDisk - Linux Partitioning Tool

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy