linux设置ramdisk模块,Linux系统中的RAMdisk设置

本文最后更新于2015年1月24日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!

内存盘:在你的内存有较多剩余的情况下,可以考虑使用RAMdisk的技术,将内存当作硬盘使用,提高程序运行速度。

搜索关键字:

参考链接:

先总结处理步骤:

1.设定Ramdisk支持&内存盘大小(需重启)

# dmesg | grep RAMDISK

# ls -l /dev/ram*

# vi /etc/grub.conf

...

kernel /vmlinuz-2.6.9-42.0.10.EL ro root=/dev/VolGroup00/LogVol00 ramdisk_size=131072

...

2.格式化内存盘

# mke2fs -m 0 /dev/ram0

3.挂载内存盘至某一指定目录

# mount /dev/ram0 /home/ramdisk

4.修改目录权限

# chown -R crazy:root /home/ramdisk

====

Ramdisk is very good to have if you want something to stay in memory. Files in memory makes it so you can access them without having to access hard drive all the time. Perfect candidates would be things which do not change eg. web images or downloadable files, etc. If you have Linux Kernel 2.4 or later, you already have support of ramdisk built in.{Ramdisk对于某些特定的应用(比如:Web图片、下载文件)来说是非常有用的,它可以让某些内容待在内存而不是硬盘中,从而提高访问速度,如果你的Linux内核是2.4及以后版本的,那已经内建了Ramdisk功能} You can check if ramdisk is setup by doing:

# dmesg | grep RAMDISK

RAMDISK driver initialized: 16 RAM disks of 16384K size 1024 blocksize

You should get above output on CentOS and RHEL. Other linux flavors will have similar output as well. If you would like to see how they are named and what you would need to refer to, do the following:

# ls -l /dev/ram*

lrwxrwxrwx 1 root root 4 Apr 24 12:05 /dev/ram -> ram1

brw-rw---- 1 root disk 1, 0 Apr 24 12:05 /dev/ram0

brw-rw---- 1 root disk 1, 1 Apr 24 12:05 /dev/ram1

brw-rw---- 1 root disk 1, 10 Apr 24 12:05 /dev/ram10

brw-rw---- 1 root disk 1, 11 Apr 24 12:05 /dev/ram11

brw-rw---- 1 root disk 1, 12 Apr 24 12:05 /dev/ram12

brw-rw---- 1 root disk 1, 13 Apr 24 12:05 /dev/ram13

brw-rw---- 1 root disk 1, 14 Apr 24 12:05 /dev/ram14

brw-rw---- 1 root disk 1, 15 Apr 24 12:05 /dev/ram15

brw-rw---- 1 root disk 1, 2 Apr 24 12:05 /dev/ram2

brw-rw---- 1 root disk 1, 3 Apr 24 12:05 /dev/ram3

brw-rw---- 1 root disk 1, 4 Apr 24 12:05 /dev/ram4

brw-rw---- 1 root disk 1, 5 Apr 24 12:05 /dev/ram5

brw-rw---- 1 root disk 1, 6 Apr 24 12:05 /dev/ram6

brw-rw---- 1 root disk 1, 7 Apr 24 12:05 /dev/ram7

brw-rw---- 1 root disk 1, 8 Apr 24 12:05 /dev/ram8

brw-rw---- 1 root disk 1, 9 Apr 24 12:05 /dev/ram9

lrwxrwxrwx 1 root root 4 Apr 24 12:05 /dev/ramdisk -> ram0

All those ramdisks listed have same size. In above example, they are all 16MB. Let us change that so we have more space allowed. Note that I say allowed and not allocated. We allocate space in one of the later steps by formatting one of the drives above. Let us set it up so we have 128 MB. Since this has to be in multiples of 1024, we will setup Ramdisk to have 131072K.{单位是:KB}

# vi /etc/grub.conf

Find first line which looks similar to following:

kernel /vmlinuz-2.6.9-42.0.10.EL ro root=/dev/VolGroup00/LogVol00

add ramdisk_size=131072 to the end of the line. Now your line should look like:

kernel /vmlinuz-2.6.9-42.0.10.EL ro root=/dev/VolGroup00/LogVol00 ramdisk_size=131072

Save and exit grub.conf. At this point you have it configured to have ramdisk with new size but it does not take effect until you reboot your system. Once you have rebooted your system, we can start doing rest of configurations.

# mke2fs -m 0 /dev/ram0

This will format the ram0 ramdrive for us to use. At this point, kernel will allocate space for you. Let us setup Ramdisk mount point so we can use it. We will also have it be owned by user “sunny” so that user can read/write to that mount.

mkdir /home/ramdisk

mount /dev/ram0 /home/ramdisk

chown -R crazy:root /home/ramdisk

At this point you should be able to type: mount and see your new Ramdisk drive mounted on /home/ramdisk

Remember that everything you put on this drive will be gone if you reboot your server.{请牢记:你放在这个drive上的任何东西都会在重启服务器之后丢失} If you unmounted the Ramdisk drive and remounted it, your files will still be there. It is because your system has that much ram set aside for your Ramdisk and will not use it for anything else.{不过如果你只是先卸载Ramdisk分区然后再次挂载,你的文件并不会丢失,因为系统已经将那块内存分配给了内存盘所以不会挪作他用,文件等信息也就不会丢失了} If you would like to setup Ramdisk the same next time you boot up, add these lines to your /etc/rc.local files.{如果你想在每次开机启动的时候自动启动Ramdisk,你可以在“/etc/rc.local”文件中添加以下几行进行设置}

# vim /etc/rc.local

mke2fs -m 0 /dev/ram0

mount /dev/ram0 /home/ramdisk

chown -R crazy:root /home/ramdisk

===========

DISCLAIMER(免责声明): Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值