vmware_conf.c 13.2 KB
Newer Older
1
/*---------------------------------------------------------------------------*/
2
/*
3
 * Copyright (C) 2011-2012 Red Hat, Inc.
4
 * Copyright 2010, 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 "virterror_internal.h"
34
#include "vmx.h"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

#include "vmware_conf.h"

/* Free all memory associated with a vmware_driver structure */
void
vmwareFreeDriver(struct vmware_driver *driver)
{
    if (!driver)
        return;

    virMutexDestroy(&driver->lock);
    virDomainObjListDeinit(&driver->domains);
    virCapabilitiesFree(driver->caps);
    VIR_FREE(driver);
}

51

52
static int vmwareDefaultConsoleType(const char *ostype ATTRIBUTE_UNUSED,
53
                                    virArch arch ATTRIBUTE_UNUSED)
54 55 56 57 58
{
    return VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
}


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

67 68
    if ((caps = virCapabilitiesNew(virArchFromHost(),
                                   0, 0)) == NULL)
69 70 71 72 73 74 75 76 77 78
        goto error;

    if (nodeCapsInitNUMA(caps) < 0)
        goto error;

    virCapabilitiesSetMacPrefix(caps, (unsigned char[]) {0x52, 0x54, 0x00});

    /* i686 guests are always supported */
    if ((guest = virCapabilitiesAddGuest(caps,
                                         "hvm",
79
                                         VIR_ARCH_I686,
80 81 82 83 84 85 86 87
                                         NULL, NULL, 0, NULL)) == NULL)
        goto error;

    if (virCapabilitiesAddGuestDomain(guest,
                                      "vmware",
                                      NULL, NULL, 0, NULL) == NULL)
        goto error;

88
    if (VIR_ALLOC(cpu) < 0) {
89 90 91 92
        virReportOOMError();
        goto error;
    }

93
    if (!(cpu->arch = caps->host.arch)) {
94 95 96
        virReportOOMError();
        goto error;
    }
97 98 99 100 101 102 103 104 105 106 107 108
    cpu->type = VIR_CPU_TYPE_HOST;

    if (!(data = cpuNodeData(cpu->arch))
        || cpuDecode(cpu, data, NULL, 0, NULL) < 0) {
        goto error;
    }

    /* x86_64 guests are supported if
     *  - Host arch is x86_64
     * Or
     *  - Host CPU is x86_64 with virtualization extensions
     */
109
    if (caps->host.arch == VIR_ARCH_X86_64 ||
110 111 112
        (cpuHasFeature(caps->host.arch, data, "lm") &&
         (cpuHasFeature(caps->host.arch, data, "vmx") ||
          cpuHasFeature(caps->host.arch, data, "svm")))) {
113 114 115

        if ((guest = virCapabilitiesAddGuest(caps,
                                             "hvm",
116
                                             VIR_ARCH_X86_64,
117 118 119 120 121 122 123 124 125
                                             NULL, NULL, 0, NULL)) == NULL)
            goto error;

        if (virCapabilitiesAddGuestDomain(guest,
                                          "vmware",
                                          NULL, NULL, 0, NULL) == NULL)
            goto error;
    }

126 127
    caps->defaultConsoleTargetType = vmwareDefaultConsoleType;

128 129
cleanup:
    virCPUDefFree(cpu);
130
    cpuDataFree(caps->host.arch, data);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

    return caps;

error:
    virCapabilitiesFree(caps);
    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;
150
    virVMXContext ctx;
151 152 153 154 155
    char *outbuf = NULL;
    char *str;
    char *saveptr = NULL;
    virCommandPtr cmd;

156
    ctx.parseFileName = vmwareCopyVMXFileName;
157 158 159 160 161 162 163 164

    cmd = virCommandNewArgList(VMRUN, "-T",
                               driver->type == TYPE_PLAYER ? "player" : "ws",
                               "list", NULL);
    virCommandSetOutputBuffer(cmd, &outbuf);
    if (virCommandRun(cmd, NULL) < 0)
        goto cleanup;

165
    for (str = outbuf ; (vmxPath = strtok_r(str, "\n", &saveptr)) != NULL;
166 167 168 169 170 171 172 173 174
        str = NULL) {

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

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

        if ((vmdef =
175
             virVMXParseConfig(&ctx, driver->caps, vmx)) == NULL) {
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
            goto cleanup;
        }

        if (!(vm = virDomainAssignDef(driver->caps,
                                      &driver->domains, vmdef, false)))
            goto cleanup;

        pDomain = vm->privateData;

        pDomain->vmxPath = strdup(vmxPath);
        if (pDomain->vmxPath == NULL) {
            virReportOOMError();
            goto cleanup;
        }

        vmwareDomainConfigDisplay(pDomain, vmdef);

        if ((vm->def->id = vmwareExtractPid(vmxPath)) < 0)
            goto cleanup;
        /* vmrun list only reports running vms */
J
Jiri Denemark 已提交
196 197
        virDomainObjSetState(vm, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_UNKNOWN);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        vm->persistent = 1;

        virDomainObjUnlock(vm);

        vmdef = NULL;
        vm = NULL;
    }

    ret = 0;

cleanup:
    virCommandFree(cmd);
    VIR_FREE(outbuf);
    virDomainDefFree(vmdef);
    VIR_FREE(directoryName);
    VIR_FREE(fileName);
    VIR_FREE(vmx);
215
    virObjectUnref(vm);
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    return ret;
}

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

    while (tmp && *tmp) {
        if (*tmp == PROGRAM_SENTINAL) {
            *tmp = key;
            break;
        }
        tmp++;
    }
}

int
vmwareExtractVersion(struct vmware_driver *driver)
{
    unsigned long version = 0;
    char *tmp;
    int ret = -1;
    virCommandPtr cmd;
    char * outbuf = NULL;
    const char * bin = (driver->type == TYPE_PLAYER) ? "vmplayer" : "vmware";
    const char * pattern = (driver->type == TYPE_PLAYER) ?
                "VMware Player " : "VMware Workstation ";

    cmd = virCommandNewArgList(bin, "-v", NULL);
    virCommandSetOutputBuffer(cmd, &outbuf);

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

    if ((tmp = STRSKIP(outbuf, pattern)) == NULL) {
252 253
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to parse %s version"), bin);
254 255 256
        goto cleanup;
    }

257
    if (virParseVersionString(tmp, &version, false) < 0) {
258 259
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("version parsing error"));
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
        goto cleanup;
    }

    driver->version = version;
    ret = 0;

cleanup:
    virCommandFree(cmd);
    VIR_FREE(outbuf);
    return ret;
}

int
vmwareDomainConfigDisplay(vmwareDomainPtr pDomain, virDomainDefPtr def)
{
    int i = 0;

    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;
    }
}

int
vmwareParsePath(char *path, char **directory, char **filename)
{
    char *separator;

    separator = strrchr(path, '/');

    if (separator != NULL) {
        *separator++ = '\0';

        if (*separator == '\0') {
303 304
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("path '%s' doesn't reference a file"), path);
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
            return -1;
        }

        if ((*directory = strdup(path)) == NULL)
            goto no_memory;
        if ((*filename = strdup(separator)) == NULL) {
            VIR_FREE(*directory);
            goto no_memory;
        }

    } else {
        if ((*filename = strdup(path)) == NULL)
            goto no_memory;
    }

    return 0;

no_memory:
    virReportOOMError();
    return -1;
}

int
vmwareConstructVmxPath(char *directoryName, char *name, char **vmxPath)
{
    if (directoryName != NULL) {
        if (virAsprintf(vmxPath, "%s/%s.vmx", directoryName, name) < 0) {
            virReportOOMError();
            return -1;
        }
    } else {
        if (virAsprintf(vmxPath, "%s.vmx", name) < 0) {
            virReportOOMError();
            return -1;
        }
    }
    return 0;
}

int
vmwareVmxPath(virDomainDefPtr vmdef, char **vmxPath)
{
    virDomainDiskDefPtr disk = NULL;
    char *directoryName = NULL;
    char *fileName = NULL;
    int ret = -1;
    int i = 0;

    /*
     * 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
     * first disk, because it may be CDROM disk and ISO images are normaly not
     * located in the virtual machine's directory. This approach
     * isn't perfect but should work in the majority of cases.
     */
    if (vmdef->ndisks < 1) {
361 362 363
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Domain XML doesn't contain any disks, "
                         "cannot deduce datastore and path for VMX file"));
364 365 366 367 368 369 370 371 372 373 374 375
        goto cleanup;
    }

    for (i = 0; i < vmdef->ndisks; ++i) {
        if (vmdef->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK &&
            vmdef->disks[i]->type == VIR_DOMAIN_DISK_TYPE_FILE) {
            disk = vmdef->disks[i];
            break;
        }
    }

    if (disk == NULL) {
376 377 378
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Domain XML doesn't contain any file-based harddisks, "
                         "cannot deduce datastore and path for VMX file"));
379 380 381 382
        goto cleanup;
    }

    if (disk->src == NULL) {
383 384 385
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("First file-based harddisk has no source, cannot "
                         "deduce datastore and path for VMX file"));
386 387 388 389 390 391 392 393
        goto cleanup;
    }

    if (vmwareParsePath(disk->src, &directoryName, &fileName) < 0) {
        goto cleanup;
    }

    if (!virFileHasSuffix(fileName, ".vmdk")) {
394 395 396
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting source '%s' of first file-based harddisk "
                         "to be a VMDK image"), disk->src);
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
        goto cleanup;
    }

    if (vmwareConstructVmxPath(directoryName, vmdef->name, vmxPath) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    ret = 0;

  cleanup:
    VIR_FREE(directoryName);
    VIR_FREE(fileName);
    return ret;
}

int
vmwareMoveFile(char *srcFile, char *dstFile)
{
    const char *cmdmv[] =
        { "mv", PROGRAM_SENTINAL, PROGRAM_SENTINAL, NULL };

    if (!virFileExists(srcFile)) {
420 421
        virReportError(VIR_ERR_INTERNAL_ERROR, _("file %s does not exist"),
                       srcFile);
422 423 424 425 426 427 428 429 430
        return -1;
    }

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

    vmwareSetSentinal(cmdmv, srcFile);
    vmwareSetSentinal(cmdmv, dstFile);
    if (virRun(cmdmv, NULL) < 0) {
431 432
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to move file to %s "), dstFile);
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
        return -1;
    }

    return 0;
}

int
vmwareMakePath(char *srcDir, char *srcName, char *srcExt, char **outpath)
{
    if (virAsprintf(outpath, "%s/%s.%s", srcDir, srcName, srcExt) < 0) {
        virReportOOMError();
        return -1;
    }
    return 0;
}

int
vmwareExtractPid(const char * vmxPath)
{
    char *vmxDir = NULL;
    char *logFilePath = NULL;
    FILE *logFile = NULL;
    char line[1024];
    char *tmp = NULL;
457
    int pid_value = -1;
458 459 460 461 462 463 464 465 466 467 468 469 470 471

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

    if (virAsprintf(&logFilePath, "%s/vmware.log",
                    vmxDir) < 0) {
        virReportOOMError();
        goto cleanup;
    }

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

    if (!fgets(line, sizeof(line), logFile)) {
472 473
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("unable to read vmware log file"));
474 475 476 477
        goto cleanup;
    }

    if ((tmp = strstr(line, " pid=")) == NULL) {
478 479
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot find pid in vmware log file"));
480 481 482 483 484
        goto cleanup;
    }

    tmp += strlen(" pid=");

485 486 487
    /* 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 != ' ') {
488 489
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot parse pid in vmware log file"));
490 491 492 493 494 495 496
        goto cleanup;
    }

cleanup:
    VIR_FREE(vmxDir);
    VIR_FREE(logFilePath);
    VIR_FORCE_FCLOSE(logFile);
497
    return pid_value;
498 499 500
}

char *
501
vmwareCopyVMXFileName(const char *datastorePath, void *opaque ATTRIBUTE_UNUSED)
502 503 504 505 506 507 508 509 510 511
{
    char *path = strdup(datastorePath);

    if (path == NULL) {
        virReportOOMError();
        return NULL;
    }

    return path;
}