Option to automatically create swap space when creating droplet?

April 29, 2015 1.5k views
Control Panels Configuration Management Ubuntu

Hi guys,
Huge fan of DO - been using it for a few years now on heaps of different projects. One thing I've noticed myself doing repeatedly on almost every small (i.e 20gb) droplet that I spin up is creating a gig or so of swap space.

Seeing as this is such a common thing to do for a lot of use cases (i.e running passenger or unicorn on a 512mb ram droplet) would you consider adding an option to the "create droplet" wizard that allows you to automatically create / turn on swap of a certain size?


1 Answer

We do have this feature on some one-click applications (where a swap file is automatically created on smaller droplets) and some distributions like FreeBSD include a swap partition by default. You can add a swap file at creation by using the following script in user-data on the create page:

 sudo fallocate -l 1G /swapfile;
 chmod 600 /swapfile;
 mkswap /swapfile;
 swapon /swapfile;
echo "/swapfile   none    swap    sw    0   0" >> /etc/fstab;

This will create a 1GB swap file as soon as your droplet boots for the first time and add it to your fstab to be automatically used on each subsequent boot.

Have another answer? Share your knowledge.