Advertisement
odcold

S00ubifs

Aug 29th, 2024 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.98 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.  
  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"
  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. fi
  44. }
  45.  
  46. do_check() {
  47.   printf "$ansi_white %-${COLUMNS}s $ansi_std" "Checking the mount points ..."
  48.  
  49.   if [ "$ENABLED" = "no" ]; then
  50.     printf "$ansi_cyan %-${COLUMNS}s $ansi_std\n" "autorun disabled."
  51.   elif [ "$ACTION" = "start" ]; then
  52.     for d in $DIR; do
  53.       if grep -qs "$d" /proc/mounts; then
  54.         printf "$ansi_yellow %-${COLUMNS}s $ansi_std\n" "$d already mounted."
  55.       else
  56.         printf "$ansi_red %-${COLUMNS}s $ansi_std\n" "$d not mounted."
  57.       fi
  58.     done
  59.   elif [ "$ACTION" = "reload" ] || [ "$ACTION" = "restart" ]; then
  60.     printf "$ansi_yellow %-${COLUMNS}s $ansi_std\n" "\"$ACTION\" not supported."
  61.   else
  62.     for d in $DIR; do
  63.       if grep -qs "$d" /proc/mounts; then
  64.         printf "\n$ansi_white %-${COLUMNS}s $ansi_std$ansi_yellow %-${COLUMNS}s $ansi_std" "$d" "already mounted."
  65.       else
  66.         printf "\n$ansi_white %-${COLUMNS}s $ansi_std$ansi_red %-${COLUMNS}s $ansi_std" "$d" "not mounted."
  67.       fi
  68.     done
  69.     printf "\n"
  70.   fi
  71. }
  72.  
  73. do_start() {
  74.   [ "$ENABLED" = "yes" ] || return 1
  75.  
  76.   for d in $DIR; do
  77.     mode_size
  78.     printf "$ansi_white %-${COLUMNS}s $ansi_std" "Mounting $d ..."
  79.       if grep -qs "$d" /proc/mounts; then
  80.         printf "$ansi_yellow %-${COLUMNS}s $ansi_std\n" "already mounted."
  81.         continue || return 0
  82.       else
  83.         if mount -t tmpfs -o size="$SIZE",mode="$MODE" tmpfs "$d"; then
  84.           printf "$ansi_green %-${COLUMNS}s $ansi_std\n" "done."
  85.           continue || return 0
  86.         else
  87.           printf "$ansi_red %-${COLUMNS}s $ansi_std\n" "failed."
  88.           continue || return 1
  89.       fi
  90.     fi
  91.   done
  92. }
  93.  
  94. do_stop() {
  95.   [ "$ENABLED" = "yes" ] || return 1
  96.  
  97.   for d in $DIR; do
  98.     printf "$ansi_white %-${COLUMNS}s $ansi_std" "Unmounting $d ..."
  99.       if ! grep -qs "$d" /proc/mounts; then
  100.         printf "$ansi_red %-${COLUMNS}s $ansi_std\n" "not mounted."
  101.         continue || return 0
  102.       else
  103.         if umount "$d"; then
  104.           printf "$ansi_green %-${COLUMNS}s $ansi_std\n" "done."
  105.           continue || return 0
  106.         else
  107.           printf "$ansi_red %-${COLUMNS}s $ansi_std\n" "failed."
  108.           continue || return 1
  109.       fi
  110.     fi
  111.   done
  112. }
  113.  
  114. do_enable() {
  115.   printf "$ansi_white %-${COLUMNS}s $ansi_std" "Enabling autorun $DESC ..."
  116.   if [ "$ENABLED" = "no" ]; then
  117.     sed -i 's,^ENABLED=no,ENABLED=yes,' "$0"
  118.     printf "$ansi_green %-${COLUMNS}s $ansi_std\n" "done."
  119.   else
  120.     printf "$ansi_yellow %-${COLUMNS}s $ansi_std\n" "already enabled."
  121.   fi
  122.   return 0
  123. }
  124.  
  125. do_disable() {
  126.   printf "$ansi_white %-${COLUMNS}s $ansi_std" "Disabling autorun $DESC ..."
  127.   if [ "$ENABLED" = "yes" ]; then
  128.     sed -i 's,^ENABLED=yes,ENABLED=no,' "$0"
  129.     printf "$ansi_green %-${COLUMNS}s $ansi_std\n" "done."
  130.   else
  131.     printf "$ansi_yellow %-${COLUMNS}s $ansi_std\n" "already disabled."
  132.   fi
  133.   return 0
  134. }
  135.  
  136. case "$1" in
  137.     start)
  138.         { do_check > /dev/null && do_start ; } || do_check
  139.     ;;
  140.     kill|stop)
  141.         { do_check > /dev/null && do_stop ; } || do_check
  142.     ;;
  143.     check|reload|restart|status)
  144.         do_check
  145.     ;;
  146.     enable)
  147.         do_enable
  148.     ;;
  149.     disable)
  150.         "$0" stop > /dev/null 2>&1 && do_disable
  151.     ;;
  152.     *)
  153.         printf "$ansi_white %-${COLUMNS}s %-${COLUMNS}s $ansi_std\n" "Usage: $0" "{start|[kill|stop]|[check|status]}"
  154.         exit 1
  155.     ;;
  156. esac
  157.  
  158. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement