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_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
    if (virISCSIScanTargets(portal,
                            source->initiator.iqn,
199
                            false,
200
                            &ntargets, &targets) < 0)
201 202
        goto cleanup;

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

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

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

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

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

    *isActive = false;

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

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

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

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

    return ret;
}


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

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

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

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

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

306 307 308 309 310
    if (VIR_REALLOC_N(secret_value, secret_size + 1) < 0)
        goto cleanup;

    secret_value[secret_size] = '\0';

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

    ret = 0;

327
 cleanup:
328
    VIR_DISPOSE_N(secret_value, secret_size);
329
    virObjectUnref(conn);
330 331 332 333
    return ret;
}

static int
334
virStorageBackendISCSIStartPool(virStoragePoolObjPtr pool)
335
{
336
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
337
    char *portal = NULL;
338 339
    char *session = NULL;
    int ret = -1;
340

341
    if (def->source.nhost != 1) {
342 343
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Expected exactly 1 host for the storage pool"));
344 345 346
        return -1;
    }

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

353 354
    if (def->source.ndevice != 1 ||
        def->source.devices[0].path == NULL) {
355 356
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing source device"));
357 358 359
        return -1;
    }

360
    if ((session = virStorageBackendISCSISession(pool, true)) == NULL) {
361
        if ((portal = virStorageBackendISCSIPortal(&def->source)) == NULL)
362
            goto cleanup;
363 364 365

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

369
        if (virStorageBackendISCSISetAuth(portal, &def->source) < 0)
370 371
            goto cleanup;

372
        if (virISCSIConnectionLogin(portal,
373 374
                                    def->source.initiator.iqn,
                                    def->source.devices[0].path) < 0)
375
            goto cleanup;
376
    }
377 378
    ret = 0;

379
 cleanup:
380
    VIR_FREE(portal);
381 382
    VIR_FREE(session);
    return ret;
383 384 385
}

static int
386
virStorageBackendISCSIRefreshPool(virStoragePoolObjPtr pool)
387
{
388
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
389 390
    char *session = NULL;

391
    def->allocation = def->capacity = def->available = 0;
392

393
    if ((session = virStorageBackendISCSISession(pool, false)) == NULL)
394
        goto cleanup;
395
    if (virISCSIRescanLUNs(session) < 0)
396
        goto cleanup;
397
    if (virStorageBackendISCSIFindLUs(pool, session) < 0)
398
        goto cleanup;
399
    VIR_FREE(session);
400 401 402

    return 0;

403
 cleanup:
404
    VIR_FREE(session);
405 406 407 408 409
    return -1;
}


static int
410
virStorageBackendISCSIStopPool(virStoragePoolObjPtr pool)
411
{
412
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
413
    char *portal;
414
    char *session;
415
    int ret = -1;
416

417 418 419 420
    if ((session = virStorageBackendISCSISession(pool, true)) == NULL)
        return 0;
    VIR_FREE(session);

421
    if ((portal = virStorageBackendISCSIPortal(&def->source)) == NULL)
422 423
        return -1;

424
    if (virISCSIConnectionLogout(portal,
425 426
                                 def->source.initiator.iqn,
                                 def->source.devices[0].path) < 0)
427 428
        goto cleanup;
    ret = 0;
429

430
 cleanup:
431 432
    VIR_FREE(portal);
    return ret;
433 434 435
}

virStorageBackend virStorageBackendISCSI = {
436
    .type = VIR_STORAGE_POOL_ISCSI,
437

438
    .checkPool = virStorageBackendISCSICheckPool,
439 440 441
    .startPool = virStorageBackendISCSIStartPool,
    .refreshPool = virStorageBackendISCSIRefreshPool,
    .stopPool = virStorageBackendISCSIStopPool,
442
    .findPoolSources = virStorageBackendISCSIFindPoolSources,
443 444
    .uploadVol = virStorageBackendVolUploadLocal,
    .downloadVol = virStorageBackendVolDownloadLocal,
445
    .wipeVol = virStorageBackendVolWipeLocal,
446
};
447 448 449 450 451 452 453


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