Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added swap config on low-mem nodes #2063

Closed
wants to merge 11 commits into from
98 changes: 96 additions & 2 deletions scripts/installscripts/buster-install-default.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#
# 1. download the install file from github
# https://github.com/MiczFlor/RPi-Jukebox-RFID/tree/develop/scripts/installscripts
# (note: currently only works for buster and newer OS)
# 2. make the file executable: chmod +x
# 3. place the PhonieboxInstall.conf in the folder $HOME
# 4. run the installscript with option -a like this:
Expand Down Expand Up @@ -501,6 +500,22 @@ config_spotify() {
read -rp "Type your Spotify password: " SPOTIpass
read -rp "Type your client_id: " SPOTIclientid
read -rp "Type your client_secret: " SPOTIclientsecret
clear
echo "#####################################################
#
# Swapfile creation
#
# With the current way Spotify support is build, a lot of RAM needs
# to be available in order for the required components to build
# correctly. This includes even recent Raspberry Pi versions.
# In general: If less than 2 GB RAM is available in your system, the
# build of gst-plugin-spotify will most certainly fail.
#
# This script can detect if a swapfile is required and have one
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the question below, before or after the necessity for a swap file is checked?

# created for you if needed.
#
"
read -rp "Would you like to have a swapfile created and activated now? [Y/n] " SPOTIcreateswap
;;
esac
# append variables to config file
Expand All @@ -510,6 +525,7 @@ config_spotify() {
echo "SPOTIpass=\"$SPOTIpass\"";
echo "SPOTIclientid=\"$SPOTIclientid\"";
echo "SPOTIclientsecret=\"$SPOTIclientsecret\""
echo "SPOTIcreateswap=\"$SPOTIcreateswap\""
} >> "${HOME_DIR}/PhonieboxInstall.conf"
read -rp "Hit ENTER to proceed to the next step." INPUT
}
Expand Down Expand Up @@ -664,6 +680,7 @@ check_config_file() {
check_variable "SPOTIpass"
check_variable "SPOTIclientid"
check_variable "SPOTIclientsecret"
check_variable "SPOTIcreateswap"
fi
fi
check_variable "MPDconfig"
Expand Down Expand Up @@ -836,13 +853,16 @@ install_main() {

# Install required spotify packages
if [ "${SPOTinstall}" == "YES" ]; then
echo "Creating and activating SWAP file if requested..."
create_swap "${SPOTIcreateswap}"

echo "Installing dependencies for Spotify support..."
# keep major verson 3 of mopidy
echo -e "Package: mopidy\nPin: version 3.*\nPin-Priority: 1001" | sudo tee /etc/apt/preferences.d/mopidy

sudo wget -q -O /etc/apt/keyrings/mopidy-archive-keyring.gpg https://apt.mopidy.com/mopidy.gpg
sudo wget -q -O /etc/apt/sources.list.d/mopidy.list https://apt.mopidy.com/${OS_CODENAME}.list

${apt_get} update
${apt_get} upgrade
${apt_get} install libspotify-dev
Expand Down Expand Up @@ -1282,6 +1302,80 @@ finish_installation() {
esac
}

create_swap() {
# If Available memory is less than 2 GB (which is round about what's required during
# gst-plugin-spotify compilation later, ask if another 2 GB swap should be created and
# activated.
local YES_TO_ALL=${1:-false}

# Install required tools not yet installed
local apt_get="sudo apt-get -qq --yes"
for app in awk bc; do
if ! compgen -A function -abck | grep -Eq '^'"${app}"'$'; then
${apt_get} install ${app}
fi
done

# Get Available memory in GB
local MEM_AVAILABLE
MEM_AVAILABLE=$(awk '/MemAvailable/ { printf "%.3f\n", $2/1024/1024 }' /proc/meminfo)
# Get free swap in GB
local SWAP_FREE
SWAP_FREE=$(awk '/SwapFree/ { printf "%.3f\n", $2/1024/1024 }' /proc/meminfo)
# Get available storage space on '/'
local ROOT_FREE
ROOT_FREE=$(df -BG -P / | tail -n 1 | awk '{print $4}')

# If available memory + free swap is less than 2 GB
if (( $(echo "(${MEM_AVAILABLE} + ${SWAP_FREE}) < 2" | bc -l) )); then
clear

cat <<EOF
#####################################################
#
# Creating and activating a swap file
#
# The amount of memory available in your system (${MEM_AVAILABLE} GB)
# is less than the recommended minimum of 2 GB. It is recommended to
# have this script create and activate a 2 GB sized swapfile at '/swapfile'
# for you now.
EOF
if [ "${ROOT_FREE//G}" -le 2 ]; then
cat <<EOF
#
# It seems that you do not have sufficient storage available at '/'.
# So this script can't create and activate a swapfile for you.
# Continue at your own risk and consider to use a bigger storage
# before trying again.
EOF
else
if [ ! -f /swapfile ]; then
case "$YES_TO_ALL" in
[nN])
# Do not create swap
echo -e "\nNot creating nor activating swapfile."
echo -e "Your system still lacks available memory though ...\n"
;;
*)
# Do create swap
echo -e "\nCreating 2 GB swapfile at '/swapfile'. This may take several minutes ...\n"
sudo dd if=/dev/zero of=/swapfile bs=1024k count=2k
sudo chmod 600 /swapfile
sudo mkswap /swapfile
if ! grep -E '^/swapfile' /etc/fstab; then
echo -e "\n/swapfile\tnone\tswap\tdefaults\t0\t0" | sudo tee -a /etc/fstab >/dev/null
fi
;;
esac
else
echo -e "\nWARNING: '/swapfile' already exists. Not changing it, just activating all swaps.\n"
fi
fi
fi

sudo swapon -a
}

########
# Main #
########
Expand Down