Statistics
| Branch: | Tag: | Revision:

one / src / datastore_mad / remotes / ceph / export @ dde9cf31

History | View | Annotate | Download (4.53 KB)

1
#!/bin/bash
2

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

    
19
###############################################################################
20
# This script is used to export an image to qcow2 file
21
###############################################################################
22

    
23
# ------------ Set up the environment to source common tools ------------
24

    
25
if [ -z "${ONE_LOCATION}" ]; then
26
    LIB_LOCATION=/usr/lib/one
27
else
28
    LIB_LOCATION=$ONE_LOCATION/lib
29
fi
30

    
31
. $LIB_LOCATION/sh/scripts_common.sh
32

    
33
DRIVER_PATH=$(dirname $0)
34
source ${DRIVER_PATH}/../libfs.sh
35
source ${DRIVER_PATH}/ceph.conf
36

    
37
# -------- Get rm and datastore arguments from OpenNebula core ------------
38

    
39
DRV_ACTION=$1
40
ID=$2
41

    
42
XPATH="${DRIVER_PATH}/../xpath.rb -b $DRV_ACTION"
43

    
44
unset i XPATH_ELEMENTS
45

    
46
while IFS= read -r -d '' element; do
47
    XPATH_ELEMENTS[i++]="$element"
48
done < <($XPATH     /DS_DRIVER_ACTION_DATA/IMAGE/SOURCE \
49
                    /DS_DRIVER_ACTION_DATA/IMAGE/SIZE \
50
                    /DS_DRIVER_ACTION_DATA/IMAGE/TEMPLATE/MD5 \
51
                    /DS_DRIVER_ACTION_DATA/IMAGE/TEMPLATE/SHA1 \
52
                    /DS_DRIVER_ACTION_DATA/IMAGE/TEMPLATE/FORMAT \
53
                    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/BRIDGE_LIST \
54
                    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/POOL_NAME \
55
                    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/CEPH_USER \
56
                    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/CEPH_KEY \
57
                    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/CEPH_CONF)
58

    
59
unset i
60

    
61
SRC="${XPATH_ELEMENTS[i++]}"
62
SIZE="${XPATH_ELEMENTS[i++]}"
63
MD5="${XPATH_ELEMENTS[i++]}"
64
SHA1="${XPATH_ELEMENTS[i++]}"
65
FORMAT="${XPATH_ELEMENTS[i++]:-raw}"
66
BRIDGE_LIST="${XPATH_ELEMENTS[i++]}"
67
POOL_NAME="${XPATH_ELEMENTS[i++]:-$POOL_NAME}"
68
CEPH_USER="${XPATH_ELEMENTS[i++]}"
69
CEPH_KEY="${XPATH_ELEMENTS[i++]}"
70
CEPH_CONF="${XPATH_ELEMENTS[i++]}"
71

    
72
DST_HOST=`get_destination_host $ID`
73

    
74
if [ -z "$DST_HOST" ]; then
75
    error_message "Datastore template missing 'BRIDGE_LIST' attribute."
76
    exit -1
77
fi
78

    
79
IMPORT_SOURCE="rbd://$DST_HOST/$SRC"
80
IS_JOIN="?"
81

    
82
if [ -n "$CEPH_USER" ]; then
83
    RBD="$RBD --id ${CEPH_USER}"
84
    IMPORT_SOURCE="${IMPORT_SOURCE}${IS_JOIN}CEPH_USER=${CEPH_USER}"
85
    IS_JOIN="&"
86
fi
87

    
88
if [ -n "$CEPH_KEY" ]; then
89
    RBD="$RBD --keyfile ${CEPH_USER}"
90
    IMPORT_SOURCE="${IMPORT_SOURCE}${IS_JOIN}CEPH_KEY=${CEPH_KEY}"
91
    IS_JOIN="&"
92
fi
93

    
94
if [ -n "$CEPH_CONF" ]; then
95
    RBD="$RBD --conf ${CEPH_CONF}"
96
    IMPORT_SOURCE="${IMPORT_SOURCE}${IS_JOIN}CEPH_CONF=${CEPH_CONF}"
97
fi
98

    
99
#-------------------------------------------------------------------------------
100
# Output image source and format
101
#-------------------------------------------------------------------------------
102

    
103
INFO_SCRIPT=$(cat <<EOF
104
if [ -z "$MD5" ]; then
105
    CHECKSUM=\$(
106
        $RBD export ${SRC} - | ${MD5SUM} | cut -f1 -d' '
107
        ps=\$PIPESTATUS
108

    
109
        if [ "\$ps" != "0" ]; then
110
            exit \$ps
111
        fi
112
    )
113

    
114
    status=\$?
115

    
116
    [ "\$status" != "0" ] && exit \$status
117
else
118
    CHECKSUM="$MD5"
119
fi
120

    
121
if [ -z "\$CHECKSUM" ]; then
122
    exit 1
123
fi
124

    
125
cat <<EOT
126
<MD5><![CDATA[\$CHECKSUM]]></MD5>
127
<SIZE><![CDATA[$SIZE]]></SIZE>
128
<FORMAT><![CDATA[${FORMAT}]]></FORMAT>
129
EOT
130
EOF
131
)
132

    
133
INFO=$(ssh_monitor_and_log "$DST_HOST" "$INFO_SCRIPT" "Image info script" 2>&1)
134
INFO_STATUS=$?
135

    
136
if [ "$INFO_STATUS" != "0" ]; then
137
    echo "$INFO"
138
    exit $INFO_STATUS
139
fi
140

    
141
cat <<EOF
142
<IMPORT_INFO>
143
    <IMPORT_SOURCE><![CDATA[$IMPORT_SOURCE]]></IMPORT_SOURCE>
144
    $INFO
145
    <DISPOSE>NO</DISPOSE>
146
</IMPORT_INFO>"
147
EOF
148