This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layersnap_
executable file
·518 lines (424 loc) · 9.79 KB
/
layersnap_
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#!/bin/bash
# layersnap
#
# overlayfs- and rsync-based simple snapshots
#
# - ls_bootstrap is called at the EOF
# - ls_bootstrap calls parseRequest and calls the appropriate controller
# - controller uses helpers to do the job
#
#
# convention:
# - GlobalVariable
# - localVariable
#
# todo
# ubuntu 12.04 syntax
# $((2 ** 4))
##############
##############
## ##
## ROUTER ##
## ##
##############
##############
#
# status, prepare, snapshot, merge or restore?
#
ls_bootstrap() { # (request)
# parse request parameters
parseRequest $*
# choose mode
setMode
if [ "$LSMode" == "0" ]
then
echo "Sorry, but your System is not capable of running layersnap."
echo "Layersnap depends on overlayfs, which was included in kernel 3.18 and extended by support of multiple lower-layers in 4.0. Besides that, Ubuntu supports overlayfs partly since at least 12.04 and fully since at least 14.04"
exit
fi
# basic paths and vars
TargetName="$(basename $TargetPath)"
PoolPath="$(dirname $TargetPath)/.layersnap/$TargetName"
DateFormat="+%s"
DisplayDateFormat="+%d.%m.%Y %H:%M:%S"
case $ACTION in
# status
status)
if [ ! -d "$PoolPath" ]
then
echo "$TargetPath is not layersnapped."
else
ls_status
fi
;;
# snapshot
snapshot)
ls_snapshot
;;
#merge
merge)
if [ -d "$PoolPath" ]
then
ls_merge
else
echo ERROR: $TargetPath is not layersnapped, so nothing to merge!
fi
;;
#restore
restore)
if [ -d "$PoolPath" ]
then
ls_restore
else
echo ERROR: $TargetPath is not layersnapped, so nothing to restore!
fi
;;
esac
}
###################
###################
## ##
## CONTROLLERS ##
## ##
###################
###################
#
# show status and layers
#
ls_status() {
setPoolVars
# ensure target is mounted
if [ ! "$PoolMounted" == "true" ]
then
ls_mount
echo "layers have just been mounted."
fi
# display summerizing information
echo "LAYERSNAP: $TargetPath consists of $(expr $LayerCount + 1) layers. $(pluralize "layer" $LayersAvailable) available."
# show detailed inforamtion for each layer
i=1
for f in "$PoolPath/lower-"*
do
layerStatusLine $i $f
i=$(expr $i + 1)
done
layerStatusLine $(expr $LayerCount + 1) $UpperPath upper
}
#
# mount pool
#
ls_mount() {
# select mode
case $LSMode in
1)
mount -t overlayfs -o rw,lowerdir="$LowerList",upperdir="$UpperPath" overlayfs "$TargetPath"
;;
2)
;;
3)
mount -t overlay overlay -o rw,lowerdir="$LowerList",upperdir="$UpperPath",workdir="$WorkPath" "$TargetPath"
;;
esac
}
#
# create new snapshot-layer
#
# - unmount layers, except for first snapshot
# - make lower-layer from target- or form upper-directory (on first snapshot)
# - create new empty upper directory
# - remount layers
#
ls_snapshot() {
setPoolVars
# select mode
case $LSMode in
1)
ls_snapshot_mode1_mode3
;;
2)
ls_snapshot_mode2
;;
3)
ls_snapshot_mode1_mode3
;;
esac
}
ls_snapshot_mode1_mode3() {
# initial or consecutive snapshot?
if [ ! -d "$PoolPath" ]
then
# create pool dir
mkdir -p "$PoolPath"
# create workdir if mode3
if [ "$LSMode" == "3" ]
then
mkdir "$WorkPath"
fi
# make target dir the mew lower dir
mv "$TargetPath" "$LowerPath"
# create new target dir as mountpoint
mkdir "$TargetPath"
else
# abort when trying to make a socend snapshot in mode1
if [ "$LSMode" == "1" ]
then
echo "Sorry, your system only allows one snapshot."
exit
fi
# unmount target dir
umount "$TargetPath"
# make target dir the mew lower dir
mv "$TargetPath" "$LowerPath"
fi
# create upper dir anyway
mkdir "$UpperPath"
# mount it
ls_mount
}
ls_snapshot_mode2() {
ls
}
#
# restore layer
#
# - let user select layer-Id to restore
# - merge layer-after-layer into lowest layer
# - make lowest layer the new target dir
# - clean up
#
ls_restore() {
ls_status
read -p "Snapshot ID to restore: " toLayerID
umount "$TargetPath"
rmdir "$TargetPath"
i=1
for f in "$PoolPath/lower-"*; do
# remember lowest layer and merge all upper layer into it
if [ "$i" == 1 ]; then
LowestPath="$f"
else
mergeLayersByPath "$f/" "$LowestPath"
fi
if [ "$i" == "$toLayerID" ]; then break; fi
i=$(expr $i + 1)
done
mv "$LowestPath" "$TargetPath"
rm -R "$PoolPath"
}
#
# merge layers
#
# - ask for layers to merge
# - merge
#
ls_merge() {
ls_status
read -p "Merge layers starting from: " fromLayerID
read -p "Merge layers ending with: " toLayerID
echo "All layers between and including layers $fromLayerID and $toLayerID will be merged into layer $toLayerID."
read -p "Enter 'merge' to confirm: " confirmation
if [ ! "$confirmation" == "merge" ]; then
echo "aborting."
#return 2
fi
# find toLayer-path
setToLayerPath
i=1
for f in "$PoolPath/lower-"*
do
if [ "$i" -ge "$fromLayerID" ] && [ "$i" -lt "$toLayerID" ]
then
mergeLayersByPath $f $toLayerPath
elif [ "$i" -ge "$toLayerID" ]
then
break
fi
i=$(expr $i + 1)
done
echo "done"
}
#############
#############
## ##
## HELPERS ##
## ##
#############
#############
# choose layersnap mode fitting the host-system
setMode() {
# try to check by ubuntu-version first, then by kernel-version
if [ "$(cat /etc/issue | grep -c Ubuntu)" -gt "0" ]
then
ubuntuMainVersion=$(cat /etc/lsb-release | grep DISTRIB_RELEASE | cut -c 17- | cut -d "." -f1)
ubuntuSubVersion=$(cat /etc/lsb-release | grep DISTRIB_RELEASE | cut -c 17- | cut -d "." -f2)
else
kernelMainVersion=$(uname -r | cut -d "." -f1)
kernelSubVersion=$(uname -r | cut -d "." -f2)
fi
if [ "$ubuntuMainVersion" -ge "14" ] && [ "$ubuntuSubVersion" -ge "04" ]
then
LSMode="2"
elif [ "$ubuntuMainVersion" -ge "12" ] && [ "$ubuntuSubVersion" -ge "04" ]
then
LSMode="1"
elif [ "$kernelMainVersion" == "4" ]
then
LSMode="2"
elif [ "$kernelMainVersion" == "3" ] && [ "$kernelSubVersion" -ge "18" ]
then
LSMode="1"
else
LSMode="0"
fi
# LSMode="1"
}
# get human readable total size of directory
sizeOfDir() { # (pathname)
location=$(dirname $(realpath $1))
name=$(basename $(realpath $1))
sizeCachePath="$location/.size_of_$name"
if [ ! -e "$sizeCachePath" ]
then
echo $(du -sch $1 | head -n 1 | cut -d " " -f1 ) > $sizeCachePath
fi
echo "$(cat "$sizeCachePath")"
}
# output number and pluralized version of word if needed
pluralize() { # (word, count)
echo "$2 $1$(if [ "$2" != "1" ]; then echo "s"; fi)"
}
# compose status line of layer
layerStatusLine() { # (count, path)
if [ "$3" != "upper" ]
then
# lower layers
LayerDate=$(date -d @$(basename $2 | cut -c 7-) "$DisplayDateFormat")
else
# upper layer
LayerDate=$(printf "%${#LayerDate}s" "now")
fi
LayerFileCount=$(find $2 -type f | wc -l)
echo "layer $(printf "%${#LayerCount}d" $1) from $LayerDate with $(sizeOfDir "$2") in $(pluralize "file" $LayerFileCount)"
}
# set all the variables needed for working with a layer-pool
setPoolVars() {
# display "not " if Pool is not mounted
if mount | grep $TargetPath > /dev/null
then
PoolMounted="true"
else
PoolMounted="false"
fi
# date in format suitable for use in pathname
Now=$(date "$DateFormat")
# whole path with date
LowerPath="$PoolPath/lower-$Now"
# path to upper dir
UpperPath="$PoolPath/upper"
# path to work dir
WorkPath="$PoolPath/work"
# total lower-layer-count
LayerCount=$(find "$PoolPath" -name lower-* | wc -l)
# ":"-seperated list of layer-paths
LowerList=""
for f in "$PoolPath/lower-"*
do
LowerList="$(realpath "$f"):$LowerList"
done
LowerList=${LowerList::-1}
# calculate number of still available layers
if [ "$LSMode" == "3" ]
then
LayersAvailable="Unlimited"
else
LayersAvailable=$(expr "$LSMode" - "$LayerCount")
fi
}
# used by --merge
setToLayerPath() {
i=1
for f in "$PoolPath/lower-"*
do
if [ "$i" == "$toLayerID" ]
then
toLayerPath="$f"
break
fi
i=$(expr $i + 1)
done
}
# sets LowerList-variable
setLowerList() { # (from-ID, to-ID)
if [[ "$1" =~ "^[0-9]+$" ]] && [[ "$1" =~ "^[0-9]+$" ]]
then
from="$1";to="$2"
else
from="1";to="999999999"
fi
LowerList=""
i=1
for f in "$PoolPath/lower-"*
do
if [ "$i" >= "$from" ] && [ "$i" <= "$to" ]
then
LowerList="$(realpath "$f"):$LowerList"
fi
i=$(expr $i + 1)
done
LowerList=${LowerList::-1}
}
# merges one directory into another, overwriting existing files
mergeLayersByPath() { # (fromLayerPath, toLayerPath)
echo "merging $1 into $2"
rsync --remove-source-files "$1/"* "$2"
# find "$1/"* -type d -empty -delete
# displays message if layer has not been emptied and deleted completely
rmdir "$1"
}
# parse request params
parseRequest() { # ()
ACTION="status"
while [[ $# -gt 1 ]]
do
key="$1"
case $key in
-m|--mount)
ACTION="mount"
shift
;;
-s|--snapshot)
ACTION="snapshot"
shift
;;
-c|--checkout)
ACTION="checkout"
shift
;;
-r|--restore)
ACTION="restore"
shift
;;
-m|--merge)
ACTION="merge"
shift
;;
esac
done
# last param
if [[ -n $1 ]]
then
TargetPath=$(realpath $1)
#TargetPath="$(pwd)/$1"
else
echo "ERROR: please provide a target-dir!"
fi
}
###############
###############
## ##
## BOOTSTRAP ##
## ##
###############
###############
# bootstrap and pass request params
ls_bootstrap $*