且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-03-04 19:32:53

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

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

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

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

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

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

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


  1. free -h 
  2.  
  3.               total        used        free      shared  buff/cache   available 
  4.  
  5. Mem:           2.0G        1.3G        139M         45M        483M        426M 
  6.  
  7. Swap:          2.0G        655M        1.4G 
  8.  
  9. $ swapon --show 
  10.  
  11. NAME      TYPE      SIZE   USED PRIO 
  12.  
  13. /dev/sda5 partition   2G 655.2M   -1 

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

创建交换文件

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


  1. $ nano create_swap.sh 
  2.  
  3. #!/bin/sh 
  4.  
  5. size of swapfile in megabytes 
  6.  
  7. swapsize=1024 
  8.  
  9. # does the swap file already exist? 
  10.  
  11. grep -q "swapfile" /etc/fstab 
  12.  
  13. # if not then create it 
  14.  
  15. if [ $? -ne 0 ]; then 
  16.  
  17.     echo 'swapfile not found. Adding swapfile.' 
  18.  
  19.     fallocate -l ${swapsize}M /swapfile 
  20.  
  21.     chmod 600 /swapfile 
  22.  
  23.     mkswap /swapfile 
  24.  
  25.     swapon /swapfile 
  26.  
  27.     echo '/swapfile none swap defaults 0 0' >> /etc/fstab 
  28.  
  29. else 
  30.  
  31.     echo 'swapfile found. No changes made.' 
  32.  
  33. fi 
  34.  
  35. echo '--------------------------------------------' 
  36.  
  37. echo 'Check whether the swap space created or not?' 
  38.  
  39. echo '--------------------------------------------' 
  40.  
  41. swapon --show 

给文件添加执行权限。


  1. $ sudo +x create_swap.sh 

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


  1. $ sudo ./create_swap.sh 
  2.  
  3. swapfile not found. Adding swapfile. 
  4.  
  5. Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes) 
  6.  
  7. no label, UUID=d9004261-396a-4321-a45f-9923e3e1328c 
  8.  
  9. -------------------------------------------- 
  10.  
  11. Check whether the swap space created or not
  12.  
  13. -------------------------------------------- 
  14.  
  15. NAME      TYPE       SIZE   USED PRIO 
  16.  
  17. /dev/sda5 partition    2G 954.1M   -1 
  18.  
  19. /swapfile file      1024M     0B   -2 

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

移除交换文件

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


  1. $ nano remove_swap.sh 
  2.  
  3. #!/bin/sh 
  4.  
  5. # does the swap file exist? 
  6.  
  7. grep -q "swapfile" /etc/fstab 
  8.  
  9. # if it does then remove it 
  10.  
  11. if [ $? -eq 0 ]; then 
  12.  
  13.     echo 'swapfile found. Removing swapfile.' 
  14.  
  15.     sed -i '/swapfile/d' /etc/fstab 
  16.  
  17.     echo "3" > /proc/sys/vm/drop_caches 
  18.  
  19.     swapoff -a 
  20.  
  21.     rm -f /swapfile 
  22.  
  23. else 
  24.  
  25.     echo 'No swapfile found. No changes made.' 
  26.  
  27. fi 
  28.  
  29. echo '--------------------------------------------' 
  30.  
  31. echo 'Check whether the swap space removed or not?' 
  32.  
  33. echo '--------------------------------------------' 
  34.  
  35. swapon --show 

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


  1. $ sudo +x remove_swap.sh 

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


  1. $ sudo ./remove_swap.sh  
  2. swapfile found. Removing swapfile. 
  3.  
  4. swapoff: /dev/sda5: swapoff failed: Cannot allocate memory 
  5.  
  6. -------------------------------------------- 
  7.  
  8. Check whether the swap space removed or not
  9.  
  10. -------------------------------------------- 
  11.  
  12. NAME      TYPE      SIZE   USED PRIO 
  13.  
  14. /dev/sda5 partition   2G 951.8M   -1  



作者:geekpi译
来源:51CTO