Ramdisk:在linux下如何使用 RAM 作为虚拟硬盘?( 二 )



boot=/dev/hda
map=/boot/map
install=/boot/boot.b
prompt
timeout=50
image=/boot/vmlinuz
label=linux
root=/dev/hda2
read-only
ramdisk_size=10000

确切的说 , 我只是使用了9M多的空间 , 文件系统也将占用一定空间 。

当你以模块的形式编译ramdisk时 , 你可以在加载的时候决定ramdisk的大小 。这也可以通过修改/etc/conf.modules;的选项设置来做到 。;

options;rd;rd_size=10000

或是在命令行中指定参数给ismod:

insmod;rd;rd_size=10000

以下是介绍如何使用这样的模块的例子:;

卸载ramdisk , umount;/tmp/ramdisk0;.;
卸载模块(再上一节所提到的过程中自动加载),;rmmod;rd;
加载ramdisk模块并且把它的大校设为20M , insmod;rd;rd_size=20000;
创建一个文件系统,;mke2fs;/dev/ram0;
加载ramdisk,;mount;/dev/ram0;/tmp/ramdisk0;

--------------------------------------------------------------------------------
使用;RamDisk;做;webserver例子.
--------------------------------------------------------------------------------

Okay,;这是一个用3个ramdisk做webserver的例子 。让我们设想你在;RedHat;6.0;上默认安装的;Apache;的cgi-scripts,;html,;和;icons都不超过9M 。这就是如何实现这一点 。
首先 , 利用命令将webserver的跟目录移到另外一个地方 。并且加载ramdisk 。

mv;/home/httpd/;/home/httpd_real
mkdir;/home/httpd
mkdir;/home/httpd/cgi-bin
mkdir;/home/httpd/html
mkdir;/home/httpd/icons

接下来 , 将这些命令加到启动进程中/etc/rc.d/init.d/httpd.init;(或者是系统启动httpd的地方):;


###;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;(the
###;data;on;the;ramdisks;is;lost;after;a;reboot)
tar;-C;/home/httpd_real;-c;.;|;tar;-C;/home/httpd;-x

###;After;this;you;can;start;the;web-server.

--------------------------------------------------------------------------------
注释
--------------------------------------------------------------------------------
请记住一件事如果你改变了数据备份你的数据 。在系统重新启动时 , 所有的变化将失去 。
应该设置一个cron进程 。使它每十分钟时检查一下文件是否有变动 , 并且备份这些改动 。另一种可能是你对真实的目录进行了改动 , 所以要拷贝这些变动到ramdisk 。这样做也许更安全 。;
一个非常酷的做法是用一个有1G内存的计算机并且将其中的256M作为"/tmp".;如果你有很多的进程使用"/tmp" , 这样做会提高你的系统速度 。同时 , /tmp目录中的东西将在系统重新启动的时候被删除 , 这可是一件好事 。;
Linux使用所有没有被程序使用的内存作为一个缓存 , 但我的经历告诉我ramdisk将会更多的提高速度 。;

--------------------------------------------------------------------------------

推荐阅读