Statistics
| Branch: | Tag: | Revision:

one / src / datastore_mad / remotes / ceph / ceph_utils.sh @ f0c137f8

History | View | Annotate | Download (5.76 KB)

1
# ----------------------------------------------------------------------------- #
2
# Copyright 2002-2015, OpenNebula Project (OpenNebula.org), C12G Labs           #
3
#                                                                               #
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may       #
5
# not use this file except in compliance with the License. You may obtain       #
6
# a copy of the License at                                                      #
7
#                                                                               #
8
# http://www.apache.org/licenses/LICENSE-2.0                                    #
9
#                                                                               #
10
# Unless required by applicable law or agreed to in writing, software           #
11
# distributed under the License is distributed on an "AS IS" BASIS,             #
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.      #
13
# See the License for the specific language governing permissions and           #
14
# limitations under the License.                                                #
15
#------------------------------------------------------------------------------ #
16

    
17
#--------------------------------------------------------------------------------
18
# Make a base @snap for image clones
19
#  @param $1 the volume
20
#--------------------------------------------------------------------------------
21
rbd_make_snap() {
22
    if [ "$(rbd_format $1)" = "2" ]; then
23
        $RBD info "$1@snap" >/dev/null 2>&1
24

    
25
        if [ "$?" != "0" ]; then
26
            $RBD snap create  "$1@snap"
27
            $RBD snap protect "$1@snap"
28
        fi
29
    fi
30
}
31

    
32
#--------------------------------------------------------------------------------
33
# Remove the base @snap for image clones
34
#  @param $1 the volume
35
#  @param $2 (Optional) the snapshot name. If empty it defaults to 'snap'
36
#--------------------------------------------------------------------------------
37
rbd_rm_snap() {
38
    local snap
39
    snap=${2:-snap}
40

    
41
    if [ "$(rbd_format $1)" = "2" ]; then
42
        $RBD info "$1@$snap" >/dev/null 2>&1
43

    
44
        if [ "$?" = "0" ]; then
45
            $RBD snap unprotect "$1@$snap"
46
            $RBD snap rm "$1@$snap"
47
        fi
48
    fi
49
}
50

    
51
#--------------------------------------------------------------------------------
52
# Find the snapshot in current volume or any of the snapshot volumes
53
#   @param $1 volume base, i.e. <pool>/one-<image_id>[-<vm_id>-<disk_id>]
54
#   @param $2 snapshot id
55
#   @return volume name, exits if not found
56
#--------------------------------------------------------------------------------
57
rbd_find_snap() {
58
    local rbd_tgt pool vol
59

    
60
    $RBD --format json snap ls $1 2>/dev/null | grep -q "\"name\":\"$2\""
61

    
62
    if [ "$?" = "0" ]; then
63
        rbd_tgt=$1
64
    else
65
        pool=$(echo $1 | cut -f1 -d'/')
66
        vol=$(echo $1 | cut -f2 -d'/')
67

    
68
        rbd_tgt=$($RBD ls $pool | grep -E "$vol-(.+:)?$2(:|$)")
69

    
70
        if [ -z "${rbd_tgt}" ]; then
71
            echo "Could not find a volume with snapshot $2" >&2
72
            exit 1
73
        fi
74

    
75
        rbd_tgt=$pool/$rbd_tgt
76
    fi
77

    
78
    echo $rbd_tgt
79
}
80

    
81
#--------------------------------------------------------------------------------
82
# Rename the target volume to include the snapshot list or remove it if it has
83
# no snapshots
84
#   @param $1 volume base, i.e. <pool>/one-<image_id>[-<vm_id>-<disk_id>]
85
#   @param $2 volume to rename or remove
86
#--------------------------------------------------------------------------------
87
rbd_rename_rm() {
88
    local snapids
89

    
90
    snapids=$($RBD --format json snap ls $2 | \
91
           grep -Po '(?<="name":")\d+' | \
92
           paste -d: -s)
93

    
94
    if [ -z "$snapids" ]; then
95
        $RBD rm $2
96
    else
97
        $RBD rename $2 $1-$snapids
98
    fi
99
}
100

    
101
#--------------------------------------------------------------------------------
102
# Return format of rbd volume
103
#   @param $1 the volume
104
#   @return the format of the volume
105
#--------------------------------------------------------------------------------
106
rbd_format() {
107
    $RBD info $1 | sed -n 's/.*format: // p'
108
}
109

    
110
#--------------------------------------------------------------------------------
111
# Check if given volume id formta 2, exits otherwise
112
#   @param $1 the volume
113
#--------------------------------------------------------------------------------
114
rbd_check_2() {
115
    if [ "$(rbd_format $1)" != "2" ]; then
116
        echo "Only RBD Format 2 is supported for this operation" >&2
117
        exit 1
118
    fi
119
}
120

    
121
#--------------------------------------------------------------------------------
122
# Get top parent of a snapshot hierarchy if the volume has snapshots.
123
#   @param $1 the volume
124
#   @return the top parent or volume in no snapshots found
125
#--------------------------------------------------------------------------------
126
rbd_top_parent() {
127
    local pool snap0 volume
128

    
129
    pool=$(echo $1 | cut -f1 -d'/')
130
    volume=$(echo $1 | cut -f2 -d'/')
131

    
132
    snap0=$($RBD ls $pool | grep -E "$volume(-.+)?@0")
133

    
134
    if [ -n "$snap0" ]; then
135
        volume=$pool/${snap0%%@*}
136
    else
137
        volume=$1
138
    fi
139

    
140
    echo $volume
141
}
142

    
143
#--------------------------------------------------------------------------------
144
# Remove all the images and snapshots from a given volume
145
#   @param $1 the volume (or snapshot to delete)
146
#--------------------------------------------------------------------------------
147
rbd_rm_r() {
148
    local rbd rbd_base children snaps
149

    
150
    rbd=$1
151
    rbd_base=${rbd%%@*}
152

    
153
    if [ "$rbd" != "$rbd_base" ]; then
154
        children=$($RBD children $rbd 2>/dev/null)
155

    
156
        for child in $children; do
157
            rbd_rm_r $child
158
        done
159

    
160
        $RBD snap unprotect $rbd
161
        $RBD snap rm $rbd
162
    else
163
        snaps=$($RBD snap ls $rbd 2>/dev/null| awk 'NR > 1 {print $2}')
164

    
165
        for snap in $snaps; do
166
            rbd_rm_r $rbd@$snap
167
        done
168

    
169
        $RBD rm $rbd
170
    fi
171
}