#!/bin/bash # Hugo Menzaghi - 2024-12-22 # Entware Installer & Re-enabler for RMPP Devices ################################################################# ########################### VARIABLES ########################### ################################################################# # Exit on any error set -e FORCE=no CLEANUP=no REENABLE=no HELP=no # Variable to capture exit code EXIT_CODE=0 ################################################################# ########################### FUNCTIONS ########################### ################################################################# usage() { cat </dev/null; then echo "$PATH_ENTRY" >>"$TARGET_FILE" echo "Updated PATH in $TARGET_FILE." else echo "/opt/bin and /opt/sbin already in PATH for $TARGET_FILE." fi } # Checksum function verify_checksum() { local file="$1" local expected_hash="$2" local computed_hash if [ ! -f "$file" ]; then echo "Error: File '$file' not found." return 1 fi if command -v sha256sum >/dev/null 2>&1; then computed_hash=$(sha256sum "$file" | awk '{print $1}') else echo "Error: 'sha256sum' is not available." return 1 fi if [ "$computed_hash" = "$expected_hash" ]; then echo "Checksum verification passed for $file." return 0 else echo "Checksum verification failed for $file." echo "Expected: $expected_hash" echo "Computed: $computed_hash" return 1 fi } # Cleanup function on error or abort cleanup() { set +e trap - ERR if [ "$CLEANUP" = "yes" ]; then answer="y" EXIT_CODE=0 else echo "An error occurred during installation." echo "Would you like to clean up the partial installation? (y/N)" if [ "$FORCE" = "yes" ]; then echo "Force option enabled. Proceeding with cleanup..." answer="y" else read -r answer /dev/null 2>&1; then systemctl disable opt.mount systemctl daemon-reload fi rm -f /etc/systemd/system/opt.mount systemctl daemon-reload # Unmount and remove /opt if mountpoint -q /opt; then umount /opt fi rm -rf /opt /home/root/.entware # Remove symbolic links in /opt/etc for file in passwd group shells shadow gshadow localtime; do rm -f "/opt/etc/$file" done # Remove PATH modifications echo "Removing PATH modifications..." # Remove system-wide PATH modification SYSTEM_PROFILE_D="/etc/profile.d" ENTWARE_PROFILE="$SYSTEM_PROFILE_D/entware.sh" if [ -f "$ENTWARE_PROFILE" ]; then rm -f "$ENTWARE_PROFILE" echo "Removed $ENTWARE_PROFILE." fi # Remove user-specific PATH modifications PATH_ENTRY='export PATH=/opt/bin:/opt/sbin:\$PATH' USER_NAME=${SUDO_USER:-$(whoami)} USER_HOME=$(eval echo "~$USER_NAME") USER_PROFILE="$USER_HOME/.profile" USER_BASHRC="$USER_HOME/.bashrc" remove_path_entry() { TARGET_FILE=$1 if [ -f "$TARGET_FILE" ] && [ -w "$TARGET_FILE" ]; then sed -i "\|$PATH_ENTRY|d" "$TARGET_FILE" echo "Removed PATH modification from $TARGET_FILE." fi } remove_path_entry "$USER_PROFILE" remove_path_entry "$USER_BASHRC" # Restore filesystem mount options restore_filesystem_state echo "Cleanup completed. Exiting..." exit $EXIT_CODE fi echo "Cleanup canceled. Exiting..." exit $EXIT_CODE } # Re-enable Entware function reenable_entware() { echo "=============================" echo " ** IMPORTANT **" echo "This script is intended for RMPP devices only. If you are not using an RMPP device, please exit immediately." echo "=============================" echo "Disclaimer:" echo "- **It is recommended to back up important data before proceeding.**" echo "- This script has been tested exclusively on firmware version v3.17.0.62." echo "- Do not power off the device during execution." echo "- The author is not liable for any potential damage caused by this script." echo "- Please review the script thoroughly before executing." echo "- No guaranteed support is provided for issues arising from this script." echo "=============================" echo "This script will perform the following actions:" echo "- Check if Entware is already enabled and exit if it is." echo "- Recreate the /opt directory if missing." echo "- Temporarily bind /home/root/.entware to /opt." echo "- Create a systemd unit file to persist the bind mount across reboots." echo "- Enable and start the opt.mount systemd service." echo "=============================" if [ "$FORCE" = "yes" ]; then echo "Force option enabled. Proceeding..." else echo "Press Ctrl+C to abort or press Enter to continue." read -r /dev/null 2>&1; then echo "Entware is already enabled." exit 0 fi # Create /opt if missing echo "Creating /opt directory if it doesn't exist..." mkdir -p /opt # Temporarily bind-mount /home/root/.entware to /opt echo "Mounting /home/root/.entware to /opt..." mount --bind /home/root/.entware /opt # Create the systemd mount unit for /opt echo "Creating systemd unit for /opt mount..." cat >/etc/systemd/system/opt.mount </etc/systemd/system/opt.mount <"$ENTWARE_PROFILE" else echo "Cannot write to $SYSTEM_PROFILE_D. Skipping system-wide PATH update." fi USER_NAME=${SUDO_USER:-$(whoami)} USER_HOME=$(eval echo "~$USER_NAME") USER_PROFILE="$USER_HOME/.profile" USER_BASHRC="$USER_HOME/.bashrc" if [ -w "$USER_PROFILE" ]; then add_to_path "$USER_PROFILE" elif [ -w "$USER_BASHRC" ]; then add_to_path "$USER_BASHRC" else echo "Cannot write to $USER_PROFILE or $USER_BASHRC. Skipping user-specific PATH update." fi echo "PATH updated. Please reload your shell or log out and back in for changes to take effect." else echo "Skipping PATH update as per user request." fi echo "" echo "If PATH was not updated automatically, add the following to ~/.bashrc or ~/.profile:" echo "export PATH=/opt/bin:/opt/sbin:\$PATH" echo "Then run 'source ~/.bashrc' or 'source ~/.profile' to apply." echo "" echo "Manage packages using Opkg:" echo " opkg update" echo " opkg install " echo "" echo "Installation complete. Thank you for using the RMPP Entware installer." echo "Happy hacking!" exit 0