Skip to content

Instantly share code, notes, and snippets.

@whitekid
Last active September 21, 2015 09:04
Show Gist options
  • Save whitekid/aeb3043d235b48ebb5c5 to your computer and use it in GitHub Desktop.
Save whitekid/aeb3043d235b48ebb5c5 to your computer and use it in GitHub Desktop.
swap file creation & resize
#!/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