Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions packer/build.retropie.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ build {
provisioner "file" {
source = "${path.root}/../rpi/configs/retropie/retropie.conf"
destination = "/boot/firmware/retropie.conf"
}
}

# Upload overlays
provisioner "file" {
Expand All @@ -68,7 +68,7 @@ build {
provisioner "file" {
source = "${path.root}/../rpi/libraries/raspios/"
destination = "/usr/lib/"
}
}

# Upload drivers
provisioner "file" {
Expand Down Expand Up @@ -111,6 +111,13 @@ build {
}
}

# Reboot
provisioner "shell" {
execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'"
expect_disconnect = true
inline = ["echo 'Reboot VM'", "reboot"]
}

# Upload retropie installer
provisioner "file" {
source = "${path.root}/scripts/installers/install-retropie.sh"
Expand All @@ -128,11 +135,19 @@ build {
execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'"
expect_disconnect = true
inline = [
"echo 'Enable RetroPie Service'",
"echo 'Enable RetroPie Service'",
"systemctl enable retropie.service",
"chmod +x /usr/local/bin/install-retropie.sh"
]
}
}

# Install retropie
provisioner "shell" {
execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'"
scripts = [
"${path.root}scripts/installers/install-retropie.sh"
]
}

# Reboot
provisioner "shell" {
Expand Down
183 changes: 43 additions & 140 deletions packer/scripts/installers/install-retropie.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/bin/bash -e
################################################################################
## File: install-pspi6.sh
## Desc: This script is used to install Retropie onto a PiOS Aarch64 image. It contains the necessary installation steps and dependencies required for the installation process.
## https://retropie.org.uk/docs/Manual-Installation/
## https://www.youtube.com/watch?v=PAePvz6YSWo
## Desc: This script is used to install Retropie onto a PiOS Aarch64 image.
## It contains the necessary installation steps and dependencies
## required for the installation process.
## Links: https://retropie.org.uk/docs/Manual-Installation/
## https://www.youtube.com/watch?v=PAePvz6YSWo
################################################################################
set -x

Expand All @@ -13,151 +15,52 @@ LOG_FILE="/var/log/install-retropie-$(date +%Y%m%d%H%M%S).log"
# Redirect all output (stdout and stderr) to the log file
exec > >(tee -a "$LOG_FILE") 2>&1

# Add a file to track installation progress
PROGRESS_FILE="/opt/retropie_installation_progress"

# Function to check if a step is complete
is_step_complete() {
grep -q "$1" "$PROGRESS_FILE" 2>/dev/null
}

# Function to mark a step as complete
mark_step_complete() {
echo "$1" >> "$PROGRESS_FILE"
}

# Function to handle step failure
handle_failure() {
echo "Step failed: $1. Rebooting..."
sleep 60
reboot
}

# Ensure the progress file exists
touch "$PROGRESS_FILE"
chmod 666 "$PROGRESS_FILE"

# Variable to track if any changes were made
CHANGES_MADE=false

# Update and upgrade system
if ! is_step_complete "update_upgrade"; then
apt update || handle_failure "update"
apt upgrade -y || handle_failure "upgrade"
mark_step_complete "update_upgrade"
CHANGES_MADE=true
fi

# Install dependencies
if ! is_step_complete "install_dependencies"; then
apt install git lsb-release -y || handle_failure "install_dependencies"
mark_step_complete "install_dependencies"
CHANGES_MADE=true
fi

# Install RetroPie
if ! is_step_complete "clone_retropie"; then
cd /opt || handle_failure "change_directory"
if [ ! -d "/opt/RetroPie-Setup" ]; then
git clone --depth=1 https://github.com/RetroPie/RetroPie-Setup.git || handle_failure "clone_retropie"
fi
mark_step_complete "clone_retropie"
CHANGES_MADE=true
# Create user "pi" if it doesn't exist
if ! id -u pi > /dev/null 2>&1; then
useradd -m -s /bin/bash pi || { echo "Failed to create user pi"; exit 1; }
echo "pi:raspberry" | chpasswd || { echo "Failed to set password for pi"; exit 1; }
usermod -aG sudo pi || { echo "Failed to add pi to sudo group"; exit 1; }
fi

if ! is_step_complete "setup_retropie"; then
cd /opt/RetroPie-Setup || handle_failure "change_directory_retropie"
chmod +x /opt/RetroPie-Setup/retropie_packages.sh || handle_failure "chmod_retropie_packages"
# Switch to user "pi"
su - pi <<'EOF'

# Break down each retropie_packages.sh command into its own step
if ! is_step_complete "install_retroarch"; then
/opt/RetroPie-Setup/retropie_packages.sh retroarch || handle_failure "install_retroarch"
mark_step_complete "install_retroarch"
CHANGES_MADE=true
fi
# Update and upgrade system packages
sudo apt update
sudo apt upgrade -y

if ! is_step_complete "install_emulationstation"; then
/opt/RetroPie-Setup/retropie_packages.sh emulationstation || handle_failure "install_emulationstation"
mark_step_complete "install_emulationstation"
CHANGES_MADE=true
fi

if ! is_step_complete "install_retropiemenu"; then
/opt/RetroPie-Setup/retropie_packages.sh retropiemenu || handle_failure "install_retropiemenu"
mark_step_complete "install_retropiemenu"
CHANGES_MADE=true
fi

if ! is_step_complete "install_runcommand"; then
/opt/RetroPie-Setup/retropie_packages.sh runcommand || handle_failure "install_runcommand"
mark_step_complete "install_runcommand"
CHANGES_MADE=true
fi
# Install required dependencies
sudo apt install git lsb-release -y

if ! is_step_complete "install_samba_depends"; then
/opt/RetroPie-Setup/retropie_packages.sh samba depends || handle_failure "install_samba_depends"
mark_step_complete "install_samba_depends"
CHANGES_MADE=true
fi

if ! is_step_complete "install_samba_shares"; then
/opt/RetroPie-Setup/retropie_packages.sh samba install_shares || handle_failure "install_samba_shares"
mark_step_complete "install_samba_shares"
CHANGES_MADE=true
fi

if ! is_step_complete "install_splashscreen_default"; then
/opt/RetroPie-Setup/retropie_packages.sh splashscreen default || handle_failure "install_splashscreen_default"
mark_step_complete "install_splashscreen_default"
CHANGES_MADE=true
fi

if ! is_step_complete "enable_splashscreen"; then
/opt/RetroPie-Setup/retropie_packages.sh splashscreen enable || handle_failure "enable_splashscreen"
mark_step_complete "enable_splashscreen"
CHANGES_MADE=true
# Install RetroPie
cd /opt
if [ ! -d "/opt/RetroPie-Setup" ]; then
sudo git clone --depth=1 https://github.com/RetroPie/RetroPie-Setup.git
fi

if ! is_step_complete "install_bashwelcometweak"; then
/opt/RetroPie-Setup/retropie_packages.sh bashwelcometweak || handle_failure "install_bashwelcometweak"
mark_step_complete "install_bashwelcometweak"
CHANGES_MADE=true
fi

if ! is_step_complete "install_joy2key"; then
/opt/RetroPie-Setup/retropie_packages.sh joy2key || handle_failure "install_joy2key"
mark_step_complete "install_joy2key"
CHANGES_MADE=true
fi
cd /opt/RetroPie-Setup || { echo "Failed to change directory to RetroPie-Setup"; exit 1; }
sudo chmod +x /opt/RetroPie-Setup/retropie_packages.sh || { echo "Failed to set executable permissions on retropie_packages.sh"; exit 1; }

# Install RetroPie components
sudo /opt/RetroPie-Setup/retropie_packages.sh retroarch || { echo "Failed to install retroarch"; exit 1; }
sudo /opt/RetroPie-Setup/retropie_packages.sh emulationstation || { echo "Failed to install emulationstation"; exit 1; }
sudo /opt/RetroPie-Setup/retropie_packages.sh retropiemenu || { echo "Failed to install retropiemenu"; exit 1; }
sudo /opt/RetroPie-Setup/retropie_packages.sh runcommand || { echo "Failed to install runcommand"; exit 1; }
sudo /opt/RetroPie-Setup/retropie_packages.sh samba depends || { echo "Failed to install samba dependencies"; exit 1; }
sudo /opt/RetroPie-Setup/retropie_packages.sh samba install_shares || { echo "Failed to install samba shares"; exit 1; }
sudo /opt/RetroPie-Setup/retropie_packages.sh splashscreen default || { echo "Failed to install default splashscreen"; exit 1; }
sudo /opt/RetroPie-Setup/retropie_packages.sh splashscreen enable || { echo "Failed to enable splashscreen"; exit 1; }
sudo /opt/RetroPie-Setup/retropie_packages.sh bashwelcometweak || { echo "Failed to install bashwelcometweak"; exit 1; }
sudo /opt/RetroPie-Setup/retropie_packages.sh joy2key || { echo "Failed to install joy2key"; exit 1; }

# Enable autostart for EmulationStation
if ! is_step_complete "enable_autostart"; then
/opt/RetroPie-Setup/retropie_packages.sh autostart enable || handle_failure "enable_autostart"

# Update the autostart.sh script to wait for all processes running /usr/local/bin/install-retropie.sh to end
sed -i '3i\ sleep 3\n while pgrep -f "/usr/local/bin/install-retropie.sh" > /dev/null; do\n sleep 5\n done' /etc/profile.d/10-retropie.sh

mark_step_complete "enable_autostart"
reboot
fi
sudo /opt/RetroPie-Setup/retropie_packages.sh autostart enable || { echo "Failed to enable autostart"; exit 1; }
sudo sed -i '3i\ sleep 3\n while pgrep -f "/usr/local/bin/install-retropie.sh" > /dev/null; do\n sleep 5\n done' /etc/profile.d/10-retropie.sh

mark_step_complete "setup_retropie"
CHANGES_MADE=true
fi

# Install RetroPie cores
# Load cores from configuration file
CORES=$(grep -oP '^\s*"[^"]+"(?=\s*#|$)' /boot/firmware/retropie.conf | tr -d '"')

for CORE in $CORES; do
if ! is_step_complete "install_$CORE"; then
/opt/RetroPie-Setup/retropie_packages.sh "$CORE" || handle_failure "install_$CORE"
mark_step_complete "install_$CORE"
CHANGES_MADE=true
fi
done
# Install RetroPie cores
PACKAGES=$(grep -oP '^\s*"[^"]+"(?=\s*#|$)' /boot/firmware/retropie.conf | tr -d '"')

# Reboot only if changes were made
if [ "$CHANGES_MADE" = true ]; then
reboot
fi
for PACKAGE in $PACKAGES; do
sudo /opt/RetroPie-Setup/retropie_packages.sh "$PACKAGE" || { echo "Failed to install $PACKAGE"; exit 1; }
done
EOF