在Linux中使用shell腳本自動(dòng)創(chuàng)建/移除并掛載交換文件
幾天前我們寫了一篇關(guān)于在 Linux 中 3 種創(chuàng)建交換文件的方法,它們是常見的方法,但是需要人工操作。
今天我發(fā)現(xiàn)了一個(gè) Gary Stafford[1] 寫的 shell 小腳本(兩個(gè) shell 腳本,一個(gè)用于創(chuàng)建交換文件,另外一個(gè)用于移除交換文件),它可以幫助我們?cè)?Linux 中創(chuàng)建/移除并且自動(dòng)掛載交換文件。
默認(rèn)這個(gè)腳本創(chuàng)建并掛載 512MB 的交換文件。如果你想要更多的交換空間和不同的文件名,你需要相應(yīng)地修改腳本。修改腳本不是一件困難的事,因?yàn)檫@是一個(gè)容易上手而且很小的腳本。
推薦閱讀: Linux 中 3 種簡易創(chuàng)建或擴(kuò)展交換空間的方法
如何檢查當(dāng)前交換文件大小
使用 free[3] 和 swapon 命令檢查已經(jīng)存在交換空間。
- $ free -h
- total used free shared buff/cache available
- Mem: 2.0G 1.3G 139M 45M 483M 426M
- Swap: 2.0G 655M 1.4G
- $ swapon --show
- NAME TYPE SIZE USED PRIO
- /dev/sda5 partition 2G 655.2M -1
上面的輸出顯示我當(dāng)前的交換空間是 2GB。
創(chuàng)建交換文件
創(chuàng)建 create_swap.sh 文件并添加下面的內(nèi)容來自動(dòng)化交換空間的創(chuàng)建和掛載。
- $ nano create_swap.sh
- #!/bin/sh
- # size of swapfile in megabytes
- swapsize=1024
- # does the swap file already exist?
- grep -q "swapfile" /etc/fstab
- # if not then create it
- if [ $? -ne 0 ]; then
- echo 'swapfile not found. Adding swapfile.'
- fallocate -l ${swapsize}M /swapfile
- chmod 600 /swapfile
- mkswap /swapfile
- swapon /swapfile
- echo '/swapfile none swap defaults 0 0' >> /etc/fstab
- else
- echo 'swapfile found. No changes made.'
- fi
- echo '--------------------------------------------'
- echo 'Check whether the swap space created or not?'
- echo '--------------------------------------------'
- swapon --show
給文件添加執(zhí)行權(quán)限。
- $ sudo +x create_swap.sh
運(yùn)行文件來創(chuàng)建和掛載交換文件。
- $ sudo ./create_swap.sh
- swapfile not found. Adding swapfile.
- Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
- no label, UUID=d9004261-396a-4321-a45f-9923e3e1328c
- --------------------------------------------
- Check whether the swap space created or not?
- --------------------------------------------
- NAME TYPE SIZE USED PRIO
- /dev/sda5 partition 2G 954.1M -1
- /swapfile file 1024M 0B -2
你可以看到新的 1024M 的 swapfile。重啟系統(tǒng)以使用新的交換文件。
移除交換文件
如果不再需要交換文件,接著創(chuàng)建 remove_swap.sh 文件并添加下面的內(nèi)容來移除交換文件以及它的 /etc/fstab 掛載點(diǎn)。
- $ nano remove_swap.sh
- #!/bin/sh
- # does the swap file exist?
- grep -q "swapfile" /etc/fstab
- # if it does then remove it
- if [ $? -eq 0 ]; then
- echo 'swapfile found. Removing swapfile.'
- sed -i '/swapfile/d' /etc/fstab
- echo "3" > /proc/sys/vm/drop_caches
- swapoff -a
- rm -f /swapfile
- else
- echo 'No swapfile found. No changes made.'
- fi
- echo '--------------------------------------------'
- echo 'Check whether the swap space removed or not?'
- echo '--------------------------------------------'
- swapon --show
并給文件添加可執(zhí)行權(quán)限。
- $ sudo +x remove_swap.sh
運(yùn)行腳本來移除并卸載交換文件。
- $ sudo ./remove_swap.sh
- swapfile found. Removing swapfile.
- swapoff: /dev/sda5: swapoff failed: Cannot allocate memory
- --------------------------------------------
- Check whether the swap space removed or not?
- --------------------------------------------
- NAME TYPE SIZE USED PRIO
- /dev/sda5 partition 2G 951.8M -1