How to make a ram disk

Tony Harris
version 1.1, 23 Feb 2001
ram disk eenie-weenie HOWTO

If your root file system is on a device for which your kernel has no compiled-in driver, you will need to use lilo to load that driver as a module very early in the boot cycle. There are only two steps:
  1. make a ram disk image with mkinitrd
  2. modify lilo.conf to point to the image
First, I cd over to /boot:		 chain.b		  module-info-2.2.16-3ext3	 initrd-2.2.16-3.img	  vmlinux-2.2.16-3  vmlinux-2.2.16-3ext3
vmlinuz                  kernel.h		  
boot.b			 map			  vmlinuz-2.2.16-3
module-info		 vmlinuz-2.2.16-3ext3     module-info-2.2.16-3

Here you can see that I have a 2.2.16-3 kernel and I have added a second kernel with ext3 support (vmlinuz-2.2.16-3ext3). There is already a ram disk image for my first kernel (initrd-2.2.16-3.img)

To make a new image for the second kernel, I type the following (stuff I type is in bold):

/boot# mkinitrd initrd-2.2.16-3ext3.img 2.2.16-3ext3

mkinitrd is a shellscript that looks at the modules needed by my kernel, then makes an ext2 filesystem containing them.
If we look inside the image we see this is the case:
/boot# cat initrd-2.2.16-3ext3.img | gunzip > /tmp/myimage
/boot# file /tmp/myimage
/tmp/myimage: Linux/i386 ext2 filesystem
You do not have to look inside your image. Only making the image and modifying lilo.conf are necessary steps. However, discussion of the ramdisk image is provided for pedagogic purposes.

In order to look inside, I need to mount the image as though it were a filesystem:

/boot# mount /tmp/myimage /mnt/tmp -t ext2 -o loop=/dev/loop3
/boot# ls /mnt/tmp
bin  dev  etc  lib  linuxrc

/boot# find /mnt/tmp


The most important part of this ram disk image is aic7xxx.o, which is my scsi module.

Finally, I move on to the last step, modifying /etc/lilo.conf:

Here is my entry in lilo.conf that corresponds to the kernel and image I just created:
That's it. Run lilo as root and reboot.

If you have problems, check out the kernel HOWTO. There are a couple things you need to have covered: you need your kernel modules compiled and living in /lib/modules and you need to have an entry for each module in /etc/conf.modules.

Making a generic ram disk

  1. Make sure ram disk support is compiled in your kernel. If you don't have it, see the miniroot HOWTO for mounting via loopback.

  2. Create a ramdisk device if one does not already exist in /dev:

    /home/tony# mknod -m 660 /dev/ram b 1 1
    /home/tony# chown root.disk /dev/ram

  3. Zero out the space you need on the ramdisk device:

    /home/tony# dd if=/dev/zero of=/dev/ram bs=1k count=4k
    4096+0 records in
    4096+0 records out
  4. Make a file system on the ram disk, specifying the size:
    /home/tony# /sbin/mkfs -t ext2 -m 0 /dev/ram 4096
    mke2fs 1.18, 11-Nov-1999 for EXT2 FS 0.5b, 95/08/09
    Filesystem label=
    OS type: Linux
    Block size=1024 (log=0)
    Fragment size=1024 (log=0)
    1024 inodes, 4096 blocks
    204 blocks (4.98%) reserved for the super user
    First data block=1
    1 block group
    8192 blocks per group, 8192 fragments per group
    1024 inodes per group
    Writing inode tables: done                            
    Writing superblocks and filesystem accounting information: done
  5. Mount the ram disk as you would a regular device:
    /home/tony# mount -t ext2 /dev/ram /mnt/disk