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

    ret = 0;

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

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

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

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

348 349
    if (def->source.ndevice != 1 ||
        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
        if ((portal = virStorageBackendISCSIPortal(&def->source)) == NULL)
357
            goto cleanup;
358 359 360

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

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

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

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

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

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

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

    return 0;

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


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

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

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

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

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

virStorageBackend virStorageBackendISCSI = {
431
    .type = VIR_STORAGE_POOL_ISCSI,
432

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


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