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

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

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

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

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

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

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

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

  1. $ free -h
  2. total used free shared buff/cache available
  3. Mem:2.0G1.3G139M45M483M426M
  4. Swap:2.0G655M1.4G
  5. $ swapon--show
  6. NAME TYPE SIZE USED PRIO
  7. /dev/sda5 partition 2G655.2M-1

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

创建交换文件

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

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

给文件添加执行权限。

扫描二维码关注公众号,回复: 1959018 查看本文章
  1. $ sudo+x create_swap.sh

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

  1. $ sudo./create_swap.sh
  2. swapfile not found.Adding swapfile.
  3. Setting up swapspace version 1,size=1024MiB(1073737728 bytes)
  4. no label, UUID=d9004261-396a-4321-a45f-9923e3e1328c
  5. --------------------------------------------
  6. Check whether the swap space created ornot?
  7. --------------------------------------------
  8. NAME TYPE SIZE USED PRIO
  9. /dev/sda5 partition 2G954.1M-1
  10. /swapfile file1024M0B-2

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

移除交换文件

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

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

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

  1. $ sudo+x remove_swap.sh

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

  1. $ sudo./remove_swap.sh
  2. swapfile found.Removing swapfile.
  3. swapoff:/dev/sda5:swapoff failed:Cannot allocate memory
  4. --------------------------------------------
  5. Check whether the swap space removed ornot?
  6. --------------------------------------------
  7. NAME TYPE SIZE USED PRIO
  8. /dev/sda5 partition 2G951.8M-1

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

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

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

猜你喜欢

转载自www.linuxidc.com/Linux/2017-07/145332.htm
今日推荐