forked from gluster/glusterfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot_zfs.rc
174 lines (138 loc) · 3.86 KB
/
snapshot_zfs.rc
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
#!/bin/bash
ZFS_DEFINED=0
# Replace any '-' by '_' to avoid that device manager modifies the name of
# the device (it replaces '-' by '--' in /dev/mapper)
ZFS_PREFIX="patchy_snap_${GFREG_ID//-/_}"
ZFS_COUNT=0
VHD_SIZE="300M"
function init_zfs() {
if [ "$1" == "" ]; then
echo "Error: Invalid argument supplied"
return 1
fi
ZFS_COUNT=$1
if [ "$2" != "" ]; then
VHD_SIZE=$2
fi
local b
local i
if [ "$B1" = "" ]; then
B1=$B0
fi
for i in `seq 1 $ZFS_COUNT`; do
b="B$i"
if [ "${!b}" = "" ]; then
echo "Error: $b not defined."
echo "Please run launch_cluster with atleast $ZFS_COUNT nodes"
return 1
fi
eval "L$i=/${ZFS_PREFIX}_pool_${i}/bricks/brick"
eval "LM$i=/${ZFS_PREFIX}_pool_${i}/bricks"
done
ZFS_DEFINED=1
return 0
}
function verify_zfs_version() {
if command -v zfs &> /dev/null; then
return 0;
fi
return 1;
}
function setup_zfs() {
_cleanup_zfs
init_zfs $@ || return 1
_setup_zfs
return 0
}
function cleanup_zfs() {
pkill gluster
sleep 2
if [ "$ZFS_DEFINED" = "1" ]; then
_cleanup_zfs >/dev/null 2>&1
fi
_cleanup_zfs_again >/dev/null 2>&1
\rm -rf /var/run/gluster/snaps/*
zfs list | grep "${ZFS_PREFIX}" | awk '{print $1}'| xargs -L 1 -r zpool destroy -f 2>/dev/null
return 0
}
# Find out how this file was sourced, source traps.rc the same way, and use
# push_trapfunc to make sure cleanup_lvm gets called before we exit.
. $(dirname ${BASH_SOURCE[0]})/traps.rc
push_trapfunc cleanup_zfs
########################################################
# Private Functions
########################################################
function _setup_zfs() {
local count=$ZFS_COUNT
local b
local i
for i in `seq 1 $count`; do
b="B$i"
_create_zfs_vhd ${!b} $i
_create_zpool ${!b} $i
_create_zfs_dataset ${!b} $i
done
}
function _cleanup_zfs() {
local count=$ZFS_COUNT
local b
local i
for i in `seq 1 $count`; do
b="B$i"
_remove_zfs_dataset $i
_remove_zpool $i
_remove_zfs_vhd ${!b}
done
}
function _cleanup_zfs_again() {
local file
/sbin/zfs list | grep $ZFS_PREFIX | awk '{print $1}' | xargs -r -L 1 zpool destroy -f
for dev in $(ls ${DEVDIR}/loop* 2>/dev/null); do
losetup -d "${dev}"
rm -f "${dev}"
done
find $B0 -name "*${ZFS_PREFIX}*" | xargs -r rm -rf
find /run/gluster/snaps -name "*${ZFS_PREFIX}*" | xargs -r rm -rf
for file in `ls /run/gluster/snaps`; do
find /run/gluster/snaps/$file -mmin -2 | xargs -r rm -rf
done
}
########################################################
########################################################
function _create_zfs_vhd() {
local dir=$1
mkdir -p $dir
fallocate -l${VHD_SIZE} $dir/${ZFS_PREFIX}_vhd
dev="$(losetup -f --show "${dir}/${ZFS_PREFIX}_vhd")"
ln -sf "${dev}" "${DEVDIR}/$(basename "${dev}")"
ln -sf "${DEVDIR}/$(basename "${dev}")" "${dir}/${ZFS_PREFIX}_loop"
}
function _create_zpool() {
local dir=$1
local num=$2
local zpool="${ZFS_PREFIX}_pool_${num}"
/sbin/zpool create ${zpool} $dir/${ZFS_PREFIX}_loop
}
function _create_zfs_dataset() {
local dir=$1
local num=$2
local dataset="${ZFS_PREFIX}_pool_${num}/bricks"
/sbin/zfs create ${dataset}
}
function _remove_zpool() {
local num=$1
local zpool="${ZFS_PREFIX}_pool_${num}"
/sbin/zpool destroy -f ${!zpool}
}
function _remove_zfs_dataset() {
local num=$1
local dataset="${ZFS_PREFIX}_pool_${num}/bricks"
/sbin/zfs destroy -f ${!dataset}
}
function _remove_zfs_vhd() {
local dir=$1
losetup -d $dir/${ZFS_PREFIX}_loop
rm -f "$(readlink "${dir}/${ZFS_PREFIX}_loop")"
rm -f $dir/${ZFS_PREFIX}_loop
rm -f $dir/${ZFS_PREFIX}_vhd
}