{"id":65733,"date":"2024-09-13T19:06:41","date_gmt":"2024-09-13T18:06:41","guid":{"rendered":"https:\/\/ekiwi-blog.de\/65733\/setting-up-and-using-a-ram-disc-in-linux\/"},"modified":"2024-09-14T11:39:20","modified_gmt":"2024-09-14T10:39:20","slug":"setting-up-and-using-a-ram-disc-in-linux","status":"publish","type":"post","link":"https:\/\/ekiwi-blog.de\/en\/65733\/setting-up-and-using-a-ram-disc-in-linux\/","title":{"rendered":"Setting up and using a RAM disc in Linux"},"content":{"rendered":"<p>Script for setting up a RAM disc in Linux.<\/p>\n<p><!--more--><\/p>\n<p>In contrast to Windows, a RAM disc can be set up quite easily in Linux using board tools. Below you will find a script that will do this for you.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of content<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" 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: #999;color:#999\" 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><\/span><\/a><\/span><\/div>\n<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:\/\/ekiwi-blog.de\/en\/65733\/setting-up-and-using-a-ram-disc-in-linux\/#Video\" >Video<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ekiwi-blog.de\/en\/65733\/setting-up-and-using-a-ram-disc-in-linux\/#Setting_up_a_RAM_disc_in_Linux\" >Setting up a RAM disc in Linux<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ekiwi-blog.de\/en\/65733\/setting-up-and-using-a-ram-disc-in-linux\/#Size_and_configuration\" >Size and configuration<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ekiwi-blog.de\/en\/65733\/setting-up-and-using-a-ram-disc-in-linux\/#Start_of_the_script\" >Start of the script<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ekiwi-blog.de\/en\/65733\/setting-up-and-using-a-ram-disc-in-linux\/#The_code\" >The code<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Video\"><\/span>Video<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/ktd0c5ajO-Q?si=bzFRtk1YyeOCvaq1\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Setting_up_a_RAM_disc_in_Linux\"><\/span>Setting up a RAM disc in Linux<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We copy the code (below) into a new file, e.g. with the name &#8220;ramdisk.sh&#8221;. We then make the file executable.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/09\/ramdisk_linux_1.png\" alt=\"\" width=\"957\" height=\"535\" class=\"aligncenter size-full wp-image-65717\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/09\/ramdisk_linux_1.png 957w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/09\/ramdisk_linux_1-300x168.png 300w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/09\/ramdisk_linux_1-768x429.png 768w\" sizes=\"auto, (max-width: 957px) 100vw, 957px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Size_and_configuration\"><\/span>Size and configuration<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>We can define the size and mount point for integration at the beginning of the file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/09\/ramdisk_linux_2.png\" alt=\"\" width=\"548\" height=\"243\" class=\"aligncenter size-full wp-image-65721\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/09\/ramdisk_linux_2.png 548w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/09\/ramdisk_linux_2-300x133.png 300w\" sizes=\"auto, (max-width: 548px) 100vw, 548px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Start_of_the_script\"><\/span>Start of the script<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Ideally, we start the script via a terminal. We can now use the menu to mount the RAM disc, remove it from the system or check the status.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/09\/ramdisk_linux_3.png\" alt=\"\" width=\"853\" height=\"339\" class=\"aligncenter size-full wp-image-65725\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/09\/ramdisk_linux_3.png 853w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/09\/ramdisk_linux_3-300x119.png 300w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/09\/ramdisk_linux_3-768x305.png 768w\" sizes=\"auto, (max-width: 853px) 100vw, 853px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"The_code\"><\/span>The code<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Alternatively, you can also find the script as a ZIP file here:<\/p>\n<p><a href=\"https:\/\/ekiwi-blog.de\/65716\/ram-disk-in-linux-einrichten-und-nutzen\/ramdisk\/\" rel=\"attachment wp-att-65729\">ramdisk.sh<\/a><\/p>\n<pre>#!\/bin\/bash\r\n\r\n# Define variables\r\nMOUNT_POINT=&quot;\/mnt\/ramdisk&quot;\r\nSIZE=&quot;1G&quot; # You can adjust this size (e.g., 512M, 2G)\r\n\r\n# Function to create and mount tmpfs RAM disk\r\ncreate_ramdisk() {\r\n    if [ ! -d &quot;$MOUNT_POINT&quot; ]; then\r\n        echo &quot;Creating mount point at $MOUNT_POINT...&quot;\r\n        sudo mkdir -p &quot;$MOUNT_POINT&quot;\r\n    fi\r\n\r\n    echo &quot;Mounting tmpfs at $MOUNT_POINT with size $SIZE...&quot;\r\n    sudo mount -t tmpfs -o size=$SIZE tmpfs $MOUNT_POINT\r\n\r\n    echo &quot;RAM disk mounted at $MOUNT_POINT with size $SIZE&quot;\r\n}\r\n\r\n# Function to check the RAM disk\r\ncheck_ramdisk() {\r\n    echo &quot;Checking mounted RAM disks...&quot;\r\n    df -h | grep &quot;$MOUNT_POINT&quot;\r\n}\r\n\r\n# Function to unmount and clean up the RAM disk\r\nunmount_ramdisk() {\r\n    echo &quot;Unmounting RAM disk at $MOUNT_POINT...&quot;\r\n    sudo umount $MOUNT_POINT\r\n\r\n    if [ $? -eq 0 ]; then\r\n        echo &quot;Successfully unmounted $MOUNT_POINT&quot;\r\n    else\r\n        echo &quot;Failed to unmount $MOUNT_POINT&quot;\r\n    fi\r\n}\r\n\r\n# Main menu\r\necho &quot;Choose an option:&quot;\r\necho &quot;1. create and mount RAM disk&quot;\r\necho &quot;2. check RAM disk&quot;\r\necho &quot;3. unmount RAM disk&quot;\r\necho &quot;4. exit&quot;\r\n\r\nread -p &quot;Enter your choice [1-4]: &quot; choice\r\n\r\ncase $choice in\r\n    1)\r\n        create_ramdisk\r\n        ;;\r\n    2)\r\n        check_ramdisk\r\n        ;;\r\n    3)\r\n        unmount_ramdisk\r\n        ;;\r\n    4)\r\n        echo &quot;Exiting &quot;\r\n        ;;\r\n    *)\r\n        echo &quot;Invalid option!&quot;\r\n        ;;\r\nesac\r\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>Script for setting up a RAM disc in Linux.<\/p>\n","protected":false},"author":1,"featured_media":23181,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1555],"tags":[1682,2893,1559],"class_list":["post-65733","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-en","tag-linux-en","tag-script-en","tag-tutorial-en"],"_links":{"self":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/65733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/comments?post=65733"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/65733\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media\/23181"}],"wp:attachment":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media?parent=65733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/categories?post=65733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/tags?post=65733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}