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

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

#This function will init B# bricks
#This is used when launch_cluster is
#not called to init B#. Call it before
#setup_lvm
function init_n_bricks() {
    local count=$1
    for i in `seq 1 $count`; do
        eval "B$i=/d/backends/$i"
    done
}

count_snaps () {
    ls $1/.snaps | wc -l
}

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 verify_lvm_version() {
    if `/sbin/lvcreate --help | grep -q thin`; then
        return 0;
    fi
    return 1;
}

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

function cleanup_lvm() {
    pkill gluster
    sleep 2

    if [ "$LVM_DEFINED" = "1" ]; then
        _cleanup_lvm >/dev/null 2>&1
    fi

    _cleanup_lvm_again >/dev/null 2>&1
    # TODO Delete cleanup has open bug
    # once fixed delete this
    mount | grep "run/gluster/snaps" | awk '{print $3}' | xargs umount 2> /dev/null
    mount | grep "patchy_snap" | awk '{print $3}' | xargs umount 2> /dev/null
    \rm -rf /var/run/gluster/snaps/*
    lvscan | grep "/dev/patchy_snap" | awk '{print $2}'| xargs lvremove -f 2> /dev/null
    vgs | grep patchy_snap | awk '{print $1}' | xargs vgremove -f 2>/dev/null
    \rm -rf /dev/patchy*
    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"
    local thinpoolsize="0.8G"
    local virtualsize="0.6G"

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

    /sbin/lvcreate -L ${thinpoolsize} -T /dev/${!vg}/thinpool
    /sbin/lvcreate -V ${virtualsize} -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 clitype=$1
        local snapname=$2
        local cli=$CLI
        if [ "$clitype" == "1" ]; then
                cli=$CLI_1;
        fi
        if [ "$clitype" == "2" ]; then
                cli=$CLI_2;
        fi
        $cli snapshot list | 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 cli=$1
        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} no-timestamp &
                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 1 $snap_name$i
                ret=$?
                if [ "$ret" != "0" ]; then
                        break
                fi
         done
         return $ret
}

#Check for the status of snapshot for a volume
#Arg1 : <Snap Name>
function snapshot_status()
{
    local snap=$1;
    local cli=$CLI_1;
    if [ "$cli" = "" ]; then
        cli=$CLI
    fi

    #TODO: Right now just fetches the status of the single snap volume.
    #When snapshot will have multiple snap volumes, should have a
    #cummulative logic for status
    $cli snapshot info $snap | grep "Status" | sed 's/.*: //';
}


# TODO: Cleanup code duplication
function volinfo_field()
{
    local vol=$1;
    local field=$2;

    $CLI_1 volume info $vol | grep "^$field: " | sed 's/.*: //';
}


function volume_exists() {
        $CLI_1 volume info $1 > /dev/null 2>&1
        if [ $? -eq 0 ]; then
                echo "Y"
        else
                echo "N"
        fi
}

# arg-1 : From which node the command should be trigerred
#         Ex : $CLI_1, $CLI_2, etc.
# arg-2 : Volume name
# arg-3 : Starting index for the snapname "snap$i"
# arg-4 : Number of snapshots to be taken
function snap_create()
{
        eval local cli_index=\$$1
        local volname=$2
        local i=$3
        local limit=$[$i + $4]

        while [ $i -lt $limit ]
        do
                $cli_index snapshot create snap$i $volname no-timestamp
                i=$[$i+1]
        done
}

# arg-1 : From which node the command should be trigerred
#         Ex : $CLI_1. $CLI_2, etc.
# arg-2 : Volume name.
function get_snap_count()
{
        eval local cli_index=\$$1
        local volname=$2


        if [ -z "$2" ]
        then
                $cli_index snapshot list | grep -v "No snapshots present"\
                                         | wc -l
        else
                $cli_index snapshot list $volname\
                                         | grep -v "No snapshots present"\
                                         | wc -l
        fi
}

# arg-1 : From which node the command should be trigerred
#         Ex : $CLI_1, $CLI_2, etc.
# arg-2 : Starting index for the snapname "snap$i"
# arg-3 : Number of snapshots to be deleted.
function snap_delete()
{
        eval local cli_index=\$$1
        local i=$2
        local limit=$[$i + $3]

        while [ $i -lt $limit ]
        do
                $cli_index snapshot delete snap$i
                i=$[$i+1]
        done
}

# arg-1 : From which node the command should be triggered
#         Ex : $CLI_1, $CLI_2, etc.
# arg-2 : key value
function snap_config()
{
        eval local cli_index=\$$1
        local var=$2
        $cli_index snapshot config | grep "^$var" | sed 's/.*: //'
}

function check_if_snapd_exist() {
        local pid
        pid=$(ps aux | grep "snapd" | grep -v grep | awk '{print $2}')
        if [ -n "$pid" ]; then echo "Y"; else echo "N"; fi
}

# returns number of snapshot being displayed in ".snaps" directory
function uss_count_snap_displayed() {
        local path=$1
        ls $path/.snaps | wc -l
}

case $OSTYPE in
NetBSD)
        echo "Skip test on LVM which is not available on NetBSD" >&2
        SKIP_TESTS
        exit 0
        ;;
*)
        ;;
esac