在 Linux 中使用 shell 脚本自动创建/移除并挂载交换文件

YaoGUET 2017-07-01

在 Linux 中使用 shell 脚本自动创建/移除并挂载交换文件

几天前我们写了一篇关于在 Linux 中 3 种创建交换文件的方法,它们是常见的方法,但是需要人工操作。

今天我发现了一个 Gary Stafford 写的 shell 小脚本(两个 shell 脚本,一个用于创建交换文件,另外一个用于移除交换文件),它可以帮助我们在 Linux 中创建/移除并且自动挂载交换文件。

默认这个脚本创建并挂载 512MB 的交换文件。如果你想要更多的交换空间和不同的文件名,你需要相应地修改脚本。修改脚本不是一件困难的事,因为这是一个容易上手而且很小的脚本。

推荐阅读: Linux 中 3 种简易创建或扩展交换空间的方法

 

如何检查当前交换文件大小

使用 free 和 swapon 命令检查已经存在交换空间。

  1. <span class="pln">$ free </span><span class="pun">-</span><span class="pln">h</span>
  2. <span class="pln">total used free shared buff</span><span class="pun">/</span><span class="pln">cache available</span>
  3. <span class="typ">Mem</span><span class="pun">:</span><span class="lit">2.0G</span><span class="lit">1.3G</span><span class="lit">139M</span><span class="lit">45M</span><span class="lit">483M</span><span class="lit">426M</span>
  4. <span class="typ">Swap</span><span class="pun">:</span><span class="lit">2.0G</span><span class="lit">655M</span><span class="lit">1.4G</span>
  5. <span class="pln">$ </span><span class="kwd">swapon</span><span class="pun">--</span><span class="pln">show</span>
  6. <span class="pln">NAME TYPE SIZE USED PRIO</span>
  7. <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda5 partition </span><span class="lit">2G</span><span class="lit">655.2M</span><span class="pun">-</span><span class="lit">1</span>

上面的输出显示我当前的交换空间是 2GB

 

创建交换文件

创建 create_swap.sh 文件并添加下面的内容来自动化交换空间的创建和挂载。

  1. <span class="pln">$ </span><span class="kwd">nano</span><span class="pln"> create_swap</span><span class="pun">.</span><span class="pln">sh</span>
  2. <span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="pln">sh</span>
  3. <span class="com">#</span><span class="kwd">size</span><span class="pln"> of swapfile </span><span class="kwd">in</span><span class="pln"> megabytes</span>
  4. <span class="pln">swapsize</span><span class="pun">=</span><span class="lit">1024</span>
  5. <span class="com">#</span><span class="pln"> does the swap </span><span class="kwd">file</span><span class="pln"> already exist</span><span class="pun">?</span>
  6. <span class="kwd">grep</span><span class="pun">-</span><span class="pln">q </span><span class="str">"swapfile"</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">fstab</span>
  7. <span class="com">#</span><span class="kwd">if</span><span class="kwd">not</span><span class="kwd">then</span><span class="pln"> create it</span>
  8. <span class="kwd">if</span><span class="pun">[</span><span class="pln"> $</span><span class="pun">?</span><span class="pun">-</span><span class="pln">ne </span><span class="lit">0</span><span class="pun">];</span><span class="kwd">then</span>
  9. <span class="kwd">echo</span><span class="str">'swapfile not found. Adding swapfile.'</span>
  10. <span class="pln">fallocate </span><span class="pun">-</span><span class="pln">l $</span><span class="pun">{</span><span class="pln">swapsize</span><span class="pun">}</span><span class="pln">M </span><span class="pun">/</span><span class="pln">swapfile</span>
  11. <span class="kwd">chmod</span><span class="lit">600</span><span class="pun">/</span><span class="pln">swapfile</span>
  12. <span class="kwd">mkswap</span><span class="pun">/</span><span class="pln">swapfile</span>
  13. <span class="kwd">swapon</span><span class="pun">/</span><span class="pln">swapfile</span>
  14. <span class="kwd">echo</span><span class="str">'/swapfile none swap defaults 0 0'</span><span class="pun">>></span><span class="str">/etc/</span><span class="pln">fstab</span>
  15. <span class="kwd">else</span>
  16. <span class="kwd">echo</span><span class="str">'swapfile found. No changes made.'</span>
  17. <span class="kwd">fi</span>
  18. <span class="kwd">echo</span><span class="str">'--------------------------------------------'</span>
  19. <span class="kwd">echo</span><span class="str">'Check whether the swap space created or not?'</span>
  20. <span class="kwd">echo</span><span class="str">'--------------------------------------------'</span>
  21. <span class="kwd">swapon</span><span class="pun">--</span><span class="pln">show</span>

给文件添加执行权限。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pun">+</span><span class="pln">x create_swap</span><span class="pun">.</span><span class="pln">sh</span>

运行文件来创建和挂载交换文件。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pun">./</span><span class="pln">create_swap</span><span class="pun">.</span><span class="pln">sh</span>
  2. <span class="pln">swapfile </span><span class="kwd">not</span><span class="pln"> found</span><span class="pun">.</span><span class="typ">Adding</span><span class="pln"> swapfile</span><span class="pun">.</span>
  3. <span class="typ">Setting</span><span class="pln"> up swapspace version </span><span class="lit">1</span><span class="pun">,</span><span class="kwd">size</span><span class="pun">=</span><span class="lit">1024</span><span class="typ">MiB</span><span class="pun">(</span><span class="lit">1073737728</span><span class="pln"> bytes</span><span class="pun">)</span>
  4. <span class="kwd">no</span><span class="pln"> label</span><span class="pun">,</span><span class="pln"> UUID</span><span class="pun">=</span><span class="pln">d9004261</span><span class="pun">-</span><span class="lit">396a</span><span class="pun">-</span><span class="lit">4321</span><span class="pun">-</span><span class="pln">a45f</span><span class="pun">-</span><span class="lit">9923e3e1328c</span>
  5. <span class="pun">--------------------------------------------</span>
  6. <span class="typ">Check</span><span class="pln"> whether the swap space created </span><span class="kwd">or</span><span class="kwd">not</span><span class="pun">?</span>
  7. <span class="pun">--------------------------------------------</span>
  8. <span class="pln">NAME TYPE SIZE USED PRIO</span>
  9. <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda5 partition </span><span class="lit">2G</span><span class="lit">954.1M</span><span class="pun">-</span><span class="lit">1</span>
  10. <span class="pun">/</span><span class="pln">swapfile </span><span class="kwd">file</span><span class="lit">1024M</span><span class="lit">0B</span><span class="pun">-</span><span class="lit">2</span>

你可以看到新的 1024M 的 swapfile。重启系统以使用新的交换文件。

 

移除交换文件

如果不再需要交换文件,接着创建 remove_swap.sh 文件并添加下面的内容来移除交换文件以及它的 /etc/fstab 挂载点。

  1. <span class="pln">$ </span><span class="kwd">nano</span><span class="pln"> remove_swap</span><span class="pun">.</span><span class="pln">sh</span>
  2. <span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="pln">sh</span>
  3. <span class="com">#</span><span class="pln"> does the swap </span><span class="kwd">file</span><span class="pln"> exist</span><span class="pun">?</span>
  4. <span class="kwd">grep</span><span class="pun">-</span><span class="pln">q </span><span class="str">"swapfile"</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">fstab</span>
  5. <span class="com">#</span><span class="kwd">if</span><span class="pln"> it does </span><span class="kwd">then</span><span class="pln"> remove it</span>
  6. <span class="kwd">if</span><span class="pun">[</span><span class="pln"> $</span><span class="pun">?</span><span class="pun">-</span><span class="pln">eq </span><span class="lit">0</span><span class="pun">];</span><span class="kwd">then</span>
  7. <span class="kwd">echo</span><span class="str">'swapfile found. Removing swapfile.'</span>
  8. <span class="kwd">sed</span><span class="pun">-</span><span class="pln">i </span><span class="str">'/swapfile/d'</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">fstab</span>
  9. <span class="kwd">echo</span><span class="str">"3"</span><span class="pun">></span><span class="str">/proc/</span><span class="pln">sys</span><span class="pun">/</span><span class="pln">vm</span><span class="pun">/</span><span class="pln">drop_caches</span>
  10. <span class="kwd">swapoff</span><span class="pun">-</span><span class="pln">a</span>
  11. <span class="kwd">rm</span><span class="pun">-</span><span class="pln">f </span><span class="pun">/</span><span class="pln">swapfile</span>
  12. <span class="kwd">else</span>
  13. <span class="kwd">echo</span><span class="str">'No swapfile found. No changes made.'</span>
  14. <span class="kwd">fi</span>
  15. <span class="kwd">echo</span><span class="str">'--------------------------------------------'</span>
  16. <span class="kwd">echo</span><span class="str">'Check whether the swap space removed or not?'</span>
  17. <span class="kwd">echo</span><span class="str">'--------------------------------------------'</span>
  18. <span class="kwd">swapon</span><span class="pun">--</span><span class="pln">show</span>

并给文件添加可执行权限。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pun">+</span><span class="pln">x remove_swap</span><span class="pun">.</span><span class="pln">sh</span>

运行脚本来移除并卸载交换文件。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pun">./</span><span class="pln">remove_swap</span><span class="pun">.</span><span class="pln">sh</span>
  2. <span class="pln">swapfile found</span><span class="pun">.</span><span class="typ">Removing</span><span class="pln"> swapfile</span><span class="pun">.</span>
  3. <span class="kwd">swapoff</span><span class="pun">:</span><span class="str">/dev/</span><span class="pln">sda5</span><span class="pun">:</span><span class="kwd">swapoff</span><span class="pln"> failed</span><span class="pun">:</span><span class="typ">Cannot</span><span class="pln"> allocate memory</span>
  4. <span class="pun">--------------------------------------------</span>
  5. <span class="typ">Check</span><span class="pln"> whether the swap space removed </span><span class="kwd">or</span><span class="kwd">not</span><span class="pun">?</span>
  6. <span class="pun">--------------------------------------------</span>
  7. <span class="pln">NAME TYPE SIZE USED PRIO</span>
  8. <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda5 partition </span><span class="lit">2G</span><span class="lit">951.8M</span><span class="pun">-</span><span class="lit">1</span>

via: http://www.2daygeek.com/shell-script-create-add-extend-swap-space-linux/

作者:2DAYGEEK 译者:geekpi 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关推荐