mgutt Posted March 16, 2021 Share Posted March 16, 2021 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: 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 1 Quote Link to comment
Squid Posted March 16, 2021 Share Posted March 16, 2021 Any real fundamental difference between this and the plugin? Quote Link to comment
mgutt Posted March 18, 2021 Author Share Posted March 18, 2021 On 3/16/2021 at 1:07 PM, Squid said: Any real fundamental difference between this and the plugin? Never tested the plugin. But I found out that swap breaks stopping the array if it isn't stopped, too. I don't know how the plugin handles this so I asked. Quote Link to comment
Hugo Martins Posted May 25, 2021 Share Posted May 25, 2021 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 Quote Link to comment
Recommended Posts
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.