在 Linux 中使用 shell 脚本自动创建/移除并挂载交换文件
几天前我们写了一篇关于在 Linux 中 3 种创建交换文件的方法,它们是常见的方法,但是需要人工操作。
今天我发现了一个 Gary Stafford 写的 shell 小脚本(两个 shell 脚本,一个用于创建交换文件,另外一个用于移除交换文件),它可以帮助我们在 Linux 中创建/移除并且自动挂载交换文件。
默认这个脚本创建并挂载 512MB 的交换文件。如果你想要更多的交换空间和不同的文件名,你需要相应地修改脚本。修改脚本不是一件困难的事,因为这是一个容易上手而且很小的脚本。
推荐阅读: Linux 中 3 种简易创建或扩展交换空间的方法
如何检查当前交换文件大小
使用 free 和 swapon
命令检查已经存在交换空间。
<span class="pln">$ free </span><span class="pun">-</span><span class="pln">h</span>
<span class="pln">total used free shared buff</span><span class="pun">/</span><span class="pln">cache available</span>
<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>
<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>
<span class="pln">$ </span><span class="kwd">swapon</span><span class="pun">--</span><span class="pln">show</span>
<span class="pln">NAME TYPE SIZE USED PRIO</span>
<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
文件并添加下面的内容来自动化交换空间的创建和挂载。
<span class="pln">$ </span><span class="kwd">nano</span><span class="pln"> create_swap</span><span class="pun">.</span><span class="pln">sh</span>
<span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="pln">sh</span>
<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>
<span class="pln">swapsize</span><span class="pun">=</span><span class="lit">1024</span>
<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>
<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>
<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>
<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>
<span class="kwd">echo</span><span class="str">'swapfile not found. Adding swapfile.'</span>
<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>
<span class="kwd">chmod</span><span class="lit">600</span><span class="pun">/</span><span class="pln">swapfile</span>
<span class="kwd">mkswap</span><span class="pun">/</span><span class="pln">swapfile</span>
<span class="kwd">swapon</span><span class="pun">/</span><span class="pln">swapfile</span>
<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>
<span class="kwd">else</span>
<span class="kwd">echo</span><span class="str">'swapfile found. No changes made.'</span>
<span class="kwd">fi</span>
<span class="kwd">echo</span><span class="str">'--------------------------------------------'</span>
<span class="kwd">echo</span><span class="str">'Check whether the swap space created or not?'</span>
<span class="kwd">echo</span><span class="str">'--------------------------------------------'</span>
<span class="kwd">swapon</span><span class="pun">--</span><span class="pln">show</span>
给文件添加执行权限。
<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>
运行文件来创建和挂载交换文件。
<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>
<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>
<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>
<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>
<span class="pun">--------------------------------------------</span>
<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>
<span class="pun">--------------------------------------------</span>
<span class="pln">NAME TYPE SIZE USED PRIO</span>
<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>
<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
挂载点。
<span class="pln">$ </span><span class="kwd">nano</span><span class="pln"> remove_swap</span><span class="pun">.</span><span class="pln">sh</span>
<span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="pln">sh</span>
<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>
<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>
<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>
<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>
<span class="kwd">echo</span><span class="str">'swapfile found. Removing swapfile.'</span>
<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>
<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>
<span class="kwd">swapoff</span><span class="pun">-</span><span class="pln">a</span>
<span class="kwd">rm</span><span class="pun">-</span><span class="pln">f </span><span class="pun">/</span><span class="pln">swapfile</span>
<span class="kwd">else</span>
<span class="kwd">echo</span><span class="str">'No swapfile found. No changes made.'</span>
<span class="kwd">fi</span>
<span class="kwd">echo</span><span class="str">'--------------------------------------------'</span>
<span class="kwd">echo</span><span class="str">'Check whether the swap space removed or not?'</span>
<span class="kwd">echo</span><span class="str">'--------------------------------------------'</span>
<span class="kwd">swapon</span><span class="pun">--</span><span class="pln">show</span>
并给文件添加可执行权限。
<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>
运行脚本来移除并卸载交换文件。
<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>
<span class="pln">swapfile found</span><span class="pun">.</span><span class="typ">Removing</span><span class="pln"> swapfile</span><span class="pun">.</span>
<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>
<span class="pun">--------------------------------------------</span>
<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>
<span class="pun">--------------------------------------------</span>
<span class="pln">NAME TYPE SIZE USED PRIO</span>
<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/
相关推荐
tianhuak 2020-11-24
以梦为马不负韶华 2020-10-20
彼岸随笔 2020-10-20
yutou0 2020-10-17
applecarelte 2020-10-16
ourtimes 2020-10-16
jarrygao 2020-11-02
shipinsky 2020-09-23
touchfuture 2020-09-16
hongsheyoumo 2020-09-04
huha 2020-10-16
lianshaohua 2020-09-23
laisean 2020-11-11
zhangjie 2020-11-11
大牛牛 2020-10-30
firefaith 2020-10-30
liguojia 2020-10-20
wangzhaotongalex 2020-10-20
CARBON 2020-10-20