The first ramdisk image

Now it's time to make a ramdisk image, so we can test our fresh build stuff.
My ramdisk image is 8 MB in size, and i don't really want it to get any bigger than that.

First you need an empty file of 8 MB, you can create that with dd, i used:
dd if=/dev/zero of=initrd_router bs=1024 count=8k
BS is the block size (1024 bytes= 1 KB), and count is the number of blocks, and 8192 times 1024 bytes is 8 MB.
Now you have 8 megs of zero's. To use the image, it must be formatted. For such small filesystems, i think ext2 is the best choice. Just type:
mke2fs initrd_router
I needed to be root for this. Mkfs also asked me if it should procees, because the file is not a block special device. You can answer y savely.

So now you have a empty filesystem, which must be mounted before we use it. Ever tried to mount a file ? You won't succeed without any special options. After you've made a mountpoint do this:
mount /initrd_router -o loop
This will mount your little filesystem. And don't forget to unmount at the end !

Now you can put some files on this filesystem. Lets start with this:
Be sure to use cp -ap when because some are symbolic links. If you don't use this, the file where the link points to is copied instead of the link itself, resulting in too much data in you /lib directory.
When i check my disk usage, with df, i see that i'm allready using 35% of my 8 MB file ...

Now the libraries need to be configured. The configuration file is in /etc and is called ld.so.conf, just create an empty file for this with touch. Then run ldconfig -r , this will create ld.so.cache for you.

Now init needs some configuration too, this is the smallest i could think of:
id:3:initdefault:
 
si::bootwait:/bin/bash
 
l3:3:wait:/bin/bash
 
3:3:respawn:/bin/bash
It won't do anything usefull, except running bash ... running bash again .... did i allready tell you it's gonna run bash ?

The last thing to must do it create some devices. The command for that is mknod.
This command will need a few parameters, like the type of device, and major/minor number of the driver. These are the commands i used:
  • mknod console c 5 1
  • mknod initrd b 1 250
  • mknod ram0 b 1 0
  • mknod ram0 b 1 1
  • mknod ram0 b 1 2
  • mknod ram0 b 1 3
  • mknod ram0 b 1 4
  • mknod ram0 b 1 5
  • mknod ram0 b 1 6
  • mknod ram0 b 1 7
  • ln -s ram0 ramdisk
  • ln -s ram ram0
  • chown root:disk ram[0-7] initrd

Now i want to give booting a try, so i unmounted the image, and copy the compiled kernel and the image to the /boot directory. My router PC now uses grub as bootloader. So i had to edit the /boot/grub/menu.lst
I added this:
title router
 kernel (hd0,0)/boot/vmlinuz-2.6.4 root=/dev/ram0
 initrd (hd0.0)/boot/inird_router
And then run grub-install /dev/hda to install the bootloader.

I you boot this, and everything is correct, you will see the kernel booting, then a message from init, and a bash prompt.
There is not much you can do with it. even ls won't work. As a workaround for now, you can echo echo * which will show you the files. To navigate trough the filesystem you can use cd.

If you want to reboot, you can savely just reset the box, that's one of the advantages of a ramdisk based system.

Back