testutilsqemu.c 23.9 KB
Newer Older
1
#include <config.h>
A
Atsushi SAKAI 已提交
2
#ifdef WITH_QEMU
3

4
# include "testutilsqemu.h"
P
Pavel Hrdina 已提交
5
# include "testutilshostcpus.h"
6
# include "testutils.h"
7
# include "viralloc.h"
J
Jiri Denemark 已提交
8
# include "cpu_conf.h"
9
# include "qemu/qemu_driver.h"
10
# include "qemu/qemu_domain.h"
11
# define __QEMU_CAPSPRIV_H_ALLOW__
12
# include "qemu/qemu_capspriv.h"
13
# include "virstring.h"
14
# include "virfilecache.h"
15 16

# define VIR_FROM_THIS VIR_FROM_QEMU
17

18 19
virCPUDefPtr cpuDefault;
virCPUDefPtr cpuHaswell;
20
virCPUDefPtr cpuPower8;
21
virCPUDefPtr cpuPower9;
22

23
typedef enum {
24 25 26 27 28 29
    TEST_UTILS_QEMU_BIN_I686,
    TEST_UTILS_QEMU_BIN_X86_64,
    TEST_UTILS_QEMU_BIN_AARCH64,
    TEST_UTILS_QEMU_BIN_ARM,
    TEST_UTILS_QEMU_BIN_PPC64,
    TEST_UTILS_QEMU_BIN_PPC,
L
Lubomir Rintel 已提交
30 31
    TEST_UTILS_QEMU_BIN_RISCV32,
    TEST_UTILS_QEMU_BIN_RISCV64,
32
    TEST_UTILS_QEMU_BIN_S390X
33 34 35
} QEMUBinType;

static const char *QEMUBinList[] = {
36
    "/usr/bin/qemu-system-i686",
37 38 39 40 41
    "/usr/bin/qemu-system-x86_64",
    "/usr/bin/qemu-system-aarch64",
    "/usr/bin/qemu-system-arm",
    "/usr/bin/qemu-system-ppc64",
    "/usr/bin/qemu-system-ppc",
L
Lubomir Rintel 已提交
42 43
    "/usr/bin/qemu-system-riscv32",
    "/usr/bin/qemu-system-riscv64",
44 45 46 47
    "/usr/bin/qemu-system-s390x"
};


48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
static virCapsGuestMachinePtr *testQemuAllocMachines(int *nmachines)
{
    virCapsGuestMachinePtr *machines;
    static const char *const x86_machines[] = {
        "pc", "isapc"
    };

    machines = virCapabilitiesAllocMachines(x86_machines,
                                            ARRAY_CARDINALITY(x86_machines));
    if (machines == NULL)
        return NULL;

    *nmachines = ARRAY_CARDINALITY(x86_machines);

    return machines;
}

M
Mark McLoughlin 已提交
65 66 67 68 69 70 71 72 73 74 75 76
/* Newer versions of qemu have versioned machine types to allow
 * compatibility with older releases.
 * The 'pc' machine type is an alias of the newest machine type.
 */
static virCapsGuestMachinePtr *testQemuAllocNewerMachines(int *nmachines)
{
    virCapsGuestMachinePtr *machines;
    char *canonical;
    static const char *const x86_machines[] = {
        "pc-0.11", "pc", "pc-0.10", "isapc"
    };

77
    if (VIR_STRDUP(canonical, x86_machines[0]) < 0)
M
Mark McLoughlin 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        return NULL;

    machines = virCapabilitiesAllocMachines(x86_machines,
                                            ARRAY_CARDINALITY(x86_machines));
    if (machines == NULL) {
        VIR_FREE(canonical);
        return NULL;
    }

    machines[1]->canonical = canonical;

    *nmachines = ARRAY_CARDINALITY(x86_machines);

    return machines;
}

94

95 96 97 98 99 100 101 102 103 104 105 106 107
static int
testQemuAddI686Guest(virCapsPtr caps)
{
    int nmachines = 0;
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    if (!(machines = testQemuAllocMachines(&nmachines)))
        goto error;

    if (!(guest = virCapabilitiesAddGuest(caps,
                                          VIR_DOMAIN_OSTYPE_HVM,
                                          VIR_ARCH_I686,
108
                                          QEMUBinList[TEST_UTILS_QEMU_BIN_I686],
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
                                          NULL,
                                          nmachines,
                                          machines)))
        goto error;

    if (!virCapabilitiesAddGuestFeature(guest, "cpuselection", true, false))
        goto error;

    machines = NULL;

    if (!virCapabilitiesAddGuestDomain(guest,
                                       VIR_DOMAIN_VIRT_QEMU,
                                       NULL,
                                       NULL,
                                       0,
                                       NULL))
        goto error;

    if (!(machines = testQemuAllocMachines(&nmachines)))
        goto error;

    if (!virCapabilitiesAddGuestDomain(guest,
                                       VIR_DOMAIN_VIRT_KVM,
132
                                       QEMUBinList[TEST_UTILS_QEMU_BIN_I686],
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
                                       NULL,
                                       nmachines,
                                       machines))
        goto error;

    return 0;

 error:
    virCapabilitiesFreeMachines(machines, nmachines);
    return -1;
}


static int
testQemuAddX86_64Guest(virCapsPtr caps)
{
    int nmachines = 0;
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    if (!(machines = testQemuAllocNewerMachines(&nmachines)))
        goto error;

    if (!(guest = virCapabilitiesAddGuest(caps,
                                          VIR_DOMAIN_OSTYPE_HVM,
                                          VIR_ARCH_X86_64,
159
                                          QEMUBinList[TEST_UTILS_QEMU_BIN_X86_64],
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
                                          NULL,
                                          nmachines,
                                          machines)))
        goto error;

    if (!virCapabilitiesAddGuestFeature(guest, "cpuselection", true, false))
        goto error;

    machines = NULL;

    if (!virCapabilitiesAddGuestDomain(guest,
                                       VIR_DOMAIN_VIRT_QEMU,
                                       NULL,
                                       NULL,
                                       0,
                                       NULL))
        goto error;

    if (!(machines = testQemuAllocMachines(&nmachines)))
        goto error;

    if (!virCapabilitiesAddGuestDomain(guest,
                                       VIR_DOMAIN_VIRT_KVM,
183
                                       QEMUBinList[TEST_UTILS_QEMU_BIN_X86_64],
184 185 186 187 188 189 190 191 192
                                       NULL,
                                       nmachines,
                                       machines))
        goto error;

    machines = NULL;

    if (!virCapabilitiesAddGuestDomain(guest,
                                       VIR_DOMAIN_VIRT_KVM,
193
                                       QEMUBinList[TEST_UTILS_QEMU_BIN_X86_64],
194 195 196 197 198 199 200 201 202 203 204 205 206
                                       NULL,
                                       0,
                                       NULL))
        goto error;

    return 0;

 error:
    virCapabilitiesFreeMachines(machines, nmachines);
    return -1;
}


207 208 209 210 211 212 213 214 215 216
static int testQemuAddPPC64Guest(virCapsPtr caps)
{
    static const char *machine[] = { "pseries" };
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    machines = virCapabilitiesAllocMachines(machine, 1);
    if (!machines)
        goto error;

217
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_PPC64,
218
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_PPC64],
219
                                    NULL, 1, machines);
220 221 222
    if (!guest)
        goto error;

223
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
224
        goto error;
225 226 227
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM,
                                       NULL, NULL, 0, NULL))
        goto error;
228 229 230

    return 0;

231
 error:
232 233 234 235 236
    /* No way to free a guest? */
    virCapabilitiesFreeMachines(machines, 1);
    return -1;
}

237 238 239 240 241 242 243 244 245 246
static int testQemuAddPPC64LEGuest(virCapsPtr caps)
{
    static const char *machine[] = { "pseries" };
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    machines = virCapabilitiesAllocMachines(machine, 1);
    if (!machines)
        goto error;

247
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_PPC64LE,
248
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_PPC64],
249
                                    NULL, 1, machines);
250 251 252
    if (!guest)
        goto error;

253
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
254
        goto error;
255 256 257
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM,
                                       NULL, NULL, 0, NULL))
        goto error;
258 259 260 261 262 263 264 265 266

    return 0;

 error:
    /* No way to free a guest? */
    virCapabilitiesFreeMachines(machines, 1);
    return -1;
}

O
Olivia Yin 已提交
267 268 269 270 271
static int testQemuAddPPCGuest(virCapsPtr caps)
{
    static const char *machine[] = { "g3beige",
                                     "mac99",
                                     "prep",
272
                                     "ppce500" };
O
Olivia Yin 已提交
273 274 275 276 277 278 279
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    machines = virCapabilitiesAllocMachines(machine, 1);
    if (!machines)
        goto error;

280
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_PPC,
281
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_PPC],
282
                                    NULL, 1, machines);
O
Olivia Yin 已提交
283 284 285
    if (!guest)
        goto error;

286
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
O
Olivia Yin 已提交
287
        goto error;
288 289 290
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM,
                                       NULL, NULL, 0, NULL))
        goto error;
O
Olivia Yin 已提交
291 292 293

    return 0;

294
 error:
O
Olivia Yin 已提交
295 296 297 298 299
    /* No way to free a guest? */
    virCapabilitiesFreeMachines(machines, 1);
    return -1;
}

L
Lubomir Rintel 已提交
300 301 302 303 304 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 361
static int testQemuAddRISCV32Guest(virCapsPtr caps)
{
    static const char *names[] = { "spike_v1.10",
                                   "spike_v1.9.1",
                                   "sifive_e",
                                   "virt",
                                   "sifive_u" };
    static const int nmachines = ARRAY_CARDINALITY(names);
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    machines = virCapabilitiesAllocMachines(names, nmachines);
    if (!machines)
        goto error;

    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_RISCV32,
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_RISCV32],
                                    NULL, nmachines, machines);
    if (!guest)
        goto error;

    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
        goto error;

    return 0;

 error:
    virCapabilitiesFreeMachines(machines, nmachines);
    return -1;
}

static int testQemuAddRISCV64Guest(virCapsPtr caps)
{
    static const char *names[] = { "spike_v1.10",
                                   "spike_v1.9.1",
                                   "sifive_e",
                                   "virt",
                                   "sifive_u" };
    static const int nmachines = ARRAY_CARDINALITY(names);
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    machines = virCapabilitiesAllocMachines(names, nmachines);
    if (!machines)
        goto error;

    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_RISCV64,
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_RISCV64],
                                    NULL, nmachines, machines);
    if (!guest)
        goto error;

    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
        goto error;

    return 0;

 error:
    virCapabilitiesFreeMachines(machines, nmachines);
    return -1;
}

362 363
static int testQemuAddS390Guest(virCapsPtr caps)
{
364 365
    static const char *s390_machines[] = { "s390-virtio",
                                           "s390-ccw-virtio" };
366 367 368 369 370 371 372 373
    virCapsGuestMachinePtr *machines = NULL;
    virCapsGuestPtr guest;

    machines = virCapabilitiesAllocMachines(s390_machines,
                                            ARRAY_CARDINALITY(s390_machines));
    if (!machines)
        goto error;

374
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_S390X,
375
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_S390X],
376
                                    NULL,
377 378 379 380 381
                                    ARRAY_CARDINALITY(s390_machines),
                                    machines);
    if (!guest)
        goto error;

382
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
383
        goto error;
384 385 386
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM,
                                       NULL, NULL, 0, NULL))
        goto error;
387 388 389

    return 0;

390
 error:
391 392 393 394
    virCapabilitiesFreeMachines(machines, ARRAY_CARDINALITY(s390_machines));
    return -1;
}

S
Stefan Schallenberg 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
static int testQemuAddArm6Guest(virCapsPtr caps)
{
    static const char *machines[] = { "versatilepb" };
    virCapsGuestMachinePtr *capsmachines = NULL;
    virCapsGuestPtr guest;

    capsmachines = virCapabilitiesAllocMachines(machines,
                                                ARRAY_CARDINALITY(machines));
    if (!capsmachines)
        goto error;

    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_ARMV6L,
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_ARM],
                                    NULL,
                                    ARRAY_CARDINALITY(machines),
                                    capsmachines);
    if (!guest)
        goto error;

    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
        goto error;
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM,
                                       NULL, NULL, 0, NULL))
        goto error;

    return 0;

 error:
    virCapabilitiesFreeMachines(capsmachines, ARRAY_CARDINALITY(machines));
    return -1;
}

static int testQemuAddArm7Guest(virCapsPtr caps)
428 429 430 431 432 433 434 435 436 437 438 439
{
    static const char *machines[] = { "vexpress-a9",
                                      "vexpress-a15",
                                      "versatilepb" };
    virCapsGuestMachinePtr *capsmachines = NULL;
    virCapsGuestPtr guest;

    capsmachines = virCapabilitiesAllocMachines(machines,
                                                ARRAY_CARDINALITY(machines));
    if (!capsmachines)
        goto error;

440
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_ARMV7L,
441
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_ARM],
442
                                    NULL,
443 444 445 446 447
                                    ARRAY_CARDINALITY(machines),
                                    capsmachines);
    if (!guest)
        goto error;

448
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
449
        goto error;
450 451 452
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM,
                                       NULL, NULL, 0, NULL))
        goto error;
453 454 455

    return 0;

456
 error:
457 458 459 460
    virCapabilitiesFreeMachines(capsmachines, ARRAY_CARDINALITY(machines));
    return -1;
}

461 462 463 464 465 466 467 468 469 470 471
static int testQemuAddAARCH64Guest(virCapsPtr caps)
{
    static const char *machines[] = { "virt"};
    virCapsGuestMachinePtr *capsmachines = NULL;
    virCapsGuestPtr guest;

    capsmachines = virCapabilitiesAllocMachines(machines,
                                                ARRAY_CARDINALITY(machines));
    if (!capsmachines)
        goto error;

472
    guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_AARCH64,
473
                                    QEMUBinList[TEST_UTILS_QEMU_BIN_AARCH64],
474
                                    NULL,
475 476 477 478 479
                                    ARRAY_CARDINALITY(machines),
                                    capsmachines);
    if (!guest)
        goto error;

480
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL))
481
        goto error;
482 483 484
    if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM,
                                       NULL, NULL, 0, NULL))
        goto error;
485 486 487

    return 0;

488
 error:
489 490 491
    virCapabilitiesFreeMachines(capsmachines, ARRAY_CARDINALITY(machines));
    return -1;
}
492

493 494
virCapsPtr testQemuCapsInit(void)
{
495 496
    virCapsPtr caps;

497
    if (!(caps = virCapabilitiesNew(VIR_ARCH_X86_64, false, false)))
498 499
        return NULL;

500 501 502 503 504 505 506 507 508 509
    /* Add dummy 'none' security_driver. This is equal to setting
     * security_driver = "none" in qemu.conf. */
    if (VIR_ALLOC_N(caps->host.secModels, 1) < 0)
        goto cleanup;
    caps->host.nsecModels = 1;

    if (VIR_STRDUP(caps->host.secModels[0].model, "none") < 0 ||
        VIR_STRDUP(caps->host.secModels[0].doi, "0") < 0)
        goto cleanup;

510
    if (!(cpuDefault = virCPUDefCopy(&cpuDefaultData)) ||
511
        !(cpuHaswell = virCPUDefCopy(&cpuHaswellData)) ||
512 513
        !(cpuPower8 = virCPUDefCopy(&cpuPower8Data)) ||
        !(cpuPower9 = virCPUDefCopy(&cpuPower9Data)))
514 515
        goto cleanup;

516
    qemuTestSetHostCPU(caps, NULL);
517

518 519 520 521 522 523
    /*
     * Build a NUMA topology with cell_id (NUMA node id
     * being 3(0 + 3),4(1 + 3), 5 and 6
     */
    if (virTestCapsBuildNUMATopology(caps, 3) < 0)
        goto cleanup;
524

525
    if (testQemuAddI686Guest(caps) < 0)
526 527
        goto cleanup;

528
    if (testQemuAddX86_64Guest(caps) < 0)
529 530
        goto cleanup;

531 532 533
    if (testQemuAddPPC64Guest(caps))
        goto cleanup;

534 535 536
    if (testQemuAddPPC64LEGuest(caps))
        goto cleanup;

O
Olivia Yin 已提交
537 538 539
    if (testQemuAddPPCGuest(caps))
        goto cleanup;

L
Lubomir Rintel 已提交
540 541 542 543 544 545
    if (testQemuAddRISCV32Guest(caps) < 0)
        goto cleanup;

    if (testQemuAddRISCV64Guest(caps) < 0)
        goto cleanup;

546 547 548
    if (testQemuAddS390Guest(caps))
        goto cleanup;

S
Stefan Schallenberg 已提交
549 550 551 552
    if (testQemuAddArm6Guest(caps))
        goto cleanup;

    if (testQemuAddArm7Guest(caps))
553 554
        goto cleanup;

555 556 557
    if (testQemuAddAARCH64Guest(caps))
        goto cleanup;

558
    if (virTestGetDebug()) {
559 560 561 562 563 564
        char *caps_str;

        caps_str = virCapabilitiesFormatXML(caps);
        if (!caps_str)
            goto cleanup;

565
        VIR_TEST_DEBUG("QEMU driver capabilities:\n%s", caps_str);
566 567 568 569

        VIR_FREE(caps_str);
    }

570 571
    return caps;

572
 cleanup:
573 574 575
    caps->host.cpu = NULL;
    virCPUDefFree(cpuDefault);
    virCPUDefFree(cpuHaswell);
576
    virCPUDefFree(cpuPower8);
577
    virObjectUnref(caps);
578 579
    return NULL;
}
580 581


582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
void
qemuTestSetHostArch(virCapsPtr caps,
                    virArch arch)
{
    if (arch == VIR_ARCH_NONE)
        arch = VIR_ARCH_X86_64;
    caps->host.arch = arch;
    qemuTestSetHostCPU(caps, NULL);
}


void
qemuTestSetHostCPU(virCapsPtr caps,
                   virCPUDefPtr cpu)
{
    virArch arch = caps->host.arch;

    if (!cpu) {
        if (ARCH_IS_X86(arch))
            cpu = cpuDefault;
602 603
        else if (ARCH_IS_PPC64(arch))
            cpu = cpuPower8;
604 605
    }

606
    unsetenv("VIR_TEST_MOCK_FAKE_HOST_CPU");
P
Pavel Hrdina 已提交
607
    if (cpu) {
608
        caps->host.arch = cpu->arch;
P
Pavel Hrdina 已提交
609 610 611
        if (cpu->model)
            setenv("VIR_TEST_MOCK_FAKE_HOST_CPU", cpu->model, 1);
    }
612 613 614 615
    caps->host.cpu = cpu;
}


616
virQEMUCapsPtr
617 618
qemuTestParseCapabilitiesArch(virArch arch,
                              const char *capsFile)
619 620 621
{
    virQEMUCapsPtr qemuCaps = NULL;

622
    if (!(qemuCaps = virQEMUCapsNew()) ||
623
        virQEMUCapsLoadCache(arch, qemuCaps, capsFile) < 0)
624 625 626 627 628 629 630 631
        goto error;

    return qemuCaps;

 error:
    virObjectUnref(qemuCaps);
    return NULL;
}
632

633 634 635 636 637 638 639 640 641 642 643 644

virQEMUCapsPtr
qemuTestParseCapabilities(virCapsPtr caps,
                          const char *capsFile)
{
    if (!caps)
        return NULL;

    return qemuTestParseCapabilitiesArch(caps->host.arch, capsFile);
}


645 646
void qemuTestDriverFree(virQEMUDriver *driver)
{
647
    virMutexDestroy(&driver->lock);
648 649 650 651
    if (driver->config) {
        virFileDeleteTree(driver->config->stateDir);
        virFileDeleteTree(driver->config->configDir);
    }
652
    virObjectUnref(driver->qemuCapsCache);
653 654 655
    virObjectUnref(driver->xmlopt);
    virObjectUnref(driver->caps);
    virObjectUnref(driver->config);
656
    virObjectUnref(driver->securityManager);
657 658
}

659
int qemuTestCapsCacheInsert(virFileCachePtr cache,
660 661
                            virQEMUCapsPtr caps)
{
662 663
    size_t i;
    virQEMUCapsPtr tmpCaps;
664 665

    if (caps) {
666
        tmpCaps = caps;
667
    } else {
668
        if (!(tmpCaps = virQEMUCapsNew()))
669 670 671
            return -ENOMEM;
    }

672 673
    for (i = 0; i < ARRAY_CARDINALITY(QEMUBinList); i++) {
        virObjectRef(tmpCaps);
674
        if (virFileCacheInsertData(cache, QEMUBinList[i], tmpCaps) < 0) {
675 676 677 678
            virObjectUnref(tmpCaps);
            return -1;
        }
    }
679

680 681
    if (!caps)
        virObjectUnref(tmpCaps);
682

683
    return 0;
684 685
}

686

687 688 689
# define STATEDIRTEMPLATE abs_builddir "/qemustatedir-XXXXXX"
# define CONFIGDIRTEMPLATE abs_builddir "/qemuconfigdir-XXXXXX"

690 691
int qemuTestDriverInit(virQEMUDriver *driver)
{
692
    virSecurityManagerPtr mgr = NULL;
693 694
    char statedir[] = STATEDIRTEMPLATE;
    char configdir[] = CONFIGDIRTEMPLATE;
695

696 697
    memset(driver, 0, sizeof(*driver));

698 699 700
    if (virMutexInit(&driver->lock) < 0)
        return -1;

701 702
    driver->config = virQEMUDriverConfigNew(false);
    if (!driver->config)
703
        goto error;
704

705 706 707 708 709
    /* Do this early so that qemuTestDriverFree() doesn't see (unlink) the real
     * dirs. */
    VIR_FREE(driver->config->stateDir);
    VIR_FREE(driver->config->configDir);

710 711 712 713 714 715 716
    /* Overwrite some default paths so it's consistent for tests. */
    VIR_FREE(driver->config->libDir);
    VIR_FREE(driver->config->channelTargetDir);
    if (VIR_STRDUP(driver->config->libDir, "/tmp/lib") < 0 ||
        VIR_STRDUP(driver->config->channelTargetDir, "/tmp/channel") < 0)
        goto error;

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
    if (!mkdtemp(statedir)) {
        virFilePrintf(stderr, "Cannot create fake stateDir");
        goto error;
    }

    if (VIR_STRDUP(driver->config->stateDir, statedir) < 0) {
        rmdir(statedir);
        goto error;
    }

    if (!mkdtemp(configdir)) {
        virFilePrintf(stderr, "Cannot create fake configDir");
        goto error;
    }

    if (VIR_STRDUP(driver->config->configDir, configdir) < 0) {
        rmdir(configdir);
        goto error;
    }

737 738 739 740
    driver->caps = testQemuCapsInit();
    if (!driver->caps)
        goto error;

P
Pavel Fedin 已提交
741 742
    /* Using /dev/null for libDir and cacheDir automatically produces errors
     * upon attempt to use any of them */
743
    driver->qemuCapsCache = virQEMUCapsCacheNew("/dev/null", "/dev/null", 0, 0, 0);
P
Pavel Fedin 已提交
744 745 746
    if (!driver->qemuCapsCache)
        goto error;

747 748 749 750
    driver->xmlopt = virQEMUDriverCreateXMLConf(driver);
    if (!driver->xmlopt)
        goto error;

751
    if (qemuTestCapsCacheInsert(driver->qemuCapsCache, NULL) < 0)
P
Pavel Fedin 已提交
752 753
        goto error;

754
    if (!(mgr = virSecurityManagerNew("none", "qemu",
755 756 757 758 759
                                      VIR_SECURITY_MANAGER_PRIVILEGED)))
        goto error;
    if (!(driver->securityManager = virSecurityManagerNewStack(mgr)))
        goto error;

760 761 762
    return 0;

 error:
763
    virObjectUnref(mgr);
764 765 766
    qemuTestDriverFree(driver);
    return -1;
}
767

768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
int
testQemuCapsSetGIC(virQEMUCapsPtr qemuCaps,
                   int gic)
{
    virGICCapability *gicCapabilities = NULL;
    size_t ngicCapabilities = 0;
    int ret = -1;

    if (VIR_ALLOC_N(gicCapabilities, 2) < 0)
        goto out;

# define IMPL_BOTH \
         VIR_GIC_IMPLEMENTATION_KERNEL|VIR_GIC_IMPLEMENTATION_EMULATED

    if (gic & GIC_V2) {
        gicCapabilities[ngicCapabilities].version = VIR_GIC_VERSION_2;
        gicCapabilities[ngicCapabilities].implementation = IMPL_BOTH;
        ngicCapabilities++;
    }
    if (gic & GIC_V3) {
        gicCapabilities[ngicCapabilities].version = VIR_GIC_VERSION_3;
        gicCapabilities[ngicCapabilities].implementation = IMPL_BOTH;
        ngicCapabilities++;
    }

# undef IMPL_BOTH

    virQEMUCapsSetGICCapabilities(qemuCaps,
                                  gicCapabilities, ngicCapabilities);

    ret = 0;

 out:
    return ret;
}

A
Atsushi SAKAI 已提交
804
#endif
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867


char *
testQemuGetLatestCapsForArch(const char *dirname,
                             const char *arch,
                             const char *suffix)
{
    struct dirent *ent;
    DIR *dir = NULL;
    int rc;
    char *fullsuffix = NULL;
    char *tmp = NULL;
    unsigned long maxver = 0;
    unsigned long ver;
    const char *maxname = NULL;
    char *ret = NULL;

    if (virAsprintf(&fullsuffix, "%s.%s", arch, suffix) < 0)
        goto cleanup;

    if (virDirOpen(&dir, dirname) < 0)
        goto cleanup;

    while ((rc = virDirRead(dir, &ent, dirname)) > 0) {
        VIR_FREE(tmp);

        if ((rc = VIR_STRDUP(tmp, STRSKIP(ent->d_name, "caps_"))) < 0)
            goto cleanup;

        if (rc == 0)
            continue;

        if (virFileStripSuffix(tmp, fullsuffix) != 1)
            continue;

        if (virParseVersionString(tmp, &ver, false) < 0) {
            VIR_TEST_DEBUG("skipping caps file '%s'\n", ent->d_name);
            continue;
        }

        if (ver > maxver) {
            maxname = ent->d_name;
            maxver = ver;
        }
    }

    if (rc < 0)
        goto cleanup;

    if (!maxname) {
        VIR_TEST_VERBOSE("failed to find capabilities for '%s' in '%s'\n",
                         arch, dirname);
        goto cleanup;
    }

    ignore_value(virAsprintf(&ret, "%s/%s", dirname, maxname));

 cleanup:
    VIR_FREE(tmp);
    VIR_FREE(fullsuffix);
    virDirClose(&dir);
    return ret;
}