2023-04-15 16:14:30 -07:00
|
|
|
{{- if (eq .host.distro.family "linux") -}}
|
2022-12-06 02:26:47 -08:00
|
|
|
#!/usr/bin/env bash
|
2023-04-12 18:27:13 -07:00
|
|
|
# @file Linux Swap
|
|
|
|
# @brief Determines the ideal size `/swapfile`, ensures it exists, and then enables it on Linux systems
|
|
|
|
# @description
|
|
|
|
# This script determines the ideal `/swapfile` size by checking how much RAM is available on the system.
|
|
|
|
# It then creates the appropriate `/swapfile` by considering factors such as the file system type. It
|
|
|
|
# currently supports BTRFS and regular file systems.
|
|
|
|
#
|
|
|
|
# After the `/swapfile` is created, it is enabled and assigned the appropriate permissions.
|
|
|
|
#
|
|
|
|
# ## TODO
|
|
|
|
#
|
|
|
|
# * Add logic that creates a swapfile for ZFS-based systems
|
|
|
|
# * Integrate logic from https://gitlab.com/megabyte-labs/gas-station/-/blob/master/roles/system/common/tasks/linux/swap.yml
|
2022-12-06 02:26:47 -08:00
|
|
|
|
2023-01-24 20:36:59 -08:00
|
|
|
{{ includeTemplate "universal/profile-before" }}
|
|
|
|
{{ includeTemplate "universal/logg-before" }}
|
2022-12-25 00:35:37 -08:00
|
|
|
|
2022-12-25 00:41:35 -08:00
|
|
|
if [ ! -f /swapfile ]; then
|
2023-02-01 23:17:31 -08:00
|
|
|
### Determine ideal size of /swapfile
|
2022-12-25 00:41:35 -08:00
|
|
|
MEMORY_IN_KB="$(grep MemTotal /proc/meminfo | sed 's/.* \(.*\) kB/\1/')"
|
|
|
|
MEMORY_IN_GB="$((MEMORY_IN_KB / 1024 / 1024))"
|
|
|
|
if [ "$MEMORY_IN_GB" -gt 64 ]; then
|
|
|
|
SWAP_SPACE="$((MEMORY_IN_GB / 10))"
|
|
|
|
elif [ "$MEMORY_IN_GB" -gt 32 ]; then
|
|
|
|
SWAP_SPACE="$((MEMORY_IN_GB / 8))"
|
|
|
|
elif [ "$MEMORY_IN_GB" -gt 8 ]; then
|
|
|
|
SWAP_SPACE="$((MEMORY_IN_GB / 4))"
|
|
|
|
else
|
|
|
|
SWAP_SPACE="$MEMORY_IN_GB"
|
|
|
|
fi
|
2023-02-01 23:17:31 -08:00
|
|
|
|
|
|
|
### Create /swapfile
|
|
|
|
FS_TYPE="$(df -Th | grep ' /$' | sed 's/[^ ]*\s*\([^ ]*\).*/\1/')"
|
|
|
|
if [ "$FS_TYPE" == 'btrfs' ]; then
|
|
|
|
logg info 'Creating BTRFS /swapfile'
|
|
|
|
sudo btrfs filesystem mkswapfile /swapfile
|
|
|
|
elif [ "$FS_TYPE" == 'zfs' ]; then
|
|
|
|
logg warn 'ZFS system detected - add logic here to add /swapfile'
|
2022-12-25 00:41:35 -08:00
|
|
|
else
|
2023-02-01 23:17:31 -08:00
|
|
|
logg info "Creating a $SWAP_SPACE GB /swapfile"
|
|
|
|
sudo fallocate -l "${SWAP_SPACE}G" /swapfile
|
|
|
|
sudo chmod 600 /swapfile
|
|
|
|
sudo mkswap /swapfile
|
|
|
|
fi
|
|
|
|
|
|
|
|
### Enable the /swapfile
|
|
|
|
if [ -f /swapfile ]; then
|
|
|
|
logg info 'Running `sudo swapon /swapfile`'
|
|
|
|
sudo swapon /swapfile
|
|
|
|
if cat /etc/fstab | grep "/swapfile"; then
|
|
|
|
sudo sed -i '/\/swapfile/\/swapfile none swap defaults 0 0/' /etc/fstab
|
|
|
|
else
|
|
|
|
echo "/swapfile none swap defaults 0 0" | sudo tee -a /etc/fstab > /dev/null
|
|
|
|
fi
|
2022-12-25 00:41:35 -08:00
|
|
|
fi
|
2022-12-06 02:26:47 -08:00
|
|
|
fi
|
2023-04-15 16:14:30 -07:00
|
|
|
{{ end -}}
|