Skip to content

Instantly share code, notes, and snippets.

@davidmroth
Last active September 6, 2018 03:24
Show Gist options
  • Save davidmroth/b43c7585f7ec25baa48f0871e72b08c6 to your computer and use it in GitHub Desktop.
Save davidmroth/b43c7585f7ec25baa48f0871e72b08c6 to your computer and use it in GitHub Desktop.
Create Swap on Raspberry Pi (Nodejs)
#!/bin/bash
# Check if root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# Check if dphys-swapfile installed. If not, install it
if ! dpkg --get-selections | grep -q "^dphys-swapfile\(:.*\)\?[[:space:]]*install$" >/dev/null; then
apt-get install dphys-swapfile -y
fi
# https://stackoverflow.com/questions/26193654/node-js-catch-enomem-error-thrown-after-spawn
script_file="$(cat<<'EOF'
MNT_POINT=/media/storage/
UUID=$(blkid | grep ZoneMinder | sed -n 's/.*UUID="\(.*\)".*/\1/p')
BLOCK_DEV=$(blkid | grep ZoneMinder | awk '{print $1}' | awk -F: '{print $1}')
if ! test -z "$UUID"; then
if ! test -e $MNT_POINT; then
mkdir -p $MNT_POINT
fi
if mount $BLOCK_DEV $MNT_POINT > /dev/null 2>&1; then
dphys-swapfile swapon
fi
fi
EOF
)"
nl=$'\n'
bsnl=$'\\\n'
parsed_script=${script_file//\\/\\\\}
packaged_script=${parsed_script//$nl/$bsnl}
# Prepend script to rc.local
sed -i '/^exit 0$/i'"$packaged_script" /etc/rc.local
# Modify /etc/dphys-swapfile config file
if ! grep "^CONF_SWAPFILE=" /etc/dphys-swapfile > /dev/null 2>&1; then
echo -e "CONF_SWAPFILE=/media/storage/swapfile\nCONF_MAXSWAP=2048" | tee -a /etc/dphys-swapfile
fi
# Execute script
sh /etc/rc.local
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment