old-www/LDP/LG/issue44/nielsen.html

246 lines
8.4 KiB
HTML

<!--startcut ==========================================================-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<title>TitleOfArticle LG #44</title>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
ALINK="#FF0000">
<!--endcut ============================================================-->
<H4>
"Linux Gazette...<I>making Linux just a little more fun!</I>"
</H4>
<P> <HR> <P>
<!--===================================================================-->
<center>
<H1><font color="maroon">How to use a Ramdisk for Linux</font></H1>
<H4>By <a href="mailto:mark@tcu-inc.com">Mark Nielsen</a></H4>
</center>
If this document changes, it will be available at
<a href="http://www.tcu-inc.com/mark/articles/Ramdisk.html">
http://www.tcu-inc.com/mark/articles/Ramdisk.html
</a>.
<P> <HR> <P>
<h2>Index:</h2>
<ol>
<li><a href="#Intro"> Introduction to RamDisk</a>
<li><a href="#How"> How to use RamDisk</a>
<li><a href="#Change"> Changing the size of the ramdisks</a>
<li><a href="#Example">Example of how to use a RamDisk for a webserver.</a>
<li><a href="#Comments"> Comments</a>
</ol>
<hr>
<a name="Intro"></a>
<h2>Introduction to RamDisk</h2>
Hello! This is a brief article about how to setup a RamDisk on a RedHat 6.0
system. It should be very similar for other Linux distributions.
<p>
What is a RamDisk? A RamDisk is a portion of memory that you allocate to use as
a partition. Or, in other words, you are taking memory, pretending to treat it
as a hard drive, and you are saving your
files to it. Why would you want to use a RamDisk? Well, if you know that
certain files you have are constantly going to be used, putting the files into
memory will increase the performance of your computer since your memory is faster
than your hard drive. Things like web servers with lots of data can be sped up
in the this way.
Or, if you are insane, and you have a PII 550 Mhz computer
with 1 gig of memory and an old 500 meg hard drive, you can use it just to
increase your hard drive space. Then again, if you want an almost diskless machine, it
might not be that crazy afterall.
<p>
Here are some more resources to help you.
<ol>
<li><a href="http://metalab.unc.edu/LDP/HOWTO/Kernel-HOWTO.html">
http://metalab.unc.edu/LDP/HOWTO/Kernel-HOWTO.html</a>
<li>
<a href="http://metalab.unc.edu/LDP/HOWTO/mini/LILO.html">
http://metalab.unc.edu/LDP/HOWTO/mini/LILO.html</a>
<li> /usr/src/linux/Documentation/ramdisk.txt
</ol>
<hr>
<a name="How"></a>
<h2>How to use RamDisk</h2>
Well, it is very easy to use a ramdisk. First of all, the default installation
of RedHat 6.0 comes with ramdisk support. All you have to do is format a
ramdisk and then mount it to a directory. To find out all the ramdisks you have
available, do a "ls -al /dev/ram*". This gives you the preset ramdisks
available to your liking. These ramdisks don't actually grab memory until you
use them somehow (like formatting them). Here is a very simple example of how to
use a ramdisk.
<pre>
mkdir -p /tmp/ramdisk0
mkfs -t ext2 /dev/ram0
mount /dev/ram0 /tmp/ramdisk0
</pre>
Those three commands will make a directory for the ramdisk to be located at,
format a ramdisk (default being 4 megs), and mount the
ramdisk to the directory "/tmp/ramdisk0". Now you can treat that directory as a
pretend partition! Go ahead and use it like any other directory or as any other
partition.
<p>
What are some catches? Well, when the computer reboots, it gets wiped. Don't put
any data there that isn't copied somewhere else or if it is critical data. If
you make changes to that directory, and you need to keep the changes, figure out
some way to back them up.
<hr>
<a name="Change"></a>
<h2>Changing the size of the ramdisks</h2>
Well, how do I change the size of the ramdisks? Well, personally, I could only
find two ways to change them. Neither of the options will let you change the
size of the ramdisks after the computer has started. That sucks.
<p>
Here is the hard way first. Look at this file :
<br> &nbsp;&nbsp;&nbsp;/usr/src/linux/drivers/block/rd.c
<br> then edit it, change this line
<br> &nbsp;&nbsp;&nbsp;int rd_size = 4096; /*Size of the ramdisks */
<br> by changing the number to whatever size you want in kilobytes. Then compile
the kernel, install the kernel, and reboot the computer.
<p>
Okay, now the easy way. Add this line to your lilo.conf file:
<br> &nbsp;&nbsp;&nbsp;ramdisk=10000
<br> and it will make the default ramdisks 10 megs after you type the "lilo"
command and reboot the computer. Here is an example of my /etc/lilo.conf file.
<pre>
boot=/dev/hda
map=/boot/map
install=/boot/boot.b
prompt
timeout=50
image=/boot/vmlinuz-2.2.5-15
label=linux
root=/dev/hda2
read-only
ramdisk=10000
</pre>
Actually, I got a little over 9 megs of usable space.
<hr>
<a name="Example"></a>
<h2>Example of how to use a RamDisk for a webserver.</h2>
Okay, here is an example of how to use 3 ramdisks for a webserver. Let us say
you are 99% confident that your default installation of Apache for RedHat 6.0
won't use more than 9 megs for its cgi-scripts, html, and icons. Here is how to
install one.
<br> First, issue this command to move the real copy of home directory for the
webserver to a different place. Also, make the directories to mount the ramdisks
to.
<pre>
mv /home/httpd/ /home/httpd_real
mkdir /home/httpd
mkdir /home/httpd/cgi-bin
mkdir /home/httpd/html
mkdir /home/httpd/icons
</pre>
Then, add these commands to your /etc/rc.d/rc.local file.
<pre>
### Make the ramdisk partitions
/sbin/mkfs -t ext2 /dev/ram0
/sbin/mkfs -t ext2 /dev/ram1
/sbin/mkfs -t ext2 /dev/ram2
### Mount the ramdisks to their appropriate places
mount /dev/ram0 /home/httpd/cgi-bin
mount /dev/ram1 /home/httpd/icons
mount /dev/ram2 /home/httpd/html
### Copying real directory to ramdisks
tar -C /home/httpd_real -c . | tar -C /home/httpd -x
#### Restarting the webserver
/etc/rc.d/init.d/httpd restart
</pre>
I would reboot your computer, even though you really don't have to if you just
issue this command to start the rc.local file again:
<pre>
/etc/rc.d/rc.local
</pre>
<hr>
<a name="Comments"></a>
<h2>Comments</h2>
<ol>
<li>Please remember one thing, BACKUP YOUR DATA if you change it and you need it.
When the computer reboots, any changes are lost. A cron job should do it. Have
it check every 5 minutes and see if any files have changed and backup any
changes. There are better ways of doing this, but I won't get into it. Another
thing you could do is make your changes to the real directory, and then copy
over the changes to the ramdisks. That is much safer.
<li> One thing to note, some motherboards for IBM PC compatible systems only
cache memory below 256 megs (or even 128 megs). If you use a lot of ram, take
that into consideration.
<li> A cool use of this would be to have a computer with 1 gig of memory and
then use 256 megs for "/tmp". If you have lots of processes that use "/tmp", it
should help speed up your system. Also, anything in /tmp would get lost when the
computer reboots, which can be a good thing.
<li> At first, I tried to use lilo.conf to change the sizes of the ramdisks, but
it didn't work. After Rob Funk asked me why I was trying to do it the hard way
(recompiling the kernel and such), I tried it again, and it worked. I don't
know what I did wrong the first time. Thanks Rob!
</ol>
<hr>
<a href="http://www.tcu-inc.com/mark">Mark Nielsen</a> works for
<a href="http://www.tcu-inc.com">The Computer Underground</a> as a file
clerk and as
a professional (suit and tie) consultant at <a href="http://www.800linux.com">
800linux.com</a>.
In his spare time, he does volunteer stuff, like writing
these documents for The Linux Gazette and linux.com. This document was edited
using Nedit and ispell.
<!--===================================================================-->
<P> <hr> <P>
<center><H5>Copyright &copy; 1999, Mark Nielsen <BR>
Published in Issue 44 of <i>Linux Gazette</i>, August 1999</H5></center>
<!--===================================================================-->
<!--startcut ==========================================================-->
<P> <hr> <P>
<A HREF="./index.html"><IMG ALIGN=BOTTOM SRC="../gx/indexnew.gif"
ALT="[ TABLE OF CONTENTS ]"></A>
<A HREF="../index.html"><IMG ALIGN=BOTTOM SRC="../gx/homenew.gif"
ALT="[ FRONT PAGE ]"></A>
<A HREF="macevoy/macevoy.html"><IMG SRC="../gx/back2.gif"
ALT=" Back "></A>
<A HREF="oneill.html"><IMG SRC="../gx/fwd.gif" ALT=" Next "></A>
<P> <hr> <P>
</BODY>
</HTML>
<!--endcut ============================================================-->