# Ramdisk init script

## delta407

On a whim (mostly), I wrote an init script for handling ramdrives. Basically, you tell it where you want it to make a ramdrive, and it will do all kinds of nifty things such as automatically save its contents and restore it as necessary. Anyway, here's the code:

/etc/init.d/ramdisk:

```
#!/sbin/runscript

# Written by delta407 (delta407@lerfjhax.com)

# Distributed under the terms of the GNU General Public License, v2 or later

depend() {

        need localmount

}

start() {

        mkdir -p ${RDPATH}

        for MP in ${RAMDISKS}; do

                MPN=`echo ${MP} | sed -e 's/\//-/g' -e 's/^-//g' -e 's/-$//g'`

                ebegin "Starting ramdisk on ${MP}"

                TARCMD="tar xpf${RDTAROPTS} ${RDPATH}/${MPN}${RDTARSUFFIX}"

                [[ -e ${RDPATH}/${MPN}${RDTARSUFFIX} ]] || TARCMD="true"

                touch ${RDPATH}/${MPN}${RDTARSUFFIX}

                mount ${MPN} ${MP} -t tmpfs && \

                        cd ${MP}/.. && \

                        `${TARCMD}`

                eend $?

        done

}

stop() {

        mkdir -p ${RDPATH}

        for MP in ${RAMDISKS}; do

                MPN=`echo ${MP} | sed -e 's/\//-/g' -e 's/^-//g' -e 's/-$//g'`

                ebegin "Stopping ramdisk on ${MP}"

                cd ${MP}/.. && \

                        tar cpf${RDTAROPTS} ${RDPATH}/${MPN}${RDTARSUFFIX} `basename ${MP}` && \

                        umount ${MP}

                eend $?

        done

}
```

/etc/conf.d/ramdisk:

```
# Config file for /etc/init.d/ramdisk

# Space-separated mountpoints that should be ramdisks

RAMDISKS="/mnt/rd"

# Path to store ramdisk tarballs in

# This will be created as necessary

RDPATH="/var/ramdisk"

# Extra flags to pass to tar

# Change this to "z" to gzip, "j" to bzip2, or empty for no compression

# Note that changing this will require modifying the file in RDPATH accordingly

#   (i.e. going from z to j you would gunzip then bzip2 the tar file, etc.)

RDTAROPTS="z"

# Tar file suffix

# If you enabled compression, you might want to make this .tar.gz or .tbz2 or something

RDTARSUFFIX=".tar.gz"

```

Think about this if you're running a rsync mirror, since rsync mtime()s the whole tree whenever someone connects. You can get some serious performance boosts out of it.

Potential problems, though, include loss of data since the last /etc/init.d/ramdisk restart if power is lost. (That's what happens, it's in RAM.)

Anyway, hope you find this useful.

----------

## SAckerman

Actually I am interested in your script...

I am trying to setup a system using a 512mb mem card with an IDE interface. The cards are not reccommended for continuous r/w so I thought I could boot from it then copy /, /bin, /sbin and /lib to a ramdisk and run /var, /usr and /opt from a hard disk. This is for a firewall/proxy machine. I think your script could probably be easily modified to do this... but I am not a very good scripter.

----------

## TuxFriend

Thanks for this script. I do have a few questions:

- I have tmpfs enabled in my fstab (doesn't seem to do anything though) is it that required or prohibited?

- Is ramdisk "user-friendly" when doing memory allocation, I mean does it give itself the lowest priority so other programs keep running or is it too "greedy" when allocating memory? Is the allocation static or dynamic?

- Do you want to submit this to bugzilla so it will be available in Gentoo? I think a lot of people would appreciate it.

TuxFriend

----------

## delta407

I have a few answers:  :Wink: 

- Just "rc-update add ramdisk boot" and there will be no need for an fstab entry. (It calls mount explicitly, so it will override your fstab either way.)

- The ramdisk acts just like any other tmpfs drive. I think it obeys the same swapping rules as user-mode programs -- i.e. it will act intelligently if you run out of RAM.

- I don't know where I would submit it to Bugzilla, and as such, I won't (at least not yet). I thought others would appreciate it; that's why I posted it here.

----------

## TuxFriend

Thanks for your quick reply.

Since you are in the mood for providing information, (hopefully) last question:

I saw in another post your tip "# mount portagetemp /var/tmp/portage/ -t tmpfs". Is this still required or is the "rc-update add ramdisk boot" sufficient?

TuxFriend

BTW. don't you sleep? It looks like you post 24/7

----------

## delta407

I do sleep, but my hours vary.  :Wink: 

Anyway, if you wanted /var/tmp/portage to be a ramdrive (tmpfs), just change or add '/var/tmp/portage' to the RAMDISKS variable.

----------

## delta407

 *SAckerman wrote:*   

> I am trying to setup a system using a 512mb mem card with an IDE interface. The cards are not reccommended for continuous r/w so I thought I could boot from it then copy /, /bin, /sbin and /lib to a ramdisk and run /var, /usr and /opt from a hard disk.

 

I thought about this a while, and I think you would be better off using an initrd. (Ask Google.)

----------

## arkane

There is documentation for initrd in the kernel documentation.

/usr/src/linux/Documentation/initrd.txt

Also documentation on ramdisk usage in there... same path, ramdisk.txt.

Great supplement to that script  :Smile: 

----------

## arkane

I just copied over the init.d and conf.d scripts, made a directory in /var called ramdisk, chmod'ed 755 /etc/init.d/ramdisk /etc/conf.d/ramdisk, and then tried executing /etc/init.d/ramdisk, but it gives me the error:

bash-2.05a# /etc/init.d/ramdisk start 

 * ERROR:  "/etc/init.d/ramdisk" has syntax errors in it; not executing...

(I copied/pasted to Kate, so the format was kept)

I looked at it, but from my eyes I can't see anything wrong....

----------

## pjp

```
tar cpf${RDTAROPTS} ${RDPATH}/${MPN}${RDTARSUFFIX} `basename ${MP}` && \
```

Is that all on one line?  In the code block from delta407's post, it looks like the && \ is on a seperate line.  I'm guessing browser width wrapped it.  Any other similar lines get wrapped for you?

----------

## delta407

 *arkane wrote:*   

> bash-2.05a# /etc/init.d/ramdisk start 
> 
>  * ERROR:  "/etc/init.d/ramdisk" has syntax errors in it; not executing...

 

See, that's one thing I detest about /sbin/runscript.sh. It blows up but doesn't tell you what's wrong.  :Wink: 

Does `bash /etc/init.d/ramdisk` and get a more descriptive message?

----------

## arkane

Nevermind...

It probably was the formatting.  I removed everything and copied/pasted through VI, now everything is working....

strange.

----------

## dreamer3

Along those same lines you might find this and this useful as well.

----------

