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

    for i in `seq 1 $LVM_COUNT`; do
        b="B$i"
        if [ "${!b}" = "" ]; then
            echo "Error: $b not defined."
            echo "Please run launch_cluster before creating lvm"
            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 umount -f

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

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

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

    find /run/gluster/snaps -name "*${LVM_PREFIX}*" | xargs rm -rf

    for file in `ls /run/gluster/snaps`; do
        find /run/gluster/snaps/$file -mmin -2 | xargs 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 snapname=$1
        $CLI_1 snapshot list | egrep -q "\b$snapname\b"
        return $?
}