-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlvm-snaptool.lib.sh
1519 lines (1305 loc) · 40.6 KB
/
lvm-snaptool.lib.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## vim:ts=4:sw=4:tw=200:nu:ai:nowrap:
##
## application library for lvm-snaptool
##
## Created by Wolfram Schlich <[email protected]>
## Licensed under the GNU GPLv3
## Web: http://www.bashinator.org/projects/lvm-snaptool/
## Code: https://github.com/wschlich/lvm-snaptool/
##
##
## NOTES
## =====
## - with XFS, do NOT manually call xfs_freeze:
## http://readlist.com/lists/redhat.com/linux-lvm/0/952.html
## http://readlist.com/lists/redhat.com/linux-lvm/0/957.html
## http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=285979
##
##
## REQUIRED PROGRAMS IN PATH
## =========================
## - bc
## - cut
## - env
## - sort
## - touch
## - rm
## - mkdir
## - mount
## - umount
## - readlink
## - mktemp
## - blkid
## - xargs
## - hostname
## - uname
## - lvs
## - lvcreate
## - lvremove
##
##
## application control functions
##
function __init() {
## parse command line options
while getopts ':CDMd:e:f:hqs:' opt; do
case "${opt}" in
## create snapshots
C)
Task="create"
;;
## delete snapshots
D)
Task="delete"
;;
## mirror complete system
M)
Mode="mirror"
;;
## snapshot volume mount directory
d)
SnapshotVolumeMountDirectory="${OPTARG}"
;;
## exclude specific LVs
e)
LogicalVolumeExcludeList="${OPTARG}"
;;
## snapshot volume size factor
f)
SnapshotVolumeSizeFactor="${OPTARG}"
;;
## help
h)
Task="help"
;;
## quiet operation
q)
declare -i __MsgQuiet=1
;;
## snapshot volume name suffix
s)
SnapshotVolumeNameSuffix="${OPTARG}"
;;
## option without a required argument
:)
__die 2 "option -${OPTARG} requires an argument" # TODO FIXME: switch to __msg err
;;
## unknown option
\?)
__die 2 "unknown option -${OPTARG}" # TODO FIXME: switch to __msg err
;;
## this should never happen
*)
__die 2 "there's an error in the matrix!" # TODO FIXME: switch to __msg err
;;
esac
__msg debug "command line argument: -${opt}${OPTARG:+ '${OPTARG}'}"
done
## check if command line options were given at all
if [[ ${OPTIND} -eq 1 ]]; then
__msg err "no command line option specified"
printUsage && exit 2
fi
## shift off options + arguments
let OPTIND--; shift ${OPTIND}; unset OPTIND
args="${@}"
set --
## just show help?
if [[ ${Task} == "help" ]]; then
printUsage && exit 0
fi
## populate logical volume array
## from logical volume list
IFS=','
declare -a LogicalVolumeArray=( ${LogicalVolumeList} )
IFS=$' \t\n'
#validateLogicalVolumeArray # TODO FIXME: implement function
##*/*) # "any/any"
## populate logical volume exclude array
## from logical volume exclude list
IFS=','
declare -a LogicalVolumeExcludeArray=( ${LogicalVolumeExcludeList} )
IFS=$' \t\n'
#validateLogicalVolumeExcludeArray # TODO FIXME: implement function
## populate snapshot volume size factor array
## from snapshot volume size factor list
IFS=','
declare -a SnapshotVolumeSizeFactorArray=( ${SnapshotVolumeSizeFactor} )
IFS=$' \t\n'
#validateSnapshotVolumeSizeFactorArray # TODO FIXME: implement function
##*/*:+([0-9])|*/*:+([0-9].[0-9])) # "any/any:num" or "any/any:num.num"
##+([0-9])|+([0-9].[0-9])) # "num" or "num.num"
## populate snapshot volume mount directory array
## from snapshot volume mount directory list
IFS=','
declare -a SnapshotVolumeMountDirectoryArray=( ${SnapshotVolumeMountDirectory} )
IFS=$' \t\n'
#validateSnapshotVolumeMountDirectoryArray # TODO FIXME: implement function
## tasks are mode-specific
case ${Mode} in
mirror)
case ${Task} in
create)
## check arguments for mirror creation
if [[ -z ${SnapshotVolumeMountDirectory} ]]; then
__msg err "snapshot volume mount directory not specified"
printUsage && exit 2
fi
;;
delete)
## check arguments for mirror deletion
if [[ -z ${SnapshotVolumeMountDirectory} ]]; then
__msg err "snapshot volume mount directory not specified"
printUsage && exit 2
fi
;;
'')
__msg err "mode '${Mode}': no task specified"
printUsage && exit 2
;;
*)
__msg err "mode '${Mode}': invalid task specified: '${Task}'"
printUsage && exit 2
;;
esac
;;
'')
__msg err "no mode specified"
printUsage && exit 2
;;
*)
__msg err "invalid mode specified: '${Mode}'"
printUsage && exit 2
;;
esac
} # __init()
function __main() {
if [[ ${Mode} == "mirror" ]]; then
case ${Task} in
create)
## create mirror
if ! createSystemMirror; then
__die 2 "failed to create system mirror"
else
__msg info "successfully created system mirror"
fi
;;
delete)
## delete mirror
if ! deleteSystemMirror; then
__die 2 "failed to delete system mirror"
else
__msg info "successfully deleted system mirror"
fi
;;
esac
fi
} # __main()
##
## application worker functions
##
function printUsage() {
## ----- head -----
##
## DESCRIPTION:
## prints usage information
##
## ARGUMENTS:
## /
##
## GLOBAL VARIABLES USED:
## /
##
## ----- main -----
cat <<-USAGE
Usage: ${__ScriptFile} [options]
Options:
-M: system mirror mode, creates a mirror of all currently
mounted volumes in a new directory structure.
requires: one of -C -D
accepts: -d -e -f
-C: create snapshot(s).
requires: -M
excludes: -D
accepts: -d -e -f -s
-D: delete snapshot(s).
requires: -M
excludes: -C
accepts: -d -e -s
ignores: -f
-e arg: logical volumes to exclude.
example: -e vg.sys/lv.home
-e vg.sys/lv.home,vg.sys/lv.usr
-f arg: snapshot volume size factor.
example: -f 0.2
-f vg.sys/lv.home:0.1,vg.sys/lv.var:0.2,0.5
default: ${SnapshotVolumeSizeFactor}
-s arg: snapshot volume name suffix.
example: -s .snapshot
default: ${SnapshotVolumeNameSuffix}
-d arg: mount directory for system mirror.
example: -d /mnt/lvm-snapshots
default: ${SnapshotVolumeMountDirectory}
Options -M and one of -C -D are required.
USAGE
return 0 # success
}
function createSnapshot() {
## ----- head -----
##
## DESCRIPTION:
## creates a snapshot of a logical volume.
##
## ARGUMENTS:
## 1: volumeGroupName (req): vg.sys
## 2: logicalVolumeName (req): lv.home
## 3: snapshotVolumeName (req): lv.home.snapshot
## 4: snapshotVolumeSizeFactor (req): 0.2 (=20%)
##
## GLOBAL VARIABLES USED:
## LvmDevicesDirectory
##
local volumeGroupName=${1}
if [[ -z "${volumeGroupName}" ]]; then
__msg err "argument 1 (volumeGroupName) missing"
return 2 # error
fi
__msg debug "volumeGroupName: ${volumeGroupName}"
local logicalVolumeName=${2}
if [[ -z "${logicalVolumeName}" ]]; then
__msg err "argument 2 (logicalVolumeName) missing"
return 2 # error
fi
__msg debug "logicalVolumeName: ${logicalVolumeName}"
local snapshotVolumeName=${3}
if [[ -z "${snapshotVolumeName}" ]]; then
__msg err "argument 3 (snapshotVolumeName) missing"
return 2 # error
fi
__msg debug "snapshotVolumeName: ${snapshotVolumeName}"
local snapshotVolumeSizeFactor=${4}
if [[ -z "${snapshotVolumeSizeFactor}" ]]; then
__msg err "argument 4 (snapshotVolumeSizeFactor) missing"
return 2 # error
fi
__msg debug "snapshotVolumeSizeFactor: ${snapshotVolumeSizeFactor}"
## ----- main -----
## generate logical volume device (e.g. /dev/vg.sys/lv.home)
local logicalVolumeDevice="${LvmDevicesDirectory:-/dev}/${volumeGroupName}/${logicalVolumeName}"
__msg debug "logicalVolumeDevice: ${logicalVolumeDevice}"
## check if device is a logical volume
if ! deviceIsLogicalVolume "${logicalVolumeDevice}"; then
__msg err "device '${logicalVolumeDevice}' is not a logical volume"
return 2 # error
fi
## get logical volume size
local logicalVolumeSize=$(env LVM_SUPPRESS_FD_WARNINGS=1 lvs --separator : --noheadings --nosuffix \
--units m -o lv_name,lv_size "${logicalVolumeDevice}" 2>>"${_L}" | cut -d : -f 2 2>>"${_L}")
local -i lvsExitCode=${PIPESTATUS[0]}
local -i cutExitCode=${PIPESTATUS[1]}
if [[ ${lvsExitCode} -ne 0 || ${cutExitCode} -ne 0 || -z "${logicalVolumeSize}" ]]; then
__msg err "failed to get size of logical volume '${logicalVolumeDevice}'"
return 2 # error
fi
__msg debug "logicalVolumeSize: ${logicalVolumeSize}"
## calculate snapshot volume size
local snapshotVolumeSize=$(echo "${logicalVolumeSize} * ${snapshotVolumeSizeFactor}" | bc)
local -i bcExitCode=${PIPESTATUS[1]}
if [[ ${bcExitCode} -ne 0 || -z "${snapshotVolumeSize}" ]]; then
__msg err "failed to calculate snapshot volume size for logical volume '${logicalVolumeDevice}'"
return 2 # error
fi
__msg debug "snapshotVolumeSize: ${snapshotVolumeSize}"
## generate snapshot volume device (e.g. /dev/vg.sys/lv.home.snapshot)
local snapshotVolumeDevice="${LvmDevicesDirectory:-/dev}/${volumeGroupName}/${snapshotVolumeName}"
__msg debug "snapshotVolumeDevice: ${snapshotVolumeDevice}"
## check if snapshot volume device already exists
if [[ -e "${snapshotVolumeDevice}" ]]; then
__msg err "snapshot volume device '${snapshotVolumeDevice}' already exists"
return 2 # error
fi
## create snapshot volume
if ! env LVM_SUPPRESS_FD_WARNINGS=1 lvcreate -s -p r -c 512 -C y --addtag sysmirror \
-L "${snapshotVolumeSize}m" -n "${snapshotVolumeName}" \
"${logicalVolumeDevice}" >>"${_L}" 2>&1; then
__msg err "failed to create snapshot volume '${snapshotVolumeName}' of" \
"logical volume '${volumeGroupName}/${logicalVolumeName}'"
return 2 # error
else
__msg debug "successfully created snapshot volume '${snapshotVolumeName}' of" \
"logical volume '${volumeGroupName}/${logicalVolumeName}'"
fi
return 0 # success
} # createSnapshot()
function deleteSnapshot() {
## ----- head -----
##
## DESCRIPTION:
## deletes a snapshot volume.
##
## ARGUMENTS:
## 1: volumeGroupName (req): vg.sys
## 2: snapshotVolumeName (req): lv.home.snapshot
##
## GLOBAL VARIABLES USED:
## LvmDevicesDirectory
##
local volumeGroupName=${1}
if [[ -z "${volumeGroupName}" ]]; then
__msg err "argument 1 (volumeGroupName) missing"
return 2 # error
fi
__msg debug "volumeGroupName: ${volumeGroupName}"
local snapshotVolumeName=${2}
if [[ -z "${snapshotVolumeName}" ]]; then
__msg err "argument 2 (snapshotVolumeName) missing"
return 2 # error
fi
__msg debug "snapshotVolumeName: ${snapshotVolumeName}"
## ----- main -----
## generate snapshot volume device (e.g. /dev/vg.sys/lv.home.snapshot)
local snapshotVolumeDevice="${LvmDevicesDirectory:-/dev}/${volumeGroupName}/${snapshotVolumeName}"
__msg debug "snapshotVolumeDevice: ${snapshotVolumeDevice}"
## check if device is a snapshot volume
if ! deviceIsSnapshotVolume "${snapshotVolumeDevice}"; then
__msg err "device '${snapshotVolumeDevice}' is not a snapshot volume"
return 2 # error
fi
## remove snapshot volume
if ! env LVM_SUPPRESS_FD_WARNINGS=1 lvremove -f "${snapshotVolumeDevice}" >>"${_L}" 2>&1; then
__msg err "failed to remove snapshot volume device '${snapshotVolumeDevice}'"
return 2 # error
else
__msg debug "successfully removed snapshot volume device '${snapshotVolumeDevice}'"
fi
return 0 # success
} # deleteSnapshot()
function mountSnapshot() {
## ----- head -----
##
## DESCRIPTION:
## mounts a snapshot volume on a directory.
##
## ARGUMENTS:
## 1: volumeGroupName (req): vg.sys
## 2: snapshotVolumeName (req): lv.home
## 3: snapshotVolumeMountDirectory (req): /mnt/snapshots/home
##
## GLOBAL VARIABLES USED:
## LvmDevicesDirectory (fallback: /dev)
##
local volumeGroupName=${1}
if [[ -z "${volumeGroupName}" ]]; then
__msg err "argument 1 (volumeGroupName) missing"
return 2 # error
fi
__msg debug "volumeGroupName: ${volumeGroupName}"
local snapshotVolumeName=${2}
if [[ -z "${snapshotVolumeName}" ]]; then
__msg err "argument 2 (snapshotVolumeName) missing"
return 2 # error
fi
__msg debug "snapshotVolumeName: ${snapshotVolumeName}"
local snapshotVolumeMountDirectory=${3}
if [[ -z "${snapshotVolumeMountDirectory}" ]]; then
__msg err "argument 3 (snapshotVolumeMountDirectory) missing"
return 2 # error
fi
__msg debug "snapshotVolumeMountDirectory: ${snapshotVolumeMountDirectory}"
## ----- main -----
## generate snapshot volume device (e.g. /dev/vg.sys/lv.home)
local snapshotVolumeDevice="${LvmDevicesDirectory:-/dev}/${volumeGroupName}/${snapshotVolumeName}"
__msg debug "snapshotVolumeDevice: ${snapshotVolumeDevice}"
## check if snapshot volume device exists
if [[ ! -e "${snapshotVolumeDevice}" ]]; then
__msg err "snapshot volume device '${snapshotVolumeDevice}' does not exist"
return 2 # error
fi
## check if device is a snapshot volume
if ! deviceIsSnapshotVolume "${snapshotVolumeDevice}"; then
__msg err "device '${snapshotVolumeDevice}' is not a snapshot volume"
return 2 # error
fi
## use mount options based on determined filesystem type
local filesystemType=$(blkid -c /dev/null -s TYPE -o value ${snapshotVolumeDevice} 2>>"${_L}")
local -i blkidExitCode=${?}
if [[ ${blkidExitCode} -ne 0 ]]; then
__msg err "failed running blkid to determine filesystem type of snapshot volume device '${snapshotVolumeDevice}'"
return 2 # error
fi
__msg debug "filesystemType: ${filesystemType}"
local mountOpts=
case "${filesystemType}" in
xfs)
## special mount options for XFS
mountOpts="nouuid,norecovery,ro"
;;
*)
## default mount options
mountOpts="ro"
;;
esac
__msg debug "mountOpts: ${mountOpts}"
## check for mount directory
if [[ ! -d "${snapshotVolumeMountDirectory}" ]]; then
__msg info "snapshot volume mount directory '${snapshotVolumeMountDirectory}'" \
"doesn't exist -- creating..."
if ! mkdir -p "${snapshotVolumeMountDirectory}"; then
__msg err "failed to create snapshot volume mount directory" \
"'${snapshotVolumeMountDirectory}'"
return 2 # error
else
__msg debug "successfully created snapshot volume mount directory" \
"'${snapshotVolumeMountDirectory}'"
fi
fi
## mount snapshot volume
if ! mount -o ${mountOpts} "${snapshotVolumeDevice}" "${snapshotVolumeMountDirectory}"; then
__msg err "failed to mount snapshot volume device '${snapshotVolumeDevice}'" \
"on '${snapshotVolumeMountDirectory}' with options '${mountOpts}'"
return 2 # error
else
__msg debug "successfully mounted snapshot volume device '${snapshotVolumeDevice}'" \
"on '${snapshotVolumeMountDirectory}' with options '${mountOpts}"
fi
return 0 # success
} # mountSnapshot()
function bindMountDirectory() {
## ----- head -----
##
## DESCRIPTION:
## bind-mounts a directory on another.
##
## ARGUMENTS:
## 1: sourceDirectory (req)
## 2: targetDirectory (req)
##
## GLOBAL VARIABLES USED:
## /
##
local sourceDirectory=${1}
if [[ -z "${sourceDirectory}" ]]; then
__msg err "argument 1 (sourceDirectory) missing"
return 2 # error
fi
__msg debug "sourceDirectory: ${sourceDirectory}"
local targetDirectory=${2}
if [[ -z "${targetDirectory}" ]]; then
__msg err "argument 2 (targetDirectory) missing"
return 2 # error
fi
__msg debug "targetDirectory: ${targetDirectory}"
## ----- main -----
## check for source directory
if [[ ! -d "${sourceDirectory}" ]]; then
__msg err "source directory '${sourceDirectory}' doesn't exist"
return 2 # error
fi
## check for target directory
if [[ ! -d "${targetDirectory}" ]]; then
__msg info "target directory '${targetDirectory}' doesn't exist -- creating..."
if ! mkdir -p "${targetDirectory}"; then
__msg err "failed to create target directory '${targetDirectory}'"
return 2 # error
else
__msg debug "successfully created target directory '${targetDirectory}'"
fi
fi
## check if mount point directory actually has something mounted on
if directoryHasMount "${targetDirectory}"; then
__msg err "something is already mounted on directory '${targetDirectory}'"
return 2 # error
fi
## bind-mount source directory on target directory
if ! mount -o bind "${sourceDirectory}" "${targetDirectory}" >>"${_L}" 2>&1; then
__msg err "failed to bind-mount directory '${sourceDirectory}' on directory '${targetDirectory}'"
return 2 # error
else
__msg debug "successfully bind-mounted directory '${sourceDirectory}' on directory '${targetDirectory}'"
fi
## remount bind-mounted directory ro (supported from kernel 2.6.26 on),
## see http://lwn.net/Articles/281157/
local kernelRelease=$(uname -r)
local kernelRelease=${kernelRelease/[^0-9.]*/} # strip localversion
IFS='.'
set -- ${kernelRelease}
IFS=$' \t\n'
local -i kernelVersionMajor=${1}
local -i kernelVersionMinor=${2}
local -i kernelVersionPatchlevel=${3}
set --
if [[ ${kernelVersionMajor} -ge 2 && ${kernelVersionMinor} -ge 6 && ${kernelVersionPatchlevel} -ge 26 ]]; then
__msg debug "trying to remount bind-mounted directory read-only (kernel version >= 2.6.26)"
if ! mount -o remount,ro "${targetDirectory}" >>"${_L}" 2>&1; then
__msg err "failed to remount bind-mounted directory '${targetDirectory}' read-only"
return 2 # error
else
__msg debug "successfully remounted bind-mounted directory '${targetDirectory}' read-only"
fi
else
__msg debug "not trying to remount bind-mounted directory read-only (kernel version < 2.6.26)"
fi
return 0 # success
} # bindMountDirectory()
function unmount() {
## ----- head -----
##
## DESCRIPTION:
## unmounts a filesystem from a directory.
##
## ARGUMENTS:
## 1: mountPointDirectory (req)
##
## GLOBAL VARIABLES USED:
## /
##
local mountPointDirectory=${1}
if [[ -z "${mountPointDirectory}" ]]; then
__msg err "argument 1 (mountPointDirectory) missing"
return 2 # error
fi
__msg debug "mountPointDirectory: ${mountPointDirectory}"
## ----- main -----
## check if mount point directory actually has something mounted on
if ! directoryHasMount "${mountPointDirectory}"; then
__msg err "nothing mounted on directory '${mountPointDirectory}'"
return 2 # error
fi
## unmount filesystem from mount point directory
if ! umount "${mountPointDirectory}" >>"${_L}" 2>&1; then
__msg err "failed to unmount filesystem from directory '${mountPointDirectory}'"
return 2 # error
else
__msg debug "successfully unmounted filesystem from directory '${mountPointDirectory}'"
fi
return 0 # success
} # unmount()
function deviceIsLogicalVolume() {
## ----- head -----
##
## DESCRIPTION:
## checks if the given device is a logical volume.
##
## ARGUMENTS:
## 1: device (req)
##
## GLOBAL VARIABLES USED:
## /
##
local device=${1}
if [[ -z "${device}" ]]; then
__msg err "argument 1 (device) missing"
return 2 # error
fi
__msg debug "device: ${device}"
## ----- main -----
## check if device exists
if [[ ! -e "${device}" ]]; then
__msg err "device '${device}' does not exist"
return 2 # error
fi
## check if device is a block device
local realDevice="$(readlink -f ${device})"
__msg debug "realDevice: ${realDevice}"
if [[ ! -b "${realDevice}" ]]; then
__msg err "device '${device}' does not resolve to a block device"
return 2 # error
fi
env LVM_SUPPRESS_FD_WARNINGS=1 lvs --noheadings -o lv_name "${device}" >>"${_L}" 2>&1
local -i lvsExitCode=${?}
if [[ ${lvsExitCode} -ne 0 ]]; then
return 1 # check result: negative
fi
return 0 # check result: positive
} # deviceIsLogicalVolume()
function deviceIsSnapshotVolume() {
## ----- head -----
##
## DESCRIPTION:
## checks if the given device is a snapshot volume.
##
## ARGUMENTS:
## 1: device (req)
##
## GLOBAL VARIABLES USED:
## /
##
local device=${1}
if [[ -z "${device}" ]]; then
__msg err "argument 1 (device) missing"
return 2 # error
fi
__msg debug "device: ${device}"
## ----- main -----
## check if device exists
if [[ ! -e "${device}" ]]; then
__msg err "device '${device}' does not exist"
return 2 # error
fi
## check if device is a block device
local realDevice="$(readlink -f ${device})"
__msg debug "realDevice: ${realDevice}"
if [[ ! -b "${realDevice}" ]]; then
__msg err "device '${device}' does not resolve to a block device"
return 2 # error
fi
## get snapshot volume origin
local snapshotVolumeOrigin=$(
env LVM_SUPPRESS_FD_WARNINGS=1 lvs --separator / --noheadings --nosuffix \
--units m -o origin "${device}" 2>>"${_L}" | xargs -n 1 2>>"${_L}"
)
local -i lvsExitCode=${?}
if [[ ${lvsExitCode} -ne 0 ]]; then
__msg err "failed running 'lvs' to check if device '${device}' is a snapshot volume"
return 2 # error
fi
__msg debug "snapshotVolumeOrigin: ${snapshotVolumeOrigin}"
## empty origin indicates non-snapshot volume
if [[ -z ${snapshotVolumeOrigin} ]]; then
return 1 # check result: negative
fi
return 0 # check result: positive
} # deviceIsSnapshotVolume()
function mountPointIsBindMount() {
## ----- head -----
##
## DESCRIPTION:
## checks if the given mount point is a bind-mount.
## looks in /etc/mtab as /proc/mounts shows bind-mounts
## as regular mounts .
##
## ARGUMENTS:
## 1: mountPoint (req)
##
## GLOBAL VARIABLES USED:
## /
##
local mountPoint=${1}
if [[ -z "${mountPoint}" ]]; then
__msg err "argument 1 (mountPoint) missing"
return 2 # error
fi
__msg debug "mountPoint: ${mountPoint}"
## ----- main -----
## check for /etc/mtab
if [[ ! -e /etc/mtab ]]; then
__msg err "/etc/mtab does not exist"
return 2 # error
fi
## split /etc/mtab into array by newline
IFS=$'\n'
local -a mtabMountArray=( $(sort -k 2,2 < /etc/mtab) )
local -a sortExitCode=${?}
IFS=$' \t\n'
if [[ ${sortExitCode} -ne 0 ]]; then
__msg err "failed sorting /etc/mtab"
return 2 # error
fi
## loop through array of mounts
local -i i
for ((i = 0; i < ${#mtabMountArray[@]}; i++)); do
## split mount line into fields
IFS=' '
set -- ${mtabMountArray[i]}
IFS=$' \t\n'
#local mtabMountDevice=${1}
local mtabMountPoint=${2}
#local mtabMountFilesystemType=${3}
local mtabMountOpts=${4}
set --
#__msg debug "mtabMountPoint: ${mtabMountPoint}; mtabMountOpts: ${mtabMountOpts}"
## check if current mtab entry equals the mount point we're looking for
if [[ "${mtabMountPoint}" == "${mountPoint%/}" ]]; then
case "${mtabMountOpts}" in
## check if current mtab entry is a bind mount
bind|bind,*|*,bind|*,bind,*)
return 0 # check result: positive
;;
*)
continue
;;
esac
fi
done
return 1 # check result: negative
} # mountPointIsBindMount()
function directoryHasMount() {
## ----- head -----
##
## DESCRIPTION:
## checks if the given directory has something mounted on.
## looks in /proc/mounts.
##
## ARGUMENTS:
## 1: directory (req)
##
## GLOBAL VARIABLES USED:
## /
##
local directory=${1}
if [[ -z "${directory}" ]]; then
__msg err "argument 1 (directory) missing"
return 2 # error
fi
__msg debug "directory: ${directory}"
## ----- main -----
## check for /proc/mounts
if [[ ! -e /proc/mounts ]]; then
__msg err "/proc/mounts does not exist"
return 2 # error
fi
## split /proc/mounts into array by newline
IFS=$'\n'
local -a procMountArray=( $(sort -k 2,2 < /proc/mounts) )
local -i sortExitCode=${?}
IFS=$' \t\n'
if [[ ${sortExitCode} -ne 0 ]]; then
__msg err "failed sorting /proc/mounts"
return 2 # error
fi
## loop through array of mounts
local -i i
for ((i = 0; i < ${#procMountArray[@]}; i++)); do
## split mount line into fields
IFS=' '
set -- ${procMountArray[i]}
IFS=$' \t\n'
#local procMountDevice=${1}
local procMountPoint=${2}
#local procMountFilesystemType=${3}
local procMountOpts=${4}
set --
#__msg debug "procMountPoint: ${procMountPoint}; procMountOpts: ${procMountOpts}"
## check if current proc entry equals the mount point we're looking for
if [[ "${procMountPoint}" == "${directory%/}" ]]; then
return 0 # check result: positive
fi
done
return 1 # check result: negative
} # directoryHasMount()
function printAllMountedVolumes() {
## ----- head -----
##
## DESCRIPTION:
## prints a list of all mounted volumes (including bind-mounts and excluding special filesystems).
## looks in /proc/mounts.
##
## ARGUMENTS:
## 1: baseDirectory (opt): / or /mnt/snapshots (default: /)
## 2: sortOrder (opt): 'asc' or 'desc' (default: 'asc')
##
## GLOBAL VARIABLES USED:
## /
##
local baseDirectory=${1:-/}
local sortOrder=${2:-asc}
__msg -q debug "baseDirectory: ${baseDirectory}"
__msg -q debug "sortOrder: ${sortOrder}"
## ----- main -----
## check for /proc/mounts
if [[ ! -e /proc/mounts ]]; then
__msg -q err "/proc/mounts does not exist"
return 2 # error
fi
## determine sort arguments based on sort order
case ${sortOrder} in
asc)
local sortArgs=
;;
desc)
local sortArgs="-r"
;;
*)
;;
esac
## split /proc/mounts into array by newline
IFS=$'\n'
local -a procMountArray=( $(sort -k 2,2 ${sortArgs} < /proc/mounts) )
local -i sortExitCode=${?}
IFS=$' \t\n'
if [[ ${sortExitCode} -ne 0 ]]; then
__msg -q err "failed sorting /proc/mounts"
return 2 # error
fi
## loop through array of mounts
local -i i
for ((i = 0; i < ${#procMountArray[@]}; i++)); do
## split mount line into fields
IFS=' '
set -- ${procMountArray[i]}
IFS=$' \t\n'
local procMountDevice=${1}
local procMountPoint=${2}
local procMountFilesystemType=${3}
#local procMountOpts=${4}
set --
#__msg -q debug "procMountDevice: ${procMountDevice}; procMountPoint: ${procMountPoint}; procMountFilesystemType: ${procMountFilesystemType}"
## check for special filesystem types to skip
case "${procMountFilesystemType}" in
debugfs|devpts|nfsd|proc|rootfs|securityfs|sysfs|tmpfs|usbfs)
continue
;;
ext[234]|jfs|reiserfs|xfs) # TODO FIXME: extend list
;;
*)
continue # TODO FIXME: log unsupported fstype?
;;
esac
## check if mount point is given base directory itself
## or below given base directory
case "${procMountPoint}" in
${baseDirectory%/}) ;;
${baseDirectory%/}/*) ;;
*) continue ;;
esac
## finally print device and mount point
echo "${procMountDevice}:${procMountPoint}"
done
return 0 # success
} # printAllMountedVolumes()
function printAllLogicalVolumes() {
## ----- head -----
##
## DESCRIPTION:
## prints a list of all logical volumes in the form "VG/LV"
## (without device directory prefix).