Swap Creator


Recommended Posts

This script adds a swap file to the cache disk and enables swapping, which can reduce some memory usage. Swap needs some time to get used and its more used by systems with small RAM sizes / higher RAM usage, but it is also used if there is plenty of free RAM. If your system writes often to the swap it can wear out your SSD. A swap can avoid stalling systems or processes being killed if the RAM is fully utilized.

 

The following script should be executed by the User Scripts Plugin "At first Array Start only":

 

#!/bin/bash
# #####################################
# Script:      Swap Creator v0.1
# Description: Creates swap file on cache to reduce RAM usage
# Author:      Marc Gutt
# ######### Settings ##################
swapfile='/mnt/cache/swapfile'
swapfile_size=16G
# #####################################
# 
# ######### Script ####################
# make script race condition safe
if [[ -d "/tmp/${0///}" ]] || ! mkdir "/tmp/${0///}"; then exit 1; fi; trap 'rmdir "/tmp/${0///}"' EXIT;
# create swap file https://linuxize.com/post/create-a-linux-swap-file/
if ! [ -f "$swapfile" ]; then
    fallocate -l "$swapfile_size" "$swapfile"
    chmod 600 "$swapfile"
    mkswap "$swapfile"
    echo "Swap file created"
else
    echo "Swap file already exists"
fi
# enable swap
if [[ $(swapon --show) == *"${swapfile}"* ]]; then
    echo "Swap is already enabled"
else
    swapon "$swapfile"
    echo "Swap has been enabled"
fi

 

 

Screenshot of User Scripts Plugin:

1076366012_2021-03-1609_53_31.png.95e50afbb99a706c267a668b4d8e397e.png

 

Test swap by executing:

free -m -h

 

Before:

              total        used        free      shared  buff/cache   available
Mem:           62Gi       3.0Gi       7.1Gi       1.4Gi        52Gi        57Gi
Swap:           0           0           0

 

After (several days):

              total        used        free      shared  buff/cache   available
Mem:           62Gi       3.0Gi       7.1Gi       1.4Gi        52Gi        57Gi
Swap:          15Gi         1Gi        14Gi

 

If you like to disable swap:

swapoff -a

 

If you like to delete the swapfile:

rm /mnt/cache/swapfile

 

  • Like 1
Link to comment
  • 2 months later...

Workaround for "swapon failed: invalid argument"

 

#!/bin/bash
# #####################################
# Script:      Swap Creator v1.1
# Description: Creates swap file on cache to reduce RAM usage
# Author:      Marc Gutt
# edited by:   Hugo Martins
# ######### Settings ##################
swapfile='/mnt/cache/swapfile'
swapfile_size=6G
# #####################################
# 
# ######### Script ####################
# make script race condition safe
if [[ -d "/tmp/${0///}" ]] || ! mkdir "/tmp/${0///}"; then exit 1; fi; trap 'rmdir "/tmp/${0///}"' EXIT;
# create swap file https://linuxize.com/post/create-a-linux-swap-file/
if ! [ -f "$swapfile" ]; then
    #fallocate -l "$swapfile_size" "$swapfile"
    truncate -s 0 "$swapfile"
    chattr +C "$swapfile"
    btrfs property set "$swapfile" compression none
    dd if=/dev/zero of="$swapfile" bs=1M count=6144 status=progress
    chmod 600 "$swapfile"
    mkswap "$swapfile"
    echo "Swap file created"
else
    echo "Swap file already exists"
fi
# enable swap
if [[ $(swapon --show) == *"${swapfile}"* ]]; then
    echo "Swap is already enabled"
else
    swapon "$swapfile"
    echo "Swap has been enabled"
    echo "Finally, edit the fstab configuration to add an entry for the swap file:"
    echo "nano /etc/fstab"
    echo "to add"
    echo "/swapfile none swap defaults 0 0"
fi

 

After running:

 

              total        used        free      shared  buff/cache   available
Mem:           15Gi       5.0Gi       409Mi       1.0Gi        10Gi       9.3Gi
Swap:         6.0Gi        16Mi       6.0Gi

 

image.thumb.png.6e77219e12329e41e0306969f7cc7eb7.png

 

 

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.