storage_backend_iscsi.c 12.2 KB
Newer Older
1 2 3
/*
 * storage_backend_iscsi.c: storage backend for iSCSI handling
 *
4
 * Copyright (C) 2007-2016 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2007-2008 Daniel P. Berrange
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

26
#include <dirent.h>
27 28 29 30
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
D
David Allan 已提交
31
#include <sys/stat.h>
32

33 34
#include "datatypes.h"
#include "driver.h"
35
#include "storage_backend_iscsi.h"
36
#include "viralloc.h"
37
#include "vircommand.h"
38 39
#include "virerror.h"
#include "virfile.h"
40
#include "viriscsi.h"
41
#include "virlog.h"
42
#include "virobject.h"
43
#include "virstring.h"
44
#include "viruuid.h"
45
#include "secret_util.h"
46
#include "storage_util.h"
47

48 49
#define VIR_FROM_THIS VIR_FROM_STORAGE

50 51
VIR_LOG_INIT("storage.storage_backend_iscsi");

52 53
#define ISCSI_DEFAULT_TARGET_PORT 3260

54 55 56
static char *
virStorageBackendISCSIPortal(virStoragePoolSourcePtr source)
{
57
    char *portal = NULL;
58

59
    if (source->nhost != 1) {
60 61
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Expected exactly 1 host for the storage pool"));
62 63 64
        return NULL;
    }

65 66
    if (source->hosts[0].port == 0)
        source->hosts[0].port = ISCSI_DEFAULT_TARGET_PORT;
67

68 69 70 71
    if (strchr(source->hosts[0].name, ':')) {
        ignore_value(virAsprintf(&portal, "[%s]:%d,1",
                                 source->hosts[0].name,
                                 source->hosts[0].port));
72
    } else {
73 74 75
        ignore_value(virAsprintf(&portal, "%s:%d,1",
                                 source->hosts[0].name,
                                 source->hosts[0].port));
76 77 78 79 80
    }

    return portal;
}

81

82 83 84 85
static char *
virStorageBackendISCSISession(virStoragePoolObjPtr pool,
                              bool probe)
{
86 87
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
    return virISCSIGetSession(def->source.devices[0].path, probe);
88 89 90
}


91 92 93 94
static int
virStorageBackendISCSIGetHostNumber(const char *sysfs_path,
                                    uint32_t *host)
{
95
    int ret = -1;
96 97
    DIR *sysdir = NULL;
    struct dirent *dirent = NULL;
E
Eric Blake 已提交
98
    int direrr;
99 100 101

    VIR_DEBUG("Finding host number from '%s'", sysfs_path);

J
John Ferlan 已提交
102
    virWaitForDevices();
103

104 105
    if (virDirOpen(&sysdir, sysfs_path) < 0)
        goto cleanup;
106

E
Eric Blake 已提交
107
    while ((direrr = virDirRead(sysdir, &dirent, sysfs_path)) > 0) {
108
        if (STRPREFIX(dirent->d_name, "target")) {
109 110 111 112 113 114 115
            if (sscanf(dirent->d_name, "target%u:", host) == 1) {
                ret = 0;
                goto cleanup;
            } else {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Failed to parse target '%s'"), dirent->d_name);
                goto cleanup;
116 117 118 119
            }
        }
    }

120 121 122 123 124 125 126 127
    if (direrr == 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to get host number for iSCSI session "
                         "with path '%s'"), sysfs_path);
        goto cleanup;
    }

 cleanup:
J
Ján Tomko 已提交
128
    VIR_DIR_CLOSE(sysdir);
129
    return ret;
130
}
131

132
static int
133
virStorageBackendISCSIFindLUs(virStoragePoolObjPtr pool,
134
                              const char *session)
135
{
136
    char *sysfs_path;
137
    int retval = -1;
138
    uint32_t host;
139

140
    if (virAsprintf(&sysfs_path,
141
                    "/sys/class/iscsi_session/session%s/device", session) < 0)
142
        goto cleanup;
143

144
    if (virStorageBackendISCSIGetHostNumber(sysfs_path, &host) < 0)
145
        goto cleanup;
146

147
    if (virStorageBackendSCSIFindLUs(pool, host) < 0)
148 149 150 151 152
        goto cleanup;

    retval = 0;

 cleanup:
153

154 155
    VIR_FREE(sysfs_path);

156 157
    return retval;
}
158 159


160
static char *
161
virStorageBackendISCSIFindPoolSources(const char *srcSpec,
E
Eric Blake 已提交
162
                                      unsigned int flags)
163 164 165 166 167
{
    virStoragePoolSourcePtr source = NULL;
    size_t ntargets = 0;
    char **targets = NULL;
    char *ret = NULL;
168
    size_t i;
169 170 171 172 173 174 175
    virStoragePoolSourceList list = {
        .type = VIR_STORAGE_POOL_ISCSI,
        .nsources = 0,
        .sources = NULL
    };
    char *portal = NULL;

E
Eric Blake 已提交
176 177
    virCheckFlags(0, NULL);

178
    if (!srcSpec) {
179 180
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("hostname must be specified for iscsi sources"));
181 182 183
        return NULL;
    }

184 185 186 187
    if (!(source = virStoragePoolDefParseSourceString(srcSpec,
                                                      list.type)))
        return NULL;

188
    if (source->nhost != 1) {
189 190
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Expected exactly 1 host for the storage pool"));
191 192 193
        goto cleanup;
    }

194 195 196
    if (!(portal = virStorageBackendISCSIPortal(source)))
        goto cleanup;

197 198 199
    if (virISCSIScanTargets(portal,
                            source->initiator.iqn,
                            &ntargets, &targets) < 0)
200 201
        goto cleanup;

202
    if (VIR_ALLOC_N(list.sources, ntargets) < 0)
203 204
        goto cleanup;

205
    for (i = 0; i < ntargets; i++) {
E
Eric Blake 已提交
206
        if (VIR_ALLOC_N(list.sources[i].devices, 1) < 0 ||
207
            VIR_ALLOC_N(list.sources[i].hosts, 1) < 0)
208
            goto cleanup;
E
Eric Blake 已提交
209 210
        list.sources[i].nhost = 1;
        list.sources[i].hosts[0] = source->hosts[0];
211 212 213 214 215 216
        list.sources[i].initiator = source->initiator;
        list.sources[i].ndevice = 1;
        list.sources[i].devices[0].path = targets[i];
        list.nsources++;
    }

217
    if (!(ret = virStoragePoolSourceListFormat(&list)))
218 219
        goto cleanup;

220
 cleanup:
221
    if (list.sources) {
222
        for (i = 0; i < ntargets; i++) {
E
Eric Blake 已提交
223
            VIR_FREE(list.sources[i].hosts);
224
            VIR_FREE(list.sources[i].devices);
E
Eric Blake 已提交
225
        }
226 227
        VIR_FREE(list.sources);
    }
228
    for (i = 0; i < ntargets; i++)
229 230 231 232 233 234 235
        VIR_FREE(targets[i]);
    VIR_FREE(targets);
    VIR_FREE(portal);
    virStoragePoolSourceFree(source);
    return ret;
}

236
static int
237
virStorageBackendISCSICheckPool(virStoragePoolObjPtr pool,
238 239
                                bool *isActive)
{
240
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
241 242 243 244 245
    char *session = NULL;
    int ret = -1;

    *isActive = false;

246
    if (def->source.nhost != 1) {
247 248
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Expected exactly 1 host for the storage pool"));
249 250 251
        return -1;
    }

252
    if (def->source.hosts[0].name == NULL) {
253 254
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing source host"));
255 256 257
        return -1;
    }

258 259
    if (def->source.ndevice != 1 ||
        def->source.devices[0].path == NULL) {
260 261
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing source device"));
262 263 264
        return -1;
    }

265
    if ((session = virStorageBackendISCSISession(pool, true)) != NULL) {
266 267 268 269 270 271 272 273 274
        *isActive = true;
        VIR_FREE(session);
    }
    ret = 0;

    return ret;
}


275
static int
276
virStorageBackendISCSISetAuth(const char *portal,
277
                              virStoragePoolSourcePtr source)
278 279
{
    unsigned char *secret_value = NULL;
280
    size_t secret_size;
281
    virStorageAuthDefPtr authdef = source->auth;
282
    int ret = -1;
283
    virConnectPtr conn = NULL;
284

285
    if (!authdef || authdef->authType == VIR_STORAGE_AUTH_TYPE_NONE)
286 287
        return 0;

288 289
    VIR_DEBUG("username='%s' authType=%d seclookupdef.type=%d",
              authdef->username, authdef->authType, authdef->seclookupdef.type);
290
    if (authdef->authType != VIR_STORAGE_AUTH_TYPE_CHAP) {
291 292 293 294 295
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("iscsi pool only supports 'chap' auth type"));
        return -1;
    }

296 297
    conn = virGetConnectSecret();
    if (!conn)
298 299
        return -1;

300 301
    if (virSecretGetSecretString(conn, &authdef->seclookupdef,
                                 VIR_SECRET_USAGE_TYPE_ISCSI,
302
                                 &secret_value, &secret_size) < 0)
303 304
        goto cleanup;

305
    if (virISCSINodeUpdate(portal,
306
                           source->devices[0].path,
307 308 309
                           "node.session.auth.authmethod",
                           "CHAP") < 0 ||
        virISCSINodeUpdate(portal,
310
                           source->devices[0].path,
311
                           "node.session.auth.username",
312
                           authdef->username) < 0 ||
313
        virISCSINodeUpdate(portal,
314
                           source->devices[0].path,
315 316
                           "node.session.auth.password",
                           (const char *)secret_value) < 0)
317 318 319 320
        goto cleanup;

    ret = 0;

321
 cleanup:
322
    VIR_DISPOSE_N(secret_value, secret_size);
323
    virObjectUnref(conn);
324 325 326 327
    return ret;
}

static int
328
virStorageBackendISCSIStartPool(virStoragePoolObjPtr pool)
329
{
330
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
331
    char *portal = NULL;
332 333
    char *session = NULL;
    int ret = -1;
334

335
    if (def->source.nhost != 1) {
336 337
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Expected exactly 1 host for the storage pool"));
338 339 340
        return -1;
    }

341
    if (def->source.hosts[0].name == NULL) {
342 343
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing source host"));
344 345 346
        return -1;
    }

347 348
    if (def->source.ndevice != 1 ||
        def->source.devices[0].path == NULL) {
349 350
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing source device"));
351 352 353
        return -1;
    }

354
    if ((session = virStorageBackendISCSISession(pool, true)) == NULL) {
355
        if ((portal = virStorageBackendISCSIPortal(&def->source)) == NULL)
356
            goto cleanup;
357 358 359

        /* Create a static node record for the IQN target. Must be done
         * in order for login to the target */
360
        if (virISCSINodeNew(portal, def->source.devices[0].path) < 0)
361 362
            goto cleanup;

363
        if (virStorageBackendISCSISetAuth(portal, &def->source) < 0)
364 365
            goto cleanup;

366
        if (virISCSIConnectionLogin(portal,
367 368
                                    def->source.initiator.iqn,
                                    def->source.devices[0].path) < 0)
369
            goto cleanup;
370
    }
371 372
    ret = 0;

373
 cleanup:
374
    VIR_FREE(portal);
375 376
    VIR_FREE(session);
    return ret;
377 378 379
}

static int
380
virStorageBackendISCSIRefreshPool(virStoragePoolObjPtr pool)
381
{
382
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
383 384
    char *session = NULL;

385
    def->allocation = def->capacity = def->available = 0;
386

387
    if ((session = virStorageBackendISCSISession(pool, false)) == NULL)
388
        goto cleanup;
389
    if (virISCSIRescanLUNs(session) < 0)
390
        goto cleanup;
391
    if (virStorageBackendISCSIFindLUs(pool, session) < 0)
392
        goto cleanup;
393
    VIR_FREE(session);
394 395 396

    return 0;

397
 cleanup:
398
    VIR_FREE(session);
399 400 401 402 403
    return -1;
}


static int
404
virStorageBackendISCSIStopPool(virStoragePoolObjPtr pool)
405
{
406
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
407
    char *portal;
408
    char *session;
409
    int ret = -1;
410

411 412 413 414
    if ((session = virStorageBackendISCSISession(pool, true)) == NULL)
        return 0;
    VIR_FREE(session);

415
    if ((portal = virStorageBackendISCSIPortal(&def->source)) == NULL)
416 417
        return -1;

418
    if (virISCSIConnectionLogout(portal,
419 420
                                 def->source.initiator.iqn,
                                 def->source.devices[0].path) < 0)
421 422
        goto cleanup;
    ret = 0;
423

424
 cleanup:
425 426
    VIR_FREE(portal);
    return ret;
427 428 429
}

virStorageBackend virStorageBackendISCSI = {
430
    .type = VIR_STORAGE_POOL_ISCSI,
431

432
    .checkPool = virStorageBackendISCSICheckPool,
433 434 435
    .startPool = virStorageBackendISCSIStartPool,
    .refreshPool = virStorageBackendISCSIRefreshPool,
    .stopPool = virStorageBackendISCSIStopPool,
436
    .findPoolSources = virStorageBackendISCSIFindPoolSources,
437 438
    .uploadVol = virStorageBackendVolUploadLocal,
    .downloadVol = virStorageBackendVolDownloadLocal,
439
    .wipeVol = virStorageBackendVolWipeLocal,
440
};
441 442 443 444 445 446 447


int
virStorageBackendISCSIRegister(void)
{
    return virStorageBackendRegister(&virStorageBackendISCSI);
}