Skip to content

Instantly share code, notes, and snippets.

@Kowalski7
Last active May 7, 2022 14:05
Show Gist options
  • Save Kowalski7/415377458164b9c020966f8672040eb2 to your computer and use it in GitHub Desktop.
Save Kowalski7/415377458164b9c020966f8672040eb2 to your computer and use it in GitHub Desktop.
Kowalski7's QNAP Toolkit
#!/bin/bash
# Kowalski7's QNAP Toolkit
#
# This script helps make the management of my QNAP NAS a bit easier. It includes tools and quick
# fixes for problems I encountered while using my NAS.
#
# THIS SCRIPT COMES WITH NO WARRANTY AND YOU ARE COMPLETELY RESOPONSIBLE FOR ANY DAMAGE IT MIGHT
# CAUSE TO YOUR NAS AND/OR YOUR DATA!!!
toolkit_version=2
update_server="https://pastebin.com/raw/2JhaEEUP"
# Function to check if script is ran with superuser privilages
function check_sudo() {
if [[ $EUID -ne 0 ]]; then
local confirm
echo "WARNING! Most of the features of this script require root privilages to work!"
echo "To run the script as root, you can run the following command:"
echo ""
echo " sudo $0"
echo ""
echo -n "Would you like to continue running it as a regular user? (Y/N) "
read confirm
echo $confirm | grep -q "^[yY]"
if [[ $? -ne 0 ]]; then
exit 0
fi
fi
return 0
}
# Function to kill all instances of ffmpeg repeatedly until they stop spawning
function kill_ffmpeg() {
echo "Killing ffmpeg... (this might take a while)"
local count=-1
while [[ $? -eq 0 ]]; do
((count++))
sleep 0.25
killall ffmpeg &> /dev/null
done
echo "Done! Killed ffmpeg $count time(s)."
sleep 3
return 0
}
# Function to download and/or run the blkdevMonitor script made for checking disk usage on NAS
function blkdevMonitor_Tool() {
if [[ ! -f "/root/blkdevMonitor_20151225.sh" ]]; then
echo "Downloading blkdevMonitor script..."
curl "https://pastebin.com/raw/n4euByuE" | tr -d '\r' > "/root/blkdevMonitor_20151225.sh"
if [[ $? -ne 0 ]]; then
echo "Unable to download the blkdevMonitor script!"
sleep 3
return 1
fi
fi
if [[ ! -x "/root/blkdevMonitor_20151225.sh" ]]; then
chmod +x "/root/blkdevMonitor_20151225.sh"
if [[ $? -ne 0 ]]; then
echo "Unable to make the script executable!"
sleep 3
return 2
fi
fi
clear
/root/blkdevMonitor_20151225.sh
read -n 1 -s -r -p "Press any key to return to the main menu"
return 0
}
# Function to edit the NAS' startup script allowing the user to run custom commands on startup
function edit_autorun_script() {
echo "Please wait..."
mount $(/sbin/hal_app --get_boot_pd port_id=0)6 /tmp/config &> /dev/null
if [[ $? -ne 0 ]]; then
echo "Failed to mount config partition!"
sleep 3
return 1
fi
vi /tmp/config/autorun.sh
chmod +x /tmp/config/autorun.sh
umount /tmp/config
if [[ $? -ne 0 ]]; then
echo "Failed to unmount config partition!"
sleep 3
return 2
fi
return 0
}
# Function to check for updates
function update_checker() {
echo "Checking for updates..."
local confirm
local latest_ver=$(curl --silent $update_server | tr -d '\r' | head -1)
if [[ latest_ver -eq toolkit_version ]]; then
echo "You are already using the latest version!"
echo "Installed version: $toolkit_version"
sleep 3
return 0
elif [[ latest_ver -gt toolkit_version ]]; then
echo "A newer version of the toolkit is available!"
echo "Latest version: $latest_ver"
echo "Installed version: $toolkit_version"
echo ""
echo -n "Would you like to download the update? (Y/N)"
else
echo "You appear to be running a newer version than the one publicly available!"
echo "Latest version: $latest_ver"
echo "Installed version: $toolkit_version"
echo ""
echo -n "Would you like to downgrade to the latest public release? (Y/N)"
fi
read confirm
echo $confirm | grep -q "^[yY]"
if [[ $? -ne 0 ]]; then
return 0
fi
echo ""
echo "Downloading update..."
local latest_hash="$(curl --silent $update_server | tr -d '\r' | tail -1)"
local latest_link="$(curl --silent $update_server | tr -d '\r' | head -2 | tail -1)"
curl "$latest_link" | tr -d '\r' > "/tmp/qnap_toolkit.sh.new"
echo "$latest_hash /tmp/qnap_toolkit.sh.new" | sha256sum --status --check
if [[ $? -ne 0 ]]; then
echo "An error occured while updating! Please try again."
echo "Error code: 1"
sleep 3
return 1
fi
mv -f /tmp/qnap_toolkit.sh.new $0
if [[ $? -ne 0 ]]; then
echo "An error occured while updating! Please try again."
echo "Error code: 2"
sleep 3
return 2
fi
chmod +x $0
if [[ $? -ne 0 ]]; then
echo "An error occured while updating! Please try again."
echo "Error code: 3"
sleep 3
return 3
fi
echo ""
echo "Update complete!"
echo "Please re-run the script to use the latest version."
exit 0
}
function main() {
check_sudo
local choice
while true; do
clear
echo "Kowalski7's QNAP Toolkit"
echo ""
echo "Choose an option:"
echo "1. Kill ffmpeg processes (root required)"
echo "2. Run blkdevMonitor script (root required)"
echo "3. Edit 'autorun.sh' script (for HAL-based Intel and AMD NAS) (root required)"
echo ""
echo "9. Check for toolkit updates"
echo "0. Exit"
echo ""
echo -n "> "
read choice
echo ""
case "$choice" in
1)
kill_ffmpeg
;;
2)
blkdevMonitor_Tool
;;
3)
edit_autorun_script
;;
9)
update_checker
;;
0)
break
;;
*)
echo "Invalid option! Try again."
sleep 2
;;
esac
done
exit 0
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment