summaryrefslogtreecommitdiffstats
path: root/tests/snapshot.rc
blob: 20bddcf0f1a809495dc132ee788ea01efb6db593 (plain)
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
#!/bin/bash

LVM_DEFINED=0
LVM_PREFIX="patchy_snap"
LVM_COUNT=0
VHD_SIZE="1G"

function init_lvm() {
    if [ "$1" == "" ]; then
        echo "Error: Invalid argument supplied"
        return 1
    fi
    LVM_COUNT=$1

    if [ "$2" != "" ]; then
        VHD_SIZE=$2
    fi

    local b
    local i

    if [ "$B1" = "" ]; then
        B1=$B0
    fi

    for i in `seq 1 $LVM_COUNT`; do
        b="B$i"
        if [ "${!b}" = "" ]; then
            echo "Error: $b not defined."
            echo "Please run launch_cluster with atleast $LVM_COUNT nodes"
            return 1
        fi

        eval "L$i=${!b}/${LVM_PREFIX}_mnt"
        l="L$i"
        mkdir -p ${!l}
        if [ $? -ne 0 ]; then
            echo "Error: failed to create dir ${!l}"
            return 1
        fi

        eval "VG$i=${LVM_PREFIX}_vg_${i}"
    done

    LVM_DEFINED=1
    return 0
}

function setup_lvm() {
    init_lvm $@ || return 1
    _setup_lvm
    return 0
}

function cleanup_lvm() {
    pkill gluster
    if [ "$LVM_DEFINED" = "1" ]; then
        _cleanup_lvm
    fi

    _cleanup_lvm_again >/dev/null 2>&1
    return 0
}

########################################################
# Private Functions
########################################################
function _setup_lvm() {
    local count=$LVM_COUNT
    local b
    local i

    for i in `seq 1 $count`; do
        b="B$i"

        _create_vhd ${!b} $i
        _create_lv ${!b} $i
        _mount_lv $i
    done
}

function _cleanup_lvm() {
    local count=$LVM_COUNT
    local b
    local i

    for i in `seq 1 $count`; do
        b="B$i"
        _umount_lv $i
        _remove_lv $i
        _remove_vhd ${!b}
    done
}

function _cleanup_lvm_again() {
    local file

    mount | grep $LVM_PREFIX | awk '{print $3}' | xargs -r umount -f

    /sbin/vgs | grep $LVM_PREFIX | awk '{print $1}' | xargs -r vgremove -f

    find $B0 -name "${LVM_PREFIX}_loop" | xargs -r losetup -d

    find $B0 -name "${LVM_PREFIX}*" | xargs -r rm -rf

    find /run/gluster/snaps -name "*${LVM_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_vhd() {
    local dir=$1
    local num=$2
    local loop_num=`expr $2 + 8`

    fallocate -l${VHD_SIZE} $dir/${LVM_PREFIX}_vhd
    mknod -m660 $dir/${LVM_PREFIX}_loop b 7 $loop_num
    /sbin/losetup $dir/${LVM_PREFIX}_loop $dir/${LVM_PREFIX}_vhd
}

function _create_lv() {
    local dir=$1
    local num=$2
    local vg="VG$num"

    /sbin/pvcreate $dir/${LVM_PREFIX}_loop
    /sbin/vgcreate ${!vg} $dir/${LVM_PREFIX}_loop

    /sbin/lvcreate -l 100%FREE -T /dev/${!vg}/thinpool
    /sbin/lvcreate -V $VHD_SIZE -T /dev/${!vg}/thinpool -n brick_lvm

    mkfs.xfs -f /dev/${!vg}/brick_lvm
}

function _mount_lv() {
    local num=$1
    local vg="VG$num"
    local l="L$num"

    mount -t xfs -o nouuid /dev/${!vg}/brick_lvm ${!l}
}

function _umount_lv() {
    local num=$1
    local l="L$num"

    umount -f ${!l} 2>/dev/null || true
    rmdir ${!l} 2>/dev/null || true
}

function _remove_lv() {
    local num=$1
    local vg="VG$num"

    vgremove -f ${!vg}
}

function _remove_vhd() {
    local dir=$1

    losetup -d $dir/${LVM_PREFIX}_loop
    rm -f $dir/${LVM_PREFIX}_loop
    rm -f $dir/${LVM_PREFIX}_vhd
}

########################################################
# Utility Functions
########################################################
function snapshot_exists() {
        local volname=$1
        local snapname=$2
        $CLI snapshot list $volname | egrep -q "^$snapname\$"
        return $?
}

#Create N number of snaps in a given volume
#Arg1 : <Volume Name>
#Arg2 : <Count of snaps to be created>
#Arg3 : <Snap Name Pattern>
#Return: Returns 0 if all snaps are created ,
#        if not will return exit code of last failed
#        snap create command.
function create_n_snapshots() {
        local vol=$1
        local snap_count=$2
        local snap_name=$3
        local ret=0
        for i in `seq 1 $snap_count`; do
                $CLI_1 snapshot create $snap_name$i ${vol}&
                PID_1=$!
                wait $PID_1
                ret=$?
                if [ "$ret" != "0" ]; then
                        break
                fi
         done
         return $ret
}


#Delete N number of snaps in a given volume
#Arg1 : <Volume Name>
#Arg2 : <Count of snaps to be deleted>
#Arg3 : <Snap Name Pattern>
#Return: Returns 0 if all snaps are Delete,
#        if not will return exit code of last failed
#        snap delete command.
function delete_n_snapshots() {
        local vol=$1
        local snap_count=$2
        local snap_name=$3
        local ret=0
        for i in `seq 1 $snap_count`; do
                $CLI_1 snapshot delete $snap_name$i &
                PID_1=$!
                wait $PID_1
                temp=$?
                if [ "$temp" != "0" ]; then
                        ret=$temp
                fi
         done
         return $ret
}

#Check for the existance of N number of snaps in a given volume
#Arg1 : <Volume Name>
#Arg2 : <Count of snaps to be checked>
#Arg3 : <Snap Name Pattern>
#Return: Returns 0 if all snaps exists,
#        if not will return exit code of last failed
#        snapshot_exists().
function snapshot_n_exists() {
        local vol=$1
        local snap_count=$2
        local snap_name=$3
        local ret=0
        for i in `seq 1 $snap_count`; do
                snapshot_exists $snap_name$i
                ret=$?
                if [ "$ret" != "0" ]; then
                        break
                fi
         done
         return $ret
}