OSDN Git Service

[fix] : Fixed pikaur config
[alterlinux/alterlinux.git] / tools / umount.sh
1 #!/usr/bin/env bash
2 #
3 # Yamada Hayao
4 # Twitter: @Hayao0819
5 # Email  : hayao@fascode.net
6 #
7 # (c) 2019-2021 Fascode Network.
8 #
9 # umount.sh
10 #
11 # Simple script to unmmount everything under the specified directory
12 #
13
14 set -eu
15
16 declare target_dir
17 script_path="$( cd -P "$( dirname "$(readlink -f "$0")" )" && cd .. && pwd )"
18 tools_dir="${script_path}/tools/"
19 debug=false
20 nocolor=false
21 force=false
22 maxdepth="2"
23
24 _help() {
25     echo "usage ${0} [options] [dir]"
26     echo
27     echo "Unmount everything under the specified directory" 
28     echo
29     echo " General options:"
30     echo "    -f | --force              Force umount (No warning)"
31     echo "    -d | --debug              Enable debug message"
32     echo "    -m | --maxdepth           Specify the maximum hierarchy (set 0 to no limit)"
33     echo "    -h | --help               This help message"
34     echo "         --nocolor            No output color message"
35 }
36
37 # Message common function
38 # msg_common [type] [-n] [string]
39 msg_common(){
40     local _msg_opts=("-a" "umount.sh" "--label-space" "6") _type="${1}"
41     shift 1
42     [[ "${1}" = "-n" ]] && _msg_opts+=("-o" "-n") && shift 1
43     [[ "${nocolor}"  = true ]] && _msg_opts+=("-n")
44     _msg_opts+=("${_type}" "${@}")
45     "${tools_dir}/msg.sh" "${_msg_opts[@]}" &
46 }
47
48 # Show an INFO message
49 # ${1}: message string
50 msg_info() { msg_common info "${@}"; }
51
52 # Show an Warning message
53 # ${1}: message string
54 msg_warn() { msg_common warn "${@}"; }
55
56 # Show an debug message
57 # ${1}: message string
58 msg_debug() { 
59     [[ "${debug}" = true ]] && msg_common debug "${@}"
60     return 0
61 }
62
63 # Show an ERROR message then exit with status
64 # ${1}: message string
65 # ${2}: exit code number (with 0 does not exit)
66 msg_error() {
67     msg_common error "${1}"
68     [[ -n "${2:-}" ]] && exit "${2}"
69 }
70
71 # Unmount helper Usage: _umount <target>
72 _umount() { if mountpoint -q "${1}"; then umount -lf "${1}"; fi; }
73
74 # Unmount work dir
75 umount_work () {
76     local _mount
77     if [[ ! -v "target_dir" ]] || [[ "${target_dir}" = "" ]]; then
78         msg_error "Exception error about working directory" 1
79     fi
80     [[ ! -d "${target_dir}" ]] && return 0
81     while read -r _mount; do
82         if [[ "${force}" = true ]] || [[ "${_mount}" = "${target_dir}"* ]] > /dev/null 2>&1; then
83             msg_debug "Checking ${_mount}"
84             if mountpoint -q "${_mount}"; then
85                 msg_info "Unmounting ${_mount}"
86                 _umount "${_mount}" 2> /dev/null
87             fi
88         else
89             msg_error "It is dangerous to unmount a directory that is not managed by the script."
90         fi
91     done < <(
92         if (( maxdepth == 0 )); then
93             find "${target_dir}" -mindepth 1 -type d -printf "%p\n" | tac
94         else
95             find "${target_dir}" -mindepth 1 -maxdepth "${maxdepth}" -type d -printf "%p\n" | tac
96         fi
97     )
98 }
99
100
101 # Parse options
102 OPTS=("d" "f" "h" "m:")
103 OPTL=("debug" "force" "help" "maxdepth:" "nocolor")
104 if ! OPT=$(getopt -o "$(printf "%s," "${OPTS[@]}")" -l "$(printf "%s," "${OPTL[@]}")" --  "${@}"); then
105     exit 1
106 fi
107
108 eval set -- "${OPT}"
109 msg_debug "Argument: ${OPT}"
110 unset OPT OPTS OPTL
111
112 while true; do
113     case "${1}" in
114         -d | --debug)
115             debug=true
116             shift 1
117             ;;
118         -f | --force)
119             force=true
120             shift 1
121             ;;
122         -m | --maxdepth)
123             maxdepth="${2}"
124             shift 2
125             ;;
126         -h | --help)
127             _help
128             exit 0
129             ;;
130         --nocolor)
131             nocolor=true
132             shift 1
133             ;;
134         --)
135             shift
136             break
137             ;;
138         *)
139             msg_error "Invalid argument '${1}'"
140             _help
141             exit 1
142             ;;
143     esac
144 done
145
146 # Check root.
147 if (( ! "${EUID}" == 0 )); then
148     msg_error "This script must be run as root." "1"
149 fi
150
151
152 if [[ -z "${1+SET}" ]]; then
153     msg_error "Please specify the target directory." "1"
154 else
155     target_dir="$(realpath "${1}")"
156 fi
157
158 umount_work