网普技术论坛 网普科技  
» 游客:   网普主页 | 注册 | 登录 | 帮助
 

作者:
标题: [zt]在Linux下使用RamDisk 上一主题 | 下一主题
网普科技
网普管理员

网普科技人民公仆


积分 3080
发贴 2863
注册 2004-6-26
来自 网普科技
状态 离线
#1  [zt]在Linux下使用RamDisk

一、什么是RamDisk
顾名思义,Ram:内存,Disk:磁盘,RamDisk就是指使用你的一部分内存空间来模拟出一个硬盘分区。不过这在硬盘越来越便宜的今天好象并不是很有用,但现在内存也越来越便宜呀,对于一些我们想让其访问速度很高的情况下,还是可以试一试的。

二、创建一个RamDisk
其实创建一个RamDisk是一件很简单的事,由于RedHat Linux 6.0在默认安装下就支持了RamDisk,你只需要格式化一个RamDisk,并将其mount到某一个目录上就可以了。
1、 查一下所有可用的ramdisk:
ls –al /dev/ram*
这就会列出你现在可用的ramdisk,这些ramdisk现在还不占用内存,除非你对它进行格式化。
2、 创建一个RamDisk:
mke2fs /dev/ram0
执行该命令将出现以下提示
mke2fs 1.14, 9-Jan-1999 for EXT2 FS 0.5b, 95/08/09
Linux ext2 filesystem format
Filesystem label=
1024 inodes, 4096 blocks
204 blocks (4.98%) reserved for the super user
First data block=1
Block size=1024 (log=0)
Fragment size=1024 (log=0)
1 block group
8192 blocks per group, 8192 fragments per group
1024 inodes per group
如果该操作执行失败,那么有可能是因为你的内核不支持ramdisk,这时你只有重新配置、编译内核。在配置时将CONFIG_BLK_DEV_RAM设置为Enable。
3、 将其mount上来使用:
mkdir /tmp/ramdisk0 新建一个目录
mount /dev/ram0 /tmp/ramdisk0 将其mount到刚才新建的目录上
这样,我们就可以使用这个新建的RamDisk--/tmp/ramdisk0

另外,我们可以执行df命令来查看一下该RamDisk的大小:
>df -k /dev/ram0
Filesystem 1k-blocks Used Available Use% Mounted on
/dev/ram0 3963 13 3746 0% /tmp/ramdisk0
要注意的一点是:由于这个部分是内存,因此在系统重新启动的时候,将刷新这个区域。所以不要将任何没有拷贝的数据放在这个区域。如果你对这个目录进行了修改,并且需要保留这些修改,采取一些办法进行备份。也就是说,这个目录只能用于为了提高访问速度而暂时存储数据的地方。

三、创建大小合适的RamDisk
上面我们创建了一个RamDisk,但是无法指定大小,现在我们来学习如何创建大小合适的RamDisk。

1、 使用LILO配置:
我们在lilo.conf文件中加入一行:
ramdisk_size=10000 (如果是老版内核,则写ramdisk=10000)
然后运行 /sbin/lilo 生成新的LILO。
这样当你重新启动计算机之后,ramdisk的默认大小将会是10M。

2、使用/etc/conf.modules配置:
如果你的内核是以模块的形式编译ramdisk的话,那么就可以在加载的时候决定ramdisk的大小。也就是可以通过修改/etc/conf.modules 的选项设置来做到,加入:
options rd rd_size=10000
当然也可以在命令行中指定参数给ismod来实现:
insmod rd rd_size=10000

当使用以上两种方法创建了合适大小的ramdisk时,我们还需要执行以下命令来使用它:
mke2fs /dev/ram0
mkdir /tmp/ramdisk0
mount /dev/ram0 /tmp/ramdisk0
而当不使用时,应执行:
umount /tmp/ramdisk0
若是使用insmod来加载模块的,应该再执行:
rmmod rd

四、使用RamDisk的一个实例
下面,我们通过一个使用Ramdisk做WEB服务器的实例来说明其应用。
1、 首先将WEB服务器移到另外一个地方
mv /home/httpd/ /home/httpd_real
mkdir /home/httpd
mkdir /home/httpd/cgi-bin
mkdir /home/httpd/html
mkdir /home/httpd/icons
2、 将以下命令加入到/etc/rc.d/init.d/httpd.init中去:
/sbin/mkfs -t ext2 /dev/ram0
/sbin/mkfs -t ext2 /dev/ram1
/sbin/mkfs -t ext2 /dev/ram2
mount /dev/ram0 /home/httpd/cgi-bin
mount /dev/ram1 /home/httpd/icons
mount /dev/ram2 /home/httpd/html
tar -C /home/httpd_real -c . | tar -C /home/httpd -x

这样就可以了,但是请记住,你更新数据时,应更新httpd_real目录,而不要更新httpd目录,否则在系统重新启动时,所有的更新都将丢失。你最好设置一个cron进程,让其监视httpd_real是否有改变,一有改变就将其复制到ramdisk中去。

五、使用RamDisk做/tmp目录
我还想推荐一个更酷的方法,如果你的内存太多,那么可以将其中一部分做为/tmp目录,这样将大大提高你的系统执行速度。而且,/tmp将会在系统重新启动时被删除,多么惬意呀。



天理路上甚宽,稍游心,胸中便觉广大宏朗;
人欲路上甚窄,才寄迹,眼前俱是荆棘泥涂。



网普科技,优质美国主机服务!
美国Linux主机,美国虚拟主机
支持PHP+MYSQL+cPanel+EMAIL
为用户负责,拒绝反动、赌博及色情内容! QQ:126818

发送QQ消息
2005-8-18 01:42 PM
查看资料  访问主页  发短消息  QQ   编辑帖子  引用回复 顶部
网普科技
网普管理员

网普科技人民公仆


积分 3080
发贴 2863
注册 2004-6-26
来自 网普科技
状态 离线
#2  Introduction to RamDisk

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.

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 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.

Here are some more resources to help you.

   1. http://metalab.unc.edu/LDP/HOWTO/Kernel-HOWTO.html
   2. http://metalab.unc.edu/LDP/HOWTO/mini/LILO.html
   3. /usr/src/linux/Documentation/ramdisk.txt


How to use RamDisk
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.

# create a mount point:
mkdir /tmp/ramdisk0
# create a filesystem:
mke2fs /dev/ram0
# mount the ramdisk:
mount /dev/ram0 /tmp/ramdisk0

Those three commands will make a directory for the ramdisk , format the ramdisk (create a filesystem), 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.
If the formatting of the ramdisk faild then you might have no support for ramdisk compiled into the Kernel. The Kernel configuration option for ramdisk is CONFIG_BLK_DEV_RAM .

The default size of the ramdisk is 4Mb=4096 blocks. You saw what ramdisk size you got while you were running mke2fs. mke2fs /dev/ram0 should have produced a message like this:

mke2fs 1.14, 9-Jan-1999 for EXT2 FS 0.5b, 95/08/09
Linux ext2 filesystem format
Filesystem label=
1024 inodes, 4096 blocks
204 blocks (4.98%) reserved for the super user
First data block=1
Block size=1024 (log=0)
Fragment size=1024 (log=0)
1 block group
8192 blocks per group, 8192 fragments per group
1024 inodes per group

Running df -k /dev/ram0 tells you how much of that you can really use (The filesystem takes also some space):

>df -k /dev/ram0
Filesystem  1k-blocks  Used Available Use% Mounted on
/dev/ram0        3963    13      3746   0% /tmp/ramdisk0

What are some catches? Well, when the computer reboots, it gets wiped. Don't put any data there that isn't copied somewhere else. If you make changes to that directory, and you need to keep the changes, figure out some way to back them up.   
Changing the size of the ramdisks
To use a ram disk you either need to have ramdisk support compiled into the Kernel or you need to compile it as loadable module. The Kernel configuration option is CONFIG_BLK_DEV_RAM . Compiling the ramdisk a loadable module has the advantage that you can decide at load time what the size of your ramdisks should be.

Okay, first the hard way. Add this line to your lilo.conf file:
   ramdisk_size=10000 (or ramdisk=10000 for old kernels)
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.

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

Actually, I got a little over 9 megs of usable space as the filesystem takes also a little space.

When you compile ramdisk support as loadable module then you can decide at load time what the size should be. This is done either with an option line in the /etc/conf.modules file:

options rd rd_size=10000

or as a command line parameter to ismod:

insmod rd rd_size=10000

Here is an example which shows how to use the module:

   1. Unmount the ramdisk mounted in the previous chapter, umount /tmp/ramdisk0 .
   2. Unload the module (it was automatically loaded in the previous chapter), rmmod rd
   3. Load the ramdisk module and set the size to 20Mb, insmod rd rd_size=20000
   4. create a file system, mke2fs /dev/ram0
   5. mount the ramdisk, mount /dev/ram0 /tmp/ramdisk0


Example of how to use a RamDisk for a webserver.
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.
First, issue this command to move the real copy of the document root directory of your webserver to a different place. Also, make the directories to mount the ramdisks .

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

Then, add these commands to the start procedure in your /etc/rc.d/init.d/httpd.init (or where ever the httpd gets started on your system):

        ### 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.



天理路上甚宽,稍游心,胸中便觉广大宏朗;
人欲路上甚窄,才寄迹,眼前俱是荆棘泥涂。



网普科技,优质美国主机服务!
美国Linux主机,美国虚拟主机
支持PHP+MYSQL+cPanel+EMAIL
为用户负责,拒绝反动、赌博及色情内容! QQ:126818

发送QQ消息
2005-8-18 01:46 PM
查看资料  访问主页  发短消息  QQ   编辑帖子  引用回复 顶部
茱莉娅
THE BODY SHOP美容顾问

茱莉娅美体小铺


积分 3080
发贴 2863
注册 2009-5-21
来自 茱莉娅美体小铺
状态 离线
#2  赞助商信息The body shop

茱莉娅美体小铺
茱莉娅美体小铺淘宝店
茱莉娅美体小铺


茱莉娅美体小铺淘宝店
2005-8-18 01:46 PM
查看资料  访问主页  发短消息  QQ   编辑帖子  引用回复 顶部


可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题



论坛跳转:  




Powered by Discuz! 2.5 © 2001-2005 Comsenz Technology Ltd.
Processed in 0.022533 second(s), 7 queries, Gzip enabled
------------------------------------------------------------------------------
本论坛属网普科技交流与技术支持论坛!
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论!
美国主机, 美国虚拟主机, cPanel+PHP+Mysql+Ftp+Email+Zend+GD2+国际域名支持
技术支持 QQ: 126818 EMail & MSN: support[AT]netpu.net
[ 联系我们 ] - [ 网普科技 ]