s4ros icon

create-swap.sh

s4ros | PRO | 02/07/22 11:04:19 AM UTC | 0 ⭐ | 1131 👁️ | Never ⏰ | []
Bash |

470 B

|

None

|

0 👍

/

0 👎

#!/bin/bash
 
if [[ $1 == "-d" ]];then
  shift
  set -x
fi
 
[ $(id -u) -ne 0 ] && {
  echo You need to be root; exit 1
}
 
_usage() {
  echo "
Usage: $0 [-d] <size>
  -d  - (optional) debug mode
  <size> - size of the SWAP file in GB
  "
}
 
if [[ $# -lt 1 ]]; then
  _usage
  exit 1
fi
 
fallocate -l ${1}G /${1}GB.swap
chmod 0600 /${1}GB.swap
mkswap /${1}GB.swap
swapon /${1}GB.swap
cat << EOF | tee -a /etc/fstab
/${1}GB.swap none swap sw 0 0
EOF
 

Comments