How to add Swap on Linux/Android/Docker stack

viktorbenei
Tue, 19 Dec 2017 11:14:08 +0000
Android

UPDATE: swap is now pre-configured on all stacks since: Weekly Virtual Machine / Stack updates - 2018.02.24 (added swap to Linux/Android stacks, the Mac ones already had swap configured).

You can of course still reconfigure it via the instructions here, but in general there’s no reason to do that anymore.

Currently the Android / Linux / Docker stack comes with no swap configured at all. There are two reasons for this:

1. The standard Ubuntu Server installer does not configure a swap by default, and our policy is to deviate as little as possible from the “default” install of the operating systems.

2. The right Swap amount might depends on the actual project, and it’s actually quite simple to configure if required.

So, if you need Swap (“additional RAM” / virtual memory - but keep in mind swap as RAM is way slower than actual RAM - Swap - Debian Wiki) for some reason you can simply configure it with a Script step. It takes only about 2-3 seconds to create 4 GB swap.

Just drop in a Script step into your workflow, as the very first step in the workflow, with this content, to create 4 GB swap (if you need more or less swap just change the sudo fallocate -l 4G /bitrise/swapfile line):

#!/usr/bin/env bash
set -ex

# create a swap file
sudo fallocate -l 4G /bitrise/swapfile
sudo chmod 600 /bitrise/swapfile

# now make this file a swap file
sudo mkswap /bitrise/swapfile
# and activate it as swap
sudo swapon /bitrise/swapfile

That’s all, no other steps required

Just for fun here’s an alternative version with additional debug logs, “just in case”:

#!/usr/bin/env bash
set -ex

# check swap
sudo swapon --show
# and free memory / RAM
free -h

# create a swap file
sudo fallocate -l 4G /bitrise/swapfile
ls -lh /bitrise/swapfile
sudo chmod 600 /bitrise/swapfile
ls -lh /bitrise/swapfile

# now make this file a swap file
sudo mkswap /bitrise/swapfile
# and activate it as swap
sudo swapon /bitrise/swapfile

# it should show up now here in the swap stats
sudo swapon --show
# and in the free memory / RAM stats
free -h

This one prints the swap config (swapon --show) and the available memory (including both swap and RAM - free -h) before it would create the swap, then creates the swap (with some debug logs along the way) and prints the swap and RAM/memory infos again at the end, after the swap is created.

The first version should be enough if you don’t want to do anything else other than just creating the swap, the second one might be useful for those who want to “explore” additional options

If you have any questions just let us know!
Happy Building!

8 posts - 3 participants

Read full topic