glusterfs

Форк
0
/
snapshot.rc 
442 строки · 10.5 Кб
1
#!/bin/bash
2

3
LVM_DEFINED=0
4
# Replace any '-' by '_' to avoid that device manager modifies the name of
5
# the device (it replaces '-' by '--' in /dev/mapper)
6
LVM_PREFIX="patchy_snap_${GFREG_ID//-/_}"
7
LVM_COUNT=0
8
VHD_SIZE="300M"
9

10
#This function will init B# bricks
11
#This is used when launch_cluster is
12
#not called to init B#. Call it before
13
#setup_lvm
14
function init_n_bricks() {
15
    local count=$1
16
    for i in `seq 1 $count`; do
17
        eval "B$i=/d/backends/$i"
18
    done
19
}
20

21
count_snaps () {
22
    ls $1/.snaps | wc -l
23
}
24

25
function init_lvm() {
26
    if [ "$1" == "" ]; then
27
        echo "Error: Invalid argument supplied"
28
        return 1
29
    fi
30
    LVM_COUNT=$1
31

32
    if [ "$2" != "" ]; then
33
        VHD_SIZE=$2
34
    fi
35

36
    local b
37
    local i
38

39
    if [ "$B1" = "" ]; then
40
        B1=$B0
41
    fi
42

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

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

59
        eval "VG$i=${LVM_PREFIX}_vg_${i}"
60
    done
61

62
    LVM_DEFINED=1
63
    return 0
64
}
65

66
function verify_lvm_version() {
67
    if `/sbin/lvcreate --help | grep -q thin`; then
68
        return 0;
69
    fi
70
    return 1;
71
}
72

73
function setup_lvm() {
74
    init_lvm $@ || return 1
75
    _setup_lvm
76
    return 0
77
}
78

79
function cleanup_lvm() {
80
    pkill gluster
81
    sleep 2
82

83
    _cleanup_lvm_again
84
    return 0
85
}
86

87
# Find out how this file was sourced, source traps.rc the same way, and use
88
# push_trapfunc to make sure cleanup_lvm gets called before we exit.
89
. $(dirname ${BASH_SOURCE[0]})/traps.rc
90
push_trapfunc cleanup_lvm
91

92
########################################################
93
# Private Functions
94
########################################################
95
function _setup_lvm() {
96
    local count=$LVM_COUNT
97
    local b
98
    local i
99

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

103
        _create_vhd ${!b} $i
104
        _create_lv ${!b} $i
105
        _mount_lv $i
106
    done
107
}
108

109
function _cleanup_lvm_again() {
110
    local file
111

112
    findmnt -nRlT "${B0}" -o TARGET,SOURCE | grep "${LVM_PREFIX}" | awk '{print $2}' | xargs -r ${UMOUNT_F}
113
    findmnt -nRlo TARGET,SOURCE | grep "run/gluster/snaps" | awk '{print $2}' | xargs -r ${UMOUNT_F}
114
    \rm -rf /var/run/gluster/snaps/*
115

116
    vgremove -fyS "vg_name=~^${LVM_PREFIX}_vg"
117

118
    for dev in $(ls ${DEVDIR}/loop* 2>/dev/null); do
119
        losetup -d "${dev}"
120
        rm -f "${dev}"
121
    done
122

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

126
########################################################
127
########################################################
128
function _create_vhd() {
129
    local dir=$1
130

131
    fallocate -l${VHD_SIZE} $dir/${LVM_PREFIX}_vhd
132
    dev="$(losetup -f --show "${dir}/${LVM_PREFIX}_vhd")"
133
    ln -sf "${dev}" "${DEVDIR}/$(basename "${dev}")"
134
    ln -sf "${DEVDIR}/$(basename "${dev}")" "${dir}/${LVM_PREFIX}_loop"
135
}
136

137
function _create_lv() {
138
    local dir=$1
139
    local num=$2
140
    local vg="VG$num"
141
    local thinpoolsize="200M"
142
    local virtualsize="150M"
143
    local path="$(realpath "${dir}/${LVM_PREFIX}_loop")"
144

145
    wipefs -a "${path}"
146
    /sbin/pvcreate --zero n "${path}"
147
    /sbin/vgcreate --zero n ${!vg} "${path}"
148

149
    /sbin/lvcreate --zero n -L ${thinpoolsize} -T ${!vg}/thinpool
150
    /sbin/lvcreate -V ${virtualsize} -T ${!vg}/thinpool -n brick_lvm
151

152
    mkfs.xfs -K -f /dev/${!vg}/brick_lvm
153
}
154

155
function _mount_lv() {
156
    local num=$1
157
    local vg="VG$num"
158
    local l="L$num"
159

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

163
function _umount_lv() {
164
    local num=$1
165
    local l="L$num"
166

167
    ${UMOUNT_F} ${!l} 2>/dev/null || true
168
    rmdir ${!l} 2>/dev/null || true
169
}
170

171
function _remove_lv() {
172
    local num=$1
173
    local vg="VG$num"
174

175
    vgremove -fy ${!vg}
176
}
177

178
function _remove_vhd() {
179
    local dir=$1
180

181
    losetup -d $dir/${LVM_PREFIX}_loop
182
    rm -f "$(readlink "${dir}/${LVM_PREFIX}_loop")"
183
    rm -f $dir/${LVM_PREFIX}_loop
184
    rm -f $dir/${LVM_PREFIX}_vhd
185
}
186

187
########################################################
188
# Utility Functions
189
########################################################
190
function snapshot_exists() {
191
        local clitype=$1
192
        local snapname=$2
193
        local cli=$CLI
194
        if [ "$clitype" == "1" ]; then
195
                cli=$CLI_1;
196
        fi
197
        if [ "$clitype" == "2" ]; then
198
                cli=$CLI_2;
199
        fi
200
        $cli snapshot list | egrep -q "^$snapname\$"
201
        return $?
202
}
203

204
#Create N number of snaps in a given volume
205
#Arg1 : <Volume Name>
206
#Arg2 : <Count of snaps to be created>
207
#Arg3 : <Snap Name Pattern>
208
#Return: Returns 0 if all snaps are created ,
209
#        if not will return exit code of last failed
210
#        snap create command.
211
function create_n_snapshots() {
212
        local cli=$1
213
        local vol=$1
214
        local snap_count=$2
215
        local snap_name=$3
216
        local ret=0
217
        for i in `seq 1 $snap_count`; do
218
                $CLI_1 snapshot create $snap_name$i ${vol} no-timestamp &
219
                PID_1=$!
220
                wait $PID_1
221
                ret=$?
222
                if [ "$ret" != "0" ]; then
223
                        break
224
                fi
225
         done
226
         return $ret
227
}
228

229

230
#Delete N number of snaps in a given volume
231
#Arg1 : <Volume Name>
232
#Arg2 : <Count of snaps to be deleted>
233
#Arg3 : <Snap Name Pattern>
234
#Return: Returns 0 if all snaps are Delete,
235
#        if not will return exit code of last failed
236
#        snap delete command.
237
function delete_n_snapshots() {
238
        local vol=$1
239
        local snap_count=$2
240
        local snap_name=$3
241
        local ret=0
242
        for i in `seq 1 $snap_count`; do
243
                $CLI_1 snapshot delete $snap_name$i &
244
                PID_1=$!
245
                wait $PID_1
246
                temp=$?
247
                if [ "$temp" != "0" ]; then
248
                        ret=$temp
249
                fi
250
         done
251
         return $ret
252
}
253

254
#Check for the existance of N number of snaps in a given volume
255
#Arg1 : <Volume Name>
256
#Arg2 : <Count of snaps to be checked>
257
#Arg3 : <Snap Name Pattern>
258
#Return: Returns 0 if all snaps exists,
259
#        if not will return exit code of last failed
260
#        snapshot_exists().
261
function snapshot_n_exists() {
262
        local vol=$1
263
        local snap_count=$2
264
        local snap_name=$3
265
        local ret=0
266
        for i in `seq 1 $snap_count`; do
267
                snapshot_exists 1 $snap_name$i
268
                ret=$?
269
                if [ "$ret" != "0" ]; then
270
                        break
271
                fi
272
         done
273
         return $ret
274
}
275

276
#Check for the status of snapshot for a volume
277
#Arg1 : <Snap Name>
278
function snapshot_status()
279
{
280
    local snap=$1;
281
    local cli=$CLI_1;
282
    if [ "$cli" = "" ]; then
283
        cli=$CLI
284
    fi
285

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

292

293
#Check the different status of a particular snapshot
294
#Arg1 : <Snap name>
295
#Arg2 : <Filed in status>
296
#Arg3 : <Expected value>
297
function snapshot_snap_status()
298
{
299

300
    local snap=$1;
301
    local cli=$CLI_1;
302
    local field=$2;
303
    local expected=$3;
304
    if [ "$cli" = "" ]; then
305
        cli=$CLI
306
    fi
307
    for i in $($cli snapshot status $snap | grep "$field" | \
308
               cut -d ':' -f2 | awk '{print $1}') ;
309
    do
310
           if [ "$i" != "$expected" ]; then
311
                echo "Failed"
312
                return 1;
313
           fi;
314
    done;
315
echo "Success"
316
return 0;
317
}
318

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

331
        while [ $i -lt $limit ]
332
        do
333
                $cli_index snapshot create snap$i $volname no-timestamp
334
                i=$[$i+1]
335
        done
336
}
337

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

346

347
        if [ -z "$2" ]
348
        then
349
                $cli_index snapshot list | grep -v "No snapshots present"\
350
                                         | wc -l
351
        else
352
                $cli_index snapshot list $volname\
353
                                         | grep -v "No snapshots present"\
354
                                         | wc -l
355
        fi
356
}
357

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

368
        while [ $i -lt $limit ]
369
        do
370
                $cli_index snapshot delete snap$i
371
                i=$[$i+1]
372
        done
373
}
374

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

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

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

397
function snap_info_volume()
398
{
399
        eval local cli_index=\$$1
400
        local var=$2
401
        local vol=$3
402
        $cli_index snapshot info volume $vol | grep "^$var" | sed 's/.*: //'
403
}
404

405
function snap_config_volume()
406
{
407
        eval local cli_index=\$$1
408
        local var=$2
409
        local vol=$3
410
        $cli_index snapshot config $vol| grep "^$var" | sed 's/.*: //'
411
}
412

413
#return specific fields of xml output
414
function get-cmd-field-xml()
415
{
416
        local cli=$CLI_1;
417
        if [ "$cli" = "" ]; then
418
            cli=$CLI
419
        fi
420

421
        COMMAND=$1
422
        PATTERN=$2
423

424
        $cli $COMMAND --xml | xmllint --format - | grep $PATTERN
425
}
426

427
function get_snap_brick_status()
428
{
429
    local snap=$1;
430

431
    $CLI snapshot status $snap | grep "Brick Running" | sed 's/.*: //';
432
}
433

434
case $OSTYPE in
435
NetBSD)
436
        echo "Skip test on LVM which is not available on NetBSD" >&2
437
        SKIP_TESTS
438
        exit 0
439
        ;;
440
*)
441
        ;;
442
esac
443

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.