-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnvmesh_update_RO_fs
200 lines (175 loc) · 4.47 KB
/
nvmesh_update_RO_fs
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash
#
# This script is orchestating the flow of updating a shared read only file system data.
# It will umount all of the read-only mounted clients and mount it as read write on the desired client - and the opposite
#
# Defaults
RO2RW=0
RW2RO=0
CONF_PATH="/etc/opt/NVMesh/nvmesh_update_RO_fs.conf"
UMOUNT_CMD="sudo umount "
DETACH_CMD="sudo nvmesh_detach_volumes "
ATTACH_RO_CMD="sudo nvmesh_attach_volumes -p --access SHARED_READ_ONLY "
MOUNT_RO_CMD=" sudo mount -o ro,norecovery "
ATTACH_RW_CMD="sudo nvmesh_attach_volumes -p --access EXCLUSIVE_READ_WRITE "
MOUNT_RW_CMD=" sudo mount -o rw "
CLIENT_SHOW_CMD="nvmesh client show --output-format tabular"
MOUNT_PATH="/proc/mounts "
# Print usage info and exit
usage()
{
echo "About:"
echo " This script is orchestating the flow of updating a shared read only file system data."
echo " It will umount all of the read-only mounted clients and mount it as read write on the desired client - and the opposite"
echo
echo "Usage:"
echo " $0 <Mode> [Node] "
echo
echo "Modes:"
echo " -r Unmount the R/W unique client and mount the RO clients."
echo " -w Unmount the RO clients and mount the R/W unique client."
echo
echo "Node:"
echo " -n STRING client to use as unique writter. (Default: localhost)"
echo
echo "Examples:"
echo " $ $0 -w"
echo " $ $0 -w -n 10.0.0.1"
echo " $ $0 -r"
echo
echo "Node:"
echo " * This script assume a valid SSH connection to each one of the clients defined in $CONF_PATH"
echo " * 'volume_name', 'mount_point' & 'client_list' should be configured in $CONF_PATH"
echo " * The user must have SUDO permissions (also over SSH)"
exit 1
}
parse_args()
{
local OPTIND # local to prevent effects from other subscripts
if [ $# -eq 0 ]; then
echo "ERROR: No mode selected."
usage
fi
while getopts ":rwn:" opt; do
case "${opt}" in
r)
# RW -> RO
RW2RO=1
;;
w)
# RO -> RW
RO2RW=1
;;
n)
# Remote writter
REMOTE_WRITTER=${OPTARG}
;;
*)
# Other option arguments are invalid
usage
;;
esac
done
shift $((OPTIND-1))
}
check_root()
{
prompt=$(sudo -nv 2>&1)
if [ $? -ne 0 ]; then
# exit code of sudo-command is 0
echo "Root or Sudo access required for script ( $(basename $0) )"
exit
fi
}
check_conf()
{
if test -f $CONF_PATH ; then
. $CONF_PATH
echo "Configuration loaded:"
echo " volume_name=$volume_name"
echo " mount_point=$mount_point"
echo " client_list=${client_list[@]}"
else
echo "Configuration file does not exist and required for script ( $(basename $0) )"
echo
echo "Example of configuration file under $CONF_PATH:"
echo " volume_name=\"<volume name>\""
echo " mount_point=\"<mountpoint>\""
echo " client_list=(\"<client1>\" \"client2\" ...)"
echo
exit 1
fi
}
run_cmd_on_remote_client(){
cmd="$@"
cmd_with_ssh="ssh $client \" $cmd \""
output=`eval $cmd_with_ssh 2>&1 > /dev/null`
if [[ $? -ne 0 ]]; then
echo "ERROR - while running $cmd_with_ssh"
echo
fi
}
run_cmd(){
cmd=$@
output=`eval $cmd`
if [[ $? -ne 0 ]]; then
echo "ERROR - while running $cmd"
echo
fi
}
check_volume_not_attached(){
error=0
cmd=$CLIENT_SHOW_CMD
output=`eval $cmd`
if [[ $? -ne 0 ]]; then
error=1
echo "ERROR - while running $cmd"
echo
elif echo "$output" | grep -q "$volume_name"; then
echo "ERROR - $volume_name is still attached"
echo
fi
if [[ $error -ne 0 ]]; then
exit 1
fi
}
umount_all()
{
for client in ${client_list[@]}; do
if ssh $client grep -qs $mount_point $MOUNT_PATH; then
run_cmd_on_remote_client "$UMOUNT_CMD $mount_point"
fi
run_cmd_on_remote_client "$DETACH_CMD $volume_name"
done
check_volume_not_attached
}
RO_mount_all()
{
check_volume_not_attached
for client in ${client_list[@]}; do
run_cmd_on_remote_client "$ATTACH_RO_CMD $volume_name"
run_cmd_on_remote_client "$MOUNT_RO_CMD $mount_point"
done
}
RW_mount()
{
check_volume_not_attached
if [ -n "$REMOTE_WRITTER" ]; then
client=$REMOTE_WRITTER
run_cmd_on_remote_client "$ATTACH_RW_CMD $volume_name"
run_cmd_on_remote_client "$MOUNT_RW_CMD $mount_point"
else
run_cmd "$ATTACH_RW_CMD $volume_name"
run_cmd "$MOUNT_RW_CMD $mount_point"
fi
}
parse_args "$@"
check_conf
if [ "$RW2RO" -eq 1 ]; then
umount_all
RO_mount_all
fi
if [ "$RO2RW" -eq 1 ]; then
umount_all
RW_mount
fi