Skip to content

Commit cec81ea

Browse files
committed
[add] : Added umount.sh
1 parent 2a36224 commit cec81ea

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

tools/umount.sh

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Yamada Hayao
4+
# Twitter: @Hayao0819
5+
6+
#
7+
# (c) 2019-2021 Fascode Network.
8+
#
9+
# build.sh
10+
#
11+
# The main script that runs the build
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+
23+
_help() {
24+
echo "usage ${0} [options] [dir]"
25+
echo
26+
echo "Unmount everything under the specified directory"
27+
echo
28+
echo " General options:"
29+
echo " -f | --force Force umount (No warning)"
30+
echo " -d | --debug Enable debug message"
31+
echo " -h | --help This help message"
32+
}
33+
34+
# Message common function
35+
# msg_common [type] [-n] [string]
36+
msg_common(){
37+
local _msg_opts=("-a" "build.sh") _type="${1}"
38+
shift 1
39+
[[ "${1}" = "-n" ]] && _msg_opts+=("-o" "-n") && shift 1
40+
[[ "${nocolor}" = true ]] && _msg_opts+=("-n")
41+
_msg_opts+=("${_type}" "${@}")
42+
"${tools_dir}/msg.sh" "${_msg_opts[@]}"
43+
}
44+
45+
# Show an INFO message
46+
# ${1}: message string
47+
msg_info() { msg_common info "${@}"; }
48+
49+
# Show an Warning message
50+
# ${1}: message string
51+
msg_warn() { msg_common warn "${@}"; }
52+
53+
# Show an debug message
54+
# ${1}: message string
55+
msg_debug() {
56+
[[ "${debug}" = true ]] && msg_common debug "${@}"
57+
return 0
58+
}
59+
60+
# Show an ERROR message then exit with status
61+
# ${1}: message string
62+
# ${2}: exit code number (with 0 does not exit)
63+
msg_error() {
64+
msg_common error "${1}"
65+
[[ -n "${2:-}" ]] && exit "${2}"
66+
}
67+
68+
# Unmount helper Usage: _umount <target>
69+
_umount() { if mountpoint -q "${1}"; then umount -lf "${1}"; fi; }
70+
71+
# Unmount work dir
72+
umount_work () {
73+
local _mount
74+
if [[ ! -v "target_dir" ]] || [[ "${target_dir}" = "" ]]; then
75+
msg_error "Exception error about working directory" 1
76+
fi
77+
[[ ! -d "${target_dir}" ]] && return 0
78+
while read -r _mount; do
79+
if echo "${_mount}" | grep "${target_dir}" > /dev/null 2>&1 || "${force}" = true; then
80+
if mountpoint -q "${_mount}"; then
81+
msg_info "Unmounting ${_mount}"
82+
_umount "${_mount}" 2> /dev/null
83+
fi
84+
else
85+
msg_error "It is dangerous to unmount a directory that is not managed by the script."
86+
fi
87+
done < <(find "${target_dir}" -mindepth 1 -type d -printf "%p\n" | tac)
88+
}
89+
90+
91+
# Check root.
92+
if (( ! "${EUID}" == 0 )); then
93+
msg_error "This script must be run as root." "1"
94+
fi
95+
96+
# Parse options
97+
OPTS=("dfh")
98+
OPTL=("debug" "force" "help:")
99+
if ! OPT=$(getopt -o "$(printf "%s," "${OPTS[@]}")" -l "$(printf "%s," "${OPTL[@]}")" -- "${@}"); then
100+
exit 1
101+
fi
102+
103+
eval set -- "${OPT}"
104+
msg_debug "Argument: ${OPT}"
105+
unset OPT OPTS OPTL
106+
107+
while true; do
108+
case "${1}" in
109+
-d | --debug)
110+
debug=true
111+
shift 1
112+
;;
113+
-f | --force)
114+
force=true
115+
shift 1
116+
;;
117+
-h | --help)
118+
_usage
119+
exit 0
120+
;;
121+
--)
122+
shift
123+
break
124+
;;
125+
*)
126+
msg_error "Invalid argument '${1}'"
127+
_usage 1
128+
;;
129+
esac
130+
done
131+
132+
133+
if [[ -z "${1+SET}" ]]; then
134+
msg_error "Please specify the target directory." "1"
135+
else
136+
target_dir="${1}"
137+
fi
138+
139+
umount_work

0 commit comments

Comments
 (0)