Last active
September 21, 2015 09:04
-
-
Save whitekid/aeb3043d235b48ebb5c5 to your computer and use it in GitHub Desktop.
swap file creation & resize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
swap_file=/swap0 | |
usage(){ | |
echo "Usage:" | |
echo " $0 0 disable swap file" | |
echo " $0 size create swap file" | |
} | |
make_swap(){ | |
local file=$1 | |
local size=$2 | |
dd if=/dev/zero of=${file} bs=1k count=${size} | |
mkswap ${file} | |
} | |
do_swap_on(){ | |
# swap size as kb | |
local swap_size=$1 | |
# minimal swap size | |
if [ ${swap_size} -lt 41 ]; then | |
swap_size=41 | |
fi | |
if [ -f ${swap_file} -a `grep -c "^${swap_file}" /proc/swaps` = '1' ]; then | |
current_size=$((`ls -l ${swap_file} | awk '{print $5}'` / 1024)) | |
if [ "$current_size" = "${swap_size}" ]; then | |
echo "swap already exists" | |
return | |
fi | |
fi | |
if ! grep -q "^${swap_file}" /proc/swaps; then | |
echo "create swap file ${swap_file}" | |
make_swap ${swap_file} ${swap_size} | |
swapon ${swap_file} | |
fi | |
if [ ! `du ${swap_file} | awk '{print $1}'` = "${swap_size}" ]; then | |
echo "resize_swap to ${swap_size}" | |
make_swap /swap.tmp ${swap_size} | |
swapon /swap.tmp | |
swapoff ${swap_file} | |
make_swap ${swap_file} ${swap_size} | |
swapon ${swap_file} | |
swapoff /swap.tmp | |
rm -f /swap.tmp | |
fi | |
} | |
if [ -z "$1" ]; then | |
usage | |
exit | |
fi | |
if [ "$1" = "0" ]; then | |
if grep -q "^${swap_file}" /proc/swaps; then | |
echo "disable swap ${swap_file}" | |
swapoff ${swap_file} | |
rm -f ${swap_file} | |
fi | |
else | |
do_swap_on $1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment