scripts/add-swap.sh
#!/bin/bash
set -eufx -o pipefail
DEVICE="${1:-}"
if [ -z "${DEVICE}" ]; then
  echo "Error: No device specified."
  exit 1
fi

TOTAL_MEM="${2:-$(grep MemTotal /proc/meminfo | awk '{print $2 * 1024}')}"
DEVICE_SIZE=$(sudo blockdev --getsize64 ${DEVICE})
if [ ${DEVICE_SIZE} -lt ${TOTAL_MEM} ]
then
  echo "Error: The selected device (${DEVICE}) is smaller than the requested swap size."
  exit 1
fi

SWAP_PARTITION="${DEVICE}p1"
SWAP_SIZE=$(numfmt --to=iec --suffix=B ${TOTAL_MEM})

echo "Creating a new partition on ${DEVICE}..."
sudo parted -s ${DEVICE} mklabel gpt
sudo parted -s ${DEVICE} mkpart primary linux-swap 0% ${SWAP_SIZE}

echo "Formatting the new partition as swap..."
sudo mkswap ${SWAP_PARTITION}

echo "Enabling the swap partition..."
sudo swapon ${SWAP_PARTITION}

echo "Updating /etc/fstab..."
UUID=$(sudo blkid -s UUID -o value ${SWAP_PARTITION})

echo "/dev/disk/by-uuid/${UUID} none swap sw 0 0" | sudo tee -a /etc/fstab
echo "Verifying the swap..."
sudo swapon --show | grep -q ${SWAP_PARTITION}

echo "Done! The new swap partition is now enabled."
