#!/bin/bash
swap_file=/data/swap/swapfile
swap_dir=$(dirname ${swap_file})
if ! [[ -d ${swap_dir} ]];then
mkdir -p ${swap_dir}
fi
if [[ -f ${swap_file} ]]; then
echo "${swap_file} already exits!"
exit 1
fi
# create swapfile
dd if=/dev/zero of=${swap_file} bs=2048 count=2048000 && \
/sbin/mkswap ${swap_file} && \
/sbin/swapon ${swap_file}
# add fstab
if ! grep $swap_file /etc/fstab; then
cp /etc/fstab /data/backup/fstab_bak
echo "${swap_file} swap swap defautls 0 0" >> /etc/fstab
fi
add_swap.sh