#!/usr/bin/env bash # BSD 3-Clause License # Author @sonique6784 # Copyright (c) 2020, sonique6784 function showRaSCSILogo(){ logo="""     .~~.   .~~.\n   '. \ ' ' / .'\n    .╔═══════╗.\n   : ║|¯¯¯¯¯|║ :\n  ~ (║|_____|║) ~\n ( : ║ .  __ ║ : )\n  ~ .╚╦═════╦╝. ~\n   (  ¯¯¯¯¯¯¯  ) RaSCSI Reloaded Assistant\n    '~ .~~~. ~'\n        '~'\n """ echo -e $logo } function showMacNetworkWired(){ logo="""                               .-~-.-~~~-.~-.\n  ╔═══════╗                  .(              )\n  ║|¯¯¯¯¯|║                 /               \`.\n  ║|_____|║>--------------<~               .   )\n  ║ .  __ ║                 (              :'-'\n  ╚╦═════╦╝                  ~-.________.:'\n   ¯¯¯¯¯¯¯\n """ echo -e $logo } function showMacNetworkWireless(){ logo="""                               .-~-.-~~~-.~-.\n  ╔═══════╗        .(       .(              )\n  ║|¯¯¯¯¯|║  .(  .(        /               \`.\n  ║|_____|║ .o    o       ~               .   )\n  ║ .  __ ║  '(  '(        (              :'-'\n  ╚╦═════╦╝        '(       ~-.________.:'\n   ¯¯¯¯¯¯¯\n """ echo -e $logo } USER=$(whoami) BASE=$(dirname "$(readlink -f "${0}")") VIRTUAL_DRIVER_PATH="$HOME/images" CFG_PATH="$HOME/.config/rascsi" WEB_INSTALL_PATH="$BASE/python/web" OLED_INSTALL_PATH="$BASE/python/oled" CTRLBOARD_INSTALL_PATH="$BASE/python/ctrlboard" PYTHON_COMMON_PATH="$BASE/python/common" SYSTEMD_PATH="/etc/systemd/system" SSL_CERTS_PATH="/etc/ssl/certs" SSL_KEYS_PATH="/etc/ssl/private" HFS_FORMAT=/usr/bin/hformat HFDISK_BIN=/usr/bin/hfdisk LIDO_DRIVER=$BASE/lido-driver.img GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD) GIT_REMOTE=${GIT_REMOTE:-origin} TOKEN="" set -e # checks to run before entering the script main menu function initialChecks() { if [ "root" == "$USER" ]; then echo "Do not run this script as $USER or with 'sudo'." exit 1 fi } # checks that the current user has sudoers privileges function sudoCheck() { echo "Input your password to allow this script to make the above changes." sudo -v } # install all dependency packages for RaSCSI Service function installPackages() { sudo apt-get update && sudo apt-get install git libspdlog-dev libpcap-dev genisoimage python3 python3-venv python3-dev python3-pip nginx libpcap-dev protobuf-compiler bridge-utils libev-dev libevdev2 unar -y /dev/null; echo $?) if [ "$APACHE_STATUS" -eq 0 ] ; then echo "Stopping old Apache2 RaSCSI Web..." sudo systemctl disable apache2 sudo systemctl stop apache2 fi } # Checks for upstream changes to the git repo and fast-forwards changes if needed function updateRaScsiGit() { cd "$BASE" || exit 1 set +e git rev-parse --is-inside-work-tree &> /dev/null if [[ $? -ge 1 ]]; then echo "Warning: This does not seem to be a valid clone of a git repository. I will not be able to pull the latest code." return 0 fi set -e stashed=0 if [[ $(git diff --stat) != '' ]]; then echo "There are local changes to the RaSCSI code; we will stash and reapply them." git -c user.name="${GIT_COMMITTER_NAME-rascsi}" -c user.email="${GIT_COMMITTER_EMAIL-rascsi@rascsi.com}" stash stashed=1 fi if [[ `git for-each-ref --format='%(upstream:short)' "$(git symbolic-ref -q HEAD)"` != "" ]]; then echo "Updating checked out git branch $GIT_REMOTE/$GIT_BRANCH" git pull --ff-only else echo "Detected a local git working branch; skipping the remote update step." fi if [ $stashed -eq 1 ]; then echo "Reapplying local changes..." git stash apply fi } # Takes a backup copy of the rascsi.service file if it exists function backupRaScsiService() { if [ -f "$SYSTEMD_PATH/rascsi.service" ]; then sudo mv "$SYSTEMD_PATH/rascsi.service" "$SYSTEMD_PATH/rascsi.service.old" SYSTEMD_BACKUP=true echo "Existing version of rascsi.service detected; Backing up to rascsi.service.old" else SYSTEMD_BACKUP=false fi } # Offers the choice of enabling token-based authentication for RaSCSI function configureTokenAuth() { echo "" echo "Do you want to protect your RaSCSI installation with a password? [y/N]" read REPLY SECRET_FILE="$HOME/.config/rascsi/rascsi_secret" if [ "$REPLY" == "y" ] || [ "$REPLY" == "Y" ]; then echo -n "Enter the password that you want to use: " read -r TOKEN if [ -f "$HOME/.rascsi_secret" ]; then sudo rm "$HOME/.rascsi_secret" echo "Removed old RaSCSI token file" fi if [ -f "$SECRET_FILE" ]; then sudo rm "$SECRET_FILE" echo "Removed old RaSCSI token file" fi echo "$TOKEN" > "$SECRET_FILE" # Make the secret file owned and only readable by root sudo chown root:root "$SECRET_FILE" sudo chmod 600 "$SECRET_FILE" echo "" echo "Configured RaSCSI to use $SECRET_FILE for authentication. This file is readable by root only." echo "Make note of your password: you will need it to use rasctl and other RaSCSI clients." fi } # Modifies and installs the rascsi service function enableRaScsiService() { if [ ! -z "$TOKEN" ]; then sudo sed -i "s@^ExecStart.*@& -F $VIRTUAL_DRIVER_PATH -P $SECRET_FILE@" "$SYSTEMD_PATH/rascsi.service" else sudo sed -i "s@^ExecStart.*@& -F $VIRTUAL_DRIVER_PATH@" "$SYSTEMD_PATH/rascsi.service" fi echo "Configured rascsi.service to use $VIRTUAL_DRIVER_PATH as default image dir." sudo systemctl daemon-reload sudo systemctl restart rsyslog sudo systemctl enable rascsi # optional - start rascsi at boot sudo systemctl start rascsi } # Modifies and installs the rascsi-web service function installWebInterfaceService() { echo "Installing the rascsi-web.service configuration..." sudo cp -f "$WEB_INSTALL_PATH/service-infra/rascsi-web.service" "$SYSTEMD_PATH/rascsi-web.service" sudo sed -i /^ExecStart=/d "$SYSTEMD_PATH/rascsi-web.service" echo "$TOKEN" if [ ! -z "$TOKEN" ]; then sudo sed -i "8 i ExecStart=$WEB_INSTALL_PATH/start.sh --password=$TOKEN" "$SYSTEMD_PATH/rascsi-web.service" # Make the service file readable by root only, to protect the token string sudo chmod 600 "$SYSTEMD_PATH/rascsi-web.service" echo "Granted access to the Web Interface with the token password that you configured for RaSCSI." else sudo sed -i "8 i ExecStart=$WEB_INSTALL_PATH/start.sh" "$SYSTEMD_PATH/rascsi-web.service" fi sudo systemctl daemon-reload sudo systemctl enable rascsi-web sudo systemctl start rascsi-web } # Stops the rascsi service if it is running function stopRaScsi() { if [[ -f "$SYSTEMD_PATH/rascsi.service" ]]; then SERVICE_RASCSI_RUNNING=0 sudo systemctl is-active --quiet rascsi.service >/dev/null 2>&1 || SERVICE_RASCSI_RUNNING=$? if [[ $SERVICE_RASCSI_RUNNING -eq 0 ]]; then sudo systemctl stop rascsi.service fi fi } # Stops the rascsi-web service if it is running function stopRaScsiWeb() { if [[ -f "$SYSTEMD_PATH/rascsi-web.service" ]]; then SERVICE_RASCSI_WEB_RUNNING=0 sudo systemctl is-active --quiet rascsi-web.service >/dev/null 2>&1 || SERVICE_RASCSI_WEB_RUNNING=$? if [[ $SERVICE_RASCSI_WEB_RUNNING -eq 0 ]]; then sudo systemctl stop rascsi-web.service fi fi } # Stops the rascsi-oled service if it is running function stopRaScsiScreen() { if [[ -f "$SYSTEMD_PATH/monitor_rascsi.service" ]]; then SERVICE_MONITOR_RASCSI_RUNNING=0 sudo systemctl is-active --quiet monitor_rascsi.service >/dev/null 2>&1 || SERVICE_MONITOR_RASCSI_RUNNING=$? if [[ $SERVICE_MONITOR_RASCSI_RUNNING -eq 0 ]]; then sudo systemctl stop monitor_rascsi.service fi fi if [[ -f "$SYSTEMD_PATH/rascsi-oled.service" ]]; then SERVICE_RASCSI_OLED_RUNNING=0 sudo systemctl is-active --quiet rascsi-oled.service >/dev/null 2>&1 || SERVICE_RASCSI_OLED_RUNNING=$? if [[ $SERVICE_RASCSI_OLED_RUNNING -eq 0 ]]; then sudo systemctl stop rascsi-oled.service fi fi } # Stops the rascsi-ctrlboard service if it is running function stopRaScsiCtrlBoard() { if [[ -f "$SYSTEMD_PATH/rascsi-ctrlboard.service" ]]; then SERVICE_RASCSI_CTRLBOARD_RUNNING=0 sudo systemctl is-active --quiet rascsi-ctrlboard.service >/dev/null 2>&1 || SERVICE_RASCSI_CTRLBOARD_RUNNING=$? if [[ SERVICE_RASCSI_CTRLBOARD_RUNNING -eq 0 ]]; then sudo systemctl stop rascsi-ctrlboard.service fi fi } # disables and removes the old monitor_rascsi service function disableOldRaScsiMonitorService() { if [ -f "$SYSTEMD_PATH/monitor_rascsi.service" ]; then SERVICE_MONITOR_RASCSI_RUNNING=0 sudo systemctl is-active --quiet monitor_rascsi.service >/dev/null 2>&1 || SERVICE_MONITOR_RASCSI_RUNNING=$? if [[ $SERVICE_MONITOR_RASCSI_RUNNING -eq 0 ]]; then sudo systemctl stop monitor_rascsi.service fi SERVICE_MONITOR_RASCSI_ENABLED=0 sudo systemctl is-enabled --quiet monitor_rascsi.service >/dev/null 2>&1 || SERVICE_MONITOR_RASCSI_ENABLED=$? if [[ $SERVICE_MONITOR_RASCSI_ENABLED -eq 0 ]]; then sudo systemctl disable monitor_rascsi.service fi sudo rm $SYSTEMD_PATH/monitor_rascsi.service fi } # disables the rascsi-oled service function disableRaScsiOledService() { if [ -f "$SYSTEMD_PATH/rascsi-oled.service" ]; then SERVICE_RASCSI_OLED_RUNNING=0 sudo systemctl is-active --quiet rascsi-oled.service >/dev/null 2>&1 || SERVICE_RASCSI_OLED_RUNNING=$? if [[ $SERVICE_RASCSI_OLED_RUNNING -eq 0 ]]; then sudo systemctl stop rascsi-oled.service fi SERVICE_RASCSI_OLED_ENABLED=0 sudo systemctl is-enabled --quiet rascsi-oled.service >/dev/null 2>&1 || SERVICE_RASCSI_OLED_ENABLED=$? if [[ $SERVICE_RASCSI_OLED_ENABLED -eq 0 ]]; then sudo systemctl disable rascsi-oled.service fi fi } # disables the rascsi-ctrlboard service function disableRaScsiCtrlBoardService() { if [ -f "$SYSTEMD_PATH/rascsi-ctrlboard.service" ]; then SERVICE_RASCSI_CTRLBOARD_RUNNING=0 sudo systemctl is-active --quiet rascsi-ctrlboard.service >/dev/null 2>&1 || SERVICE_RASCSI_CTRLBOARD_RUNNING=$? if [[ $SERVICE_RASCSI_CTRLBOARD_RUNNING -eq 0 ]]; then sudo systemctl stop rascsi-ctrlboard.service fi SERVICE_RASCSI_CTRLBOARD_ENABLED=0 sudo systemctl is-enabled --quiet rascsi-ctrlboard.service >/dev/null 2>&1 || SERVICE_RASCSI_CTRLBOARD_ENABLED=$? if [[ $SERVICE_RASCSI_CTRLBOARD_ENABLED -eq 0 ]]; then sudo systemctl disable rascsi-ctrlboard.service fi fi } # Stops the macproxy service if it is running function stopMacproxy() { if [ -f "$SYSTEMD_PATH/macproxy.service" ]; then sudo systemctl stop macproxy.service fi } # Checks whether the rascsi-oled service is installed function isRaScsiScreenInstalled() { SERVICE_RASCSI_OLED_ENABLED=0 if [[ -f "$SYSTEMD_PATH/rascsi-oled.service" ]]; then sudo systemctl is-enabled --quiet rascsi-oled.service >/dev/null 2>&1 || SERVICE_RASCSI_OLED_ENABLED=$? elif [[ -f "$SYSTEMD_PATH/monitor_rascsi.service" ]]; then sudo systemctl is-enabled --quiet monitor_rascsi.service >/dev/null 2>&1 || SERVICE_RASCSI_OLED_ENABLED=$? else SERVICE_RASCSI_OLED_ENABLED=1 fi echo $SERVICE_RASCSI_OLED_ENABLED } # Checks whether the rascsi-ctrlboard service is installed function isRaScsiCtrlBoardInstalled() { SERVICE_RASCSI_CTRLBOARD_ENABLED=0 if [[ -f "$SYSTEMD_PATH/rascsi-ctrlboard.service" ]]; then sudo systemctl is-enabled --quiet rascsi-ctrlboard.service >/dev/null 2>&1 || SERVICE_RASCSI_CTRLBOARD_ENABLED=$? else SERVICE_RASCSI_CTRLBOARD_ENABLED=1 fi echo $SERVICE_RASCSI_CTRLBOARD_ENABLED } # Checks whether the rascsi-oled service is running function isRaScsiScreenRunning() { SERVICE_RASCSI_OLED_RUNNING=0 if [[ -f "$SYSTEMD_PATH/rascsi-oled.service" ]]; then sudo systemctl is-active --quiet rascsi-oled.service >/dev/null 2>&1 || SERVICE_RASCSI_OLED_RUNNING=$? elif [[ -f "$SYSTEMD_PATH/monitor_rascsi.service" ]]; then sudo systemctl is-active --quiet monitor_rascsi.service >/dev/null 2>&1 || SERVICE_RASCSI_OLED_RUNNING=$? else SERVICE_RASCSI_OLED_RUNNING=1 fi echo $SERVICE_RASCSI_OLED_RUNNING } # Checks whether the rascsi-oled service is running function isRaScsiCtrlBoardRunning() { SERVICE_RASCSI_CTRLBOARD_RUNNING=0 if [[ -f "$SYSTEMD_PATH/rascsi-ctrlboard.service" ]]; then sudo systemctl is-active --quiet rascsi-ctrlboard.service >/dev/null 2>&1 || SERVICE_RASCSI_CTRLBOARD_RUNNING=$? else SERVICE_RASCSI_CTRLBOARD_RUNNING=1 fi echo $SERVICE_RASCSI_CTRLBOARD_RUNNING } # Starts the rascsi-oled service if installed function startRaScsiScreen() { if [[ $(isRaScsiScreenInstalled) -eq 0 ]] && [[ $(isRaScsiScreenRunning) -ne 1 ]]; then sudo systemctl start rascsi-oled.service showRaScsiScreenStatus fi } # Starts the rascsi-ctrlboard service if installed function startRaScsiCtrlBoard() { if [[ $(isRaScsiCtrlBoardInstalled) -eq 0 ]] && [[ $(isRaScsiCtrlBoardRunning) -ne 1 ]]; then sudo systemctl start rascsi-ctrlboard.service showRaScsiCtrlBoardStatus fi } # Starts the macproxy service if installed function startMacproxy() { if [ -f "$SYSTEMD_PATH/macproxy.service" ]; then sudo systemctl start macproxy.service showMacproxyStatus fi } # Shows status for the rascsi service function showRaScsiStatus() { systemctl status rascsi | tee } # Shows status for the rascsi-web service function showRaScsiWebStatus() { systemctl status rascsi-web | tee } # Shows status for the rascsi-oled service function showRaScsiScreenStatus() { systemctl status rascsi-oled | tee } # Shows status for the rascsi-ctrlboard service function showRaScsiCtrlBoardStatus() { systemctl status rascsi-ctrlboard | tee } # Shows status for the macproxy service function showMacproxyStatus() { systemctl status macproxy | tee } # Creates a drive image file with specific parameters function createDrive600MB() { createDrive 600 "HD600" } # Creates a drive image file and prompts for parameters function createDriveCustom() { driveSize=-1 until [ $driveSize -ge "10" ] && [ $driveSize -le "4000" ]; do echo "What drive size would you like (in MB) (10-4000)" read driveSize echo "How would you like to name that drive?" read driveName done createDrive "$driveSize" "$driveName" } # Creates an HFS file system function formatDrive() { diskPath="$1" volumeName="$2" if [ ! -x $HFS_FORMAT ]; then # Install hfsutils to have hformat to format HFS sudo apt-get install hfsutils --assume-yes > /etc/dhcpcd.conf' echo "Modified /etc/dhcpcd.conf" # default config file is made for eth0, this will set the right net interface sudo bash -c 'sed s/eth0/'"$LAN_INTERFACE"'/g '"$BASE"'/src/raspberrypi/os_integration/rascsi_bridge > /etc/network/interfaces.d/rascsi_bridge' echo "Modified /etc/network/interfaces.d/rascsi_bridge" echo "Configuration completed!" echo "Please make sure you attach a DaynaPORT network adapter to your RaSCSI configuration." echo "Either use the Web UI, or do this on the command line (assuming SCSI ID 6):" echo "rasctl -i 6 -c attach -t scdp -f $LAN_INTERFACE" echo "" echo "We need to reboot your Pi" echo "Press Enter to reboot or CTRL-C to exit" read echo "Rebooting..." sleep 3 sudo reboot } # Modifies system configurations for a wireless network bridge with NAT function setupWirelessNetworking() { NETWORK="10.10.20" IP=$NETWORK.2 # Macintosh or Device IP NETWORK_MASK="255.255.255.0" CIDR="24" ROUTER_IP=$NETWORK.1 ROUTING_ADDRESS=$NETWORK.0/$CIDR WLAN_INTERFACE="wlan0" echo "$WLAN_INTERFACE will be configured for network forwarding with static IP assignment." echo "Configure your Macintosh or other device with the following:" echo "IP Address (static): $IP" echo "Router Address: $ROUTER_IP" echo "Subnet Mask: $NETWORK_MASK" echo "DNS Server: Any public DNS server" echo "" echo "Do you want to proceed with network configuration using the default settings? [Y/n]" read REPLY if [ "$REPLY" == "N" ] || [ "$REPLY" == "n" ]; then echo "Available wireless interfaces on this system:" echo `ip -o addr show scope link | awk '{split($0, a); print $2}' | grep wlan` echo "Please type the wireless interface you want to use and press Enter:" read -r WLAN_INTERFACE echo "Base IP address (ex. 10.10.20):" read -r NETWORK echo "CIDR for Subnet Mask (ex. '24' for 255.255.255.0):" read -r CIDR ROUTER_IP=$NETWORK.1 ROUTING_ADDRESS=$NETWORK.0/$CIDR fi if [ "$(grep -c "^net.ipv4.ip_forward=1" /etc/sysctl.conf)" -ge 1 ]; then echo "WARNING: Network forwarding may already have been configured. Proceeding will overwrite the configuration." echo "Press enter to continue or CTRL-C to exit" read REPLY else sudo bash -c 'echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf' echo "Modified /etc/sysctl.conf" fi # Check if iptables is installed if [ `apt-cache policy iptables | grep Installed | grep -c "(none)"` -eq 0 ]; then echo "iptables is already installed" else sudo apt-get install iptables --assume-yes