install.fairie/home/.chezmoiscripts/linux/run_onchange_before_11-configure-swap.sh.tmpl
Brian Zalewski 79078aa143 Update 44 files
- /home/.chezmoiscripts/archlinux/run_onchange_before_10-install-archlinux-dependencies.tmpl
- /home/.chezmoiscripts/centos/run_onchange_before_05-upgrade.tmpl
- /home/.chezmoiscripts/centos/run_onchange_before_10-install-centos-dependencies.tmpl
- /home/.chezmoiscripts/opensuse/run_onchange_before_11-install-opensuse-software.tmpl
- /home/.chezmoiscripts/fedora/run_onchange_before_10-install-fedora-dependencies.tmpl
- /home/.chezmoiscripts/fedora/run_onchange_before_5-upgrade.tmpl
- /home/.chezmoiscripts/freebsd/run_onchange_before_11-install-freebsd-packages.tmpl
- /home/.chezmoiscripts/debian/run_onchange_before_10-install-debian-dependencies.tmpl
- /home/.chezmoiscripts/debian/run_onchange_before_14-warp.tmpl
- /home/.chezmoiscripts/ubuntu/run_onchange_before_10-install-ubuntu-dependencies.tmpl
- /home/.chezmoiscripts/ubuntu/run_onchange_before_14-warp.tmpl
- /home/.chezmoiscripts/_universal/run_before_01-add-temporary-includes.tmpl
- /home/.chezmoiscripts/_universal/run_onchange_before_05-install-homebrew.tmpl
- /home/.chezmoiscripts/linux/run_onchange_before_10-system-tweaks.tmpl
- /home/.chezmoiscripts/linux/run_onchange_before_11-configure-swap.tmpl
- /home/.chezmoiscripts/darwin/run_onchange_after_10-configure-macos.tmpl
- /home/.chezmoiscripts/darwin/run_onchange_after_20-ensure-zsh-macos.tmpl
- /home/.chezmoiscripts/darwin/run_onchange_after_21-set-wallpaper.tmpl
- /home/.chezmoiscripts/darwin/run_onchange_before_10-install-darwin-dependencies.tmpl
- /home/.chezmoiscripts/darwin/run_onchange_before_20-ensure-user-group.tmpl
- /home/.chezmoiscripts/windows/run_onchange_after_14-cleanup-windows
- /home/.chezmoiscripts/windows/run_onchange_before_10-install-windows-dependencies.tmpl
- /home/.chezmoiscripts/windows/run_onchange_before_11-install-windows-packages.tmpl
- /home/.chezmoiscripts/qubes/run_onchange_after_11-set-wallpaper.tmpl
- /home/.chezmoiscripts/qubes/run_onchange_before_11-update-timezone.tmpl
- /home/.chezmoiscripts/qubes/run_onchange_before_12-update-dom0.tmpl
- /home/.chezmoiscripts/qubes/run_onchange_before_13-install-official-templates.tmpl
- /home/.chezmoiscripts/qubes/run_onchange_before_14-ensure-minimal-vms-passwordless.tmpl
- /home/.chezmoiscripts/_universal/run_before_01-add-temporary-includes.sh.tmpl
- /home/.chezmoiscripts/_universal/run_onchange_before_05-install-homebrew.sh.tmpl
- /home/.chezmoiscripts/linux/run_onchange_before_01-requirements.sh.tmpl
- /home/.chezmoiscripts/linux/run_onchange_before_10-system-tweaks.sh.tmpl
- /home/.chezmoiscripts/linux/run_onchange_before_11-configure-swap.sh.tmpl
- /home/.chezmoiscripts/linux/run_onchange_before_14-warp.sh.tmpl
- /home/.chezmoiscripts/darwin/run_onchange_after_10-configure-macos.sh.tmpl
- /home/.chezmoiscripts/darwin/run_onchange_after_20-ensure-zsh-macos.sh.tmpl
- /home/.chezmoiscripts/darwin/run_onchange_after_21-set-wallpaper.sh.tmpl
- /home/.chezmoiscripts/darwin/run_onchange_before_10-install-darwin-dependencies.sh.tmpl
- /home/.chezmoiscripts/darwin/run_onchange_before_20-ensure-user-group.sh.tmpl
- /home/.chezmoiscripts/windows/run_onchange_after_14-cleanup-windows.ps1
- /home/.chezmoiscripts/qubes/run_onchange_after_11-set-wallpaper.sh.tmpl
- /home/.chezmoiscripts/qubes/run_onchange_before_12-update-dom0.sh.tmpl
- /home/.chezmoiscripts/qubes/run_onchange_before_13-install-official-templates.sh.tmpl
- /home/.chezmoiscripts/qubes/run_onchange_before_14-ensure-minimal-vms-passwordless.sh.tmpl
2023-04-13 01:27:13 +00:00

57 lines
2.1 KiB
Bash

#!/usr/bin/env bash
# @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
{{ includeTemplate "universal/profile-before" }}
{{ includeTemplate "universal/logg-before" }}
if [ ! -f /swapfile ]; then
### Determine ideal size of /swapfile
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
### 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'
else
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
fi
fi