{"id":211738,"date":"2021-03-26T16:00:47","date_gmt":"2021-03-26T13:00:47","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-create-a-ram-drive-in-linux-cloudsavvy-it\/"},"modified":"2021-03-26T16:00:47","modified_gmt":"2021-03-26T13:00:47","slug":"how-to-create-a-ram-drive-in-linux-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-create-a-ram-drive-in-linux-cloudsavvy-it\/","title":{"rendered":"#How to Create a RAM Drive in Linux \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a29f4140baaf\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a29f4140baaf\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-create-a-ram-drive-in-linux-cloudsavvy-it\/#What_Is_a_RAM_Drive\" >What Is a RAM Drive?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-create-a-ram-drive-in-linux-cloudsavvy-it\/#RAM_Drive_vs_tmpfs_instance\" >RAM Drive vs tmpfs instance<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-create-a-ram-drive-in-linux-cloudsavvy-it\/#Creating_a_RAM_Drive\" >Creating a RAM Drive<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-create-a-ram-drive-in-linux-cloudsavvy-it\/#Wrapping_up\" >Wrapping up<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Create a RAM Drive in Linux \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<figure style=\"width: 650px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-10351 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/fc3d8ae8693977704c87956fbbf75807\/p\/uploads\/2021\/03\/1b8ab2c3.png\" alt=\"\" width=\"650\" height=\"316\" data-crediturl=\"https:\/\/www.shutterstock.com\/image-vector\/ram-memory-icon-smart-phone-computer-1937374165\" data-credittext=\"Shutterstock\/ Suncheli Project\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-vector\/ram-memory-icon-smart-phone-computer-1937374165\">Shutterstock\/ Suncheli Project<\/a><\/span><\/figcaption><\/figure>\n<p>In case you are wondering, RAM Drives and tmpfs instances are not the same. This article will explain the difference and show you how to create a RAM drive in Linux using the command line. Setup a fast RAM drive in minutes!<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Is_a_RAM_Drive\"><\/span>What Is a <i>RAM Drive<\/i>?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The RAM chips in your computer can be used to hold a virtual drive. This drive is then not stored on the hard disk but in RAM. Not only will the speed of this virtual disk be significantly faster the a normal disk (and especially when compared with an older type spinning disk, as the physical movement of parts inside a spinning disk cause additional delays), RAM chips do not wear as fast as disks, and again especially and all the more so with older type physical drives.<\/p>\n<p>Sounds too good to be true? Well, there is indeed a caveat; if you accidentally reboot your computer, or if it crashes, your data is all gone. RAM (Random Access Memory), the memory chips in your computer, require constant power in order to retain their information. RAM storage is considered volatile.<\/p>\n<p>In other words, RAM drives lend themselves to temporary <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lications or to specific optimizations. For example, when we use testing servers to test software, we set up a RAM drive to let the many concurrent tests go quicker. And, even if the server were to lose power, not much would be lost; we would simply start another test run.<\/p>\n<p>Another application is pre-loading often accessed data into a RAM drive. For example, if you have a server that constantly accesses a given read-only database (read-write may be more complex if the data needs to be kept), which normally resides on disk, then you could copy the read-only database to RAM automatically (with some automated scripting on sever startup for example, or with a cron job), and then let the database server use that data.<\/p>\n<p>In other words, one can summarize two primary use cases, one being caching (like our R\/O database example), the other being \u201cinvaluable\u201d data storage (like our testing example). You can then take one step further (a third use case if you will) and sync data back to disk at given intervals. For example, with the testing example, which also includes writes to the RAM storage, one could write the summary and\/or test data back to disk (permanent storage) at the end of every test completion.<\/p>\n<p>Another caveat with RAM drives is that they are limited to the size of memory in your system and likely less than that as you need other memory to run the operating system and other software.<\/p>\n<p>Sizing a RAM drive to more than \u2013 let\u2019s say an arbitrary 80-85% of system memory \u2013 may be asking for problems. Of course, if you have 256GB of RAM in your server, then even 90% allotted for a RAM drive would still leave more than 25GB for the operating system and applications. With only 4GB, a 90% allotment to RAM would leave 0.4GB (400MB), which is quite likely going to cause issues. It thus depends, to some extent, what the total memory of the machine is and how much will be needed for other software.<\/p>\n<p>A RAM drive furthermore does not work the same as tmpfs allocation\/instance.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"RAM_Drive_vs_tmpfs_instance\"><\/span>RAM Drive vs tmpfs instance<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A tmpfs can also be but does not have to be, stored in your computer\u2019s RAM chips. The common <code>\/dev\/shm<\/code> tmpfs mapping automatically set up with the installation of most if not all Linux operating systems is handy but does not function the same as a RAM drive.<\/p>\n<p>The difference between the two is that a RAM drive (the term <i>rigid<\/i> comes to mind) is 100% store in actual RAM chips, whereas tmpfs is stored into the Linux Kernel memory pool, which may include things like swap space, which is regularly located on disk. Whilst the kernel would likely optimize all access to the pool, it still gives the possibility of data being written either to physical RAM or physical DISK. And, if it goes to disk, it will be slower.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Creating_a_RAM_Drive\"><\/span>Creating a <i>RAM Drive<\/i><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Creating a RAM drive is relatively straightforward. You can create a little script called <code>ramdrive.sh<\/code>, with the following code:<\/p>\n<pre>#!\/bin\/bash&#13;\nif [ \"$(mount | grep -o \"\/mnt\/ram\")\" != \"\/mnt\/ram\" ]; then&#13;\n  sudo mkdir -p \/mnt\/ram&#13;\n  sudo mount -t ramfs -o size=1g ramfs \/mnt\/ram&#13;\n  sudo chown -R $(whoami):$(whoami) \/mnt\/ram&#13;\nfi&#13;\nmount | grep ram&#13;\n<\/pre>\n<p>And another script <code>umount_ram.sh<\/code>, with the following code:<\/p>\n<pre>#!\/bin\/bash&#13;\nsudo umount \/mnt\/ram&#13;\n<\/pre>\n<p>Let\u2019s have a look at the first script. First, we indicate we want to Bash as our command interpreter with the Shebang symbol (<code>#!<\/code>). If you would like to learn more about Shebang, have a look at Bash Automation &amp; Scripting Basics, our 3 part article on Bash automation and scripting.<\/p>\n<p>After this, we check if we already have a mount under <code>\/mnt\/ram<\/code> (the directory we will be using to mount our ram drive in), by using a grep-only (<code>grep -o<\/code>) of <code>\/mnt\/ram<\/code> in the full \u2018currently mounted\u2019 list, as displayed by <code>mount<\/code>. If the same is not found, then we proceed with three sudo commands. All three require <code>sudo<\/code>, though for various reasons.<\/p>\n<p>The first command requires sudo as it makes a directory possibly of root and at least in <code>\/mnt<\/code>, which are privileged\/protected directories. The next command, our actual RAM disk mount, and creation, requires sudo as mounting is a privileged operation. We set the size to 1GB by using <code>size=1g<\/code>. We also indicate we want a ramfs type drive (<code>-t ramfs<\/code>) coming from the ramfs device (as indicated by the second <code>ramfs<\/code>), and we finally list the mount point as <code>\/mnt\/ram<\/code>.<\/p>\n<p>In the third sudo-enabled command, we change the owner of the <code>\/mnt\/ram<\/code> directory (now our RAM drive, our ramfs mount point) to the current user and the current user\u2019s own group by using the <code>whoami<\/code> command twice. You may like to change this to the specific and\/or the specific group that will be using the ramdrive or to a wider group if more users will be using the ramdrive.<\/p>\n<p>After this, we finalize our conditional <code>if .. fi<\/code> command and do a final call to <code>mount<\/code> with a grep for <code>ram<\/code> to make sure the script reports back as to what was already mounted in terms of RAM, or what was mounted just now as the script executed. This is a handy\/quick verification that the script was successful when executed.<\/p>\n<p>Our secondary script, <code>umount_ram.sh<\/code>, unmounts the RAM-based drive with mount point <code>\/mnt\/ram<\/code>, i.e. the ramfs drive we just created. WARNING: executing this im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely drop\/deletes all data stored in volatile memory and remounting the RAMFS drive will not bring this back; it will simply create a brand new but empty RAM drive. Please be forewarned!<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Wrapping_up\"><\/span>Wrapping up<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this article, we discussed RAM\/ramfs (the same thing) drives and tmpfs instances\/allocations. We then created a 1GB ramfs drive under <code>\/mnt\/ram<\/code> using a small script to mount or RAM drive.<\/p>\n<p>If you like to continue reading on Linux, have a look at our Screen Recording in Linux with SimpleScreenRecorder or Bits, Bytes and Binary or From 0 to F: Hexadecimal articles!<\/p>\n<p><strong>Enjoy!<\/strong>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/10254\/how-to-create-a-ram-drive-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Create a RAM Drive in Linux \u2013 CloudSavvy IT&#8221; Shutterstock\/ Suncheli Project In case you are wondering, RAM Drives and tmpfs instances are not the same. This article will explain the difference and show you how to create a RAM drive in Linux using the command line. Setup a fast RAM drive in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":211739,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/03\/1b8ab2c3.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-211738","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/211738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=211738"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/211738\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/211739"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=211738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=211738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=211738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}