Skip to content

Instantly share code, notes, and snippets.

@Xeckt
Last active January 11, 2024 17:39
Show Gist options
  • Save Xeckt/6932ea7bcbf66b3426f289f9ac0eb822 to your computer and use it in GitHub Desktop.
Save Xeckt/6932ea7bcbf66b3426f289f9ac0eb822 to your computer and use it in GitHub Desktop.
Change swap file size
#!/bin/bash
change_swap() {
read -rp "Size to change swap file to in whole number format e.g. 8 for 8GB >> " swap_file_size
read -rp "Specify swapfile path/to/file >> " swap_file
echo "Turning off swap for all swap files"
if swapoff -a ; then
echo "Turning off swap successful. DD'ing $swap_file to size $swap_file_size"
if dd if=/dev/zero of="$swap_file" bs=1G count="$swap_file_size" ; then
echo "dd done, setting file permissions for swap"
if chmod 600 "$swap_file" ; then
echo "Permissions done, making new file part of swap"
if mkswap "$swap_file" ; then
echo "Done, turning swap on for $swap_file"
if swapon "$swap_file" ; then
echo "Swap is now on. If not done already, add the following to /etc/fstab: "
echo "$swap_file none swap sw 0 0"
else
echo "Could not turn $swap_file on"
exit
fi
else
echo "Could not mkswap $swap_file"
exit
fi
else
echo "Could not chmod 600 $swap_file"
exit
fi
else
echo "Could not dd the swap file with count $swap_file_size bs=1G"
exit
fi
else
echo "Could not switch swapoff on $swap_file"
exit
fi
}
change_swap
@Xeckt
Copy link
Author

Xeckt commented Jan 11, 2024

I know this is a terrible script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment