1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-01-11 09:17:52 +03:00

qemu: remove duplicate code for removing remnant files

This patch also changes functionality a bit.

First if unlinking of old config file is failed we rollback and return error
previously and now we return success. I don't think this makes much difference.
I guess in both cases on libvirtd restart we have to deal with both new and old
config existing on disk with different names but same uuid.

Second if unlinking of old autolink is failed we rollback previously which
was not right as at this point we already unlink old config file. So this
is fixed now.

Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy@virtuozzo.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
Nikolay Shirokovskiy 2020-11-03 10:58:13 +03:00
parent a6c3b5e6a1
commit 1f209eac1d
3 changed files with 42 additions and 31 deletions

View File

@ -11093,3 +11093,32 @@ qemuDomainInterfaceSetDefaultQDisc(virQEMUDriverPtr driver,
return 0;
}
int
qemuDomainNamePathsCleanup(virQEMUDriverConfigPtr cfg,
const char *name,
bool bestEffort)
{
g_autofree char *cfg_file = NULL;
g_autofree char *autostart_link = NULL;
cfg_file = virDomainConfigFile(cfg->configDir, name);
autostart_link = virDomainConfigFile(cfg->autostartDir, name);
if (virFileExists(cfg_file) &&
unlink(cfg_file) < 0) {
virReportSystemError(errno, _("Failed to unlink '%s'"), cfg_file);
if (!bestEffort)
return -1;
}
if (virFileIsLink(autostart_link) == 1 &&
unlink(autostart_link) < 0) {
virReportSystemError(errno, _("Failed to unlink '%s'"), autostart_link);
if (!bestEffort)
return -1;
}
return 0;
}

View File

@ -1050,3 +1050,8 @@ qemuDomainFileWrapperFDClose(virDomainObjPtr vm,
int
qemuDomainInterfaceSetDefaultQDisc(virQEMUDriverPtr driver,
virDomainNetDefPtr net);
int
qemuDomainNamePathsCleanup(virQEMUDriverConfigPtr cfg,
const char *name,
bool bestEffort);

View File

@ -19107,6 +19107,7 @@ qemuDomainRenameCallback(virDomainObjPtr vm,
virObjectEventPtr event_new = NULL;
virObjectEventPtr event_old = NULL;
int ret = -1;
virErrorPtr err = NULL;
g_autofree char *new_dom_name = NULL;
g_autofree char *old_dom_name = NULL;
g_autofree char *new_dom_cfg_file = NULL;
@ -19153,25 +19154,7 @@ qemuDomainRenameCallback(virDomainObjPtr vm,
new_dom_name = NULL;
if (virDomainDefSave(vm->def, driver->xmlopt, cfg->configDir) < 0)
goto rollback;
if (virFileExists(old_dom_cfg_file) &&
unlink(old_dom_cfg_file) < 0) {
virReportSystemError(errno,
_("cannot remove old domain config file %s"),
old_dom_cfg_file);
goto rollback;
}
if (vm->autostart) {
if (virFileIsLink(old_dom_autostart_link) &&
unlink(old_dom_autostart_link) < 0) {
virReportSystemError(errno,
_("Failed to delete symlink '%s'"),
old_dom_autostart_link);
goto rollback;
}
}
goto cleanup;
event_old = virDomainEventLifecycleNew(vm->def->id, old_dom_name, vm->def->uuid,
VIR_DOMAIN_EVENT_UNDEFINED,
@ -19184,23 +19167,17 @@ qemuDomainRenameCallback(virDomainObjPtr vm,
ret = 0;
cleanup:
return ret;
rollback:
if (old_dom_name) {
if (old_dom_name && ret < 0) {
new_dom_name = vm->def->name;
vm->def->name = old_dom_name;
old_dom_name = NULL;
}
if (virFileExists(new_dom_cfg_file))
unlink(new_dom_cfg_file);
if (vm->autostart &&
virFileExists(new_dom_autostart_link))
unlink(new_dom_autostart_link);
goto cleanup;
if (ret < 0)
virErrorPreserveLast(&err);
qemuDomainNamePathsCleanup(cfg, ret < 0 ? new_dom_name : old_dom_name, true);
virErrorRestore(&err);
return ret;
}
static int qemuDomainRename(virDomainPtr dom,