qemu_alias.c 22.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * qemu_alias.c: QEMU alias manipulation
 *
 * Copyright (C) 2006-2016 Red Hat, Inc.
 * Copyright (C) 2006 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
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "qemu_alias.h"
#include "viralloc.h"
#include "virlog.h"
#include "virstring.h"

29 30
#define QEMU_DRIVE_HOST_PREFIX "drive-"

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#define VIR_FROM_THIS VIR_FROM_QEMU

VIR_LOG_INIT("qemu.qemu_alias");

int
qemuDomainDeviceAliasIndex(const virDomainDeviceInfo *info,
                           const char *prefix)
{
    int idx;

    if (!info->alias)
        return -1;
    if (!STRPREFIX(info->alias, prefix))
        return -1;

    if (virStrToLong_i(info->alias + strlen(prefix), NULL, 10, &idx) < 0)
        return -1;

    return idx;
}


static ssize_t
qemuGetNextChrDevIndex(virDomainDefPtr def,
                       virDomainChrDefPtr chr,
                       const char *prefix)
{
    const virDomainChrDef **arrPtr;
    size_t cnt;
    size_t i;
    ssize_t idx = 0;
    const char *prefix2 = NULL;

    if (chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE)
        prefix2 = "serial";

    virDomainChrGetDomainPtrs(def, chr->deviceType, &arrPtr, &cnt);

    for (i = 0; i < cnt; i++) {
        ssize_t thisidx;
        if (((thisidx = qemuDomainDeviceAliasIndex(&arrPtr[i]->info, prefix)) < 0) &&
            (prefix2 &&
73 74
             (thisidx = qemuDomainDeviceAliasIndex(&arrPtr[i]->info, prefix2)) < 0))
            continue;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        if (thisidx >= idx)
            idx = thisidx + 1;
    }

    return idx;
}


int
qemuAssignDeviceChrAlias(virDomainDefPtr def,
                         virDomainChrDefPtr chr,
                         ssize_t idx)
{
    const char *prefix = NULL;

90 91 92
    if (chr->info.alias)
        return 0;

93
    switch ((virDomainChrDeviceType)chr->deviceType) {
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    case VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL:
        prefix = "parallel";
        break;

    case VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL:
        prefix = "serial";
        break;

    case VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE:
        prefix = "console";
        break;

    case VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL:
        prefix = "channel";
        break;

    case VIR_DOMAIN_CHR_DEVICE_TYPE_LAST:
        return -1;
    }

    if (idx == -1 && (idx = qemuGetNextChrDevIndex(def, chr, prefix)) < 0)
        return -1;

    return virAsprintf(&chr->info.alias, "%s%zd", prefix, idx);
}


int
qemuAssignDeviceControllerAlias(virDomainDefPtr domainDef,
                                virQEMUCapsPtr qemuCaps,
                                virDomainControllerDefPtr controller)
{
    const char *prefix = virDomainControllerTypeToString(controller->type);

128 129 130
    if (controller->info.alias)
        return 0;

131 132 133 134 135 136
    if (controller->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI) {
        if (!virQEMUCapsHasPCIMultiBus(qemuCaps, domainDef)) {
            /* qemus that don't support multiple PCI buses have
             * hardcoded the name of their single PCI controller as
             * "pci".
             */
137 138
            controller->info.alias = g_strdup("pci");
            return 0;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        } else if (controller->model == VIR_DOMAIN_CONTROLLER_MODEL_PCIE_ROOT) {
            /* The pcie-root controller on Q35 machinetypes uses a
             * different naming convention ("pcie.0"), because it is
             * hardcoded that way in qemu.
             */
            return virAsprintf(&controller->info.alias, "pcie.%d", controller->idx);
        }
        /* All other PCI controllers use the consistent "pci.%u"
         * (including the hardcoded pci-root controller on
         * multibus-capable qemus).
         */
        return virAsprintf(&controller->info.alias, "pci.%d", controller->idx);
    } else if (controller->type == VIR_DOMAIN_CONTROLLER_TYPE_IDE) {
        /* for any machine based on e.g. I440FX or G3Beige, the
         * first (and currently only) IDE controller is an integrated
         * controller hardcoded with id "ide"
         */
156
        if (qemuDomainHasBuiltinIDE(domainDef) &&
157 158 159 160
            controller->idx == 0) {
            controller->info.alias = g_strdup("ide");
            return 0;
        }
161 162 163 164
    } else if (controller->type == VIR_DOMAIN_CONTROLLER_TYPE_SATA) {
        /* for any Q35 machine, the first SATA controller is the
         * integrated one, and it too is hardcoded with id "ide"
         */
165 166 167 168
        if (qemuDomainIsQ35(domainDef) && controller->idx == 0) {
            controller->info.alias = g_strdup("ide");
            return 0;
        }
169 170
    } else if (controller->type == VIR_DOMAIN_CONTROLLER_TYPE_USB) {
        /* first USB device is "usb", others are normal "usb%d" */
171 172 173 174
        if (controller->idx == 0) {
            controller->info.alias = g_strdup("usb");
            return 0;
        }
175 176 177 178 179 180 181 182
    }
    /* all other controllers use the default ${type}${index} naming
     * scheme for alias/id.
     */
    return virAsprintf(&controller->info.alias, "%s%d", prefix, controller->idx);
}


183 184
int
qemuAssignDeviceDiskAlias(virDomainDefPtr def,
185 186
                          virDomainDiskDefPtr disk,
                          virQEMUCapsPtr qemuCaps)
187
{
188
    qemuDomainDiskPrivatePtr diskPriv = QEMU_DOMAIN_DISK_PRIVATE(disk);
189 190 191
    const char *prefix = virDomainDiskBusTypeToString(disk->bus);
    int controllerModel = -1;

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    if (!disk->info.alias) {
        if (disk->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
            if (disk->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
                controllerModel = qemuDomainFindSCSIControllerModel(def,
                                                                    &disk->info);
                if (controllerModel < 0)
                    return -1;
            }

            if (disk->bus != VIR_DOMAIN_DISK_BUS_SCSI ||
                controllerModel == VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSILOGIC) {
                if (virAsprintf(&disk->info.alias, "%s%d-%d-%d", prefix,
                                disk->info.addr.drive.controller,
                                disk->info.addr.drive.bus,
                                disk->info.addr.drive.unit) < 0)
                    return -1;
            } else {
                if (virAsprintf(&disk->info.alias, "%s%d-%d-%d-%d", prefix,
                                disk->info.addr.drive.controller,
                                disk->info.addr.drive.bus,
                                disk->info.addr.drive.target,
                                disk->info.addr.drive.unit) < 0)
                    return -1;
            }
        } else {
            int idx = virDiskNameToIndex(disk->dst);
            if (virAsprintf(&disk->info.alias, "%s-disk%d", prefix, idx) < 0)
219 220
                return -1;
        }
221
    }
222

223 224 225 226
    /* For -blockdev we need to know the qom names of the disk which are based
     * on the alias in qemu. While certain disk types use just the alias, some
     * need the full path into /machine/peripheral as a historical artifact.
     */
227 228
    if (!diskPriv->qomName &&
        virQEMUCapsGet(qemuCaps, QEMU_CAPS_BLOCKDEV)) {
229 230 231 232 233
        switch ((virDomainDiskBus) disk->bus) {
        case VIR_DOMAIN_DISK_BUS_FDC:
        case VIR_DOMAIN_DISK_BUS_IDE:
        case VIR_DOMAIN_DISK_BUS_SATA:
        case VIR_DOMAIN_DISK_BUS_SCSI:
234
            diskPriv->qomName = g_strdup(disk->info.alias);
235 236 237 238 239 240
            break;

        case VIR_DOMAIN_DISK_BUS_VIRTIO:
            if (virAsprintf(&diskPriv->qomName,
                            "/machine/peripheral/%s/virtio-backend",
                            disk->info.alias) < 0)
241
                return -1;
242 243 244 245 246 247 248 249 250 251 252 253 254 255
            break;

        case VIR_DOMAIN_DISK_BUS_USB:
            if (virAsprintf(&diskPriv->qomName,
                            "/machine/peripheral/%s/%s.0/legacy[0]",
                            disk->info.alias, disk->info.alias) < 0)
                return -1;
            break;

        case VIR_DOMAIN_DISK_BUS_XEN:
        case VIR_DOMAIN_DISK_BUS_UML:
        case VIR_DOMAIN_DISK_BUS_SD:
        case VIR_DOMAIN_DISK_BUS_LAST:
            break;
256 257 258 259 260 261 262 263 264
        }
    }

    return 0;
}


int
qemuAssignDeviceHostdevAlias(virDomainDefPtr def,
265
                             char **alias,
266 267
                             int idx)
{
268 269 270
    if (*alias)
        return 0;

271 272
    if (idx == -1) {
        size_t i;
273

274 275 276
        idx = 0;
        for (i = 0; i < def->nhostdevs; i++) {
            int thisidx;
277 278 279 280 281 282 283 284 285 286 287 288

            if ((thisidx = qemuDomainDeviceAliasIndex(def->hostdevs[i]->info, "hostdev")) < 0)
                continue; /* error just means the alias wasn't "hostdevN", but something else */
            if (thisidx >= idx)
                idx = thisidx + 1;
        }
        /* network interfaces can also have a hostdevN alias */
        for (i = 0; i < def->nnets; i++) {
            int thisidx;

            if ((thisidx = qemuDomainDeviceAliasIndex(&def->nets[i]->info, "hostdev")) < 0)
                continue;
289 290 291 292 293
            if (thisidx >= idx)
                idx = thisidx + 1;
        }
    }

294
    if (virAsprintf(alias, "hostdev%d", idx) < 0)
295 296 297 298 299 300 301 302 303 304 305
        return -1;

    return 0;
}


int
qemuAssignDeviceNetAlias(virDomainDefPtr def,
                         virDomainNetDefPtr net,
                         int idx)
{
306 307 308
    if (net->info.alias)
        return 0;

309 310 311 312
    /* <interface type='hostdev'> uses "hostdevN" as the alias
     * We must use "-1" as the index because the caller doesn't know
     * that we're now looking for a unique hostdevN rather than netN
     */
313
    if (virDomainNetResolveActualType(net) == VIR_DOMAIN_NET_TYPE_HOSTDEV)
314 315
        return qemuAssignDeviceHostdevAlias(def, &net->info.alias, -1);

316 317
    if (idx == -1) {
        size_t i;
318

319 320 321 322
        idx = 0;
        for (i = 0; i < def->nnets; i++) {
            int thisidx;

323 324
            if ((thisidx = qemuDomainDeviceAliasIndex(&def->nets[i]->info, "net")) < 0)
                continue; /* failure could be due to "hostdevN" */
325 326 327 328 329 330 331 332 333 334 335
            if (thisidx >= idx)
                idx = thisidx + 1;
        }
    }

    if (virAsprintf(&net->info.alias, "net%d", idx) < 0)
        return -1;
    return 0;
}


336 337 338 339
static int
qemuAssignDeviceFSAlias(virDomainFSDefPtr fss,
                        int idx)
{
340 341 342
    if (fss->info.alias)
        return 0;

343 344 345 346 347 348 349 350
    return virAsprintf(&fss->info.alias, "fs%d", idx);
}


static int
qemuAssignDeviceSoundAlias(virDomainSoundDefPtr sound,
                           int idx)
{
351 352 353
    if (sound->info.alias)
        return 0;

354 355 356 357 358 359 360 361
    return virAsprintf(&sound->info.alias, "sound%d", idx);
}


static int
qemuAssignDeviceVideoAlias(virDomainVideoDefPtr video,
                           int idx)
{
362 363 364
    if (video->info.alias)
        return 0;

365 366 367 368 369 370 371 372
    return virAsprintf(&video->info.alias, "video%d", idx);
}


static int
qemuAssignDeviceHubAlias(virDomainHubDefPtr hub,
                         int idx)
{
373 374 375
    if (hub->info.alias)
        return 0;

376 377 378 379 380 381 382 383
    return virAsprintf(&hub->info.alias, "hub%d", idx);
}


static int
qemuAssignDeviceSmartcardAlias(virDomainSmartcardDefPtr smartcard,
                               int idx)
{
384 385 386
    if (smartcard->info.alias)
        return 0;

387 388 389 390 391 392 393 394
    return virAsprintf(&smartcard->info.alias, "smartcard%d", idx);
}


static int
qemuAssingDeviceMemballoonAlias(virDomainMemballoonDefPtr memballoon,
                                int idx)
{
395 396 397
    if (memballoon->info.alias)
        return 0;

398 399 400 401 402 403 404 405
    return virAsprintf(&memballoon->info.alias, "balloon%d", idx);
}


static int
qemuAssignDeviceTPMAlias(virDomainTPMDefPtr tpm,
                         int idx)
{
406 407 408
    if (tpm->info.alias)
        return 0;

409 410 411 412
    return virAsprintf(&tpm->info.alias, "tpm%d", idx);
}


413 414 415 416 417
int
qemuAssignDeviceRedirdevAlias(virDomainDefPtr def,
                              virDomainRedirdevDefPtr redirdev,
                              int idx)
{
418 419 420
    if (redirdev->info.alias)
        return 0;

421 422 423 424 425
    if (idx == -1) {
        size_t i;
        idx = 0;
        for (i = 0; i < def->nredirdevs; i++) {
            int thisidx;
426 427
            if ((thisidx = qemuDomainDeviceAliasIndex(&def->redirdevs[i]->info, "redir")) < 0)
                continue;
428 429 430 431 432 433 434 435 436 437 438 439
            if (thisidx >= idx)
                idx = thisidx + 1;
        }
    }

    if (virAsprintf(&redirdev->info.alias, "redir%d", idx) < 0)
        return -1;
    return 0;
}


int
440 441
qemuAssignDeviceRNGAlias(virDomainDefPtr def,
                         virDomainRNGDefPtr rng)
442
{
443 444 445 446
    size_t i;
    int maxidx = 0;
    int idx;

447 448 449
    if (rng->info.alias)
        return 0;

450 451 452 453 454 455
    for (i = 0; i < def->nrngs; i++) {
        if ((idx = qemuDomainDeviceAliasIndex(&def->rngs[i]->info, "rng")) >= maxidx)
            maxidx = idx + 1;
    }

    if (virAsprintf(&rng->info.alias, "rng%d", maxidx) < 0)
456 457 458 459 460 461
        return -1;

    return 0;
}


462 463 464 465 466 467 468 469 470 471 472 473
/**
 * qemuAssignDeviceMemoryAlias:
 * @def: domain definition. Necessary only if @oldAlias is true.
 * @mem: memory device definition
 * @oldAlias: Generate the alias according to the order of the device in @def
 *            rather than according to the slot number for legacy reasons.
 *
 * Generates alias for a memory device according to slot number if @oldAlias is
 * false or according to order in @def->mems otherwise.
 *
 * Returns 0 on success, -1 on error.
 */
474 475
int
qemuAssignDeviceMemoryAlias(virDomainDefPtr def,
476 477
                            virDomainMemoryDefPtr mem,
                            bool oldAlias)
478 479 480 481
{
    size_t i;
    int maxidx = 0;
    int idx;
M
Michal Privoznik 已提交
482 483
    const char *prefix;

484 485 486
    if (mem->info.alias)
        return 0;

M
Michal Privoznik 已提交
487 488 489 490
    if (mem->model == VIR_DOMAIN_MEMORY_MODEL_DIMM)
        prefix = "dimm";
    else
        prefix = "nvdimm";
491

492 493
    if (oldAlias) {
        for (i = 0; i < def->nmems; i++) {
M
Michal Privoznik 已提交
494
            if ((idx = qemuDomainDeviceAliasIndex(&def->mems[i]->info, prefix)) >= maxidx)
495 496 497 498
                maxidx = idx + 1;
        }
    } else {
        maxidx = mem->info.addr.dimm.slot;
499 500
    }

M
Michal Privoznik 已提交
501
    if (virAsprintf(&mem->info.alias, "%s%d", prefix, maxidx) < 0)
502 503 504 505 506 507
        return -1;

    return 0;
}


508 509 510 511 512
int
qemuAssignDeviceShmemAlias(virDomainDefPtr def,
                           virDomainShmemDefPtr shmem,
                           int idx)
{
513 514 515
    if (shmem->info.alias)
        return 0;

516 517 518 519 520 521 522
    if (idx == -1) {
        size_t i;
        idx = 0;
        for (i = 0; i < def->nshmems; i++) {
            int thisidx;

            if ((thisidx = qemuDomainDeviceAliasIndex(&def->shmems[i]->info,
523 524
                                                      "shmem")) < 0)
                continue;
525 526 527 528 529 530 531 532 533 534 535 536

            if (thisidx >= idx)
                idx = thisidx + 1;
        }
    }

    if (virAsprintf(&shmem->info.alias, "shmem%d", idx) < 0)
        return -1;
    return 0;
}


M
Michal Privoznik 已提交
537 538 539 540 541
int
qemuAssignDeviceWatchdogAlias(virDomainWatchdogDefPtr watchdog)
{
    /* Currently, there's just one watchdog per domain */

542 543 544
    if (watchdog->info.alias)
        return 0;

545
    watchdog->info.alias = g_strdup("watchdog0");
J
Ján Tomko 已提交
546 547 548 549 550 551 552 553 554

    return 0;
}

int
qemuAssignDeviceInputAlias(virDomainDefPtr def,
                           virDomainInputDefPtr input,
                           int idx)
{
555 556 557
    if (input->info.alias)
        return 0;

J
Ján Tomko 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570
    if (idx == -1) {
        int thisidx;
        size_t i;

        for (i = 0; i < def->ninputs; i++) {
            if ((thisidx = qemuDomainDeviceAliasIndex(&def->inputs[i]->info, "input")) >= idx)
                idx = thisidx + 1;
        }
    }

    if (virAsprintf(&input->info.alias, "input%d", idx) < 0)
        return -1;

M
Michal Privoznik 已提交
571 572 573 574
    return 0;
}


J
Ján Tomko 已提交
575
int
576 577 578 579
qemuAssignDeviceVsockAlias(virDomainVsockDefPtr vsock)
{
    if (vsock->info.alias)
        return 0;
580
    vsock->info.alias = g_strdup("vsock0");
581 582 583 584 585

    return 0;
}


586 587 588 589 590 591
int
qemuAssignDeviceAliases(virDomainDefPtr def, virQEMUCapsPtr qemuCaps)
{
    size_t i;

    for (i = 0; i < def->ndisks; i++) {
592
        if (qemuAssignDeviceDiskAlias(def, def->disks[i], qemuCaps) < 0)
593 594 595
            return -1;
    }
    for (i = 0; i < def->nnets; i++) {
596
        if (qemuAssignDeviceNetAlias(def, def->nets[i], -1) < 0)
597 598 599 600
            return -1;
    }

    for (i = 0; i < def->nfss; i++) {
601
        if (qemuAssignDeviceFSAlias(def->fss[i], i) < 0)
602 603 604
            return -1;
    }
    for (i = 0; i < def->nsounds; i++) {
605
        if (qemuAssignDeviceSoundAlias(def->sounds[i], i) < 0)
606 607 608
            return -1;
    }
    for (i = 0; i < def->nhostdevs; i++) {
609 610 611 612 613
        /* we can't start assigning at 0, since netdevs may have used
         * up some hostdevN entries already. Also if the HostdevDef is
         * linked to a NetDef, they will share an info and the alias
         * will already be set, so don't try to set it again.
         */
614
        if (qemuAssignDeviceHostdevAlias(def, &def->hostdevs[i]->info->alias, -1) < 0)
615 616 617 618 619 620 621
            return -1;
    }
    for (i = 0; i < def->nredirdevs; i++) {
        if (qemuAssignDeviceRedirdevAlias(def, def->redirdevs[i], i) < 0)
            return -1;
    }
    for (i = 0; i < def->nvideos; i++) {
622
        if (qemuAssignDeviceVideoAlias(def->videos[i], i) < 0)
623 624 625 626 627 628 629
            return -1;
    }
    for (i = 0; i < def->ncontrollers; i++) {
        if (qemuAssignDeviceControllerAlias(def, qemuCaps, def->controllers[i]) < 0)
            return -1;
    }
    for (i = 0; i < def->ninputs; i++) {
J
Ján Tomko 已提交
630
        if (qemuAssignDeviceInputAlias(def, def->inputs[i], i) < 0)
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
            return -1;
    }
    for (i = 0; i < def->nparallels; i++) {
        if (qemuAssignDeviceChrAlias(def, def->parallels[i], i) < 0)
            return -1;
    }
    for (i = 0; i < def->nserials; i++) {
        if (qemuAssignDeviceChrAlias(def, def->serials[i], i) < 0)
            return -1;
    }
    for (i = 0; i < def->nchannels; i++) {
        if (qemuAssignDeviceChrAlias(def, def->channels[i], i) < 0)
            return -1;
    }
    for (i = 0; i < def->nconsoles; i++) {
        if (qemuAssignDeviceChrAlias(def, def->consoles[i], i) < 0)
            return -1;
    }
    for (i = 0; i < def->nhubs; i++) {
650
        if (qemuAssignDeviceHubAlias(def->hubs[i], i) < 0)
651 652 653
            return -1;
    }
    for (i = 0; i < def->nshmems; i++) {
654
        if (qemuAssignDeviceShmemAlias(def, def->shmems[i], i) < 0)
655 656 657
            return -1;
    }
    for (i = 0; i < def->nsmartcards; i++) {
658
        if (qemuAssignDeviceSmartcardAlias(def->smartcards[i], i) < 0)
659 660 661
            return -1;
    }
    if (def->watchdog) {
M
Michal Privoznik 已提交
662
        if (qemuAssignDeviceWatchdogAlias(def->watchdog) < 0)
663 664
            return -1;
    }
665 666
    if (def->memballoon &&
        def->memballoon->model != VIR_DOMAIN_MEMBALLOON_MODEL_NONE) {
667
        if (qemuAssingDeviceMemballoonAlias(def->memballoon, 0) < 0)
668 669 670
            return -1;
    }
    for (i = 0; i < def->nrngs; i++) {
671
        if (qemuAssignDeviceRNGAlias(def, def->rngs[i]) < 0)
672 673 674
            return -1;
    }
    if (def->tpm) {
675
        if (qemuAssignDeviceTPMAlias(def->tpm, 0) < 0)
676 677 678
            return -1;
    }
    for (i = 0; i < def->nmems; i++) {
679
        if (qemuAssignDeviceMemoryAlias(NULL, def->mems[i], false) < 0)
680 681
            return -1;
    }
682 683 684 685
    if (def->vsock) {
        if (qemuAssignDeviceVsockAlias(def->vsock) < 0)
            return -1;
    }
686 687 688

    return 0;
}
P
Peter Krempa 已提交
689 690


691
/* qemuAliasDiskDriveFromDisk
692 693 694 695 696 697
 * @disk: Pointer to a disk definition
 *
 * Generate and return an alias for the device disk '-drive'
 *
 * Returns NULL with error or a string containing the alias
 */
P
Peter Krempa 已提交
698
char *
699
qemuAliasDiskDriveFromDisk(const virDomainDiskDef *disk)
P
Peter Krempa 已提交
700 701 702 703
{
    char *ret;

    if (!disk->info.alias) {
704 705
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("disk does not have an alias"));
P
Peter Krempa 已提交
706 707 708
        return NULL;
    }

709 710
    ignore_value(virAsprintf(&ret, "%s%s", QEMU_DRIVE_HOST_PREFIX,
                             disk->info.alias));
P
Peter Krempa 已提交
711 712 713

    return ret;
}
714 715


716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
/* qemuAliasDiskDriveSkipPrefix:
 * @dev_name: Pointer to a const char string
 *
 * If the QEMU_DRIVE_HOST_PREFIX exists in the input string, then
 * increment the pointer and return it
 */
const char *
qemuAliasDiskDriveSkipPrefix(const char *dev_name)
{
    if (STRPREFIX(dev_name, QEMU_DRIVE_HOST_PREFIX))
        dev_name += strlen(QEMU_DRIVE_HOST_PREFIX);
    return dev_name;
}


731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
/* qemuAliasFromHostdev
 * @hostdev: Pointer to host device
 *
 * Generate and return a string containing a drive alias
 */
char *
qemuAliasFromHostdev(const virDomainHostdevDef *hostdev)
{
    char *ret;

    if (!hostdev->info->alias) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("hostdev does not have an alias"));
        return NULL;
    }

    ignore_value(virAsprintf(&ret, "%s-%s",
                 virDomainDeviceAddressTypeToString(hostdev->info->type),
                 hostdev->info->alias));
    return ret;
}


754 755 756 757 758 759 760 761 762 763 764
/* qemuDomainGetMasterKeyAlias:
 *
 * Generate and return the masterKey alias
 *
 * Returns NULL or a string containing the master key alias
 */
char *
qemuDomainGetMasterKeyAlias(void)
{
    char *alias;

765
    alias = g_strdup("masterKey0");
766 767 768

    return alias;
}
769 770 771


/* qemuDomainGetSecretAESAlias:
772 773
 * @srcalias: Source alias used to generate the secret alias
 * @isLuks: True when we are generating a secret for LUKS encrypt/decrypt
774 775 776 777 778 779
 *
 * Generate and return an alias for the encrypted secret
 *
 * Returns NULL or a string containing the alias
 */
char *
780 781
qemuDomainGetSecretAESAlias(const char *srcalias,
                            bool isLuks)
782 783 784 785 786 787 788 789 790
{
    char *alias;

    if (!srcalias) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("encrypted secret alias requires valid source alias"));
        return NULL;
    }

791 792 793 794
    if (isLuks)
        ignore_value(virAsprintf(&alias, "%s-luks-secret0", srcalias));
    else
        ignore_value(virAsprintf(&alias, "%s-secret0", srcalias));
795 796 797

    return alias;
}
798 799


800 801
/* qemuAliasTLSObjFromSrcAlias
 * @srcAlias: Pointer to a source alias string
802 803 804 805
 *
 * Generate and return a string to be used as the TLS object alias
 */
char *
806
qemuAliasTLSObjFromSrcAlias(const char *srcAlias)
807 808 809
{
    char *ret;

810
    ignore_value(virAsprintf(&ret, "obj%s_tls0", srcAlias));
811 812 813

    return ret;
}
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829


/* qemuAliasChardevFromDevAlias:
 * @devAlias: pointer do device alias
 *
 * Generate and return a string to be used as chardev alias.
 */
char *
qemuAliasChardevFromDevAlias(const char *devAlias)
{
    char *ret;

    ignore_value(virAsprintf(&ret, "char%s", devAlias));

    return ret;
}
830 831 832 833 834 835 836 837 838 839


const char *
qemuDomainGetManagedPRAlias(void)
{
    return "pr-helper0";
}


char *
840
qemuDomainGetUnmanagedPRAlias(const char *parentalias)
841 842 843
{
    char *ret;

844
    ignore_value(virAsprintf(&ret, "pr-helper-%s", parentalias));
845 846 847

    return ret;
}
M
Marc-André Lureau 已提交
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864

char *
qemuAliasDBusVMStateFromId(const char *id)
{
    char *ret;
    size_t i;

    if (virAsprintf(&ret, "dbus-vms-%s", id) < 0)
        return NULL;

    for (i = 0; ret[i]; i++) {
        if (ret[i] == ':')
            ret[i] = '_';
    }

    return ret;
}