{"id":1836,"date":"2015-01-24T00:53:39","date_gmt":"2015-01-23T16:53:39","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1836"},"modified":"2015-01-24T00:53:39","modified_gmt":"2015-01-23T16:53:39","slug":"linux%e7%b3%bb%e7%bb%9f%e4%b8%ad%e7%9a%84ramdisk%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1836.html","title":{"rendered":"Linux\u7cfb\u7edf\u4e2d\u7684RAMdisk\u8bbe\u7f6e"},"content":{"rendered":"<p><strong>\u5185\u5b58\u76d8<\/strong>\uff1a\u5728\u4f60\u7684\u5185\u5b58\u6709\u8f83\u591a\u5269\u4f59\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528RAMdisk\u7684\u6280\u672f\uff0c<span style=\"color: #ff0000;\">\u5c06\u5185\u5b58\u5f53\u4f5c\u786c\u76d8\u4f7f\u7528<\/span>\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u8fd0\u884c\u901f\u5ea6\u3002<\/p>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p><a href=\"http:\/\/search.aol.com\/aol\/search?q=centos+how+to+set+ramdisk\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=centos+how+to+set+ramdisk<\/a><\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/crazytoon.com\/2007\/06\/01\/ramdisk-how-do-you-install-and-set-up-ramdisk-under-linux-centos-rhel-fedora\/\" target=\"_blank\">http:\/\/crazytoon.com\/2007\/06\/01\/ramdisk-how-do-you-install-and-set-up-ramdisk-under-linux-centos-rhel-fedora\/ <\/a>\u00a0#\u7f51\u4e0a+\u6211\u8fd9\u7bc7\u6587\u7ae0\u7684\u5185\u5bb9\u57fa\u672c\u4e0a\u90fd\u662f\u4ece\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u6765\u7684\uff01<\/li>\n<li><a href=\"https:\/\/black-pixel.net\/set-up-a-ramdisk-on-centos-e-g-for-a-minecraft-server.html\" target=\"_blank\">https:\/\/black-pixel.net\/set-up-a-ramdisk-on-centos-e-g-for-a-minecraft-server.html<\/a><\/li>\n<li><a href=\"http:\/\/www.blogjava.net\/czihong\/articles\/376604.html\" target=\"_blank\">http:\/\/www.blogjava.net\/czihong\/articles\/376604.html<\/a><\/li>\n<\/ul>\n<hr \/>\n<p>\u5148\u603b\u7ed3\u5904\u7406\u6b65\u9aa4\uff1a<\/p>\n<h6>1.\u8bbe\u5b9aRamdisk\u652f\u6301&amp;\u5185\u5b58\u76d8\u5927\u5c0f(\u9700\u91cd\u542f)<\/h6>\n<pre class=\"lang:default decode:true\"># dmesg | grep RAMDISK\n\n# ls -l \/dev\/ram*\n\n# vi \/etc\/grub.conf\n...\nkernel \/vmlinuz-2.6.9-42.0.10.EL ro root=\/dev\/VolGroup00\/LogVol00 ramdisk_size=131072\n...<\/pre>\n<h6>2.\u683c\u5f0f\u5316\u5185\u5b58\u76d8<\/h6>\n<pre class=\"lang:default decode:true\"># mke2fs -m 0 \/dev\/ram0<\/pre>\n<h6>3.\u6302\u8f7d\u5185\u5b58\u76d8\u81f3\u67d0\u4e00\u6307\u5b9a\u76ee\u5f55<\/h6>\n<pre class=\"lang:default decode:true\"># mount \/dev\/ram0 \/home\/ramdisk<\/pre>\n<h6>4.\u4fee\u6539\u76ee\u5f55\u6743\u9650<\/h6>\n<pre class=\"lang:default decode:true\"># chown -R crazy:root \/home\/ramdisk<\/pre>\n<p>====<\/p>\n<p><a href=\"http:\/\/crazytoon.com\/2007\/06\/01\/ramdisk-how-do-you-install-and-set-up-ramdisk-under-linux-centos-rhel-fedora\/\" target=\"_blank\">Ramdisk: How do you install and set up Ramdisk under Linux (CentOS, RHEL, Fedora)?<\/a><\/p>\n<p>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\u5bf9\u4e8e\u67d0\u4e9b\u7279\u5b9a\u7684\u5e94\u7528(\u6bd4\u5982\uff1aWeb\u56fe\u7247\u3001\u4e0b\u8f7d\u6587\u4ef6)\u6765\u8bf4\u662f\u975e\u5e38\u6709\u7528\u7684\uff0c\u5b83\u53ef\u4ee5\u8ba9\u67d0\u4e9b\u5185\u5bb9\u5f85\u5728\u5185\u5b58\u800c\u4e0d\u662f\u786c\u76d8\u4e2d\uff0c\u4ece\u800c\u63d0\u9ad8\u8bbf\u95ee\u901f\u5ea6\uff0c\u5982\u679c\u4f60\u7684Linux\u5185\u6838\u662f2.4\u53ca\u4ee5\u540e\u7248\u672c\u7684\uff0c\u90a3\u5df2\u7ecf\u5185\u5efa\u4e86Ramdisk\u529f\u80fd} You can check if ramdisk is setup by doing:<\/p>\n<pre class=\"lang:default decode:true\"># dmesg | grep RAMDISK\nRAMDISK driver initialized: 16 RAM disks of 16384K size 1024 blocksize<\/pre>\n<p>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:<\/p>\n<pre class=\"lang:default decode:true\"># ls -l \/dev\/ram*\nlrwxrwxrwx 1 root root 4 Apr 24 12:05 \/dev\/ram -&gt; ram1\nbrw-rw---- 1 root disk 1, 0 Apr 24 12:05 \/dev\/ram0\nbrw-rw---- 1 root disk 1, 1 Apr 24 12:05 \/dev\/ram1\nbrw-rw---- 1 root disk 1, 10 Apr 24 12:05 \/dev\/ram10\nbrw-rw---- 1 root disk 1, 11 Apr 24 12:05 \/dev\/ram11\nbrw-rw---- 1 root disk 1, 12 Apr 24 12:05 \/dev\/ram12\nbrw-rw---- 1 root disk 1, 13 Apr 24 12:05 \/dev\/ram13\nbrw-rw---- 1 root disk 1, 14 Apr 24 12:05 \/dev\/ram14\nbrw-rw---- 1 root disk 1, 15 Apr 24 12:05 \/dev\/ram15\nbrw-rw---- 1 root disk 1, 2 Apr 24 12:05 \/dev\/ram2\nbrw-rw---- 1 root disk 1, 3 Apr 24 12:05 \/dev\/ram3\nbrw-rw---- 1 root disk 1, 4 Apr 24 12:05 \/dev\/ram4\nbrw-rw---- 1 root disk 1, 5 Apr 24 12:05 \/dev\/ram5\nbrw-rw---- 1 root disk 1, 6 Apr 24 12:05 \/dev\/ram6\nbrw-rw---- 1 root disk 1, 7 Apr 24 12:05 \/dev\/ram7\nbrw-rw---- 1 root disk 1, 8 Apr 24 12:05 \/dev\/ram8\nbrw-rw---- 1 root disk 1, 9 Apr 24 12:05 \/dev\/ram9\nlrwxrwxrwx 1 root root 4 Apr 24 12:05 \/dev\/ramdisk -&gt; ram0<\/pre>\n<p>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.{<span style=\"color: #ff0000;\">\u5355\u4f4d\u662f\uff1aKB<\/span>}<\/p>\n<pre class=\"lang:default decode:true\"># vi \/etc\/grub.conf<\/pre>\n<p>Find first line which looks similar to following:<\/p>\n<pre class=\"lang:default decode:true\">kernel \/vmlinuz-2.6.9-42.0.10.EL ro root=\/dev\/VolGroup00\/LogVol00<\/pre>\n<p>add ramdisk_size=131072 to the end of the line. Now your line should look like:<\/p>\n<pre class=\"lang:default decode:true\">kernel \/vmlinuz-2.6.9-42.0.10.EL ro root=\/dev\/VolGroup00\/LogVol00 ramdisk_size=131072<\/pre>\n<p>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.<\/p>\n<pre class=\"lang:default decode:true\"># mke2fs -m 0 \/dev\/ram0<\/pre>\n<p>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 \u201csunny\u201d so that user can read\/write to that mount.<\/p>\n<pre class=\"lang:default decode:true\">mkdir \/home\/ramdisk\nmount \/dev\/ram0 \/home\/ramdisk\nchown -R crazy:root \/home\/ramdisk<\/pre>\n<p>At this point you should be able to type: mount and see your new Ramdisk drive mounted on \/home\/ramdisk<\/p>\n<p>Remember that everything you put on this drive will be gone if you reboot your server.{<strong><span style=\"color: #ff0000;\">\u8bf7\u7262\u8bb0\uff1a\u4f60\u653e\u5728\u8fd9\u4e2adrive\u4e0a\u7684\u4efb\u4f55\u4e1c\u897f\u90fd\u4f1a\u5728\u91cd\u542f\u670d\u52a1\u5668\u4e4b\u540e\u4e22\u5931<\/span><\/strong>} 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.{<span style=\"color: #ff0000;\">\u4e0d\u8fc7\u5982\u679c\u4f60\u53ea\u662f\u5148\u5378\u8f7dRamdisk\u5206\u533a\u7136\u540e\u518d\u6b21\u6302\u8f7d\uff0c\u4f60\u7684\u6587\u4ef6\u5e76\u4e0d\u4f1a\u4e22\u5931\uff0c\u56e0\u4e3a\u7cfb\u7edf\u5df2\u7ecf\u5c06\u90a3\u5757\u5185\u5b58\u5206\u914d\u7ed9\u4e86\u5185\u5b58\u76d8\u6240\u4ee5\u4e0d\u4f1a\u632a\u4f5c\u4ed6\u7528\uff0c\u6587\u4ef6\u7b49\u4fe1\u606f\u4e5f\u5c31\u4e0d\u4f1a\u4e22\u5931\u4e86<\/span>} If you would like to setup Ramdisk the same next time you boot up, add these lines to your \/etc\/rc.local files.{<span style=\"color: #ff0000;\">\u5982\u679c\u4f60\u60f3\u5728\u6bcf\u6b21\u5f00\u673a\u542f\u52a8\u7684\u65f6\u5019\u81ea\u52a8\u542f\u52a8Ramdisk\uff0c\u4f60\u53ef\u4ee5\u5728\u201c\/etc\/rc.local\u201d\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u51e0\u884c\u8fdb\u884c\u8bbe\u7f6e<\/span>}<\/p>\n<pre class=\"lang:default decode:true\"># vim \/etc\/rc.local\n\nmke2fs -m 0 \/dev\/ram0\nmount \/dev\/ram0 \/home\/ramdisk\nchown -R crazy:root \/home\/ramdisk<\/pre>\n<p>===========<\/p>\n<p>DISCLAIMER(\u514d\u8d23\u58f0\u660e): 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5185\u5b58\u76d8\uff1a\u5728\u4f60\u7684\u5185\u5b58\u6709\u8f83\u591a\u5269\u4f59\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528RAMdisk\u7684\u6280\u672f\uff0c\u5c06\u5185\u5b58\u5f53\u4f5c\u786c\u76d8\u4f7f\u7528\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u8fd0\u884c\u901f\u5ea6\u3002 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,12],"tags":[30,446],"class_list":["post-1836","post","type-post","status-publish","format-standard","hentry","category-linux","category-tools","tag-linux","tag-ramdisk"],"views":3054,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=1836"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1836\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}