odcold

S00ubifs

Aug 29th, 2024 (edited)
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.09 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # SPDX-License-Identifier: WTFPL
  4. #
  5. # Copyright (C) 2020-2024 Entware team
  6.  
  7. ### colors
  8. ansi_std="\033[0;0m"
  9. ansi_red="\033[1;31m"
  10. ansi_green="\033[1;32m"
  11. ansi_yellow="\033[1;33m"
  12. ansi_cyan="\033[1;36m"
  13. ansi_white="\033[1;37m"
  14.  
  15. ### columns
  16. COLUMNS="45"
  17.  
  18. ### commands
  19. ACTION="$1"
  20.  
  21. ### dirs
  22. TMP_DIR="/opt/tmp"
  23. LOG_DIR="/opt/var/log"
  24. RUN_DIR="/opt/var/run"
  25. AGH_DIR="/opt/etc/AdGuardHome/data"
  26. ### service
  27. ENABLED=yes
  28. DESC="UBIFS"
  29.  
  30. ### paths
  31. PATH="/opt/bin:/opt/sbin:/opt/usr/bin:/opt/usr/sbin"
  32.  
  33. DIR="$TMP_DIR $LOG_DIR $RUN_DIR $AGH_DIR"
  34.  
  35. mode_size() {
  36. SIZE="1M"
  37. MODE="1755"
  38. if [ "$d" = "$TMP_DIR" ]; then
  39.   SIZE="20M"
  40.   MODE="1777"
  41. elif [ "$d" = "$LOG_DIR" ]; then
  42.   SIZE="10M"
  43. elif [ "$d" = "$AGH_DIR" ]; then
  44.   SIZE="50M"
  45.   MODE="1777"
  46.     fi
  47. }
  48.  
  49. do_check() {
  50.   printf "$ansi_white %-${COLUMNS}s $ansi_std" "Checking the mount points ..."
  51.  
  52.   if [ "$ENABLED" = "no" ]; then
  53.     printf "$ansi_cyan %-${COLUMNS}s $ansi_std\n" "autorun disabled."
  54.   elif [ "$ACTION" = "start" ]; then
  55.     for d in $DIR; do
  56.       if grep -qs "$d" /proc/mounts; then
  57.         printf "$ansi_yellow %-${COLUMNS}s $ansi_std\n" "$d already mounted."
  58.       else
  59.         printf "$ansi_red %-${COLUMNS}s $ansi_std\n" "$d not mounted."
  60.       fi
  61.     done
  62.   elif [ "$ACTION" = "reload" ] || [ "$ACTION" = "restart" ]; then
  63.     printf "$ansi_yellow %-${COLUMNS}s $ansi_std\n" "\"$ACTION\" not supported."
  64.   else
  65.     for d in $DIR; do
  66.       if grep -qs "$d" /proc/mounts; then
  67.         printf "\n$ansi_white %-${COLUMNS}s $ansi_std$ansi_yellow %-${COLUMNS}s $ansi_std" "$d" "already mounted."
  68.       else
  69.         printf "\n$ansi_white %-${COLUMNS}s $ansi_std$ansi_red %-${COLUMNS}s $ansi_std" "$d" "not mounted."
  70.       fi
  71.     done
  72.     printf "\n"
  73.   fi
  74. }
  75.  
  76. do_start() {
  77.   [ "$ENABLED" = "yes" ] || return 1
  78.  
  79.   for d in $DIR; do
  80.     mode_size
  81.     printf "$ansi_white %-${COLUMNS}s $ansi_std" "Mounting $d ..."
  82.       if grep -qs "$d" /proc/mounts; then
  83.         printf "$ansi_yellow %-${COLUMNS}s $ansi_std\n" "already mounted."
  84.         continue || return 0
  85.       else
  86.         if mount -t tmpfs -o size="$SIZE",mode="$MODE" tmpfs "$d"; then
  87.           printf "$ansi_green %-${COLUMNS}s $ansi_std\n" "done."
  88.           continue || return 0
  89.         else
  90.           printf "$ansi_red %-${COLUMNS}s $ansi_std\n" "failed."
  91.           continue || return 1
  92.       fi
  93.     fi
  94.   done
  95. }
  96.  
  97. do_stop() {
  98.   [ "$ENABLED" = "yes" ] || return 1
  99.  
  100.   for d in $DIR; do
  101.     printf "$ansi_white %-${COLUMNS}s $ansi_std" "Unmounting $d ..."
  102.       if ! grep -qs "$d" /proc/mounts; then
  103.         printf "$ansi_red %-${COLUMNS}s $ansi_std\n" "not mounted."
  104.         continue || return 0
  105.       else
  106.         if umount "$d"; then
  107.           printf "$ansi_green %-${COLUMNS}s $ansi_std\n" "done."
  108.           continue || return 0
  109.         else
  110.           printf "$ansi_red %-${COLUMNS}s $ansi_std\n" "failed."
  111.           continue || return 1
  112.       fi
  113.     fi
  114.   done
  115. }
  116.  
  117. do_enable() {
  118.   printf "$ansi_white %-${COLUMNS}s $ansi_std" "Enabling autorun $DESC ..."
  119.   if [ "$ENABLED" = "no" ]; then
  120.     sed -i 's,^ENABLED=no,ENABLED=yes,' "$0"
  121.     printf "$ansi_green %-${COLUMNS}s $ansi_std\n" "done."
  122.   else
  123.     printf "$ansi_yellow %-${COLUMNS}s $ansi_std\n" "already enabled."
  124.   fi
  125.   return 0
  126. }
  127.  
  128. do_disable() {
  129.   printf "$ansi_white %-${COLUMNS}s $ansi_std" "Disabling autorun $DESC ..."
  130.   if [ "$ENABLED" = "yes" ]; then
  131.     sed -i 's,^ENABLED=yes,ENABLED=no,' "$0"
  132.     printf "$ansi_green %-${COLUMNS}s $ansi_std\n" "done."
  133.   else
  134.     printf "$ansi_yellow %-${COLUMNS}s $ansi_std\n" "already disabled."
  135.   fi
  136.   return 0
  137. }
  138.  
  139. case "$1" in
  140.     start)
  141.         { do_check > /dev/null && do_start ; } || do_check
  142.     ;;
  143.     kill|stop)
  144.         { do_check > /dev/null && do_stop ; } || do_check
  145.     ;;
  146.     check|reload|restart|status)
  147.         do_check
  148.     ;;
  149.     enable)
  150.         do_enable
  151.     ;;
  152.     disable)
  153.         "$0" stop > /dev/null 2>&1 && do_disable
  154.     ;;
  155.     *)
  156.         printf "$ansi_white %-${COLUMNS}s %-${COLUMNS}s $ansi_std\n" "Usage: $0" "{start|[kill|stop]|[check|status]}"
  157.         exit 1
  158.     ;;
  159. esac
  160.  
  161. exit 0
Advertisement
Add Comment
Please, Sign In to add comment