storage_backend_iscsi.c 14.2 KB
Newer Older
1 2 3
/*
 * storage_backend_iscsi.c: storage backend for iSCSI handling
 *
4
 * Copyright (C) 2007-2014 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_scsi.h"
36
#include "storage_backend_iscsi.h"
37
#include "viralloc.h"
38
#include "vircommand.h"
39 40
#include "virerror.h"
#include "virfile.h"
41
#include "viriscsi.h"
42
#include "virlog.h"
43
#include "virobject.h"
44
#include "virstring.h"
45
#include "viruuid.h"
46

47 48
#define VIR_FROM_THIS VIR_FROM_STORAGE

49 50
VIR_LOG_INIT("storage.storage_backend_iscsi");

51 52
#define ISCSI_DEFAULT_TARGET_PORT 3260

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

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

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

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

    return portal;
}

80

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


89 90 91 92 93 94 95
static int
virStorageBackendISCSIGetHostNumber(const char *sysfs_path,
                                    uint32_t *host)
{
    int retval = 0;
    DIR *sysdir = NULL;
    struct dirent *dirent = NULL;
E
Eric Blake 已提交
96
    int direrr;
97 98 99 100 101 102 103 104 105 106 107 108 109 110

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

    virFileWaitForDevices();

    sysdir = opendir(sysfs_path);

    if (sysdir == NULL) {
        virReportSystemError(errno,
                             _("Failed to opendir path '%s'"), sysfs_path);
        retval = -1;
        goto out;
    }

E
Eric Blake 已提交
111
    while ((direrr = virDirRead(sysdir, &dirent, sysfs_path)) > 0) {
112 113 114 115 116 117 118 119 120
        if (STREQLEN(dirent->d_name, "target", strlen("target"))) {
            if (sscanf(dirent->d_name,
                       "target%u:", host) != 1) {
                VIR_DEBUG("Failed to parse target '%s'", dirent->d_name);
                retval = -1;
                break;
            }
        }
    }
E
Eric Blake 已提交
121 122
    if (direrr < 0)
        retval = -1;
123 124

    closedir(sysdir);
125
 out:
126 127
    return retval;
}
128

129
static int
130
virStorageBackendISCSIFindLUs(virStoragePoolObjPtr pool,
131
                              const char *session)
132
{
133
    char *sysfs_path;
134 135
    int retval = 0;
    uint32_t host;
136

137
    if (virAsprintf(&sysfs_path,
138
                    "/sys/class/iscsi_session/session%s/device", session) < 0)
139
        return -1;
140

141
    if (virStorageBackendISCSIGetHostNumber(sysfs_path, &host) < 0) {
142
        virReportSystemError(errno,
143 144
                             _("Failed to get host number for iSCSI session "
                               "with path '%s'"),
145
                             sysfs_path);
146
        retval = -1;
147 148
    }

149
    if (virStorageBackendSCSIFindLUs(pool, host) < 0) {
150
        virReportSystemError(errno,
151 152
                             _("Failed to find LUs on host %u"), host);
        retval = -1;
153 154
    }

155 156
    VIR_FREE(sysfs_path);

157 158
    return retval;
}
159 160


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

E
Eric Blake 已提交
178 179
    virCheckFlags(0, NULL);

180 181 182 183 184 185 186
    if (!srcSpec) {
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("hostname and device path "
                               "must be specified for iscsi sources"));
        return NULL;
    }

187 188 189 190
    if (!(source = virStoragePoolDefParseSourceString(srcSpec,
                                                      list.type)))
        return NULL;

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

197 198 199
    if (!(portal = virStorageBackendISCSIPortal(source)))
        goto cleanup;

200 201 202
    if (virISCSIScanTargets(portal,
                            source->initiator.iqn,
                            &ntargets, &targets) < 0)
203 204
        goto cleanup;

205
    if (VIR_ALLOC_N(list.sources, ntargets) < 0)
206 207
        goto cleanup;

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

220
    if (!(ret = virStoragePoolSourceListFormat(&list)))
221 222
        goto cleanup;

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

239 240 241 242 243 244 245 246 247 248
static int
virStorageBackendISCSICheckPool(virConnectPtr conn ATTRIBUTE_UNUSED,
                                virStoragePoolObjPtr pool,
                                bool *isActive)
{
    char *session = NULL;
    int ret = -1;

    *isActive = false;

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

    if (pool->def->source.hosts[0].name == NULL) {
256 257
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing source host"));
258 259 260 261 262
        return -1;
    }

    if (pool->def->source.ndevice != 1 ||
        pool->def->source.devices[0].path == NULL) {
263 264
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing source device"));
265 266 267
        return -1;
    }

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

    return ret;
}


278
static int
279 280
virStorageBackendISCSISetAuth(const char *portal,
                              virConnectPtr conn,
281
                              virStoragePoolSourcePtr source)
282 283 284
{
    virSecretPtr secret = NULL;
    unsigned char *secret_value = NULL;
285
    virStorageAuthDefPtr authdef = source->auth;
286
    int ret = -1;
287
    char uuidStr[VIR_UUID_STRING_BUFLEN];
288

289
    if (!authdef || authdef->authType == VIR_STORAGE_AUTH_TYPE_NONE)
290 291
        return 0;

292 293 294
    VIR_DEBUG("username='%s' authType=%d secretType=%d",
              authdef->username, authdef->authType, authdef->secretType);
    if (authdef->authType != VIR_STORAGE_AUTH_TYPE_CHAP) {
295 296 297 298 299 300 301 302 303 304 305 306
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("iscsi pool only supports 'chap' auth type"));
        return -1;
    }

    if (!conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("iscsi 'chap' authentication not supported "
                         "for autostarted pools"));
        return -1;
    }

307 308
    if (authdef->secretType == VIR_STORAGE_SECRET_TYPE_UUID)
        secret = virSecretLookupByUUID(conn, authdef->secret.uuid);
309 310
    else
        secret = virSecretLookupByUsage(conn, VIR_SECRET_USAGE_TYPE_ISCSI,
311
                                        authdef->secret.usage);
312 313 314 315 316 317 318

    if (secret) {
        size_t secret_size;
        secret_value =
            conn->secretDriver->secretGetValue(secret, &secret_size, 0,
                                               VIR_SECRET_GET_VALUE_INTERNAL_CALL);
        if (!secret_value) {
319 320
            if (authdef->secretType == VIR_STORAGE_SECRET_TYPE_UUID) {
                virUUIDFormat(authdef->secret.uuid, uuidStr);
321 322 323
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("could not get the value of the secret "
                                 "for username %s using uuid '%s'"),
324
                               authdef->username, uuidStr);
325 326 327 328
            } else {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("could not get the value of the secret "
                                 "for username %s using usage value '%s'"),
329
                               authdef->username, authdef->secret.usage);
330
            }
331 332 333
            goto cleanup;
        }
    } else {
334 335
        if (authdef->secretType == VIR_STORAGE_SECRET_TYPE_UUID) {
            virUUIDFormat(authdef->secret.uuid, uuidStr);
336 337
            virReportError(VIR_ERR_NO_SECRET,
                           _("no secret matches uuid '%s'"),
338
                           uuidStr);
339 340 341
        } else {
            virReportError(VIR_ERR_NO_SECRET,
                           _("no secret matches usage value '%s'"),
342
                           authdef->secret.usage);
343
        }
344 345 346
        goto cleanup;
    }

347
    if (virISCSINodeUpdate(portal,
348
                           source->devices[0].path,
349 350 351
                           "node.session.auth.authmethod",
                           "CHAP") < 0 ||
        virISCSINodeUpdate(portal,
352
                           source->devices[0].path,
353
                           "node.session.auth.username",
354
                           authdef->username) < 0 ||
355
        virISCSINodeUpdate(portal,
356
                           source->devices[0].path,
357 358
                           "node.session.auth.password",
                           (const char *)secret_value) < 0)
359 360 361 362
        goto cleanup;

    ret = 0;

363
 cleanup:
364 365 366 367 368 369 370
    virObjectUnref(secret);
    VIR_FREE(secret_value);
    return ret;
}

static int
virStorageBackendISCSIStartPool(virConnectPtr conn,
371 372 373
                                virStoragePoolObjPtr pool)
{
    char *portal = NULL;
374 375
    char *session = NULL;
    int ret = -1;
376

377
    if (pool->def->source.nhost != 1) {
378 379
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Expected exactly 1 host for the storage pool"));
380 381 382 383
        return -1;
    }

    if (pool->def->source.hosts[0].name == NULL) {
384 385
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing source host"));
386 387 388 389 390
        return -1;
    }

    if (pool->def->source.ndevice != 1 ||
        pool->def->source.devices[0].path == NULL) {
391 392
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing source device"));
393 394 395
        return -1;
    }

396
    if ((session = virStorageBackendISCSISession(pool, true)) == NULL) {
397 398 399 400 401 402
        if ((portal = virStorageBackendISCSIPortal(&pool->def->source)) == NULL)
            goto cleanup;
        /*
         * iscsiadm doesn't let you login to a target, unless you've
         * first issued a 'sendtargets' command to the portal :-(
         */
403 404 405
        if (virISCSIScanTargets(portal,
                                pool->def->source.initiator.iqn,
                                NULL, NULL) < 0)
406 407
            goto cleanup;

408
        if (virStorageBackendISCSISetAuth(portal, conn, &pool->def->source) < 0)
409 410
            goto cleanup;

411 412 413
        if (virISCSIConnectionLogin(portal,
                                    pool->def->source.initiator.iqn,
                                    pool->def->source.devices[0].path) < 0)
414
            goto cleanup;
415
    }
416 417
    ret = 0;

418
 cleanup:
419
    VIR_FREE(portal);
420 421
    VIR_FREE(session);
    return ret;
422 423 424
}

static int
425
virStorageBackendISCSIRefreshPool(virConnectPtr conn ATTRIBUTE_UNUSED,
426 427 428 429 430 431
                                  virStoragePoolObjPtr pool)
{
    char *session = NULL;

    pool->def->allocation = pool->def->capacity = pool->def->available = 0;

432
    if ((session = virStorageBackendISCSISession(pool, false)) == NULL)
433
        goto cleanup;
434
    if (virISCSIRescanLUNs(session) < 0)
435
        goto cleanup;
436
    if (virStorageBackendISCSIFindLUs(pool, session) < 0)
437
        goto cleanup;
438
    VIR_FREE(session);
439 440 441

    return 0;

442
 cleanup:
443
    VIR_FREE(session);
444 445 446 447 448
    return -1;
}


static int
449
virStorageBackendISCSIStopPool(virConnectPtr conn ATTRIBUTE_UNUSED,
450 451 452
                               virStoragePoolObjPtr pool)
{
    char *portal;
453
    int ret = -1;
454

455
    if ((portal = virStorageBackendISCSIPortal(&pool->def->source)) == NULL)
456 457
        return -1;

458 459 460
    if (virISCSIConnectionLogout(portal,
                                 pool->def->source.initiator.iqn,
                                 pool->def->source.devices[0].path) < 0)
461 462
        goto cleanup;
    ret = 0;
463

464
 cleanup:
465 466
    VIR_FREE(portal);
    return ret;
467 468 469
}

virStorageBackend virStorageBackendISCSI = {
470
    .type = VIR_STORAGE_POOL_ISCSI,
471

472
    .checkPool = virStorageBackendISCSICheckPool,
473 474 475
    .startPool = virStorageBackendISCSIStartPool,
    .refreshPool = virStorageBackendISCSIRefreshPool,
    .stopPool = virStorageBackendISCSIStopPool,
476
    .findPoolSources = virStorageBackendISCSIFindPoolSources,
477 478
    .uploadVol = virStorageBackendVolUploadLocal,
    .downloadVol = virStorageBackendVolDownloadLocal,
479
    .wipeVol = virStorageBackendVolWipeLocal,
480
};