config/setup-scripts/sys/swap-file.sh

18 lines
415 B
Bash
Raw Normal View History

2024-07-23 05:59:12 +00:00
#!/bin/sh
[ -e /swap ] && {
# for saftey
swapoff /swap
rm /swap
sed -Ei '/\/swap\s*/d' /etc/fstab
}
touch /swap
size="$(( "$(free -g | awk '/Mem:/ {print $2}')" + 1 ))k"
dd if=/dev/zero of=/swap bs=1M count="$size" status=progress
chmod 0600 /swap
mkswap -U clear /swap
swapon /swap
echo '' >> /etc/fstab
echo '/swap none swap defaults 0 0' >> /etc/fstab
echo "Created $(echo $size | tr -d 'k') Gb swap file"