-
Notifications
You must be signed in to change notification settings - Fork 11
/
volume-sharer.sh
executable file
·306 lines (264 loc) · 10.1 KB
/
volume-sharer.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env bash
#===============================================================================
# FILE: volume-sharer.sh
#
# USAGE: ./volume-sharer.sh
#
# DESCRIPTION: Entrypoint for gdiepen/volume-sharer docker container
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: Updated version
# AUTHOR: Guido Diepen ([email protected]),
# AUTHOR: David Personette ([email protected]),
# ORGANIZATION:
# CREATED: 09/28/2014 12:11
# REVISION: 1.1
# BASED ON: https://github.com/dperson/samba ([email protected])
#===============================================================================
set -o nounset # Treat unset variables as an error
### charmap: setup character mapping for file/directory names
# Arguments:
# chars) from:to character mappings separated by ','
# Return: configured character mapings
charmap() { local chars="$1" file=/etc/samba/smb.conf
grep -q catia $file || sed -i '/TCP_NODELAY/a \
\
vfs objects = catia\
catia:mappings =\
' $file
sed -i '/catia:mappings/s/ =.*/ = '"$chars" $file
}
### import: import a smbpasswd file
# Arguments:
# file) file to import
# Return: user(s) added to container
import() { local name id file="$1"
while read name id; do
useradd "$name" -M -u "$id"
done < <(cut -d: -f1,2 --output-delimiter=' ' $file)
pdbedit -i smbpasswd:$file
}
### perms: fix ownership and permissions of share paths
# Arguments:
# none)
# Return: result
perms() { local i file=/etc/samba/smb.conf
for i in $(awk -F ' = ' '/ path = / {print $2}' $file); do
chown -Rh smbuser. $i
find $i -type d -exec chmod 775 {} \;
find $i -type f -exec chmod 664 {} \;
done
}
### recycle: disable recycle bin
# Arguments:
# none)
# Return: result
recycle() { local file=/etc/samba/smb.conf
sed -i '/recycle/d; /vfs/d' $file
}
### share: Add share
# Arguments:
# share) share name
# path) path to share
# browsable) 'yes' or 'no'
# readonly) 'yes' or 'no'
# guest) 'yes' or 'no'
# users) list of allowed users
# admins) list of admin users
# writelist) list of users that can write to a RO share
# Return: result
share() { local share="$1" path="$2" browsable=${3:-yes} ro=${4:-yes} \
guest=${5:-yes} users=${6:-""} admins=${7:-""} \
writelist=${8:-""} file=/etc/samba/cli_shares.conf
sed -i "/\\[$share\\]/,/^\$/d" $file
echo "[$share]" >>$file
echo " path = $path" >>$file
echo " browsable = $browsable" >>$file
echo " read only = $ro" >>$file
echo " guest ok = $guest" >>$file
echo -n " veto files = /._*/.apdisk/.AppleDouble/.DS_Store/" >>$file
echo -n ".TemporaryItems/.Trashes/desktop.ini/ehthumbs.db/" >>$file
echo "Network Trash Folder/Temporary Items/Thumbs.db/" >>$file
echo " delete veto files = yes" >>$file
[[ ${users:-""} && ! ${users:-""} =~ all ]] &&
echo " valid users = $(tr ',' ' ' <<< $users)" >>$file
[[ ${admins:-""} && ! ${admins:-""} =~ none ]] &&
echo " admin users = $(tr ',' ' ' <<< $admins)" >>$file
[[ ${writelist:-""} && ! ${writelist:-""} =~ none ]] &&
echo " write list = $(tr ',' ' ' <<< $writelist)" >>$file
echo "" >>$file
}
volumeshare() { local share="$1" path="$2" browsable=${3:-yes} ro=${4:-yes} \
guest=${5:-yes} users=${6:-""} admins=${7:-""} \
writelist=${8:-""} file=/etc/samba/volume_shares.conf
sed -i "/\\[$share\\]/,/^\$/d" $file
echo "[$share]" >>$file
echo " path = $path" >>$file
echo " browsable = $browsable" >>$file
echo " read only = $ro" >>$file
echo " guest ok = $guest" >>$file
echo -n " veto files = /._*/.apdisk/.AppleDouble/.DS_Store/" >>$file
echo -n ".TemporaryItems/.Trashes/desktop.ini/ehthumbs.db/" >>$file
echo "Network Trash Folder/Temporary Items/Thumbs.db/" >>$file
echo " delete veto files = yes" >>$file
[[ ${users:-""} && ! ${users:-""} =~ all ]] &&
echo " valid users = $(tr ',' ' ' <<< $users)" >>$file
[[ ${admins:-""} && ! ${admins:-""} =~ none ]] &&
echo " admin users = $(tr ',' ' ' <<< $admins)" >>$file
[[ ${writelist:-""} && ! ${writelist:-""} =~ none ]] &&
echo " write list = $(tr ',' ' ' <<< $writelist)" >>$file
echo "" >>$file
}
### smb: disable SMB2 minimun
# Arguments:
# none)
# Return: result
smb() { local file=/etc/samba/smb.conf
sed -i '/min protocol/d' $file
}
### timezone: Set the timezone for the container
# Arguments:
# timezone) for example EST5EDT
# Return: the correct zoneinfo file will be symlinked into place
timezone() { local timezone="${1:-EST5EDT}"
[[ -e /usr/share/zoneinfo/$timezone ]] || {
echo "ERROR: invalid timezone specified: $timezone" >&2
return
}
if [[ -w /etc/timezone && $(cat /etc/timezone) != $timezone ]]; then
echo "$timezone" >/etc/timezone
ln -sf /usr/share/zoneinfo/$timezone /etc/localtime
dpkg-reconfigure -f noninteractive tzdata >/dev/null 2>&1
fi
}
### user: add a user
# Arguments:
# name) for user
# password) for user
# id) for user
# group) for user
# Return: user added to container
user() { local name="${1}" passwd="${2}" id="${3:-""}" group="${4:-""}"
[[ "$group" ]] && { grep -q "^$group:" /etc/group || groupadd "$group"; }
useradd "$name" -M ${id:+-u $id} ${group:+-g $group}
echo -e "$passwd\n$passwd" | smbpasswd -s -a "$name"
}
### workgroup: set the workgroup
# Arguments:
# workgroup) the name to set
# Return: configure the correct workgroup
workgroup() { local workgroup="${1}" file=/etc/samba/smb.conf
sed -i 's|^\( *workgroup = \).*|\1'"$workgroup"'|' $file
}
### widelinks: allow access wide symbolic links
# Arguments:
# none)
# Return: result
widelinks() { local file=/etc/samba/smb.conf \
replace='\1\n wide links = yes\n unix extensions = no'
sed -i 's/\(follow symlinks = yes\)/'"$replace"'/' $file
}
create_volume_shares(){
#Now loop over all of the volume shares
rm -f /etc/samba/volume_shares.conf
touch /etc/samba/volume_shares.conf
for docker_volume in `docker volume ls | grep "^local" | sed 's/^local *//'`
do
eval volumeshare ${docker_volume} "/docker_volumes/${docker_volume}/_data" "yes" "no"
done
}
### usage: Help
# Arguments:
# none)
# Return: Help text
usage() { local RC=${1:-0}
echo "Usage: ${0##*/} [-opt] [command]
Options (fields in '[]' are optional, '<>' are required):
-h This help
-c \"<from:to>\" setup character mapping for file/directory names
required arg: \"<from:to>\" character mappings separated by ','
-i \"<path>\" Import smbpassword
required arg: \"<path>\" - full file path in container
-n Start the 'nmbd' daemon to advertise the shares
-p Set ownership and permissions on the shares
-r Disable recycle bin for shares
-S Disable SMB2 minimun version
-s \"<name;/path>[;browse;readonly;guest;users;admins;wl]\" Config a share
required arg: \"<name>;</path>\"
<name> is how it's called for clients
<path> path to share
NOTE: for the default value, just leave blank
[browsable] default:'yes' or 'no'
[readonly] default:'yes' or 'no'
[guest] allowed default:'yes' or 'no'
[users] allowed default:'all' or list of allowed users
[admins] allowed default:'none' or list of admin users
[writelist] list of users that can write to a RO share
-t \"\" Configure timezone
possible arg: \"[timezone]\" - zoneinfo timezone for container
-u \"<username;password>[;ID;group]\" Add a user
required arg: \"<username>;<passwd>\"
<username> for user
<password> for user
[ID] for user
[group] for user
-w \"<workgroup>\" Configure the workgroup (domain) samba should use
required arg: \"<workgroup>\"
<workgroup> for samba
-W Allow access wide symbolic links
The 'command' (if provided and valid) will be run instead of samba
" >&2
exit $RC
}
touch /etc/samba/cli_shares.conf
echo "include = /etc/samba/cli_shares.conf" >> /etc/samba/smb.conf
echo "include = /etc/samba/volume_shares.conf" >> /etc/samba/smb.conf
[[ "${USERID:-""}" =~ ^[0-9]+$ ]] && usermod -u $USERID -o smbuser
[[ "${GROUPID:-""}" =~ ^[0-9]+$ ]] && groupmod -g $GROUPID -o users
while getopts ":hc:i:nprs:St:u:Ww:" opt; do
case "$opt" in
h) usage ;;
c) charmap "$OPTARG" ;;
i) import "$OPTARG" ;;
n) NMBD="true" ;;
p) PERMISSIONS="true" ;;
r) recycle ;;
s) eval share $(sed 's/^\|$/"/g; s/;/" "/g' <<< $OPTARG) ;;
S) smb ;;
t) timezone "$OPTARG" ;;
u) eval user $(sed 's|;| |g' <<< $OPTARG) ;;
w) workgroup "$OPTARG" ;;
W) widelinks ;;
"?") echo "Unknown option: -$OPTARG"; usage 1 ;;
":") echo "No argument value for option: -$OPTARG"; usage 2 ;;
esac
done
shift $(( OPTIND - 1 ))
create_volume_shares
[[ "${CHARMAP:-""}" ]] && charmap "$CHARMAP"
[[ "${PERMISSIONS:-""}" ]] && perms
[[ "${RECYCLE:-""}" ]] && recycle
[[ "${TZ:-""}" ]] && timezone "$TZ"
[[ "${SMB:-""}" ]] && smb
[[ "${WORKGROUP:-""}" ]] && workgroup "$WORKGROUP"
[[ "${WIDELINKS:-""}" ]] && widelinks
if [[ $# -ge 1 && -x $(which $1 2>&-) ]]; then
exec "$@"
elif [[ $# -ge 1 ]]; then
echo "ERROR: command not found: $1"
exit 13
elif ps -ef | egrep -v grep | grep -q smbd; then
echo "Service already running, please restart container to apply changes"
else
[[ ${NMBD:-""} ]] && ionice -c 3 nmbd -D
exec ionice -c 3 smbd -FS &
echo "Listening for docker volume events"
docker events --filter "type=volume" | while read aaa
do
echo "Recreating the volume shares and restarting Samba"
create_volume_shares
killall -HUP smbd nmbd
done
fi