如何设置交换区swap空间

时间:2022-12-27 18:58:00


#!/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