Skip to content

Instantly share code, notes, and snippets.

@nlydv
Created July 14, 2021 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlydv/9fd4cdca95400ec94f38884a74418f29 to your computer and use it in GitHub Desktop.
Save nlydv/9fd4cdca95400ec94f38884a74418f29 to your computer and use it in GitHub Desktop.
for setting up swapfiles on new VMs. do not use this script as is!
#!/bin/bash
#
# SWAP
# ———the below command sequence is untested and should only be attempted/debuged————
# ———if you are frequently starting fresh VMs, too complicated and risky to have————
# ——————————————————————————————————————————————————————————————————————————————————
# ———i started creating this for an install script but quickly realized it's not————
# ———worth the effort and i'll end up screwing something up so.. saved here if i————
# ———ever end up deciding such an automation would be beneficial in the future——————
#
# proc_swap="$(cat /proc/swaps | sed -n 2p)"
# show_swapon="$(swapon --show)"
# total_swap="$(sed -n -E 's/.*SwapTotal:\s+([0-9]+).*/\1/p' /proc/meminfo)"
#
# if [[ -z $proc_swap && -z $show_swapon && $total_swap -eq 0 ]]; then
# total_mem="$(free -b | sed -n -E 2p | sed -E 's/Mem:\s+([0-9]+)\s.*/\1/')"
# vol_avail="$(df -lB 1 --output=avail / | tail +2)"
#
# if [[ $vol_avail -gt $total_mem ]]; then
# if [[ $(( $total_mem / 1000000 )) -ge 4000 ]]; then
# swap_size=4000000000
# elif [[ $(( $total_mem / 1000000 )) -lt 1200 ]]; then
# swap_size=1000000000
# else
# swap_size=$total_mem
# fi
#
# fallocate -l $swap_size /swapfile
# chmod 600 /swapfile
# mkswap /swapfile
# swapon /swapfile
#
# swapon --show; free -h
#
# cp /etc/fstab /etc/fstab.original
# echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# fi
# fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment