To add a swap file:
- open an ssh session (tideway user) and execute the command below:
su
- To choose the final/total/required swap (including files and partitions), modify an execute the command below:
required_swap_GiByte=32
- To choose the absolute path of the future swap file, modify an execute the command below:
new_swap_file=/mnt/addm/backup
df -h `dirname $new_swap_file`
If possible, don't put the swap file in the datastore partition (to optimize perfs) or /usr/tideway (to avoid saturations). The best is to put it in a partition that contains the logs, the transaction logs or the backup. Use the df output to make sure the swap file you are creating is not causing risks of saturation.
- Execute the commands below
free --gibi
existing_swap_KiByte=`cat /proc/meminfo | grep "SwapTotal" | awk '{print $2}'`
missing_swap_KiByte=$(($required_swap_GiByte*1024*1024-$existing_swap_KiByte))
if [ "$missing_swap_KiByte" -le 0 ]; then
echo "You already meet the requirement of $required_swap_GiByte GiByte of swap, no need to add more"
else
echo "You don't have enough swap, let's add it"
echo required_swap_GiByte=$required_swap_GiByte existing_swap_KiByte=$existing_swap_KiByte
echo "let's add $missing_swap_KiByte KiBytes of swap in a file"
dd if=/dev/zero of=$new_swap_file bs=1024 count=$(($missing_swap_KiByte+4))
/sbin/mkswap $new_swap_file
/sbin/swapon $new_swap_file
free --gibi
echo "$new_swap_file swap swap defaults 0 0" >> /etc/fstab
sudo -u tideway tw_service_control --restart
fi
cat /etc/fstab
It is recommended to carefully review the script output.
To remove the swap file:
- execute "swapon" to identify the absolute path of the swap file(s)
- modifiy and execute the commands below
su
swapoff <absolute swap file name>rm -f <absolute swap file name>
- edit /etc/fstab
- remove the line(s) about the swap file(s)
Alternative method to creates a 8gb swap file on an existing partition named "/mnt/addm/xxxxxx" .
- Determine the best partition on which to add the swap space. To do this, enter:
df -h - Become root user
su - root - Check the current swap allocation:
free -m - Create an 8gb swap file named /mnt/addm/xxxxxx/swapfile. Change the file name and size(count) as needed. To calculate the "count" : Xgig * 1048576 = count. For example:
8gb = 1048576 * 8 = 8388608
16gb = 1048576 * 16 = 16777216
24gb = 1048576 * 24 = 25165824
32gb = 1048576 * 32 = 33554432
- In the following commands, replace the bold filename with the desired name of the swap file. Replace the highlighted number with the count calculated above.
dd if=/dev/zero of=/mnt/addm/xxxxxx/swapfile bs=1024 count=8388608
/sbin/mkswap /mnt/addm/xxxxxx/swapfile
/sbin/swapon /mnt/addm/xxxxxx/swapfile
- Edit the /etc/fstab file to add a line to the end of the file, so that the swap space will persist after a reboot. The command below edits the file without using vi :
echo "/mnt/addm/xxxxxx/swapfile swap swap defaults 0 0" >> /etc/fstab
cat /etc/fstab
- Check the final swap allocation:
free -m
- Restart services to see the increased swap size in the UI
sudo -u tideway tw_service_control --restart