#!/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 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 } BASE="$HOME/RASCSI" VIRTUAL_DRIVER_PATH="$HOME/images" CFG_PATH="$HOME/.config/rascsi" WEBINSTDIR="$BASE/src/web" 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} set -e function initialChecks() { currentUser=$(whoami) if [ "pi" != "$currentUser" ]; then echo "You must use 'pi' user (current: $currentUser)" exit 1 fi if [ ! -d "$BASE" ]; then echo "You must checkout RASCSI repo into $BASE" echo "$ git clone git@github.com:akuker/RASCSI.git" exit 2 fi } # 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 nginx libpcap-dev protobuf-compiler bridge-utils python3-dev libev-dev libevdev2 -y > /etc/sudoers' else echo "The sudoers file is already modified for rascsi-web." fi sudo systemctl daemon-reload sudo systemctl restart rsyslog sudo systemctl enable rascsi # optional - start rascsi at boot sudo systemctl start rascsi } # install everything required to run an HTTP server (Nginx + Python Flask App) function installRaScsiWebInterface() { if [ -f "$WEBINSTDIR/rascsi_interface_pb2.py" ]; then rm "$WEBINSTDIR/rascsi_interface_pb2.py" echo "Deleting old Python protobuf library rascsi_interface_pb2.py" fi echo "Compiling the Python protobuf library rascsi_interface_pb2.py..." protoc -I="$BASE/src/raspberrypi/" --python_out="$WEBINSTDIR" rascsi_interface.proto sudo cp -f "$BASE/src/web/service-infra/nginx-default.conf" /etc/nginx/sites-available/default sudo cp -f "$BASE/src/web/service-infra/502.html" /var/www/html/502.html sudo usermod -a -G pi www-data sudo systemctl reload nginx || true echo "Installing the rascsi-web.service configuration..." sudo cp -f "$BASE/src/web/service-infra/rascsi-web.service" /etc/systemd/system/rascsi-web.service sudo systemctl daemon-reload sudo systemctl enable rascsi-web sudo systemctl start rascsi-web } function installRaScsiScreen() { echo "IMPORTANT: This configuration requires a OLED screen to be installed onto your RaSCSI board." echo "See wiki for more information: https://github.com/akuker/RASCSI/wiki/OLED-Status-Display-(Optional)" echo "" echo "Do you want to use the recommended screen rotation (180 degrees)?" echo "Press Y/n and Enter, or CTRL-C to exit" read REPLY if [ "$REPLY" == "N" ] || [ "$REPLY" == "n" ]; then echo "Proceeding with 0 degrees rotation." ROTATION="0" else echo "Proceeding with 180 degrees rotation." ROTATION="180" fi sudo systemctl stop monitor_rascsi || true updateRaScsiGit sudo apt-get update && sudo apt-get install python3-dev python3-pip python3-venv libjpeg-dev libpng-dev libopenjp2-7-dev i2c-tools -y > /boot/config.txt' REBOOT=1 else sudo bash -c 'echo "dtparam=i2c_arm=on" >> /boot/config.txt' echo "Modified /boot/config.txt" REBOOT=1 fi echo "Installing the monitor_rascsi.service configuration..." sudo cp -f "$BASE/src/oled_monitor/monitor_rascsi.service" /etc/systemd/system/monitor_rascsi.service sudo sed -i /^ExecStart=/d /etc/systemd/system/monitor_rascsi.service sudo sed -i "8 i ExecStart=$BASE/src/oled_monitor/start.sh --rotation=$ROTATION" /etc/systemd/system/monitor_rascsi.service sudo systemctl daemon-reload sudo systemctl enable monitor_rascsi if [ $REBOOT -eq 1 ]; then echo "" echo "The monitor_rascsi service will start on the next Pi boot." echo "Press Enter to reboot or CTRL-C to exit" read echo "Rebooting..." sleep 3 sudo reboot fi sudo systemctl start monitor_rascsi } function createImagesDir() { if [ -d "$VIRTUAL_DRIVER_PATH" ]; then echo "The $VIRTUAL_DRIVER_PATH directory already exists." else echo "The $VIRTUAL_DRIVER_PATH directory does not exist; creating..." mkdir -p "$VIRTUAL_DRIVER_PATH" chmod -R 775 "$VIRTUAL_DRIVER_PATH" fi if [ -d "$CFG_PATH" ]; then echo "The $CFG_PATH directory already exists." else echo "The $CFG_PATH directory does not exist; creating..." mkdir -p "$CFG_PATH" chmod -R 775 "$CFG_PATH" fi } function stopOldWebInterface() { sudo systemctl stop rascsi-web || true APACHE_STATUS=$(sudo systemctl status apache2 &> /dev/null; echo $?) if [ "$APACHE_STATUS" -eq 0 ] ; then echo "Stopping old Apache2 RaSCSI Web..." sudo systemctl disable apache2 sudo systemctl stop apache2 fi } function updateRaScsiGit() { cd "$BASE" || exit 1 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 } function showRaScsiStatus() { sudo systemctl status rascsi | tee } function showRaScsiWebStatus() { sudo systemctl status rascsi-web | tee } function showRaScsiScreenStatus() { sudo systemctl status monitor_rascsi | tee } function createDrive600MB() { createDrive 600 "HD600" } 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" } 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 } 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 sudo iptables --flush sudo iptables -t nat -F sudo iptables -X sudo iptables -Z sudo iptables -P INPUT ACCEPT sudo iptables -P OUTPUT ACCEPT sudo iptables -P FORWARD ACCEPT sudo iptables -t nat -A POSTROUTING -o "$WLAN_INTERFACE" -s "$ROUTING_ADDRESS" -j MASQUERADE # Check if iptables-persistent is installed if [ `apt-cache policy iptables-persistent | grep Installed | grep -c "(none)"` -eq 0 ]; then echo "iptables-persistent is already installed" sudo iptables-save --file /etc/iptables/rules.v4 else sudo apt-get install iptables-persistent --assume-yes > ./config/AppleVolumes.default.tmpl echo "ATALKD_RUN=yes" >> ./config/netatalk.conf echo "\"RaSCSI-Pi\" -transall -uamlist uams_guest.so,uams_clrtxt.so,uams_dhx.so -defaultvol /etc/netatalk/AppleVolumes.default -systemvol /etc/netatalk/AppleVolumes.system -nosavepassword -nouservol -guestname \"nobody\" -setuplog \"default log_maxdebug /var/log/afpd.log\"" >> ./config/afpd.conf.tmpl ( sudo apt-get update && sudo apt-get install libssl-dev libdb-dev libcups2-dev autotools-dev automake libtool --assume-yes )