qemu: Be tolerant to preexisting aliases

In the future, some aliases might be already parsed therefore we
should avoid overwriting them.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Michal Privoznik 2017-10-14 10:16:20 -07:00
parent 215ab7aa03
commit 6bc4a371cf
2 changed files with 54 additions and 6 deletions

View File

@ -93,6 +93,9 @@ qemuAssignDeviceChrAlias(virDomainDefPtr def,
{ {
const char *prefix = NULL; const char *prefix = NULL;
if (chr->info.alias)
return 0;
switch ((virDomainChrDeviceType) chr->deviceType) { switch ((virDomainChrDeviceType) chr->deviceType) {
case VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL: case VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL:
prefix = "parallel"; prefix = "parallel";
@ -128,6 +131,9 @@ qemuAssignDeviceControllerAlias(virDomainDefPtr domainDef,
{ {
const char *prefix = virDomainControllerTypeToString(controller->type); const char *prefix = virDomainControllerTypeToString(controller->type);
if (controller->info.alias)
return 0;
if (controller->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI) { if (controller->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI) {
if (!virQEMUCapsHasPCIMultiBus(qemuCaps, domainDef)) { if (!virQEMUCapsHasPCIMultiBus(qemuCaps, domainDef)) {
/* qemus that don't support multiple PCI buses have /* qemus that don't support multiple PCI buses have
@ -182,6 +188,9 @@ qemuAssignDeviceDiskAlias(virDomainDefPtr def,
const char *prefix = virDomainDiskBusTypeToString(disk->bus); const char *prefix = virDomainDiskBusTypeToString(disk->bus);
int controllerModel = -1; int controllerModel = -1;
if (disk->info.alias)
return 0;
if (disk->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) { if (disk->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
if (disk->bus == VIR_DOMAIN_DISK_BUS_SCSI) { if (disk->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
controllerModel = controllerModel =
@ -223,6 +232,9 @@ qemuAssignDeviceHostdevAlias(virDomainDefPtr def,
char **alias, char **alias,
int idx) int idx)
{ {
if (*alias)
return 0;
if (idx == -1) { if (idx == -1) {
size_t i; size_t i;
@ -259,6 +271,9 @@ qemuAssignDeviceNetAlias(virDomainDefPtr def,
int idx) int idx)
{ {
if (net->info.alias)
return 0;
/* <interface type='hostdev'> uses "hostdevN" as the alias /* <interface type='hostdev'> uses "hostdevN" as the alias
* We must use "-1" as the index because the caller doesn't know * 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 * that we're now looking for a unique hostdevN rather than netN
@ -290,6 +305,9 @@ static int
qemuAssignDeviceFSAlias(virDomainFSDefPtr fss, qemuAssignDeviceFSAlias(virDomainFSDefPtr fss,
int idx) int idx)
{ {
if (fss->info.alias)
return 0;
return virAsprintf(&fss->info.alias, "fs%d", idx); return virAsprintf(&fss->info.alias, "fs%d", idx);
} }
@ -298,6 +316,9 @@ static int
qemuAssignDeviceSoundAlias(virDomainSoundDefPtr sound, qemuAssignDeviceSoundAlias(virDomainSoundDefPtr sound,
int idx) int idx)
{ {
if (sound->info.alias)
return 0;
return virAsprintf(&sound->info.alias, "sound%d", idx); return virAsprintf(&sound->info.alias, "sound%d", idx);
} }
@ -306,6 +327,9 @@ static int
qemuAssignDeviceVideoAlias(virDomainVideoDefPtr video, qemuAssignDeviceVideoAlias(virDomainVideoDefPtr video,
int idx) int idx)
{ {
if (video->info.alias)
return 0;
return virAsprintf(&video->info.alias, "video%d", idx); return virAsprintf(&video->info.alias, "video%d", idx);
} }
@ -314,6 +338,9 @@ static int
qemuAssignDeviceHubAlias(virDomainHubDefPtr hub, qemuAssignDeviceHubAlias(virDomainHubDefPtr hub,
int idx) int idx)
{ {
if (hub->info.alias)
return 0;
return virAsprintf(&hub->info.alias, "hub%d", idx); return virAsprintf(&hub->info.alias, "hub%d", idx);
} }
@ -322,6 +349,9 @@ static int
qemuAssignDeviceSmartcardAlias(virDomainSmartcardDefPtr smartcard, qemuAssignDeviceSmartcardAlias(virDomainSmartcardDefPtr smartcard,
int idx) int idx)
{ {
if (smartcard->info.alias)
return 0;
return virAsprintf(&smartcard->info.alias, "smartcard%d", idx); return virAsprintf(&smartcard->info.alias, "smartcard%d", idx);
} }
@ -330,6 +360,9 @@ static int
qemuAssingDeviceMemballoonAlias(virDomainMemballoonDefPtr memballoon, qemuAssingDeviceMemballoonAlias(virDomainMemballoonDefPtr memballoon,
int idx) int idx)
{ {
if (memballoon->info.alias)
return 0;
return virAsprintf(&memballoon->info.alias, "balloon%d", idx); return virAsprintf(&memballoon->info.alias, "balloon%d", idx);
} }
@ -338,6 +371,9 @@ static int
qemuAssignDeviceTPMAlias(virDomainTPMDefPtr tpm, qemuAssignDeviceTPMAlias(virDomainTPMDefPtr tpm,
int idx) int idx)
{ {
if (tpm->info.alias)
return 0;
return virAsprintf(&tpm->info.alias, "tpm%d", idx); return virAsprintf(&tpm->info.alias, "tpm%d", idx);
} }
@ -347,6 +383,9 @@ qemuAssignDeviceRedirdevAlias(virDomainDefPtr def,
virDomainRedirdevDefPtr redirdev, virDomainRedirdevDefPtr redirdev,
int idx) int idx)
{ {
if (redirdev->info.alias)
return 0;
if (idx == -1) { if (idx == -1) {
size_t i; size_t i;
idx = 0; idx = 0;
@ -376,6 +415,9 @@ qemuAssignDeviceRNGAlias(virDomainDefPtr def,
int maxidx = 0; int maxidx = 0;
int idx; int idx;
if (rng->info.alias)
return 0;
for (i = 0; i < def->nrngs; i++) { for (i = 0; i < def->nrngs; i++) {
if ((idx = qemuDomainDeviceAliasIndex(&def->rngs[i]->info, "rng")) >= maxidx) if ((idx = qemuDomainDeviceAliasIndex(&def->rngs[i]->info, "rng")) >= maxidx)
maxidx = idx + 1; maxidx = idx + 1;
@ -410,6 +452,9 @@ qemuAssignDeviceMemoryAlias(virDomainDefPtr def,
int idx; int idx;
const char *prefix; const char *prefix;
if (mem->info.alias)
return 0;
if (mem->model == VIR_DOMAIN_MEMORY_MODEL_DIMM) if (mem->model == VIR_DOMAIN_MEMORY_MODEL_DIMM)
prefix = "dimm"; prefix = "dimm";
else else
@ -436,6 +481,9 @@ qemuAssignDeviceShmemAlias(virDomainDefPtr def,
virDomainShmemDefPtr shmem, virDomainShmemDefPtr shmem,
int idx) int idx)
{ {
if (shmem->info.alias)
return 0;
if (idx == -1) { if (idx == -1) {
size_t i; size_t i;
idx = 0; idx = 0;
@ -466,6 +514,9 @@ qemuAssignDeviceWatchdogAlias(virDomainWatchdogDefPtr watchdog)
{ {
/* Currently, there's just one watchdog per domain */ /* Currently, there's just one watchdog per domain */
if (watchdog->info.alias)
return 0;
if (VIR_STRDUP(watchdog->info.alias, "watchdog0") < 0) if (VIR_STRDUP(watchdog->info.alias, "watchdog0") < 0)
return -1; return -1;
@ -522,8 +573,7 @@ qemuAssignDeviceAliases(virDomainDefPtr def, virQEMUCapsPtr qemuCaps)
* linked to a NetDef, they will share an info and the alias * linked to a NetDef, they will share an info and the alias
* will already be set, so don't try to set it again. * will already be set, so don't try to set it again.
*/ */
if (!def->hostdevs[i]->info->alias && if (qemuAssignDeviceHostdevAlias(def, &def->hostdevs[i]->info->alias, -1) < 0)
qemuAssignDeviceHostdevAlias(def, &def->hostdevs[i]->info->alias, -1) < 0)
return -1; return -1;
} }
for (i = 0; i < def->nredirdevs; i++) { for (i = 0; i < def->nredirdevs; i++) {

View File

@ -4805,10 +4805,8 @@ qemuDomainDetachThisHostDevice(virQEMUDriverPtr driver,
{ {
int ret = -1; int ret = -1;
if (!detach->info->alias) { if (qemuAssignDeviceHostdevAlias(vm->def, &detach->info->alias, -1) < 0)
if (qemuAssignDeviceHostdevAlias(vm->def, &detach->info->alias, -1) < 0) return -1;
return -1;
}
switch (detach->source.subsys.type) { switch (detach->source.subsys.type) {
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: