Wacky uses for Raid, /dev/ram, and ramfs
by
Mark Nielsen
from
The Computer Underground
Index:
- Introduction to RamDisk
- How to use RamDisk
- Changing the size of the ramdisks
- Example of how to use a RamDisk for a webserver.
- Comments
- Resources
Introduction to RamDisk
Please refer to my original article for the old ramdisk. It is located
here:
http://www.gnujobs.com/mark/articles/Ramdisk.html
I won't be going into detail on the old style of ramdisk in this article.
What is a RamDisk? A RamDisk is a portion of memory that you allocate to use as
a partition. Or, in other words, you take some of your memory and pretend it is a hard drive
that you can format, mount, save files to etc.
There are two types of ramdisks we will be talking about. The first is a ramdisk that
you can format and mount. The second is a ramdisk you cannot format, but has
some nice features. Here is a breakdown:
The old /dev/ram1 approach:
- BAD: The size of the ramdisk is fixed upon boot-up.
- GOOD: You can format the ramdisk anyway you want.
- BAD: Once formatted (I don't believe you have to mount it), it takes up
a portion of your memory and I am not sure how you can get it back.
- GOOD: You can have more than one ramdisk at a time.
The new ramdisk "ramfs":
- BAD: I couldn't format it other than what it is set to. It is on
the VFS layer, whatever that is.
- GOOD: You can have more than one.
- GOOD: You can change its maximum size at the line command.
- VERY GOOD: It only uses up the memory that it needs. In other words, you
may have allocated 64 megs of ram, but if it is only has 2k of files in it,
it will only use up 2k of memory.
As soon as a file is deleted you immediately
have more memory for your computer.
- BAD: Well, it isn't really its own fault. Don't run into the trap
of filling of two ramfs ramdisks or you will blow away all your memory.
More on this.
How to use RamDisk the old way
Please read this article.
How to use a Ramdisk for Linux.
To use your kernel 2.0 and 2.2 ramdisks, just type three commands:
mkfs -t ext2 /dev/ram1
mkdir -p /RAM1
mount /dev/ram1 /RAM1
as root, and you are done. On most systems the size is limited to 4 megs.
This can be changed at boot-time or if you recompile the kernel, but read
my old article for that. You can also add more like this,
mkfs -t ext2 /dev/ram2
mkdir -p /RAM2
mount /dev/ram2 /RAM2
How to use RamDisk with ramfs for kernel 2.4
First of all, I assume that you have kernel 2.4 that
has ramfs compiled into it. Most major distributions that
use the new kernel 2.4 should have ramfs compiled in it. I was using
RedHat 7.1 for this article.
Very simple. Make a directory and mount ramfs there.
mkdir -p /RAM1
mount -t ramfs none /RAM1
If you want a second one, very simple. Make a directory and mount ramfs there.
mkdir -p /RAM2
mount -t ramfs none /RAM2
Changing the size of the ramdisks and other options.
Here is an example of how to make a 10 meg ramdisk.
mkdir -p /RAM1
mount -t ramfs none /RAM1 -o maxsize=10000
Taken directly from
http://www.linuxhq.com/kernel/v2.4/patch/patch-2.4.3-ac2/linux.ac_Documentation_filesystems_ramfs.txt.html
+ ramfs - An automatically resizing memory based filesystem
+
+
+ Ramfs is a file system which keeps all files in RAM. It allows read
+ and write access. In contrast to RAM disks, which get allocated a
+ fixed amount of RAM, ramfs grows and shrinks to accommodate the
+ files it contains.
+
+ You can mount the ramfs with:
+ mount -t ramfs none /mnt/wherever
+
+ Then just create and use files. When the filesystem is unmounted, all
+ its contents are lost.
+
+ NOTE! This filesystem is probably most useful not as a real
+ filesystem, but as an example of how virtual filesystems can be
+ written.
+
+Resource limits:
+
+By default a ramfs will be limited to using half of (physical) memory
+for storing file contents, a bit over that when the metadata is
+included. The resource usage limits of ramfs can be controlled with
+the following mount options:
+
+ maxsize=NNN
+ Sets the maximum allowed memory usage of the
+filesystem to NNN kilobytes. This will be rounded down to a multiple
+of the page size. The default is half of physical memory. NB. unlike
+most of the other limits, setting this to zero does *not* mean no
+limit, but will actually limit the size of the filesystem data to zero
+pages. There might be a use for this in some perverse situation.
+
+ maxfilesize=NNN
+ Sets the maximum size of a single file on the
+filesystem to NNN kilobytes. This will be rounded down to a multiple
+of the page size. If NNN=0 there is no limit. The default is no limit.
+
+ maxdentries=NNN
+ Sets the maximum number of directory entries (hard
+links) on the filesystem to NNN. If NNN=0 there is no limit. By
+default this is set to maxsize/4.
+
+ maxinodes=NNN
+ Sets the maximum number of inodes (i.e. distinct
+files) on the filesystem to NNN. If NNN=0 there is no limit. The
+default is no limit (but there can never be more inodes than dentries).
Running services in RamDisk
I have a thought. So listen carefully. Wouldn't it be cool if you could
run your services (like a web server, database server, or a DNS server)
on a ramdisk? Usually the standard webserver doesn't have a lot of
huge files in it. Usually a DNS server doesn't have a lot of files in it.
You could even put a database server in memory where you only need it
to store temporary data.
Ram is cheap these days. I got 128 megs of ram for $120 recently, and
I imagine the price will continue to drop. For 128 megs of ram, I can
easily fit any of my webservers into it. If my webserver has huge files,
I can configure httpd.conf to look a another directory not in the ram
disk.
My DNS server is very small.
My largest database server is only 28 megs. Considering the fact you can
get 1 gig memory computers these days real cheap, I think it would be
cool to run everything I have in ram. The only tricky part with the
database server is that you should first save all writes to a separate
database server before writing to your database server in ram. This is slow,
but considering most of the time you are reading and not writing, it should
increase performance. Perhaps if we could mirror a ram disk to a hard drive
I already explained this in my other article, so please read it here:
http://www.gnujobs.com/mark/articles/Ramdisk.html#Example. Just replace
these commands
### 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
with these commands (increase the number if 10 megs isn't good enough):
mount -t ramfs none /home/httpd/cgi-bin -o maxsize=10000
mount -t ramfs none /home/httpd/icons -o maxsize=10000
mount -t ramfs none /home/httpd/html -o maxsize=10000
Ramdisk and Raid
Why would you want to combine a hard drive parition and a ram disk into
a raid partition? I don't know. But there may be a use for it in the long
run. Intelligent operating systems just don't avoid something because
there is no immediate benefit.
That is
why many commercial operating systems are becoming unpopular and why
free software applications are replacing applications written by idiots
who are only interested in money and not technology.
There are three problems combining a ramdisk with a hard drive
partition.
- I don't know how how to do it with ramfs, but I can do it with
/dev/ram. Perhaps if you could format a hard drive partition with
the filesystem ramfs uses, you could do it. I have no idea what
the VFS layer is, so I will probably study more about it later.
- If the mirrored raid is truly running in parallel, then
the extra speed of the memory won't help. I don't know if it is
possible to make a mirrored raid to only do reads from the ramdisk,
in which case, if it is possible, there would be benefits.
- Upon reboot, you will have to reconstruct the ramdisk.
In order to setup the raid, I configured my /etc/raidtab like this.
raiddev /dev/md3
raid-level 1
nr-raid-disks 2
nr-spare-disks 0
chunk-size 4
persistent-superblock 1
device /dev/hdb6
raid-disk 1
device /dev/ram3
raid-disk 0
Then I executed,
mkraid /dev/md3
mkfs -t ext2 /dev/md3
mkdir -p /RAID1
mount /dev/md3 /RAID1
Problem, upon reboot, the ramdisk is going to get messed up.
Thus, on reboot,
raidstart /dev/md3
raidhotadd /dev/md3 /dev/ram3
mount /dev/md3 /RAID1
Now remember, if you didn't reconfigure the old ramdisks
to be larger than 4 megs, it is pointless for your hard drive
partition to be larger than 4 megs.
It would be really cool to have a mirrored raid with 3 partitions.
1 being the ramdisk and 2 being hard drive paritions and to
also make it so only the ramdisk is read for read cycles.
I don't know if it is possible to isolate reads to only one
of the partitions in a raid, but it would be cool if you
could.
Comments
This is probably the most important section of this article.
I love Linux because it lets you shoot yourself in the foot.
There is nothing like pain because it makes you learn.
- Ramfs defaults to half your physical memory (no swap space). Thus if you
create two of these things and fill them up, your computer will run out
of memory and stall, just like mine did. PLEASE specify the max amount of
memory the ramdisk should have as described above.
- I believe ramfs is its own filesystem and not ext2, so there, if you
copy files back and forth using rsync or tar, don't expect the files to be
exactly identical or the same size. You most likely won't see any major
difference in using them, but since it is not ext2, the properties might be
different on some levels.
- I was reading the docs for ramfs in the kernel and it doesn't seem like ramfs
was really intended for wide use, but I don't see why. It works the way I want it.
I am not done with ramdisks yet.
- I want to see if I can raid ramfs with a hard drive.
- If I can make it so reads to a raid are isolated to one of the partitions.
- Putting a service, like a webserver, into a ramdisk with raid.
The trick here is to use raid
so that if any changes occur, you don't loose them. That is why raid is
so freaking important to me ---- I will be able to put services into
ram SAFELY and gain all the performance speed of memory over hard drive
assuming:
- That I can get a raid to do reads only from the ramdisk partition
and not the others.
- Read files from a hard drive are cached in memory. Only if they
change, or cached out, would you notice a difference in speed.
Also, initial startup is faster the first time the file is read.
- You could use the built-in webserver that come in the new kernels.
They should go really really fast as well. Maybe combining the built-in
webserver with a ramdisk?
- Setting up a computer to boot from a cdrom, start a service, and
download changes off the network. No hard drive required. This would be
cool. They you could have a webserver (or something else) running off
a computer with no hard drive and just a cdrom with memory. If you
bump your memory to 1 gig, I don't see why you couldn't have your system
run entirely off of a cdrom. This would be nice for security reasons.
Just reboot the sucker, have it load any changes (security fixes) from
a secured resource, then start the services,
and your computer is nice and clean and hack-free.
The webserver should never save anything to the local computer but save
logs and information to a separate computer on a secured connection
or internal network. I made bootable networked cdroms with
ramdisks in the past.
It would be nice to combine my earlier project to achieve this.
Resources
-
-
http://www.linuxhq.com/kernel/v2.4/patch/patch-2.4.3-ac2/linux.ac_Documentation_filesystems_ramfs.txt.html
-
http://genericbooks.com/LDP/HOWTO/Software-RAID-HOWTO.html
-
Speeding up small databases on Linux
-
How to use a Ramdisk for Linux
- If this article changes, it will be available here:
http://www.gnujobs.com/Articles/21/Ramdisk2.html
Thanks to Katja for some help!
Mark works as an independent consultant donating time to causes like
GNUJobs.com, writing articles, writing free software, and working
as a volunteer at eastmont.net.
Copyright © 4/2001 Mark Nielsen
Article