Search code examples
minixramdisk

Create a custom RAM disk in MINIX


So I am doing a project that requires me to create a RAM disk in MINIX. The question is as follows:

Your task is to implement your own RAM disk which can be used as a location to store data where fast access is required. It should have read and write permissions for everybody and must be 10MB in size.

I am struggling to find info on this topic for MINIX, so I've been trying to work off numerous other tutorials that are for Linux distros.

Using this tutorial I have done the following in MINIX:

mknod /dev/ram0 c 0 1
chmod 777 /dev/ram0

Then mkfs -b 10240 -i 2000 -B 1024 /dev/ram0 yields the error:

mkfs: /dev/ram0: number of blocks too large for device.

This occurs even when I make the -b parameter 1. When I type mkfs /dev/ram0 an error stating the following appears:

mkfs: this device can't hold a filesystem.

In the tutorial I can see the author increasing the size of the ramdisk to 16GB in grub.conf, but that file isn't located in /etc.

ANY help would be appreciated as I am struggling to find info on MINIX in general with tasks like this.


Solution

  • I figured it out:

    On line 43 of

    /usr/src/include/minix/dmap.h

    add #define FAST_DEV 6. Now we have a symbol to represent the minor of our new device. This simply helps us avoid magic numbers.

    m_ioctl() in

    /usr/src/drivers/memory/memory.c

    is hardcoded to receive a message and create the RAM device. To make it generic change RAM_DEV (look through the function, it is in there as a parameter to some function) to m_ptr->DEVICE. RAM_DEV is the minor device number of the RAM device and m_ptr->DEVICE is the minor device number that the request wants to be created (it will make sense in a while). Also, on line 28 of this file you need to increment the value of NR_DEVS to allow the program to be able to create the new device we are going to specify now. Then around line 143 in the m_transfer() function there is a switch on m_device for case RAM_DEV, KMEM_DEV and BOOT_DEV, add case FAST_DEV underneath BOOT_DEV. This will allow the OS to transfer files to/from our new device in the same way as it does for RAM_DEV.

    In

    /usr/src/servers/fs/main.c

    you will see that main() calls fs_init() which in turns call load_ram(). In load_ram() is where the message (that is received in m_ioctl()) is constructed and sent. To create a message for our new device add the following to the beginning of the fucntion:

    m_out.m_type = DEV_IOCTL;
    m_out.PROC_NR = FS_PROC_NR;
    m_out.DEVICE = FAST_DEV; /* minor of fast device, this is why we had to make m_ioctl() generic */
    m_out.REQUEST = MIOCRAMSIZE;
    m_out.POSITION = 10485760 /* size of 10MB in bytes */
    s = sendrec(MEM_PROCNR, &m_out); /* this sends the message */
    

    Now recompile:

    cd /usr/src
    make world
    make install
    and make all the directories that you worked in (just to be safe)
    then shutdown
    

    Create fast device:

    mknod /dev/fast b 1 6
    

    EDIT:

    Clarification for load_ram():

    PRIVATE void load_ram(void)
    {
        register struct buf *bp, *bp1;
        ...
        ...
        int s;
    
        /* add the code here */
        m_out.m_type = DEV_IOCTL;
        etc
    }
    

    Clarification for the switch statement:

    case RAM_DEV:
    case KMEM_DEV:
    case BOOT_DEV:
    case FAST_DEV: /* add this line */