summaryrefslogtreecommitdiffstats
path: root/nova/virt/xenapi/volume_utils.py
blob: 7a0b4a67ee679b63c4f0828467554b3a96f73b3c (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
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright (c) 2010 Citrix Systems, Inc.
# Copyright (c) 2013 OpenStack Foundation
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
Helper methods for operations related to the management of volumes,
and storage repositories
"""

import re
import string

from oslo.config import cfg

from nova.openstack.common import log as logging

CONF = cfg.CONF
LOG = logging.getLogger(__name__)


class StorageError(Exception):
    """To raise errors related to SR, VDI, PBD, and VBD commands."""

    def __init__(self, message=None):
        super(StorageError, self).__init__(message)


def _handle_sr_params(params):
    if 'id' in params:
        del params['id']

    sr_type = params.pop('sr_type', 'iscsi')
    sr_desc = params.pop('name_description', '')
    return sr_type, sr_desc


def create_sr(session, label, params):
    LOG.debug(_('Creating SR %s'), label)
    sr_type, sr_desc = _handle_sr_params(params)
    sr_ref = session.call_xenapi("SR.create",
                session.get_xenapi_host(),
                params,
                '0', label, sr_desc, sr_type, '', False, {})
    return sr_ref


def introduce_sr(session, sr_uuid, label, params):
    LOG.debug(_('Introducing SR %s'), label)

    sr_type, sr_desc = _handle_sr_params(params)

    sr_ref = session.call_xenapi('SR.introduce', sr_uuid, label, sr_desc,
            sr_type, '', False, params)

    LOG.debug(_('Creating PBD for SR'))
    pbd_ref = create_pbd(session, sr_ref, params)

    LOG.debug(_('Plugging SR'))
    session.call_xenapi("PBD.plug", pbd_ref)

    session.call_xenapi("SR.scan", sr_ref)
    return sr_ref


def forget_sr(session, sr_ref):
    """
    Forgets the storage repository without destroying the VDIs within
    """
    LOG.debug(_('Forgetting SR...'))
    unplug_pbds(session, sr_ref)
    session.call_xenapi("SR.forget", sr_ref)


def find_sr_by_uuid(session, sr_uuid):
    """
    Return the storage repository given a uuid.
    """
    for sr_ref, sr_rec in session.get_all_refs_and_recs('SR'):
        if sr_rec['uuid'] == sr_uuid:
            return sr_ref
    return None


def find_sr_from_vbd(session, vbd_ref):
    """Find the SR reference from the VBD reference."""
    try:
        vdi_ref = session.call_xenapi("VBD.get_VDI", vbd_ref)
        sr_ref = session.call_xenapi("VDI.get_SR", vdi_ref)
    except session.XenAPI.Failure as exc:
        LOG.exception(exc)
        raise StorageError(_('Unable to find SR from VBD %s') % vbd_ref)
    return sr_ref


def create_pbd(session, sr_ref, params):
    pbd_rec = {}
    pbd_rec['host'] = session.get_xenapi_host()
    pbd_rec['SR'] = sr_ref
    pbd_rec['device_config'] = params
    pbd_ref = session.call_xenapi("PBD.create", pbd_rec)
    return pbd_ref


def unplug_pbds(session, sr_ref):
    try:
        pbds = session.call_xenapi("SR.get_PBDs", sr_ref)
    except session.XenAPI.Failure as exc:
        LOG.warn(_('Ignoring exception %(exc)s when getting PBDs'
                   ' for %(sr_ref)s'), {'exc': exc, 'sr_ref': sr_ref})
        return

    for pbd in pbds:
        try:
            session.call_xenapi("PBD.unplug", pbd)
        except session.XenAPI.Failure as exc:
            LOG.warn(_('Ignoring exception %(exc)s when unplugging'
                       ' PBD %(pbd)s'), {'exc': exc, 'pbd': pbd})


def introduce_vdi(session, sr_ref, vdi_uuid=None, target_lun=None):
    """Introduce VDI in the host."""
    try:
        session.call_xenapi("SR.scan", sr_ref)
        if vdi_uuid:
            LOG.debug("vdi_uuid: %s" % vdi_uuid)
            vdi_ref = session.call_xenapi("VDI.get_by_uuid", vdi_uuid)
        elif target_lun:
            vdi_refs = session.call_xenapi("SR.get_VDIs", sr_ref)
            for curr_ref in vdi_refs:
                curr_rec = session.call_xenapi("VDI.get_record", curr_ref)
                if ('sm_config' in curr_rec and
                        'LUNid' in curr_rec['sm_config'] and
                        curr_rec['sm_config']['LUNid'] == str(target_lun)):
                    vdi_ref = curr_ref
                    break
        else:
            vdi_ref = (session.call_xenapi("SR.get_VDIs", sr_ref))[0]
    except session.XenAPI.Failure as exc:
        LOG.exception(exc)
        raise StorageError(_('Unable to introduce VDI on SR %s') % sr_ref)

    try:
        vdi_rec = session.call_xenapi("VDI.get_record", vdi_ref)
        LOG.debug(vdi_rec)
        LOG.debug(type(vdi_rec))
    except session.XenAPI.Failure as exc:
        LOG.exception(exc)
        raise StorageError(_('Unable to get record'
                             ' of VDI %s on') % vdi_ref)

    if vdi_rec['managed']:
        # We do not need to introduce the vdi
        return vdi_ref

    try:
        return session.call_xenapi("VDI.introduce",
                                    vdi_rec['uuid'],
                                    vdi_rec['name_label'],
                                    vdi_rec['name_description'],
                                    vdi_rec['SR'],
                                    vdi_rec['type'],
                                    vdi_rec['sharable'],
                                    vdi_rec['read_only'],
                                    vdi_rec['other_config'],
                                    vdi_rec['location'],
                                    vdi_rec['xenstore_data'],
                                    vdi_rec['sm_config'])
    except session.XenAPI.Failure as exc:
        LOG.exception(exc)
        raise StorageError(_('Unable to introduce VDI for SR %s')
                            % sr_ref)


def purge_sr(session, sr_ref):
    # Make sure no VBDs are referencing the SR VDIs
    vdi_refs = session.call_xenapi("SR.get_VDIs", sr_ref)
    for vdi_ref in vdi_refs:
        vbd_refs = session.call_xenapi("VDI.get_VBDs", vdi_ref)
        if vbd_refs:
            LOG.warn(_('Cannot purge SR with referenced VDIs'))
            return

    forget_sr(session, sr_ref)


def get_device_number(mountpoint):
    device_number = mountpoint_to_number(mountpoint)
    if device_number < 0:
        raise StorageError(_('Unable to obtain target information %s') %
                           mountpoint)
    return device_number


def parse_sr_info(connection_data, description=''):
    label = connection_data.pop('name_label',
                                'tempSR-%s' % connection_data.get('volume_id'))
    params = {}
    if 'sr_uuid' not in connection_data:
        params = parse_volume_info(connection_data)
        # This magic label sounds a lot like 'False Disc' in leet-speak
        uuid = "FA15E-D15C-" + str(params['id'])
    else:
        uuid = connection_data['sr_uuid']
        for k in connection_data.get('introduce_sr_keys', {}):
            params[k] = connection_data[k]
    params['name_description'] = connection_data.get('name_description',
                                                     description)

    return (uuid, label, params)


def parse_volume_info(connection_data):
    """
    Parse device_path and mountpoint as they can be used by XenAPI.
    In particular, the mountpoint (e.g. /dev/sdc) must be translated
    into a numeric literal.
    """
    volume_id = connection_data['volume_id']
    target_portal = connection_data['target_portal']
    target_host = _get_target_host(target_portal)
    target_port = _get_target_port(target_portal)
    target_iqn = connection_data['target_iqn']
    LOG.debug('(vol_id,number,host,port,iqn): (%s,%s,%s,%s)',
              (volume_id, target_host, target_port, target_iqn))
    if (volume_id is None or
        target_host is None or
        target_iqn is None):
        raise StorageError(_('Unable to obtain target information'
                             ' %s') % connection_data)
    volume_info = {}
    volume_info['id'] = volume_id
    volume_info['target'] = target_host
    volume_info['port'] = target_port
    volume_info['targetIQN'] = target_iqn
    if ('auth_method' in connection_data and
        connection_data['auth_method'] == 'CHAP'):
        volume_info['chapuser'] = connection_data['auth_username']
        volume_info['chappassword'] = connection_data['auth_password']

    return volume_info


def mountpoint_to_number(mountpoint):
    """Translate a mountpoint like /dev/sdc into a numeric."""
    if mountpoint.startswith('/dev/'):
        mountpoint = mountpoint[5:]
    if re.match('^[hs]d[a-p]$', mountpoint):
        return (ord(mountpoint[2:3]) - ord('a'))
    elif re.match('^x?vd[a-p]$', mountpoint):
        return (ord(mountpoint[-1]) - ord('a'))
    elif re.match('^[0-9]+$', mountpoint):
        return string.atoi(mountpoint, 10)
    else:
        LOG.warn(_('Mountpoint cannot be translated: %s'), mountpoint)
        return -1


def _get_volume_id(path_or_id):
    """Retrieve the volume id from device_path."""
    # If we have the ID and not a path, just return it.
    if isinstance(path_or_id, int):
        return path_or_id
    # n must contain at least the volume_id
    # :volume- is for remote volumes
    # -volume- is for local volumes
    # see compute/manager->setup_compute_volume
    volume_id = path_or_id[path_or_id.find(':volume-') + 1:]
    if volume_id == path_or_id:
        volume_id = path_or_id[path_or_id.find('-volume--') + 1:]
        volume_id = volume_id.replace('volume--', '')
    else:
        volume_id = volume_id.replace('volume-', '')
        volume_id = volume_id[0:volume_id.find('-')]
    return int(volume_id)


def _get_target_host(iscsi_string):
    """Retrieve target host."""
    if iscsi_string:
        return iscsi_string[0:iscsi_string.find(':')]
    elif iscsi_string is None or CONF.target_host:
        return CONF.target_host


def _get_target_port(iscsi_string):
    """Retrieve target port."""
    if iscsi_string:
        return iscsi_string[iscsi_string.find(':') + 1:]
    elif iscsi_string is None or CONF.target_port:
        return CONF.target_port