# Ram Drive for loading programs fast :)  (demoing Phoenix)

## kappax

Ok, here is how to creat a ram drive at boot and copy the programs you want to it, so thoes programs load fast every time  :Smile:   I will be showing how to do this with Phoenix. This does take ram, but only 60 megs ram for Phoenix to start fast every time.  

    Some of you might say not worth it or what not, but that is up to you, My sytem is not that fast with IO and does not do good at getting my programs open fast uder High IO, even if it is cached, you dont know for sure that it will be cached every time you open it. So i am going to use 60 megs to make a ram drive so Phoenix loads from ram every time it is called. 

Step 1. 

    You are going to need ram drive support in your kernel, this is under block devices. If you dont want to recomile your kernel just ad it as a module then do a "make modules"   then.

```

# cp /usr/src/linux/drivers/block/rd.o /lib/modules/`uname -r`/kernel/drivers/block/

# depmod -a

```

Now that you have ramdisk support  we can get on to making the ram drive  :Smile: 

Step 2

    Make the RamDrive.

To make the ram drive we will use dd to clean out some space in the ram (sorry not sure all the teck terms but it works )  :Smile: 

The below code will set us up to have a 60 meg ram drive.

```

# dd if=/dev/zero of=/dev/ram0 bs=1k count=61440

```

Now we have a clean sopt of 60 megs in the ram.

Step 3

    Formating the space with a FS.

Now you can use any FS you want, but i am using ext2.

```

# mke2fs -vm0 /dev/ram0  61440

```

It is very inportant that you tell it the size, because it will not work right  and is a pain to even get mounted.

Step 4

   Mounting the drive.

Mounting the drive is not biggy  :Smile: 

You can mount it where ever you want, i used /speeddrive

```

# mount /dev/ram0 /speeddrive

```

Step 5

    Setting up phoenix to run from the ram drive.

Well i had installed phoenix to /usr/lib/phoenix So i need to move the files out of the way so i can my a symlink to that area from the ram drive. i moved it from /usr/lib/phoenix to /usr/phoenix.

```

# mv /usr/lib/phoenix /usr/

```

Now i need to copy the stuff to the ram drive so it is run from ram.

```

# cp -R /usr/phoenix /speeddrive

```

Now that we have phoenix on the ram drive we need to make a symlink to it to its orignal place. 

```

# ln -s /speeddrive/phoenix /usr/lib/

```

Now you are done!, run phoenix the way you would have normaly. 

Notes:

    You only have to make the symlink one time, just make sure you use a script to make the ram disk and copy the files over at boot.

    Add rd to the moudles.autoload

I have made a script to do this at boot, i put it in my local start. 

```

#!/bin/bash

echo -e "       *Zeroing out Ramdrive                  %10"

dd if=/dev/zero of=/dev/ram0 bs=1k count=61440 &> /dev/null

echo -e "        *Formating Ramdrive ext2              %10"

mke2fs -vm0 /dev/ram0  61440 &> /dev/null

echo -e "        *Mounting Ramdrive to /speeddrive      %05"

mount /dev/ram0 /speeddrive &> /dev/null

echo -e "       *Copying files to Ramdrive             %75"

cp -R /usr/phoenix /speeddrive/ &> /dev/null 

```

Hope this helps some ppl out. Remeber You can do this on the fly at boot, and you can use this for GAMES (such as the ut demo on the fly, i do think the live cd uses tempfs thoe) 

If someting is unclear or needs to be re explaned better just ask and i willd o my best  :Smile: 

Have fun.

----------

## dreamer3

Sorry kappax, not really meaning to one-up you, but you got me interested in doing this without the fuss of a ramdisk and it's hassles...

This script uses the tmpfs file system (which every Gentoo user should already have compiled into their kernel).

Simply modify RD to be the directory you would like your ramdrive mounted to and change COPY_DIRS to be a list of the directories you would like copied and run/accessed from your ramdisk.

Notes:

1. this uses the --bind option of the mount command to avoid messy symlinks and the need to move things around on your real file system.  You will need a 2.4.x kernal though, also shouldn't be a problem for most Gentoo users.2. Should be common sense, but don't emerge or re-emerge an application that you are running from a ramdisk, as this won't really update your filesystem, but only the ramdisk.3. To undo this and revert back to normal just unmount the ramdisk directory and eash of the directories you copied one by one (example):

```

unmount ramdisk_folder

umount /usr/share/phoenix

umount /some/other/directory

```

4. If you find this useful, let me know and I will turn it into more of a general command line tool, but keep it's current functionality as well.

If something isn't self explanatory, let me know.  Script works fine for me.

ramit - bash script

```

#!/bin/bash

# RD = location ramdisk is to be mounted too

# DO NOT SET TO ROOT / !!!!!

RD=/.rawspeed

# check to make sure we aren't about to be foolish

if [ "${RD}" == "/" ]; then

        echo "WARNING: Your ramdrive cannot be mounted at ${RD} ...";

        exit;

fi

# check for the existence of our speed up directory

# if not found, create

if [ ! -d ${RD} ]; then

        mkdir ${RD};

fi

# check to see if we've already mounted a tmpfs there

# if not, go ahead and mount

MOUNTED=`mount | grep ${RD}`

if [ -z "${MOUNTED}" ]; then

        mount tmpfs ${RD} -t tmpfs

fi

# specify directories you would like copied to the ramdisk

COPY_DIRS="/usr/share/phoenix

        /some/path

        /some/other/path/etc"

# copy directories to the ramdisk one by one

for COPY in ${COPY_DIRS};

do

        echo "Copying [${COPY}]..."

        rm -rf ${RD}${COPY} 2>> /dev/null

        umount ${COPY} 2>> /dev/null

        mkdir -p ${RD}${COPY}

        cp -R ${COPY}/. ${RD}${COPY}

        # do a bind mount, kernel 2.4 needed

        mount --bind ${RD}${COPY} ${COPY}

done

```

----------

## kappax

I would have done tmpfs, but i am told that tmpfs will swap out .  I wanted a dive that would never swap out without turning off swap.  the bind options sounds good  :Smile:  i like that i will have to use it .

----------

## dreamer3

My script is more general purpose so it's good to not have to know the storage requirements upfront...  that sucks about tmpfs swapping out... where did you read that?

Did you have to recompile your kernel for really big ramdisks to get your thing working?

----------

## kappax

 *dreamer3 wrote:*   

> My script is more general purpose so it's good to not have to know the storage requirements upfront...  that sucks about tmpfs swapping out... where did you read that?
> 
> Did you have to recompile your kernel for really big ramdisks to get your thing working?

 

when i was asking some questions about rma drives, one guy told me about it, then i learned how to make a real ram drive. because tempfs is dynamic, it can get larger and what not and be swaped out. i may be wrong but i think it is true.

----------

## dreamer3

 *kappax wrote:*   

>  *dreamer3 wrote:*   ...
> 
> Did you have to recompile your kernel for really big ramdisks to get your thing working? 
> 
> because tempfs is dynamic, it can get larger and what not and be swaped out. i may be wrong but i think it is true.

 

Ok, you're right, I checked the tmpfs kernel docs... would you mind answering my original question that you missed (see above)?  :Smile: 

----------

## kappax

 *dreamer3 wrote:*   

>  *kappax wrote:*    *dreamer3 wrote:*   ...
> 
> Did you have to recompile your kernel for really big ramdisks to get your thing working? 
> 
> because tempfs is dynamic, it can get larger and what not and be swaped out. i may be wrong but i think it is true. 
> ...

 

oh hehe, i missed that big time. no modle is all i did, i did set the default size to 60 megs, for the module but other than that i did not have to recompile my system.

----------

## TheEternalVortex

This is pretty cool I think, although it might be better for a program that does much more IO. You could load a database entirely into memory, which would greatly increase it's performance (you'd have to write copies to disk every once in a while, of course).

----------

## dreamer3

Ok, here is my new and improved bash script.  Same as before just tweak RD and DEFAULT_DIRS.  You can also change FSTYPE between tmpfs (swapable) and ramfs (non-swapable), both of which ARE dynamically sizeable however (unlike ramdisk).

Using the ramfs options causes the mount to not be shown in df output and also the output of du seems to be funny.  Files seem to load and execute just fine however.  I get the feeling from what I've read that ramfs isn't a fully-featured file system, but it seems to do the trick.  I don't know if ramfs  is ALWAYS compiled into the kernal or what, but I couldn't find any option for it in menuconfig.

Let me know if anyone finds some cool uses for this.  i've currently moved Phoenix to a ramfs just because I have nothing better to do with my memory.  *shrugs*

Examples

load all default directories onto the ramdisk (this was the default behavior of the last ramit)

```
ramit -a default
```

load a specific directory onto the ramdisk

```
ramit -a /usr/sbin
```

remove all default directories from the ramdisk

```
ramit -r default
```

run your /tmp and /var/tmp from the ramdisk

```
ramit -a /tmp /var/tmp
```

The Code

ramit - bash script

```
#!/bin/bash

# version 0.2 - last modified 11/13/02 - by Josh Goebel

# RD = location ramdisk is to be mounted too

# DO NOT SET TO ROOT / !!!!!

RD=/.rawspeed

# specify directories you would like copied to the ramdisk

DEFAULT_DIRS="/usr/share/phoenix"

# file system type for ramdisk

#

# ramfs = no swapping, no size limits

# tmpfs = swappable, max size limits (default 50% ram)

FSTYPE="ramfs"

        

#umount ${RD} 2>> /dev/null

#umount /usr/share/phoenix 2>> /dev/null

#rm -rf ${RD} 

syntax()

{

        echo "ramit v0.1 - by Josh Goebel"

        echo

        echo "Usage: ramit [OPTION] [DIRECTORY]..."

        echo 

        echo "options:"

        echo "   -a          add directory or directories to ramdisk"

        echo "   -r          remove directory or directories from ramdisk"

        echo "   --list -l   list all directories currently mapped to the ramdisk";

        echo "               (default if no options are specified)";

        echo "   --help -h   show help (short -h)";

        echo

        echo "special directory aliases:"

# not supported yet

#       echo "   all         all directories currently copied to ramdisk"

#       echo "               (useful for -r all)"

        echo "   default     those directories defined in DEFAULT_DIRS"

        echo "               (useful for directores frequentied copied to ramdisk)"

        exit;

}

del_dirs()

{

for ZAP in ${PASSED_DIRS};

do

        if [ ! -d ${RD}${ZAP} ]; then

                echo "ERROR: '${ZAP}': No such directory on ramdisk"

        else

                rm -rf ${RD}${ZAP} 2>> /dev/null

                umount ${ZAP} 2>> /dev/null

        fi

done

}

add_dirs()

{

del_dirs >>/dev/null

for COPY in ${PASSED_DIRS};

do

        if [ ! -d ${COPY} ]; then

                echo "ERROR: '${COPY}': No such directory on filesystem"

        else

                mkdir -p ${RD}${COPY}

                cp -a ${COPY}/. ${RD}${COPY}

                # do a bind mount, kernel 2.4 needed

                mount --bind ${RD}${COPY} ${COPY}

        fi

done

}

setup()

{

# check to make sure we aren't about to be foolish

if [[ "${RD}" == "/" || "${RD}" == "/." ]]; then

        echo "WARNING: Your ramdrive cannot be mounted at ${RD} ...";

        exit;

fi

# check for the existence of our speed up directory

# if not found, create

if [ ! -d ${RD} ]; then

        echo "Creating ${RD} for ramdisk..."

        mkdir ${RD};

fi

# check to see if we've already mounted a tmpfs there

# if not, go ahead and mount

MOUNTED=`mount | grep ${RD}`

if [ -z "${MOUNTED}" ]; then

        echo "Mounting ${FSTYPE} at ${RD}..."

        mount ${FSTYPE} ${RD} -t ${FSTYPE}

fi

}

list_rammed()

{

RAMMED=`mount | grep /.rawspeed/ | awk '{print $3}'`

if [ -z "${RAMMED}" ]; then

        echo "No directories mounted from the ramdisk";

else

        mount | grep /.rawspeed/ | awk '{print $3}'

fi

}

# if no parameters have been passed

if [[ -z $1 || "$1" == "-l" ]]; then

        list_rammed;

        exit;

fi

if [[ "$1" == "--help" || "$1" == "-h" ]]; then

        syntax;

        exit;

fi

# setup tmpfs directory if necessary

setup

if [ "$1" == "-a" ]; then

        if [ "$2" == "default" ]; then

                PASSED_DIRS=$DEFAULT_DIRS

                add_dirs

        else

                shift

                PASSED_DIRS=$@

                add_dirs

        fi

        exit;

fi

if [ "$1" == "-r" ]; then

        if [ "$2" == "default" ]; then

                PASSED_DIRS=$DEFAULT_DIRS

                del_dirs

        else

                shift

                PASSED_DIRS=$@

                del_dirs

        fi

        exit;

fi

echo "ERROR: Unknown or understood options"
```

----------

## arkane

 *TheEternalVortex wrote:*   

> This is pretty cool I think, although it might be better for a program that does much more IO. You could load a database entirely into memory, which would greatly increase it's performance (you'd have to write copies to disk every once in a while, of course).

 

Don't most databases load entirely into memory currently?

I know Oracle does... and mysql in my experience does, also.

(not trying to diss you, just wondering if I'm missing something)

----------

## dreamer3

 *arkane wrote:*   

> Don't most databases load entirely into memory currently?
> 
> I know Oracle does... and mysql in my experience does, also.
> 
> (not trying to diss you, just wondering if I'm missing something)

 

Not completely, but relevent tables and such would definately be cached in memory.  Everything would be actually for smaller databases and smaller tables.  I don't think any mainstream database could benefit from this kind of thing. (files on ramdisk)

Now, a file based database (kinda like /usr/portage/* or /var/db/pkg) COULD benefit greatly, but on my system I have enough memory so once I use emerge once the whole database gets cached into memory anyways.  If I ran a server where one if it's ONLY jobs was to serve the portage dirs out FAST then maybe I could see a use for always-on memory caching.  But if it was used frequently enough the kernel cache would handle it all anyways.

Does anyone have ANY idea how fast portage would be with mysql or a non-file based database as a backend?

I created my utility to learn a little more about bash scripts not really because I saw a great need for such a utility.

----------

## arkane

If there was a database connection from portage, it would speed it up exponentially....

Most of the time (at least on my system) is wasted on disk I/O.

Though, having a mysql database loaded just for package management would be an overkill, imho.

Nice utility though, for those who want an easy way to load things into ramdisk.

----------

## dreamer3

 *arkane wrote:*   

> If there was a database connection from portage, it would speed it up exponentially....

 

If only we knew...  I've been tossing the idea around of making a web based portage management system... that would load all the package data into mysql... but don't know if anyone would use such a beast (you'd have to have apache, mod_php, and mysql installed)

 *Quote:*   

> Though, having a mysql database loaded just for package management would be an overkill, imho.
> 
> 

 

It could be embedded... plus I run it anyways, so no overkill here...

 *Quote:*   

> Nice utility though, for those who want an easy way to load things into ramdisk.

 

Thanks.  On my next major reconfig I think I'm going to setup LARGE swap file (say 2-3 gigs or so) and mount /tmp and /var/tmp onto tmpfs (swapable remember)...  That would eliminate almost ALL disk activity for compiling on smaller builds... it would work well considering my system usually has a surplus of memory not being used for anything...

----------

## arkane

 *dreamer3 wrote:*   

> 
> 
> If only we knew...  I've been tossing the idea around of making a web based portage management system... that would load all the package data into mysql... but don't know if anyone would use such a beast (you'd have to have apache, mod_php, and mysql installed)
> 
> 

 

Hell, I run those right now.. I wonder how many people do the same, if not for phpmyadmin  :Razz: 

 *Quote:*   

> 
> 
> Thanks.  On my next major reconfig I think I'm going to setup LARGE swap file (say 2-3 gigs or so) and mount /tmp and /var/tmp onto tmpfs (swapable remember)...  That would eliminate almost ALL disk activity for compiling on smaller builds... it would work well considering my system usually has a surplus of memory not being used for anything...

 

would be interesting to see the outcome, although I wonder about this because of the added packetting overhead.. (ram emulated in swap as opposed to just using disk)

Do let us know how it goes through!  that would be very informative.

----------

## dreamer3

 *arkane wrote:*   

>  *dreamer3 wrote:*   you'd have to have apache, mod_php, and mysql installed... 
> 
> Hell, I run those right now.. I wonder how many people do the same, if not for phpmyadmin 
> 
> 

 

Well I wouldn't want to rewrite portage in php, so there are questions of how it would interface with the existing portage system.  How to do emerges via the web and show progress, etc...  I have some cool ideas, but again, I don't know how many would find such a thing "cool".

 *Quote:*   

>  *dreamer3 wrote:*   Thanks.  On my next major reconfig I think I'm going to setup LARGE swap file (say 2-3 gigs or so) and mount /tmp and /var/tmp onto tmpfs (swapable remember)...  
> 
> would be interesting to see the outcome, although I wonder about this because of the added packetting overhead.. (ram emulated in swap as opposed to just using disk)

 

Added packeting overhead? What?  Do you get how it would work?

Pleny of RAM scenario - ie, small to medium emerging, everyday use

Source extract, build, compile, image install (right before the real thing), ALL temp files all saved in MEMORY on the tmpfs as there is plenty of RAM.  Result: speedup.

Running out of RAM scenario - emerging Mozilla, KDE, Open Office, etc

tmpfs grows larger than available amount of RAM, /tmp and /var/tmp files start getting placed into the swap file... this can't be MUCH slower if any than simply writing them on the disk in the first place... and you wouldn't reach this point (assuming you have lots of RAM) unless you were doing a LARGE compile...

I didn't come up with this idea on my own... I read about it elsewhere and it's a great idea for people running Linux off a battery, because disk access eats batteries... but when I do get around to trying it I will let everyone know my results, don't worry.

----------

## lx

Although I've played with the same idea, (I also want to make my own server and rescue distro completly on a ramdisk), I have the following problem with using a ramdisk in daily use, while it may speed up the startup time, the program still get's loaded into the memory, so programs now occupy 2x (or more) the amount of memory then before   :Shocked:  (correct me if I'am wrong) and if open, your program can still be swapped out of memory, and if so it isn't reloaded from your ramdisk   :Shocked:  . 

Still it might be good to look at an option in the program / vm memory or somewhere which own can choose to pin processes / libraries into main memory, reason for this is that the user will know best how the computer c.q. memory will be used.

I have 256 mb of memory and only have some problems with using the computer while compiling big packages (like sun-j2sdk / open-office), normally programs are kept in memory already,  :Wink: 

Cya lX.

Ps: Reiserfs (filesystem) uses a database approach, and therefor portage runs great on it. It works great with small files.

----------

## dreamer3

 *lx wrote:*   

> Although I've played with the same idea, (I also want to make my own server and rescue distro completly on a ramdisk), I have the following problem with using a ramdisk in daily use, while it may speed up the startup time, the program still get's loaded into the memory, so programs now occupy 2x (or more) the amount of memory then before   (correct me if I'am wrong) and if open, your program can still be swapped out of memory, and if so it isn't reloaded from your ramdisk   . 

 

tmpfs will SHARE the memory when running programs off of it (I've read), and that shared memory of course can be swapped... not sure how ramfs or the ramdisk works...

 *Quote:*   

> Ps: Reiserfs (filesystem) uses a database approach, and therefor portage runs great on it. It works great with small files.

 

Hmm, your making me think of creating a portage sized file and mounting it on a loop device and formating it with reiserfs and running portage (no distfiles) off of it.  Can anyone else confirm a noticeable speed increase when running portage off of a reiserfs as opposed to ext3?

----------

## jlg

ramming things is lots of fun! I have made my own bootable cd that loads the whole os into ram and also made a cluster of servers booting into ram  through pxe. Its very practical.. If you want more power just add a new server! no config and no hd necessary. The ram disk is all created as a parameter you pass to the kernel.  something like:  

append ramdisk_size=65536

----------

## jesterspet

Interesting read.

[story]

It reminds me when I bought my first computer (p166)and shelled out way too much on memory.  One can never have too much memory, right?  Well you can if you are running Windows95.  Win95 capped out at 64mb, I had 128mb.  It was then I learned all about the glories of Ramdisks.  Now with this thread, I can safely look into them again, only this time I will not be stuck behind a <32mb size barrier.    :Very Happy: 

[/story]

----------

## uarkelley

Out of curiousity, how fast does Phoenix load off of the ramdisk?

----------

## Safrax

Phoenix on my laptop with ramdisk loads in about 1-2 seconds.  My system is entirely prelinked though.

----------

## jesterspet

 :Idea:  ::: thinking ::::  :Idea: 

I wonder if XFree86 will run faster if I move it to my USB Drive or a ram drive   :Question: 

I must look into what it would take to move XFree86 without breaking the entire thing.

----------

## Safrax

 *jesterspet wrote:*   

>  ::: thinking :::: 
> 
> I wonder if XFree86 will run faster if I move it to my USB Drive or a ram drive  
> 
> I must look into what it would take to move XFree86 without breaking the entire thing.

 

If the usb drive uses flash memory like some of the portable mp3 players do, then it would be ALOT slower.  You would probably be better off to use a ramdrive for X.  I don't think you would get much benefit from that though but I might be wrong.

----------

## TheEternalVortex

Any USB drive would be slower than the computer's HDD...

----------

## Kodama

 *jesterspet wrote:*   

>  ::: thinking :::: 
> 
> I wonder if XFree86 will run faster if I move it to my USB Drive or a ram drive  
> 
> I must look into what it would take to move XFree86 without breaking the entire thing.

 

That would be great, have you figured out what to move yet?

----------

## kappax

 *jesterspet wrote:*   

>  ::: thinking :::: 
> 
> I wonder if XFree86 will run faster if I move it to my USB Drive or a ram drive  
> 
> I must look into what it would take to move XFree86 without breaking the entire thing.

 

I dont think so, it might make X start faster, but i think after X is started it does not need much. but one thing i can think might help, if you have the ram, is moving your fonts to a ram drive ( each program scans your fonts on start ) if the fonts were located on ram you could get some speed maybe.

----------

## Jimbow

I've got both /tmp and /var/tmp mounted on tmpfs in my fstab.  I am often suprised at how fast emerges are.  I keep thinking that something borked because it gets done so quick.

----------

## dreamer3

 *Jimbow wrote:*   

> I've got both /tmp and /var/tmp mounted on tmpfs in my fstab.  I am often suprised at how fast emerges are.  I keep thinking that something borked because it gets done so quick.

 

I used to do this but big compiles ticked me off (Mozilla, etc) and started swapping and seemed to complete slower than just running from disk...

But for most smaller to medium emerges with plenty of RAM it is the coolest thing...

----------

## dave.fraser

I read a HOWTO a while back along these lines.  It described how you could use XFree to setup an old video card, then mount the video ram as a ramdisk.  It wouldn't be as fast as a regular ramdisk, because you'd be accessing it over PCI, but would otherwise have a similar effect without sacrificing main memory.  Great if you have an old card lying around idle.  I've been trying to find the link, but unsuccessfully.  I'll post it if I find it.  Has anyone else seen/done this?

----------

## nikai

A small correction in the second ramit script:

When using a different mount point with RD, list_rammed() doesn't work, because /.rawspeed is still hardcoded.

This should work better:

```
list_rammed()

{

RAMMED=`mount | grep $RD/ | awk '{print $3}'`

if [ -z "${RAMMED}" ]; then

        echo "No directories mounted from the ramdisk";

else

        mount | grep $RD/ | awk '{print $3}'

fi

}
```

----------

