-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindfs-to-home.sh
executable file
·154 lines (134 loc) · 4.83 KB
/
bindfs-to-home.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
# @author Spas Z. Spasov <spas.z.spasov@gmail.com>
# @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3
# @home https://github.com/metalevel-tech/bindfs-to-home-bash
#
# @name bindfs-to-home.sh
[[ -z ${1+x} ]] && DO_ACTION="" || DO_ACTION="$1" # Default action LIST MOUNTED, VERBOSE OUTPUT
[[ -z ${2+x} ]] && SOURCE_DIR="" || SOURCE_DIR="$2" # Default target directory '/var/www'
[[ -z ${3+x} ]] && SOURCE_USR="AUTO" || SOURCE_USR="$3" # Default source user is the user owned the target dir
if [[ ! -d $SOURCE_DIR ]]; then
if [[ -d /var/www ]]; then
SOURCE_DIR="/var/www" # Fallback of the default directory
else
SOURCE_DIR="/etc" # Fallback of the default directory
fi
fi
RELAT_PATH="${HOME}/bindfs"
TARGET_DIR="${RELAT_PATH}${SOURCE_DIR}"
function list_all_mounted_in_relative_path() {
printf "List of the directories mounted under '%s':\n\033[1;33m%s\n" \
"${RELAT_PATH}" \
"$(findmnt -nrt fuse -o TARGET | grep "$RELAT_PATH" || echo 'No mounted directories!')"
printf '\033[0m'
}
function target_dir_unmount() {
sudo fusermount -u "$TARGET_DIR" 2>/dev/null
}
function unmount_all_in_relative_path() {
echo ''
for DIR in $(findmnt -nrt fuse -o TARGET | grep "$RELAT_PATH"); do
if [[ $DIR == '' ]]; then
return false
else
sudo fusermount -u "$DIR"
if [[ $? -eq 0 ]]; then
printf '\033[1;33m%s\033[0m is unmounted.\n' "$DIR"
else
printf '\033[1;33mSomething went wrong!\033[0m\n' "$DIR"
fi
fi
done
}
function target_dir_create() {
mkdir -p "$TARGET_DIR"
}
function target_dir_mount() {
sudo bindfs -u "$TGT_USR_UID" -g "$TGT_USR_GID" \
--create-for-user="$SRC_USR_UID" \
--create-for-group="$SRC_USR_GID" \
"$SOURCE_DIR" "$TARGET_DIR"
}
function get_users() {
if [[ ${SOURCE_USR^^} == 'AUTO' ]]; then
SRC_USR_UID=$(stat -c '%u' "${SOURCE_DIR}")
SRC_USR_GID=$(stat -c '%g' "${SOURCE_DIR}")
else
SRC_USR_UID=$(id "$SOURCE_USR" -u)
SRC_USR_GID=$(id "$SOURCE_USR" -g)
fi
TGT_USR_UID=$(id -u)
TGT_USR_GID=$(id -g)
}
function print_vars() {
echo -e "Variables:\n"
echo -e "\tSOURCE_DIR: $SOURCE_DIR"
echo -e "\tSOURCE_USR: $SOURCE_USR\n"
echo -e "\tRELAT_PATH: $RELAT_PATH"
echo -e "\tTARGET_DIR: $TARGET_DIR\n"
echo -e "\tSRC_USR_UID: $SRC_USR_UID"
echo -e "\tSRC_USR_GID: $SRC_USR_GID\n"
echo -e "\tTGT_USR_UID: $TGT_USR_UID"
echo -e "\tTGT_USR_GID: $TGT_USR_GID\n"
echo -e "\tDO_ACTION: [${DO_ACTION^^}] \033[1;33m${1}\033[0m\n"
}
function get_help() {
echo -e "Syntax:"
echo -e "\t$(basename ${0}) (M|U|A|L)[-V][-H] /target/dir user-owned-the-target-dir\n"
echo -e "Examples:"
echo -e "\t$(basename ${0}) -V"
echo -e "\t$(basename ${0}) -H"
echo -e "\t$(basename ${0}) M-V [/target/dir] [user]"
echo -e "\t$(basename ${0}) U-V [/target/dir]"
echo -e "\t$(basename ${0}) L"
echo -e "\t$(basename ${0}) Mount [/target/dir] [user]"
echo -e "\n\t m: mount\n\t u: un mount\n\t a: un mount all [default]\n\t l: list all mounted directories"
echo -e "\n\t-v|v: print the variables\n\t-h|h: print the help\n"
}
function do_action() {
# Output the help and the current vars
# if there isn't provided a correct option
if [[ ! ${DO_ACTION^^} =~ ^(\-\-H|\-H|H|\-\-V|\-V|V|M|U|L) ]]; then
get_help
print_vars "Not supported!"
exit
fi
# Pseudo options process ---
if [[ ${DO_ACTION^^} =~ ^(\-\-|\-|)H ]]; then
[[ ${DO_ACTION^^} =~ (\-\-|\-|)V ]] && print_vars
get_help
exit
fi
if [[ ${DO_ACTION^^} =~ ^(\-\-|\-|)V ]]; then
print_vars
[[ ${DO_ACTION^^} =~ (\-\-|\-|)H ]] && get_help
exit
elif [[ ${DO_ACTION^^} =~ (\-\-|\-|)V ]]; then
print_vars
[[ ${DO_ACTION^^} =~ (\-\-|\-|)H ]] && get_help
fi
# ---
if [[ ${DO_ACTION^^} =~ M ]]; then
target_dir_unmount # try to unmount target dir just in case
target_dir_create # try to make the target dir just in case
target_dir_mount # finally do the bindfs mount
elif [[ ${DO_ACTION^^} =~ U ]]; then
if target_dir_unmount; then
printf '\033[1;33m%s\033[0m is unmounted.\n' "$TARGET_DIR"
else
printf '\033[1;33m%s\033[0m was not mounted.\n' "$TARGET_DIR"
fi
elif [[ ${DO_ACTION^^} =~ L ]]; then
list_all_mounted_in_relative_path
else
list_all_mounted_in_relative_path
# We do not need [[ ${DO_ACTION^^} =~ A ]]
# because will fall here if it is not M|U|L
if ! unmount_all_in_relative_path; then
list_all_mounted_in_relative_path
fi
fi
}
# Do the actions
get_users
do_action