storage_backend_iscsi.c 12.4 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_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
#include "secret_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 94 95 96
static int
virStorageBackendISCSIGetHostNumber(const char *sysfs_path,
                                    uint32_t *host)
{
    int retval = 0;
    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();

J
Ján Tomko 已提交
103
    if (virDirOpen(&sysdir, sysfs_path) < 0)
104 105 106 107 108 109
        virReportSystemError(errno,
                             _("Failed to opendir path '%s'"), sysfs_path);
        retval = -1;
        goto out;
    }

E
Eric Blake 已提交
110
    while ((direrr = virDirRead(sysdir, &dirent, sysfs_path)) > 0) {
111 112 113 114 115 116 117 118 119
        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 已提交
120 121
    if (direrr < 0)
        retval = -1;
122

J
Ján Tomko 已提交
123
    VIR_DIR_CLOSE(sysdir);
124
 out:
125 126
    return retval;
}
127

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

136
    if (virAsprintf(&sysfs_path,
137
                    "/sys/class/iscsi_session/session%s/device", session) < 0)
138
        goto cleanup;
139

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

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

    retval = 0;

 cleanup:
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
    if (!srcSpec) {
181 182
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("hostname must be specified for iscsi sources"));
183 184 185
        return NULL;
    }

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

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

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

199
    if (virISCSIScanTargets(portal, &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 240 241 242 243 244
                                bool *isActive)
{
    char *session = NULL;
    int ret = -1;

    *isActive = false;

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

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

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

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

    return ret;
}


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

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

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

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

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

    ret = 0;

323
 cleanup:
324
    VIR_DISPOSE_N(secret_value, secret_size);
325 326 327 328 329
    return ret;
}

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

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

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

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

355
    if ((session = virStorageBackendISCSISession(pool, true)) == NULL) {
356 357 358 359 360 361
        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 :-(
         */
362
        if (virISCSIScanTargets(portal, NULL, NULL) < 0)
363 364
            goto cleanup;

365
        if (virStorageBackendISCSISetAuth(portal, conn, &pool->def->source) < 0)
366 367
            goto cleanup;

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

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

static int
382
virStorageBackendISCSIRefreshPool(virConnectPtr conn ATTRIBUTE_UNUSED,
383 384 385 386 387 388
                                  virStoragePoolObjPtr pool)
{
    char *session = NULL;

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

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

    return 0;

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


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

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

417
    if ((portal = virStorageBackendISCSIPortal(&pool->def->source)) == NULL)
418 419
        return -1;

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

426
 cleanup:
427 428
    VIR_FREE(portal);
    return ret;
429 430 431
}

virStorageBackend virStorageBackendISCSI = {
432
    .type = VIR_STORAGE_POOL_ISCSI,
433

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