-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-machine-nfs.sh
executable file
·484 lines (378 loc) · 12.1 KB
/
docker-machine-nfs.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/bin/sh
#
# The MIT License (MIT)
# Copyright © 2015 Toni Van de Voorde <toni.vdv@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
set -o errexit
# BEGIN _functions
# @info: Prints the ascii logo
asciiLogo ()
{
echo
echo
echo ' ## .'
echo ' ## ## ## == _ _ _____ ____'
echo ' ## ## ## ## ## === | \ | | ___/ ___|'
echo ' /"""""""""""""""""\___/ === | \| | |_ \___ \'
echo ' ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~ | |\ | _| ___) |'
echo ' \______ o __/ |_| \_|_| |____/'
echo ' \ \ __/'
echo ' \____\_______/'
echo
echo
}
# @info: Prints the usage
usage ()
{
asciiLogo
cat <<EOF
Usage: $0 <machine-name> [options]
Options:
-f, --force Force reconfiguration of nfs
-n, --nfs-config NFS configuration to use in /etc/exports. (default to '-alldirs -mapall=\$(id -u):\$(id -g)')
-s, --shared-folder,... Folder to share (default to /Users)
-m, --mount-opts NFS mount options (default to 'noacl,async')
Examples:
$ docker-machine-nfs test
> Configure the /Users folder with NFS
$ docker-machine-nfs test --shared-folder=/Users --shared-folder=/var/www
> Configures the /Users and /var/www folder with NFS
$ docker-machine-nfs test --shared-folder=/var/www --nfs-config="-alldirs -maproot=0"
> Configure the /var/www folder with NFS and the options '-alldirs -maproot=0'
$ docker-machine-nfs test --mount-opts="noacl,async,nolock,vers=3,udp,noatime,actimeo=1"
> Configure the /User folder with NFS and specific mount options.
EOF
exit 0
}
# @info: Prints error messages
# @args: error-message
echoError ()
{
echo "\033[0;31mFAIL\n\n$1 \033[0m"
}
# @info: Prints warning messages
# @args: warning-message
echoWarn ()
{
echo "\033[0;33m$1 \033[0m"
}
# @info: Prints success messages
# @args: success-message
echoSuccess ()
{
echo "\033[0;32m$1 \033[0m"
}
# @info: Prints check messages
# @args: success-message
echoInfo ()
{
printf "\033[1;34m[INFO] \033[0m$1"
}
# @info: Prints property messages
# @args: property-message
echoProperties ()
{
echo "\t\033[0;35m- $1 \033[0m"
}
# @info: Checks if a given property is set
# @return: true, if variable is not set; else false
isPropertyNotSet()
{
if [ -z ${1+x} ]; then return 0; else return 1; fi
}
# @info: Sets the default properties
setPropDefaults()
{
prop_machine_name=
prop_shared_folders=()
prop_nfs_config="-alldirs -mapall="$(id -u):$(id -g)
prop_mount_options="noacl,async"
prop_force_configuration_nfs=false
}
# @info: Parses and validates the CLI arguments
parseCli()
{
[ "$#" -ge 1 ] || usage
prop_machine_name=$1
for i in "${@:2}"
do
case $i in
-s=*|--shared-folder=*)
local shared_folder="${i#*=}"
shift
if [ ! -d "$shared_folder" ]; then
echoError "Given shared folder '$shared_folder' does not exist!"
exit 1
fi
prop_shared_folders+=($shared_folder)
;;
-n=*|--nfs-config=*)
prop_nfs_config="${i#*=}"
;;
-m=*|--mount-opts=*)
prop_mount_options="${i#*=}"
;;
-f|--force)
prop_force_configuration_nfs=true
shift
;;
*)
echoError "Unknown argument '$i' given"
echo #EMPTY
usage
;;
esac
done
if [ ${#prop_shared_folders[@]} -eq 0 ]; then
prop_shared_folders+=("/Users")
fi;
echoInfo "Configuration:"
echo #EMPTY
echo #EMPTY
echoProperties "Machine Name: $prop_machine_name"
for shared_folder in "${prop_shared_folders[@]}"
do
echoProperties "Shared Folder: $shared_folder"
done
echoProperties "Mount Options: $prop_mount_options"
echoProperties "Force: $prop_force_configuration_nfs"
echo #EMPTY
}
# @info: Checks if the machine is present
# @args: machine-name
# @return: (none)
checkMachinePresence ()
{
echoInfo "machine presence ... \t\t\t"
if [ "" = "$(docker-machine ls | sed 1d | grep -w "$1")" ]; then
echoError "Could not find the machine '$1'!"; exit 1;
fi
echoSuccess "OK"
}
# @info: Checks if the machine is running
# @args: machine-name
# @return: (none)
checkMachineRunning ()
{
echoInfo "machine running ... \t\t\t"
machine_state=$(docker-machine ls | sed 1d | grep "^$1\s" | awk '{print $4}')
if [ "Running" != "${machine_state}" ]; then
echoError "The machine '$1' is not running but '${machine_state}'!";
exit 1;
fi
echoSuccess "OK"
}
# @info: Returns the driver used to create the machine
# @args: machine-name
# @return: The driver used to create the machine
getMachineDriver ()
{
docker-machine ls | sed 1d | grep "^$1\s" | awk '{print $3}'
}
# @info: Loads mandatory properties from the docker machine
lookupMandatoryProperties ()
{
echoInfo "Lookup mandatory properties ... "
prop_machine_ip=$(docker-machine ip $1)
prop_machine_driver=$(getMachineDriver $1)
if [ "$prop_machine_driver" = "vmwarefusion" ]; then
prop_network_id="Shared"
prop_nfshost_ip=$(ifconfig -m `route get 8.8.8.8 | awk '{if ($1 ~ /interface:/){print $2}}'` | awk 'sub(/inet /,""){print $1}')
prop_machine_ip=$prop_nfshost_ip
if [ "" = "${prop_nfshost_ip}" ]; then
echoError "Could not find the vmware fusion net IP!"; exit 1
fi
local nfsd_line="nfs.server.mount.require_resv_port = 0"
echoSuccess "\t\tOK"
echoInfo "Check NFS config settings ... \n"
if [ "$(grep -Fxq "$nfsd_line" /etc/nfs.conf)" == "0" ]; then
echoInfo "/etc/nfs.conf is setup correctly!"
else
echoWarn "\n !!! Sudo will be necessary for editing /etc/nfs.conf !!!"
# Backup /etc/nfs.conf file
sudo cp /etc/nfs.conf /etc/nfs.conf.bak && \
echo "nfs.server.mount.require_resv_port = 0" | \
sudo tee /etc/nfs.conf > /dev/null
echoWarn "\n !!! Backed up /etc/nfs.conf to /nfs.conf.bak !!!"
echoWarn "\n !!! Added 'nfs.server.mount.require_resv_port = 0' to /etc/nfs.conf !!!"
fi
echoSuccess "\n\t\t\t\t\t\tOK"
return
fi
if [ "$prop_machine_driver" = "xhyve" ]; then
prop_network_id="Shared"
prop_nfshost_ip=$(ifconfig -m `route get $prop_machine_ip | awk '{if ($1 ~ /interface:/){print $2}}'` | awk 'sub(/inet /,""){print $1}')
if [ "" = "${prop_nfshost_ip}" ]; then
echoError "Could not find the xhyve net IP!"; exit 1
fi
echoSuccess "OK"
return
fi
if [ "$prop_machine_driver" = "parallels" ]; then
prop_network_id="Shared"
prop_nfshost_ip=$(prlsrvctl net info \
"${prop_network_id}" | grep 'IPv4 address' | sed 's/.*: //')
if [ "" = "${prop_nfshost_ip}" ]; then
echoError "Could not find the parallels net IP!"; exit 1
fi
echoSuccess "OK"
return
fi
if [ "$prop_machine_driver" != "virtualbox" ]; then
echoError "Unsupported docker-machine driver: $prop_machine_driver"; exit 1
fi
prop_network_id=$(VBoxManage showvminfo $1 --machinereadable |
grep hostonlyadapter | cut -d'"' -f2)
if [ "" = "${prop_network_id}" ]; then
echoError "Could not find the virtualbox net name!"; exit 1
fi
prop_nfshost_ip=$(VBoxManage list hostonlyifs |
grep "${prop_network_id}" -A 3 | grep IPAddress |
cut -d ':' -f2 | xargs);
if [ "" = "${prop_nfshost_ip}" ]; then
echoError "Could not find the virtualbox net IP!"; exit 1
fi
echoSuccess "OK"
}
# @info: Configures the NFS
configureNFS()
{
echoInfo "Configure NFS ... \n"
if isPropertyNotSet $prop_machine_ip; then
echoError "'prop_machine_ip' not set!"; exit 1;
fi
echoWarn "\n !!! Sudo will be necessary for editing /etc/exports !!!"
for shared_folder in "${prop_shared_folders[@]}"
do
# Update the /etc/exports file and restart nfsd
(
echo '\n'$shared_folder' '$prop_machine_ip' '$prop_nfs_config'\n' |
sudo tee -a /etc/exports && awk '!a[$0]++' /etc/exports |
sudo tee /etc/exports
) > /dev/null
done
sudo nfsd restart ; sleep 2 && sudo nfsd checkexports
echoSuccess "\t\t\t\t\t\tOK"
}
# @info: Configures the VirtualBox Docker Machine to mount nfs
configureBoot2Docker()
{
echoInfo "Configure Docker Machine ... \t\t"
if isPropertyNotSet $prop_machine_name; then
echoError "'prop_machine_name' not set!"; exit 1;
fi
if isPropertyNotSet $prop_nfshost_ip; then
echoError "'prop_nfshost_ip' not set!"; exit 1;
fi
# render bootlocal.sh and copy bootlocal.sh over to Docker Machine
# (this will override an existing /var/lib/boot2docker/bootlocal.sh)
local bootlocalsh='#!/bin/sh
sudo umount /Users'
for shared_folder in "${prop_shared_folders[@]}"
do
bootlocalsh="${bootlocalsh}
sudo mkdir -p "$shared_folder
done
bootlocalsh="${bootlocalsh}
sudo /usr/local/etc/init.d/nfs-client start"
for shared_folder in "${prop_shared_folders[@]}"
do
bootlocalsh="${bootlocalsh}
sudo mount -t nfs -o "$prop_mount_options" "$prop_nfshost_ip":"$shared_folder" "$shared_folder
done
local file="/var/lib/boot2docker/bootlocal.sh"
docker-machine ssh $prop_machine_name \
"echo '$bootlocalsh' | sudo tee $file && sudo chmod +x $file && sync" > /dev/null
sleep 2
echoSuccess "OK"
}
# @info: Restarts Docker Machine
restartDockerMachine()
{
echoInfo "Restart Docker Machine ... \t\t"
if isPropertyNotSet $prop_machine_name; then
echoError "'prop_machine_name' not set!"; exit 1;
fi
docker-machine restart $prop_machine_name > /dev/null
echoSuccess "OK"
}
# @return: 'true', if NFS is mounted; else 'false'
isNFSMounted()
{
for shared_folder in "${prop_shared_folders[@]}"
do
local nfs_mount=$(docker-machine ssh $prop_machine_name "sudo df" |
grep "$prop_nfshost_ip:$prop_shared_folders")
if [ "" = "$nfs_mount" ]; then
echo "false";
return;
fi
done
echo "true"
}
# @info: Verifies that NFS is successfully mounted
verifyNFSMount()
{
echoInfo "Verify NFS mount ... \t\t\t"
local attempts=10
while [ ! $attempts -eq 0 ]; do
sleep 1
[ "$(isNFSMounted)" = "true" ] && break
attempts=$(($attempts-1))
done
if [ $attempts -eq 0 ]; then
echoError "Cannot detect the NFS mount :("; exit 1
fi
echoSuccess "OK"
}
# @info: Displays the finish message
showFinish()
{
echo "\033[0;36m"
echo "--------------------------------------------"
echo
echo " The docker-machine '$prop_machine_name'"
echo " is now mounted with NFS!"
echo
echo " ENJOY high speed mounts :D"
echo
echo "--------------------------------------------"
echo "\033[0m"
}
# END _functions
setPropDefaults
parseCli "$@"
checkMachinePresence $prop_machine_name
checkMachineRunning $prop_machine_name
lookupMandatoryProperties $prop_machine_name
if [ "$(isNFSMounted)" = "true" ] && [ "$prop_force_configuration_nfs" = false ]; then
echoSuccess "\n NFS already mounted." ; showFinish ; exit 0
fi
echo #EMPTY LINE
echoProperties "Machine IP: $prop_machine_ip"
echoProperties "Network ID: $prop_network_id"
echoProperties "NFSHost IP: $prop_nfshost_ip"
echo #EMPTY LINE
configureNFS
configureBoot2Docker
restartDockerMachine
verifyNFSMount
showFinish