Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # SPDX-License-Identifier: WTFPL
- #
- # Copyright (C) 2020-2024 Entware team
- ### colors
- ansi_std="\033[0;0m"
- ansi_red="\033[1;31m"
- ansi_green="\033[1;32m"
- ansi_yellow="\033[1;33m"
- ansi_cyan="\033[1;36m"
- ansi_white="\033[1;37m"
- ### columns
- COLUMNS="45"
- ### commands
- ACTION="$1"
- ### dirs
- TMP_DIR="/opt/tmp"
- LOG_DIR="/opt/var/log"
- RUN_DIR="/opt/var/run"
- ### service
- ENABLED=yes
- DESC="UBIFS"
- ### paths
- PATH="/opt/bin:/opt/sbin:/opt/usr/bin:/opt/usr/sbin"
- DIR="$TMP_DIR $LOG_DIR $RUN_DIR"
- mode_size() {
- SIZE="1M"
- MODE="1755"
- if [ "$d" = "$TMP_DIR" ]; then
- SIZE="20M"
- MODE="1777"
- elif [ "$d" = "$LOG_DIR" ]; then
- SIZE="10M"
- fi
- }
- do_check() {
- printf "$ansi_white %-${COLUMNS}s $ansi_std" "Checking the mount points ..."
- if [ "$ENABLED" = "no" ]; then
- printf "$ansi_cyan %-${COLUMNS}s $ansi_std\n" "autorun disabled."
- elif [ "$ACTION" = "start" ]; then
- for d in $DIR; do
- if grep -qs "$d" /proc/mounts; then
- printf "$ansi_yellow %-${COLUMNS}s $ansi_std\n" "$d already mounted."
- else
- printf "$ansi_red %-${COLUMNS}s $ansi_std\n" "$d not mounted."
- fi
- done
- elif [ "$ACTION" = "reload" ] || [ "$ACTION" = "restart" ]; then
- printf "$ansi_yellow %-${COLUMNS}s $ansi_std\n" "\"$ACTION\" not supported."
- else
- for d in $DIR; do
- if grep -qs "$d" /proc/mounts; then
- printf "\n$ansi_white %-${COLUMNS}s $ansi_std$ansi_yellow %-${COLUMNS}s $ansi_std" "$d" "already mounted."
- else
- printf "\n$ansi_white %-${COLUMNS}s $ansi_std$ansi_red %-${COLUMNS}s $ansi_std" "$d" "not mounted."
- fi
- done
- printf "\n"
- fi
- }
- do_start() {
- [ "$ENABLED" = "yes" ] || return 1
- for d in $DIR; do
- mode_size
- printf "$ansi_white %-${COLUMNS}s $ansi_std" "Mounting $d ..."
- if grep -qs "$d" /proc/mounts; then
- printf "$ansi_yellow %-${COLUMNS}s $ansi_std\n" "already mounted."
- continue || return 0
- else
- if mount -t tmpfs -o size="$SIZE",mode="$MODE" tmpfs "$d"; then
- printf "$ansi_green %-${COLUMNS}s $ansi_std\n" "done."
- continue || return 0
- else
- printf "$ansi_red %-${COLUMNS}s $ansi_std\n" "failed."
- continue || return 1
- fi
- fi
- done
- }
- do_stop() {
- [ "$ENABLED" = "yes" ] || return 1
- for d in $DIR; do
- printf "$ansi_white %-${COLUMNS}s $ansi_std" "Unmounting $d ..."
- if ! grep -qs "$d" /proc/mounts; then
- printf "$ansi_red %-${COLUMNS}s $ansi_std\n" "not mounted."
- continue || return 0
- else
- if umount "$d"; then
- printf "$ansi_green %-${COLUMNS}s $ansi_std\n" "done."
- continue || return 0
- else
- printf "$ansi_red %-${COLUMNS}s $ansi_std\n" "failed."
- continue || return 1
- fi
- fi
- done
- }
- do_enable() {
- printf "$ansi_white %-${COLUMNS}s $ansi_std" "Enabling autorun $DESC ..."
- if [ "$ENABLED" = "no" ]; then
- sed -i 's,^ENABLED=no,ENABLED=yes,' "$0"
- printf "$ansi_green %-${COLUMNS}s $ansi_std\n" "done."
- else
- printf "$ansi_yellow %-${COLUMNS}s $ansi_std\n" "already enabled."
- fi
- return 0
- }
- do_disable() {
- printf "$ansi_white %-${COLUMNS}s $ansi_std" "Disabling autorun $DESC ..."
- if [ "$ENABLED" = "yes" ]; then
- sed -i 's,^ENABLED=yes,ENABLED=no,' "$0"
- printf "$ansi_green %-${COLUMNS}s $ansi_std\n" "done."
- else
- printf "$ansi_yellow %-${COLUMNS}s $ansi_std\n" "already disabled."
- fi
- return 0
- }
- case "$1" in
- start)
- { do_check > /dev/null && do_start ; } || do_check
- ;;
- kill|stop)
- { do_check > /dev/null && do_stop ; } || do_check
- ;;
- check|reload|restart|status)
- do_check
- ;;
- enable)
- do_enable
- ;;
- disable)
- "$0" stop > /dev/null 2>&1 && do_disable
- ;;
- *)
- printf "$ansi_white %-${COLUMNS}s %-${COLUMNS}s $ansi_std\n" "Usage: $0" "{start|[kill|stop]|[check|status]}"
- exit 1
- ;;
- esac
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement