The ramdisk device driver provides a block
interface to memory. A ramdisk can be used like any other
block device, including making it into a filesystem using
mkfs(ADM).
Up to eight ramdisks may be created.
The characteristics of a ramdisk file are determined by its
minor device number. The bits in the minor device number
encode its size, longevity, and which of the eight
possible ramdisks it is.
The three low-order bits of the minor device number
determine the ramdisk number.
The next four bits of the minor device number determine the
size of the ramdisk. There are 16 possible ramdisk sizes
from 16KB to 512MB.
The possible settings of the size bits and the
corresponding ramdisk sizes are shown in the following
table.
Size bit settings
Size bits
Value
Ramdisk size
0
0
0
0
0
16KB
0
0
0
1
8
32KB
0
0
1
0
16
64KB
0
0
1
1
24
128KB
0
1
0
0
32
256KB
0
1
0
1
40
512KB
0
1
1
0
48
1MB
0
1
1
1
56
2MB
1
0
0
0
64
4MB
1
0
0
1
72
8MB
1
0
1
0
80
16MB
1
0
1
1
88
32MB
1
1
0
0
96
64MB
1
1
0
1
104
128MB
1
1
1
0
112
256MB
1
1
1
1
120
512MB
The high-order bit is a longevity indicator. If set (value
128), memory is permanently allocated to that ramdisk, and
can be deallocated only by rebooting the system. Permanent
ramdisks can only be allocated by the superuser. However,
once a permanent ramdisk is allocated (by opening it), it
can be read and written by anyone having the appropriate
permissions on the ramdisk inode.
If the high-order bit is clear (value 0), the ramdisk is
temporary and will be deallocated when no processes have it
open. An unmounted ramdisk will be deallocated immediately
when it is closed. To create an easily removable, but
semi-permanent ramdisk, use a separate process to keep the
device open for as long as necessary.
The following table shows examples of how the minor device
number is constructed:
Examples of minor device number construction
Description
Longevity
Size
RAM
Minor
disk number
device
number
16KB (#1) (Temporary)
0
0
0
0
0
0
0
1
1
16KB (#1) (Permanent)
1
0
0
0
0
0
0
1
129
64KB (#0) (Temporary)
0
0
0
1
0
0
0
0
16
512KB (#7) (Permanent)
1
0
1
0
1
1
1
1
175
Only one special
device file (/dev/ram00 corresponding to a
temporary 16KB ramdisk) is created when the system is
installed. All ramdisks created by the system
administrator
share the same major device number as
/dev/ram00.
The major device number to be used when creating ramdisks
can be found by entering:
ls -l /dev/ram00 | awk '{print $5}' | sed 's/,//'.
To create a ramdisk, follow these steps:
Create the device node.
First create the device that the ramdisk
will reside on. Use the mknod command:
mknoddevice_name [b|c] 31minor_device_number
``b'' is for blocked devices and is the one you will use.
``31'' is the major number for this type of device. The
minor number is the sum of the values of three attributes
representing longevity, size, and ramdisk number.
For example, to create a 64KB permanent ramdisk, the
minor numbers 144 to 151 represent ramdisk numbers 0 to 7.
If the disk number was 1, the mknod command would
be:
mknod /dev/ram145 b 31 145
Make a filesystem.
Use mkfs to create a filesystem on the ramdisk
(see step 3 for details of how to create a filesystem on a
temporary ramdisk):
The physical block size is 512 bytes so two blocks need to be
specified for each 1KB of filesystem.
In this example, the command to create a 64KB filesystem would be:
mkfs /dev/ram145 128
Mount the filesystem.
Use
mount(ADM)
to mount the selected device on the specified mountpoint:
mountdevice_namemount_point
In order to mount the example 64KB ramdisk on /mnt,
the command would be:
mount /dev/ram145 /mnt
To make a filesystem on a temporary ramdisk, the device file must be
held open between the mkfs and the mount operations.
Otherwise, the ramdisk is allocated at the start of the mkfs
command, and deallocated at its end, before it can be mounted. Once the
ramdisk is mounted, it is open until it is unmounted.
The following shell fragment shows one method of creating
and mounting a filesystem on a non-permanent 512KB
ramdisk:
The procedure is executed in a sub-shell taking its
standard input from /dev/ram40 in order to keep
/dev/ram40 open between its creation and the time
at which it is mounted.
Limitations
A maximum of eight ramdisks are available.
Two different sized ramdisks may not share the same ramdisk
number.
Ramdisks must occupy contiguous memory.
If free memory is fragmented, opening a ramdisk may fail even though
there is enough total memory available.
Ideally, all ramdisks should be allocated at system startup.
This helps prevent the ramdisks themselves from fragmenting memory.
Ramdisks are geared towards use in specialized applications.
In many cases, you will notice a decrease in system performance
when ramdisks are used, because UNIX can generally put the memory
to better use elsewhere.