Skip to content

Instantly share code, notes, and snippets.

@ressu
Last active March 12, 2024 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ressu/0611ba0c505af6b1d0a3704f38c5f260 to your computer and use it in GitHub Desktop.
Save ressu/0611ba0c505af6b1d0a3704f38c5f260 to your computer and use it in GitHub Desktop.
Swapfile wrapper for memory hungry installations

Wrapper for swapfile on sdcard

This is a convenient wrapper script to run bash with a swapfile and temporary folder with more capacity. The need for this script comes from The 100 printers where Banana Pi M2 Zero needs to install numpy and other Python packages which have a memory hungry build process.

The script does the following thigs:

  • Sets up a swapfile and enables it
  • Creates a temporary directory and assigns it appropriately
  • Cleans up after itself when the shell exists

Usage

To use the script, first download the script on the target machine using your preferred tools. Run the script and then run the commands which take significant amounts of ram.

for example:

printer@printer:~$ ./swappy-shell.sh
Swapfile has been created and activated!
[..]
(swappy-shell)printer@printer:~$ ~/klippy-env/bin/pip3 install numpy
[..long output here..]
(swappy-shell)printer@printer:~$ exit
## Cleaning up swapfile

One liner

While I don't recommend running arbitrary scripts from the internet without auditing them first. You can run the following one liner to start a swappy shell. If you trust me to do the right thing that is...

Run the following:

bash <(curl -s https://gist.githubusercontent.com/ressu/0611ba0c505af6b1d0a3704f38c5f260/raw/swappy-shell.sh)

About the environment

The swapfile will be available for all processes on the system and not just the shell. The temporary directory that is set up by this script on the other hand is only set up in the interactive shell. Since the /tmp diretory in Armbian is created on a memory backed device, it's common to get disk full errors for temporary files. This is why the script creates both swap and temporary directory on the sdcard.

It's not recommended to use the environment for regular work as both the temporary files and the swap put extra strain on the sdcard shortening the lifespan. (It's fine to do this short term though)

Things to do in the shell

You have the shell running, now what?

Install Numpy for Klipper

To install latest numpy for Klipper on Armbian, the ninja build system needs to be installed before. The following gets you up and running

sudo apt install libatlas-base-dev ninja-build
~/klippy-env/bin/pip3 install numpy

Install SciPy

NOTE: SciPy is only needed in special cases, so in a normal installation there is no need to install it.

SciPy is like the more demanding big brother for numpy. while numpy gets going with ease, SciPy needs a bit more.

sudo apt install libatlas-base-dev libopenblas-dev gfortran ninja-build
pip3 install scipy   ## Adjust this to match what ever location you need for the installation
#!/bin/bash
# A convenient wrapper script to run bash while having swap enabled. The script
# will drop to a shell and once you exit, the swap will be disabled and swapfile
# will be cleaned up. The script will also create a temporary folder and assign
# TMPDIR appropriately.
#
# The main purpose of the script is to install Klipper dependencies on a Banana
# Pi M2 Zero to be used with The 100 printer. But the script should work on
# almost any Linux installation.
# Swapfile location and size
SWAPFILE=/swapfile
SWAPSIZE=1G
# Temp directory location, should be on the sdcard
TMPDIR=$HOME/tmp
# Create a swapfile
create_swapfile() {
(umask 077 ; sudo fallocate -l "$SWAPSIZE" "$SWAPFILE")
sudo mkswap -q "$SWAPFILE"
sudo swapon "$SWAPFILE"
}
# Remove the swapfile
remove_swapfile() {
sudo swapoff "$SWAPFILE"
sudo rm "$SWAPFILE"
}
# Create a temporary directory
TMPDIR_CLEANUP=false
if [ ! -d "$TMPDIR" ]; then
TMPDIR_CLEANUP=true
mkdir -p "$TMPDIR"
fi
declare -x TMPDIR
# Create Swap
create_swapfile
cat << __USAGE__
Swapfile has been created and activated! When you exit the shell, swap will be
disabled and swapfile will be cleaned up.
Run commands which need extra memory. For example:
~/klippy-env/bin/pip3 install numpy
Exit the shell by typing exit or by pressing Ctrl-D.
__USAGE__
# Piggyback on debian_chroot variable for user information in prompt. Only works
# on default bash config.
declare -x debian_chroot="swappy-shell"
# Run bash and drop to a shell
bash -i
echo "## Cleaning up swapfile"
if [ "$TMPDIR_CLEANUP" = true ]; then
if [ -z "$(ls -A "$TMPDIR")" ]; then
rmdir "$TMPDIR"
fi
fi
remove_swapfile
echo "## Swapfile has been removed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment