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
#include <fcntl.h>
#include <unistd.h>
D
David Allan 已提交
27
#include <sys/stat.h>
28

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

44 45
#define VIR_FROM_THIS VIR_FROM_STORAGE

46 47
VIR_LOG_INIT("storage.storage_backend_iscsi");

48 49
#define ISCSI_DEFAULT_TARGET_PORT 3260

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

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

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

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

    return portal;
}

77

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


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

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

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

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

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

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

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

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

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

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

144
    return 0;
145
}
146 147


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

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

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

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

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

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

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

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

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

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

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

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

    *isActive = false;

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

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

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

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


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

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

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

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

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

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

    secret_value[secret_size] = '\0';

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

    ret = 0;

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

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

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

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

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

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

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

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

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

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

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

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

    return 0;
}


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

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

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

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

397
    return 0;
398 399 400
}

virStorageBackend virStorageBackendISCSI = {
401
    .type = VIR_STORAGE_POOL_ISCSI,
402

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


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