storage_backend_iscsi.c 11.9 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
 */

#include <config.h>

24
#include <dirent.h>
25 26 27
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
D
David Allan 已提交
28
#include <sys/stat.h>
29

30 31
#include "datatypes.h"
#include "driver.h"
32
#include "storage_backend_iscsi.h"
33
#include "viralloc.h"
34
#include "vircommand.h"
35 36
#include "virerror.h"
#include "virfile.h"
37
#include "viriscsi.h"
38
#include "virlog.h"
39
#include "virobject.h"
40
#include "virstring.h"
41
#include "viruuid.h"
42
#include "secret_util.h"
43
#include "storage_util.h"
44

45 46
#define VIR_FROM_THIS VIR_FROM_STORAGE

47 48
VIR_LOG_INIT("storage.storage_backend_iscsi");

49 50
#define ISCSI_DEFAULT_TARGET_PORT 3260

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

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

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

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

    return portal;
}

78

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


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

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

J
John Ferlan 已提交
99
    virWaitForDevices();
100

101 102
    if (virDirOpen(&sysdir, sysfs_path) < 0)
        goto cleanup;
103

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

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

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

136 137
    sysfs_path = g_strdup_printf("/sys/class/iscsi_session/session%s/device",
                                 session);
138

139
    if (virStorageBackendISCSIGetHostNumber(sysfs_path, &host) < 0)
140
        return -1;
141

142
    if (virStorageBackendSCSIFindLUs(pool, host) < 0)
143
        return -1;
144

145
    return 0;
146
}
147 148


149
static char *
150
virStorageBackendISCSIFindPoolSources(const char *srcSpec,
E
Eric Blake 已提交
151
                                      unsigned int flags)
152 153 154 155
{
    size_t ntargets = 0;
    char **targets = NULL;
    char *ret = NULL;
156
    size_t i;
157 158 159 160 161
    virStoragePoolSourceList list = {
        .type = VIR_STORAGE_POOL_ISCSI,
        .nsources = 0,
        .sources = NULL
    };
162
    g_autofree char *portal = NULL;
J
Ján Tomko 已提交
163
    g_autoptr(virStoragePoolSource) source = NULL;
164

E
Eric Blake 已提交
165 166
    virCheckFlags(0, NULL);

167
    if (!srcSpec) {
168 169
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("hostname must be specified for iscsi sources"));
170 171 172
        return NULL;
    }

173 174 175 176
    if (!(source = virStoragePoolDefParseSourceString(srcSpec,
                                                      list.type)))
        return NULL;

177
    if (source->nhost != 1) {
178 179
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Expected exactly 1 host for the storage pool"));
180 181 182
        goto cleanup;
    }

183 184 185
    if (!(portal = virStorageBackendISCSIPortal(source)))
        goto cleanup;

186 187
    if (virISCSIScanTargets(portal,
                            source->initiator.iqn,
188
                            false,
189
                            &ntargets, &targets) < 0)
190 191
        goto cleanup;

192
    if (VIR_ALLOC_N(list.sources, ntargets) < 0)
193 194
        goto cleanup;

195
    for (i = 0; i < ntargets; i++) {
E
Eric Blake 已提交
196
        if (VIR_ALLOC_N(list.sources[i].devices, 1) < 0 ||
197
            VIR_ALLOC_N(list.sources[i].hosts, 1) < 0)
198
            goto cleanup;
E
Eric Blake 已提交
199 200
        list.sources[i].nhost = 1;
        list.sources[i].hosts[0] = source->hosts[0];
201 202 203 204 205 206
        list.sources[i].initiator = source->initiator;
        list.sources[i].ndevice = 1;
        list.sources[i].devices[0].path = targets[i];
        list.nsources++;
    }

207
    if (!(ret = virStoragePoolSourceListFormat(&list)))
208 209
        goto cleanup;

210
 cleanup:
211
    if (list.sources) {
212
        for (i = 0; i < ntargets; i++) {
E
Eric Blake 已提交
213
            VIR_FREE(list.sources[i].hosts);
214
            VIR_FREE(list.sources[i].devices);
E
Eric Blake 已提交
215
        }
216 217
        VIR_FREE(list.sources);
    }
218
    for (i = 0; i < ntargets; i++)
219 220 221 222 223
        VIR_FREE(targets[i]);
    VIR_FREE(targets);
    return ret;
}

224
static int
225
virStorageBackendISCSICheckPool(virStoragePoolObjPtr pool,
226 227
                                bool *isActive)
{
228
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
229
    g_autofree char *session = NULL;
230 231 232

    *isActive = false;

233
    if (def->source.nhost != 1) {
234 235
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Expected exactly 1 host for the storage pool"));
236 237 238
        return -1;
    }

239
    if (def->source.hosts[0].name == NULL) {
240 241
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing source host"));
242 243 244
        return -1;
    }

245 246
    if (def->source.ndevice != 1 ||
        def->source.devices[0].path == NULL) {
247 248
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing source device"));
249 250 251
        return -1;
    }

252
    if ((session = virStorageBackendISCSISession(pool, true)))
253
        *isActive = true;
M
Michal Privoznik 已提交
254
    return 0;
255 256 257
}


258
static int
259
virStorageBackendISCSISetAuth(const char *portal,
260
                              virStoragePoolSourcePtr source)
261 262
{
    unsigned char *secret_value = NULL;
263
    size_t secret_size;
264
    virStorageAuthDefPtr authdef = source->auth;
265
    int ret = -1;
266
    virConnectPtr conn = NULL;
267

268
    if (!authdef || authdef->authType == VIR_STORAGE_AUTH_TYPE_NONE)
269 270
        return 0;

271 272
    VIR_DEBUG("username='%s' authType=%d seclookupdef.type=%d",
              authdef->username, authdef->authType, authdef->seclookupdef.type);
273
    if (authdef->authType != VIR_STORAGE_AUTH_TYPE_CHAP) {
274 275 276 277 278
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("iscsi pool only supports 'chap' auth type"));
        return -1;
    }

279 280
    conn = virGetConnectSecret();
    if (!conn)
281 282
        return -1;

283 284
    if (virSecretGetSecretString(conn, &authdef->seclookupdef,
                                 VIR_SECRET_USAGE_TYPE_ISCSI,
285
                                 &secret_value, &secret_size) < 0)
286 287
        goto cleanup;

288 289 290 291 292
    if (VIR_REALLOC_N(secret_value, secret_size + 1) < 0)
        goto cleanup;

    secret_value[secret_size] = '\0';

293
    if (virISCSINodeUpdate(portal,
294
                           source->devices[0].path,
295 296 297
                           "node.session.auth.authmethod",
                           "CHAP") < 0 ||
        virISCSINodeUpdate(portal,
298
                           source->devices[0].path,
299
                           "node.session.auth.username",
300
                           authdef->username) < 0 ||
301
        virISCSINodeUpdate(portal,
302
                           source->devices[0].path,
303 304
                           "node.session.auth.password",
                           (const char *)secret_value) < 0)
305 306 307 308
        goto cleanup;

    ret = 0;

309
 cleanup:
310
    VIR_DISPOSE_N(secret_value, secret_size);
311
    virObjectUnref(conn);
312 313 314 315
    return ret;
}

static int
316
virStorageBackendISCSIStartPool(virStoragePoolObjPtr pool)
317
{
318
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
319 320
    g_autofree char *portal = NULL;
    g_autofree char *session = NULL;
321

322
    if (def->source.nhost != 1) {
323 324
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Expected exactly 1 host for the storage pool"));
325 326 327
        return -1;
    }

328
    if (def->source.hosts[0].name == NULL) {
329 330
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing source host"));
331 332 333
        return -1;
    }

334 335
    if (def->source.ndevice != 1 ||
        def->source.devices[0].path == NULL) {
336 337
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing source device"));
338 339 340
        return -1;
    }

341
    if ((session = virStorageBackendISCSISession(pool, true)) == NULL) {
342
        if ((portal = virStorageBackendISCSIPortal(&def->source)) == NULL)
343
            return -1;
344 345 346

        /* Create a static node record for the IQN target. Must be done
         * in order for login to the target */
347
        if (virISCSINodeNew(portal, def->source.devices[0].path) < 0)
348
            return -1;
349

350
        if (virStorageBackendISCSISetAuth(portal, &def->source) < 0)
351
            return -1;
352

353
        if (virISCSIConnectionLogin(portal,
354 355
                                    def->source.initiator.iqn,
                                    def->source.devices[0].path) < 0)
356
            return -1;
357
    }
358
    return 0;
359 360 361
}

static int
362
virStorageBackendISCSIRefreshPool(virStoragePoolObjPtr pool)
363
{
364
    virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
365
    g_autofree char *session = NULL;
366

367
    def->allocation = def->capacity = def->available = 0;
368

369
    if ((session = virStorageBackendISCSISession(pool, false)) == NULL)
370
        return -1;
371
    if (virISCSIRescanLUNs(session) < 0)
372
        return -1;
373
    if (virStorageBackendISCSIFindLUs(pool, session) < 0)
374
        return -1;
375 376 377 378 379 380

    return 0;
}


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

387 388 389
    if ((session = virStorageBackendISCSISession(pool, true)) == NULL)
        return 0;

390
    if ((portal = virStorageBackendISCSIPortal(&def->source)) == NULL)
391 392
        return -1;

393
    if (virISCSIConnectionLogout(portal,
394 395
                                 def->source.initiator.iqn,
                                 def->source.devices[0].path) < 0)
396
        return -1;
397

398
    return 0;
399 400 401
}

virStorageBackend virStorageBackendISCSI = {
402
    .type = VIR_STORAGE_POOL_ISCSI,
403

404
    .checkPool = virStorageBackendISCSICheckPool,
405 406 407
    .startPool = virStorageBackendISCSIStartPool,
    .refreshPool = virStorageBackendISCSIRefreshPool,
    .stopPool = virStorageBackendISCSIStopPool,
408
    .findPoolSources = virStorageBackendISCSIFindPoolSources,
409 410
    .uploadVol = virStorageBackendVolUploadLocal,
    .downloadVol = virStorageBackendVolDownloadLocal,
411
    .wipeVol = virStorageBackendVolWipeLocal,
412
};
413 414 415 416 417 418 419


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