storage_backend_iscsi.c 12.3 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
    return virISCSIGetSession(pool->def->source.devices[0].path, probe);
87 88 89
}


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

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

    virFileWaitForDevices();

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

E
Eric Blake 已提交
106
    while ((direrr = virDirRead(sysdir, &dirent, sysfs_path)) > 0) {
107
        if (STRPREFIX(dirent->d_name, "target")) {
108 109 110 111 112 113 114
            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;
115 116 117 118
            }
        }
    }

119 120 121 122 123 124 125 126
    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 已提交
127
    VIR_DIR_CLOSE(sysdir);
128
    return ret;
129
}
130

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

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

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

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

    retval = 0;

 cleanup:
152

153 154
    VIR_FREE(sysfs_path);

155 156
    return retval;
}
157 158


159 160 161
static char *
virStorageBackendISCSIFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
                                      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
    if (virISCSIScanTargets(portal, &ntargets, &targets) < 0)
198 199
        goto cleanup;

200
    if (VIR_ALLOC_N(list.sources, ntargets) < 0)
201 202
        goto cleanup;

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

215
    if (!(ret = virStoragePoolSourceListFormat(&list)))
216 217
        goto cleanup;

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

234
static int
235
virStorageBackendISCSICheckPool(virStoragePoolObjPtr pool,
236 237 238 239 240 241 242
                                bool *isActive)
{
    char *session = NULL;
    int ret = -1;

    *isActive = false;

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

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

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

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

    return ret;
}


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

282
    if (!authdef || authdef->authType == VIR_STORAGE_AUTH_TYPE_NONE)
283 284
        return 0;

285 286
    VIR_DEBUG("username='%s' authType=%d seclookupdef.type=%d",
              authdef->username, authdef->authType, authdef->seclookupdef.type);
287
    if (authdef->authType != VIR_STORAGE_AUTH_TYPE_CHAP) {
288 289 290 291 292 293 294 295 296 297 298 299
        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;
    }

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 324 325 326 327
    return ret;
}

static int
virStorageBackendISCSIStartPool(virConnectPtr conn,
328 329 330
                                virStoragePoolObjPtr pool)
{
    char *portal = NULL;
331 332
    char *session = NULL;
    int ret = -1;
333

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

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

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

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

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

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

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

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

static int
379
virStorageBackendISCSIRefreshPool(virConnectPtr conn ATTRIBUTE_UNUSED,
380 381 382 383 384 385
                                  virStoragePoolObjPtr pool)
{
    char *session = NULL;

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

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

    return 0;

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


static int
403
virStorageBackendISCSIStopPool(virConnectPtr conn ATTRIBUTE_UNUSED,
404 405 406
                               virStoragePoolObjPtr pool)
{
    char *portal;
407
    char *session;
408
    int ret = -1;
409

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

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

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

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

virStorageBackend virStorageBackendISCSI = {
429
    .type = VIR_STORAGE_POOL_ISCSI,
430

431
    .checkPool = virStorageBackendISCSICheckPool,
432 433 434
    .startPool = virStorageBackendISCSIStartPool,
    .refreshPool = virStorageBackendISCSIRefreshPool,
    .stopPool = virStorageBackendISCSIStopPool,
435
    .findPoolSources = virStorageBackendISCSIFindPoolSources,
436 437
    .uploadVol = virStorageBackendVolUploadLocal,
    .downloadVol = virStorageBackendVolDownloadLocal,
438
    .wipeVol = virStorageBackendVolWipeLocal,
439
};