Skip to content

Instantly share code, notes, and snippets.

@shovon
Created April 10, 2015 15:18
Show Gist options
  • Star 63 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save shovon/9dd8d2d1a556b8bf9c82 to your computer and use it in GitHub Desktop.
Save shovon/9dd8d2d1a556b8bf9c82 to your computer and use it in GitHub Desktop.
Increasing swap size. Only tested on the ubuntu/trusty64 Vagrant box. CREDIT GOES TO ---> http://jeqo.github.io/blog/devops/vagrant-quickstart/
#!/bin/sh
# size of swapfile in megabytes
swapsize=8000
# does the swap file already exist?
grep -q "swapfile" /etc/fstab
# if not then create it
if [ $? -ne 0 ]; then
echo 'swapfile not found. Adding swapfile.'
fallocate -l ${swapsize}M /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap defaults 0 0' >> /etc/fstab
else
echo 'swapfile found. No changes made.'
fi
# output results to terminal
df -h
cat /proc/swaps
cat /proc/meminfo | grep Swap
@jimbasilio
Copy link

great script, thanks!!

@Mec-iS
Copy link

Mec-iS commented Jan 23, 2016

@fcarrega
Copy link

fcarrega commented Apr 1, 2016

Thanks !

@luizs81
Copy link

luizs81 commented Jul 5, 2016

For me it only worked putting sudo in front of every command and replaced

echo '/swapfile none swap defaults 0 0' >> /etc/fstab

by

echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab

I'm calling this script from my Vagrantfile.

@sergiopereiraTT
Copy link

@luizs81 normally Vagrant runs the provision script with sudo. Maybe you have something like this config.vm.provision :shell, privileged: false, path: "provision.sh", with privileged: false. It could be intentional, though.

@luizs81
Copy link

luizs81 commented Jul 21, 2016

@sergiopereiraTT You are right! I indeed had the privileged: false in my Vagrantfile. Thanks for the heads up.

@nadeemkauser
Copy link

You are helpful, your script is very clean. If logged in with vagrant, use sudo infront of script eg sudo ./swapt.sh

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