vmware_conf.c 14.2 KB
Newer Older
1
/*---------------------------------------------------------------------------*/
2
/*
3
 * Copyright (C) 2011-2014 Red Hat, Inc.
4
 * Copyright (C) 2010-2014, diateam (www.diateam.net)
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25
 */
/*---------------------------------------------------------------------------*/

#include <config.h>

#include <string.h>

26
#include "vircommand.h"
27 28
#include "cpu/cpu.h"
#include "dirname.h"
29
#include "viralloc.h"
30
#include "nodeinfo.h"
E
Eric Blake 已提交
31
#include "virfile.h"
32
#include "viruuid.h"
33
#include "virerror.h"
34
#include "vmx.h"
35
#include "vmware_conf.h"
36
#include "virstring.h"
37

38 39
VIR_ENUM_IMPL(vmwareDriver, VMWARE_DRIVER_LAST,
              "player",
40 41
              "ws",
              "fusion");
42

43 44 45 46 47 48 49 50
/* Free all memory associated with a vmware_driver structure */
void
vmwareFreeDriver(struct vmware_driver *driver)
{
    if (!driver)
        return;

    virMutexDestroy(&driver->lock);
51
    virObjectUnref(driver->domains);
52
    virObjectUnref(driver->caps);
53
    virObjectUnref(driver->xmlopt);
54
    VIR_FREE(driver->vmrun);
55 56 57
    VIR_FREE(driver);
}

58

59 60 61 62 63 64 65
virCapsPtr
vmwareCapsInit(void)
{
    virCapsPtr caps = NULL;
    virCapsGuestPtr guest = NULL;
    virCPUDefPtr cpu = NULL;

66
    if ((caps = virCapabilitiesNew(virArchFromHost(),
67
                                   false, false)) == NULL)
68 69
        goto error;

70
    if (nodeCapsInitNUMA(caps) < 0)
71 72 73 74
        goto error;

    /* i686 guests are always supported */
    if ((guest = virCapabilitiesAddGuest(caps,
75
                                         VIR_DOMAIN_OSTYPE_HVM,
76
                                         VIR_ARCH_I686,
77 78 79 80
                                         NULL, NULL, 0, NULL)) == NULL)
        goto error;

    if (virCapabilitiesAddGuestDomain(guest,
81
                                      VIR_DOMAIN_VIRT_VMWARE,
82 83 84
                                      NULL, NULL, 0, NULL) == NULL)
        goto error;

85
    if (!(cpu = virCPUGetHost(caps->host.arch, NULL)))
86 87 88 89 90 91 92
        goto error;

    /* x86_64 guests are supported if
     *  - Host arch is x86_64
     * Or
     *  - Host CPU is x86_64 with virtualization extensions
     */
93
    if (caps->host.arch == VIR_ARCH_X86_64 ||
94 95 96
        (virCPUCheckFeature(cpu->arch, cpu, "lm") &&
         (virCPUCheckFeature(cpu->arch, cpu, "vmx") ||
          virCPUCheckFeature(cpu->arch, cpu, "svm")))) {
97 98

        if ((guest = virCapabilitiesAddGuest(caps,
99
                                             VIR_DOMAIN_OSTYPE_HVM,
100
                                             VIR_ARCH_X86_64,
101 102 103 104
                                             NULL, NULL, 0, NULL)) == NULL)
            goto error;

        if (virCapabilitiesAddGuestDomain(guest,
105
                                          VIR_DOMAIN_VIRT_VMWARE,
106 107 108 109
                                          NULL, NULL, 0, NULL) == NULL)
            goto error;
    }

110
 cleanup:
111 112 113
    virCPUDefFree(cpu);
    return caps;

114
 error:
115
    virObjectUnref(caps);
116 117 118 119 120 121 122 123 124 125 126 127 128 129
    goto cleanup;
}

int
vmwareLoadDomains(struct vmware_driver *driver)
{
    virDomainDefPtr vmdef = NULL;
    virDomainObjPtr vm = NULL;
    char *vmxPath = NULL;
    char *vmx = NULL;
    vmwareDomainPtr pDomain;
    char *directoryName = NULL;
    char *fileName = NULL;
    int ret = -1;
130
    virVMXContext ctx;
131 132 133 134 135
    char *outbuf = NULL;
    char *str;
    char *saveptr = NULL;
    virCommandPtr cmd;

136
    ctx.parseFileName = vmwareCopyVMXFileName;
137 138 139
    ctx.formatFileName = NULL;
    ctx.autodetectSCSIControllerModel = NULL;
    ctx.datacenterPath = NULL;
140

141
    cmd = virCommandNewArgList(driver->vmrun, "-T",
142
                               vmwareDriverTypeToString(driver->type),
143 144 145 146 147
                               "list", NULL);
    virCommandSetOutputBuffer(cmd, &outbuf);
    if (virCommandRun(cmd, NULL) < 0)
        goto cleanup;

148
    for (str = outbuf; (vmxPath = strtok_r(str, "\n", &saveptr)) != NULL;
149 150 151 152 153 154 155 156 157
        str = NULL) {

        if (vmxPath[0] != '/')
            continue;

        if (virFileReadAll(vmxPath, 10000, &vmx) < 0)
            goto cleanup;

        if ((vmdef =
158 159
             virVMXParseConfig(&ctx, driver->xmlopt,
                               driver->caps, vmx)) == NULL) {
160 161 162
            goto cleanup;
        }

163
        if (!(vm = virDomainObjListAdd(driver->domains, vmdef,
164
                                       driver->xmlopt,
165
                                       0, NULL)))
166 167 168 169
            goto cleanup;

        pDomain = vm->privateData;

170
        if (VIR_STRDUP(pDomain->vmxPath, vmxPath) < 0)
171 172 173 174 175 176 177
            goto cleanup;

        vmwareDomainConfigDisplay(pDomain, vmdef);

        if ((vm->def->id = vmwareExtractPid(vmxPath)) < 0)
            goto cleanup;
        /* vmrun list only reports running vms */
J
Jiri Denemark 已提交
178 179
        virDomainObjSetState(vm, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_UNKNOWN);
180 181
        vm->persistent = 1;

182
        virObjectUnlock(vm);
183 184 185 186 187 188 189

        vmdef = NULL;
        vm = NULL;
    }

    ret = 0;

190
 cleanup:
191 192 193 194 195 196
    virCommandFree(cmd);
    VIR_FREE(outbuf);
    virDomainDefFree(vmdef);
    VIR_FREE(directoryName);
    VIR_FREE(fileName);
    VIR_FREE(vmx);
197
    virObjectUnref(vm);
198 199 200 201 202 203 204 205 206
    return ret;
}

void
vmwareSetSentinal(const char **prog, const char *key)
{
    const char **tmp = prog;

    while (tmp && *tmp) {
E
Eric Blake 已提交
207
        if (*tmp == PROGRAM_SENTINEL) {
208 209 210 211 212 213 214
            *tmp = key;
            break;
        }
        tmp++;
    }
}

215 216 217 218 219 220 221 222 223 224 225 226 227
int
vmwareParseVersionStr(int type, const char *verbuf, unsigned long *version)
{
    const char *pattern;
    const char *tmp;

    switch (type) {
        case VMWARE_DRIVER_PLAYER:
            pattern = "VMware Player ";
            break;
        case VMWARE_DRIVER_WORKSTATION:
            pattern = "VMware Workstation ";
            break;
228 229 230
        case VMWARE_DRIVER_FUSION:
            pattern = "\nVMware Fusion Information:\nVMware Fusion ";
            break;
231 232 233 234 235 236
        default:
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid driver type: %d"), type);
            return -1;
    }

237 238 239 240 241 242 243
    if ((tmp = strstr(verbuf, pattern)) == NULL) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("cannot find version pattern \"%s\""), pattern);
        return -1;
    }

    if ((tmp = STRSKIP(tmp, pattern)) == NULL) {
244 245 246 247 248 249 250 251 252 253 254 255 256 257
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to parse %sversion"), pattern);
        return -1;
    }

    if (virParseVersionString(tmp, version, false) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("version parsing error"));
        return -1;
    }

    return 0;
}

258 259 260 261
int
vmwareExtractVersion(struct vmware_driver *driver)
{
    int ret = -1;
262
    virCommandPtr cmd = NULL;
263
    char * outbuf = NULL;
264 265 266 267 268 269 270 271
    char *bin = NULL;
    char *vmwarePath = NULL;

    if ((vmwarePath = mdir_name(driver->vmrun)) == NULL)
        goto cleanup;

    switch (driver->type) {
        case VMWARE_DRIVER_PLAYER:
272
            if (virAsprintf(&bin, "%s/%s", vmwarePath, "vmplayer") < 0)
273 274 275 276
                goto cleanup;
            break;

        case VMWARE_DRIVER_WORKSTATION:
277
            if (virAsprintf(&bin, "%s/%s", vmwarePath, "vmware") < 0)
278 279 280
                goto cleanup;
            break;

281
        case VMWARE_DRIVER_FUSION:
282
            if (virAsprintf(&bin, "%s/%s", vmwarePath, "vmware-vmx") < 0)
283 284 285
                goto cleanup;
            break;

286 287
        default:
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
288
                           _("invalid driver type for version detection"));
289 290
            goto cleanup;
    }
291 292 293

    cmd = virCommandNewArgList(bin, "-v", NULL);
    virCommandSetOutputBuffer(cmd, &outbuf);
294
    virCommandSetErrorBuffer(cmd, &outbuf);
295 296 297 298

    if (virCommandRun(cmd, NULL) < 0)
        goto cleanup;

299
    if (vmwareParseVersionStr(driver->type, outbuf, &driver->version) < 0)
300 301 302 303
        goto cleanup;

    ret = 0;

304
 cleanup:
305 306
    virCommandFree(cmd);
    VIR_FREE(outbuf);
307 308
    VIR_FREE(bin);
    VIR_FREE(vmwarePath);
309 310 311 312 313 314
    return ret;
}

int
vmwareDomainConfigDisplay(vmwareDomainPtr pDomain, virDomainDefPtr def)
{
315
    size_t i;
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

    if (def->ngraphics == 0) {
        pDomain->gui = true;
        return 0;
    } else {
        pDomain->gui = false;
        for (i = 0; i < def->ngraphics; i++) {
            if (def->graphics[i]->type == VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP) {
                pDomain->gui = true;
                return 0;
            }
        }
        return 0;
    }
}

E
Eric Blake 已提交
332 333
static int
vmwareParsePath(const char *path, char **directory, char **filename)
334 335 336 337 338 339
{
    char *separator;

    separator = strrchr(path, '/');

    if (separator != NULL) {
E
Eric Blake 已提交
340
        separator++;
341 342

        if (*separator == '\0') {
343 344
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("path '%s' doesn't reference a file"), path);
345 346 347
            return -1;
        }

E
Eric Blake 已提交
348
        if (VIR_STRNDUP(*directory, path, separator - path - 1) < 0)
349 350
            goto error;
        if (VIR_STRDUP(*filename, separator) < 0) {
351
            VIR_FREE(*directory);
352
            goto error;
353 354 355
        }

    } else {
356 357
        if (VIR_STRDUP(*filename, path) < 0)
            goto error;
358 359 360 361
    }

    return 0;

362
 error:
363 364 365 366 367 368
    return -1;
}

int
vmwareConstructVmxPath(char *directoryName, char *name, char **vmxPath)
{
369 370
    int ret;

371
    if (directoryName != NULL)
372 373 374 375 376 377 378
        ret = virAsprintf(vmxPath, "%s/%s.vmx", directoryName, name);
    else
        ret = virAsprintf(vmxPath, "%s.vmx", name);

    if (ret < 0)
        return -1;
    return 0;
379 380 381 382 383 384 385 386 387
}

int
vmwareVmxPath(virDomainDefPtr vmdef, char **vmxPath)
{
    virDomainDiskDefPtr disk = NULL;
    char *directoryName = NULL;
    char *fileName = NULL;
    int ret = -1;
388
    size_t i;
389
    const char *src;
390 391 392 393

    /*
     * Build VMX URL. Use the source of the first file-based harddisk
     * to deduce the path for the VMX file. Don't just use the
N
Nehal J Wani 已提交
394
     * first disk, because it may be CDROM disk and ISO images are normally not
395 396 397 398
     * located in the virtual machine's directory. This approach
     * isn't perfect but should work in the majority of cases.
     */
    if (vmdef->ndisks < 1) {
399 400 401
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Domain XML doesn't contain any disks, "
                         "cannot deduce datastore and path for VMX file"));
402 403 404 405 406
        goto cleanup;
    }

    for (i = 0; i < vmdef->ndisks; ++i) {
        if (vmdef->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK &&
E
Eric Blake 已提交
407
            virDomainDiskGetType(vmdef->disks[i]) == VIR_STORAGE_TYPE_FILE) {
408 409 410 411 412 413
            disk = vmdef->disks[i];
            break;
        }
    }

    if (disk == NULL) {
414 415 416
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Domain XML doesn't contain any file-based harddisks, "
                         "cannot deduce datastore and path for VMX file"));
417 418 419
        goto cleanup;
    }

420 421
    src = virDomainDiskGetSource(disk);
    if (!src) {
422 423 424
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("First file-based harddisk has no source, cannot "
                         "deduce datastore and path for VMX file"));
425 426 427
        goto cleanup;
    }

428
    if (vmwareParsePath(src, &directoryName, &fileName) < 0)
429 430 431
        goto cleanup;

    if (!virFileHasSuffix(fileName, ".vmdk")) {
432 433
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting source '%s' of first file-based harddisk "
434
                         "to be a VMDK image"), src);
435 436 437
        goto cleanup;
    }

438
    if (vmwareConstructVmxPath(directoryName, vmdef->name, vmxPath) < 0)
439 440 441 442
        goto cleanup;

    ret = 0;

443
 cleanup:
444 445 446 447 448 449 450 451 452
    VIR_FREE(directoryName);
    VIR_FREE(fileName);
    return ret;
}

int
vmwareMoveFile(char *srcFile, char *dstFile)
{
    const char *cmdmv[] =
E
Eric Blake 已提交
453
        { "mv", PROGRAM_SENTINEL, PROGRAM_SENTINEL, NULL };
454 455

    if (!virFileExists(srcFile)) {
456 457
        virReportError(VIR_ERR_INTERNAL_ERROR, _("file %s does not exist"),
                       srcFile);
458 459 460 461 462 463 464 465 466
        return -1;
    }

    if (STREQ(srcFile, dstFile))
        return 0;

    vmwareSetSentinal(cmdmv, srcFile);
    vmwareSetSentinal(cmdmv, dstFile);
    if (virRun(cmdmv, NULL) < 0) {
467 468
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to move file to %s "), dstFile);
469 470 471 472 473 474 475 476 477
        return -1;
    }

    return 0;
}

int
vmwareMakePath(char *srcDir, char *srcName, char *srcExt, char **outpath)
{
478 479 480
    if (virAsprintf(outpath, "%s/%s.%s", srcDir, srcName, srcExt) < 0)
        return -1;
    return 0;
481 482 483 484 485 486 487 488 489 490
}

int
vmwareExtractPid(const char * vmxPath)
{
    char *vmxDir = NULL;
    char *logFilePath = NULL;
    FILE *logFile = NULL;
    char line[1024];
    char *tmp = NULL;
491
    int pid_value = -1;
492 493 494 495 496

    if ((vmxDir = mdir_name(vmxPath)) == NULL)
        goto cleanup;

    if (virAsprintf(&logFilePath, "%s/vmware.log",
497
                    vmxDir) < 0)
498 499 500 501 502 503
        goto cleanup;

    if ((logFile = fopen(logFilePath, "r")) == NULL)
        goto cleanup;

    if (!fgets(line, sizeof(line), logFile)) {
504 505
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("unable to read vmware log file"));
506 507 508 509
        goto cleanup;
    }

    if ((tmp = strstr(line, " pid=")) == NULL) {
510 511
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot find pid in vmware log file"));
512 513 514 515 516
        goto cleanup;
    }

    tmp += strlen(" pid=");

517 518 519
    /* Although 64-bit windows allows 64-bit pid_t, a domain id has to be
     * 32 bits.  For now, we just reject pid values that overflow int.  */
    if (virStrToLong_i(tmp, &tmp, 10, &pid_value) < 0 || *tmp != ' ') {
520 521
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot parse pid in vmware log file"));
522 523 524
        goto cleanup;
    }

525
 cleanup:
526 527 528
    VIR_FREE(vmxDir);
    VIR_FREE(logFilePath);
    VIR_FORCE_FCLOSE(logFile);
529
    return pid_value;
530 531 532
}

char *
533
vmwareCopyVMXFileName(const char *datastorePath, void *opaque ATTRIBUTE_UNUSED)
534
{
535
    char *path;
536

537
    ignore_value(VIR_STRDUP(path, datastorePath));
538 539
    return path;
}