hugo3
Posts: 4
Joined: Thu Jan 10, 2013 5:17 pm

Creating a RAM disk at startup

Thu Jan 10, 2013 5:37 pm

Dear all,

I am trying to create a RAMdisk to be reserved and mounted at startup. In some forums, I found the hint to
1) add ramdisk_size=65535 at the end of /boot/cmdline.txt
2) create a ramdisk initialisiation script in etc/init.d (see below)
3) set it to executable with "chmod 755 ramdisk" and
4) register it with "update-rc.d ramdisk defaults".

All done. But when I reboot, no ramdisk is created. And when I call, for instance, "invoke-rc.d ramdisk start", I get an error
/etc/init.d/ramdisk: 15: /etc/init.d/ramdisk: Syntax error: word unexpected(expecting ")")

Your help is really really appreciated and needed!

Thanks,
Stefan

#!/bin/sh
### BEGIN INIT INFO
# Provides: A ramdisk to save intermittent data
# Required-Start:
# Required-Stop:
# X-Start-Before:
# X-Stop-After:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: create a ramdisk
# Description: create a ramdisk with 64MB and mount it to /media/ram0
### END INIT INFO
#
case "$1" in
    start)
        if [ ! -e /media/ram0 ]; then
                mkdir /media/ram0
        fi
        /sbin/mke2fs -m 0 /dev/ram0
        /bin/mount /dev/ram0 /media/ram0
;;
    stop)
/bin/umount -v /media/ram0
;;
restart)
/bin/mount /dev/ram0 /media/ram0
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac
exit 0

User avatar
joan
Posts: 14079
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Creating a RAM disk at startup

Thu Jan 10, 2013 5:52 pm

I don't do it from boot as I don't always want a RAM disk.

I have an executable file called tmpfs in /usr/local/bin which contains

Code: Select all

#!/bin/bash
sudo mkdir -p /ram
sudo mount -t tmpfs -o size=100m tmpfs /ram

hugo3
Posts: 4
Joined: Thu Jan 10, 2013 5:17 pm

Re: Creating a RAM disk at startup

Fri Jan 11, 2013 2:26 pm

My intention is to use the Raspberry Pi far data logging and record values every 10 minutes. As I am afraid that wrting 525600 values to SDCard in 10 years would destroy the card, I want to use RAM instead for intermediate storage. I read that with tmpfs the system might start swapping at some point, hence I prefer using a ramdisk instead.
Data will be written to FTP Server once a day. Only if the system is shut down during a day, data is copied to SDCard; and back into ramdisk at the next startup.

danmason
Posts: 2
Joined: Fri Mar 08, 2013 3:57 am

Re: Creating a RAM disk at startup

Fri Mar 08, 2013 4:02 am

hugo your script is solid, you just have one small problem on line 29. in your ambition to have a usage help message you accidentally used an erroneous dollar sign.

you put this: echo $"Usage: $0 {start|stop|restart}"

when you really want this: echo "Usage: $0 {start|stop|restart}"

One you get rid of that the script works as you would expect.

influenza [~] > ./foo
Usage: ./foo {start|stop|restart}
influenza [~] > ./foo start
mkdir /media/ram0
/sbin/mke2fs -m 0 /dev/ram0
/bin/mount /dev/ram0 /media/ram0
influenza [~] > ./foo stop
/bin/umount -v /media/ram0
influenza [~] > ./foo restart
/bin/mount /dev/ram0 /media/ram0

danmason
Posts: 2
Joined: Fri Mar 08, 2013 3:57 am

Re: Creating a RAM disk at startup

Fri Mar 08, 2013 4:09 am

guess I just removed the extra $ and it ran without a problem but yeah different error on a different line. good luck man.
Last edited by danmason on Tue Mar 12, 2013 6:57 am, edited 1 time in total.

User avatar
jojopi
Posts: 3078
Joined: Tue Oct 11, 2011 8:38 pm

Re: Creating a RAM disk at startup

Fri Mar 08, 2013 5:25 am

In bash the extra dollar sign is used to mark a string for localization. That does not work in sh, but it harmlessly gets included in the usage message instead. It is obviously not the cause of the syntax error fourteen lines above.

The syntax error must be caused by something invisible. Possibly a non-breaking space somewhere to the left of the word start.

A ramdisk is not the best solution. It needs to be formatted, which means there is useless filesystem metadata stored in ram in addition to the dentry cache, and extra overhead. A tmpfs instead uses the page and dentry caches as the filesystem, and will waste less memory.

There is also ramfs, which is like tmpfs but not swappable. But ramfs has no size, so it is your responsibility not to fill the memory.

Actually, I think you do want the data to be swappable. If you allow the kernel to swap when it has to, it will tend to swap the data that is least recently used. If you tell it a big chunk of memory can never be swapped, it may be forced to swap more active data instead.

Return to “Beginners”