[libvirt] [PATCH 0/5] domainRename API implementation

This is an effort to implement domain rename API. Presented patch series consists of the following: virDomainRename API implementation for qemu, implementation of the virsh command domrename and the additional support code. The idea behind this endeavor is to provide convenient and safe way to rename a domain. Instead of the: virsh dumpxml domain > domain.xml (change domain name in domain.xml) virsh undefine domain virsh define domain.xml user can simply type: virsh domrename foo bar or call virDomainRename() API and domain "foo" will be renamed to "bar". We currently support only renaming inactive domains without snapshots. Renaming procedure takes care of domain log, config, guest agent path and should be able to recover in case of failure. I've been working on this functionality in collaboration with Michal Privoznik who is my mentor during the GSoC 2015. If you have any questions, ideas or criticism feel free to join the discussion. Tomas Meszaros (5): Introduce virDomainRename API virsh: Implement "domrename" command domain_conf: Introducde virDomainObjListRenameAddNew() & virDomainObjListRenameRemove() Introduce new VIR_DOMAIN_EVENT_DEFINED_RENAMED event qemu: Implement virDomainRename examples/object-events/event-test.c | 2 + include/libvirt/libvirt-domain.h | 3 + src/access/viraccessperm.c | 3 +- src/access/viraccessperm.h | 6 ++ src/conf/domain_conf.c | 35 ++++++++ src/conf/domain_conf.h | 5 ++ src/driver-hypervisor.h | 5 ++ src/libvirt-domain.c | 31 +++++++ src/libvirt_private.syms | 2 + src/libvirt_public.syms | 5 ++ src/qemu/qemu_driver.c | 172 ++++++++++++++++++++++++++++++++++++ src/remote/remote_driver.c | 1 + src/remote/remote_protocol.x | 18 +++- src/remote_protocol-structs | 8 ++ tools/virsh-domain.c | 60 ++++++++++++- tools/virsh.pod | 7 ++ 16 files changed, 360 insertions(+), 3 deletions(-) -- 2.1.0

Also, among with this new API new ACL that restricts rename capability is invented too. Signed-off-by: Tomas Meszaros <exo@tty.sk> --- include/libvirt/libvirt-domain.h | 2 ++ src/access/viraccessperm.c | 3 ++- src/access/viraccessperm.h | 6 ++++++ src/driver-hypervisor.h | 5 +++++ src/libvirt-domain.c | 31 +++++++++++++++++++++++++++++++ src/libvirt_public.syms | 5 +++++ src/remote/remote_driver.c | 1 + src/remote/remote_protocol.x | 18 +++++++++++++++++- src/remote_protocol-structs | 8 ++++++++ 9 files changed, 77 insertions(+), 2 deletions(-) diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h index e8202cf..2ddc47d 100644 --- a/include/libvirt/libvirt-domain.h +++ b/include/libvirt/libvirt-domain.h @@ -3837,4 +3837,6 @@ int virDomainSetUserPassword(virDomainPtr dom, const char *password, unsigned int flags); +int virDomainRename(virDomainPtr dom, const char *new_name); + #endif /* __VIR_LIBVIRT_DOMAIN_H__ */ diff --git a/src/access/viraccessperm.c b/src/access/viraccessperm.c index 0f58290..bdc7f60 100644 --- a/src/access/viraccessperm.c +++ b/src/access/viraccessperm.c @@ -43,7 +43,8 @@ VIR_ENUM_IMPL(virAccessPermDomain, "fs_trim", "fs_freeze", "block_read", "block_write", "mem_read", "open_graphics", "open_device", "screenshot", - "open_namespace", "set_time", "set_password"); + "open_namespace", "set_time", "set_password", + "rename"); VIR_ENUM_IMPL(virAccessPermInterface, VIR_ACCESS_PERM_INTERFACE_LAST, diff --git a/src/access/viraccessperm.h b/src/access/viraccessperm.h index 1817da7..6ae4ee7 100644 --- a/src/access/viraccessperm.h +++ b/src/access/viraccessperm.h @@ -306,6 +306,12 @@ typedef enum { */ VIR_ACCESS_PERM_DOMAIN_SET_PASSWORD, + /** + * @desc: Rename domain + * @message: Renaming the domain requires authorization + */ + VIR_ACCESS_PERM_DOMAIN_RENAME, + VIR_ACCESS_PERM_DOMAIN_LAST, } virAccessPermDomain; diff --git a/src/driver-hypervisor.h b/src/driver-hypervisor.h index 3275343..e8c8c2a 100644 --- a/src/driver-hypervisor.h +++ b/src/driver-hypervisor.h @@ -650,6 +650,10 @@ typedef int (*virDrvDomainIsActive)(virDomainPtr dom); typedef int +(*virDrvDomainRename)(virDomainPtr dom, + const char *new_name); + +typedef int (*virDrvDomainIsPersistent)(virDomainPtr dom); typedef int @@ -1347,6 +1351,7 @@ struct _virHypervisorDriver { virDrvConnectIsEncrypted connectIsEncrypted; virDrvConnectIsSecure connectIsSecure; virDrvDomainIsActive domainIsActive; + virDrvDomainRename domainRename; virDrvDomainIsPersistent domainIsPersistent; virDrvDomainIsUpdated domainIsUpdated; virDrvConnectCompareCPU connectCompareCPU; diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c index 837933f..c200965 100644 --- a/src/libvirt-domain.c +++ b/src/libvirt-domain.c @@ -8774,6 +8774,37 @@ virDomainIsPersistent(virDomainPtr dom) return -1; } +/** + * virDomainRename: + * @dom: pointer to the domain object + * @new_name: new domain name + * + * Rename an inactive domain. New domain name is specified in the second + * argument. + * + * Returns 0 if renamed, -1 on error + */ +int +virDomainRename(virDomainPtr dom, const char *new_name) +{ + VIR_DEBUG("dom=%p, new_name=%s", dom, NULLSTR(new_name)); + + virResetLastError(); + virCheckDomainReturn(dom, -1); + virCheckNonNullArgGoto(new_name, error); + + if (dom->conn->driver->domainRename) { + int ret = dom->conn->driver->domainRename(dom, new_name); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(dom->conn); + return -1; +} /** * virDomainIsUpdated: diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms index 2c653f2..dd94191 100644 --- a/src/libvirt_public.syms +++ b/src/libvirt_public.syms @@ -720,4 +720,9 @@ LIBVIRT_1.2.17 { virTypedParamsAddStringList; } LIBVIRT_1.2.16; +LIBVIRT_1.2.19 { + global: + virDomainRename; +} LIBVIRT_1.2.17; + # .... define new API here using predicted next version number .... diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 5c4cf7c..ec26ebe 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -8391,6 +8391,7 @@ static virHypervisorDriver hypervisor_driver = { .domainGetFSInfo = remoteDomainGetFSInfo, /* 1.2.11 */ .domainInterfaceAddresses = remoteDomainInterfaceAddresses, /* 1.2.14 */ .domainSetUserPassword = remoteDomainSetUserPassword, /* 1.2.16 */ + .domainRename = remoteDomainRename, /* 1.2.19 */ }; static virNetworkDriver network_driver = { diff --git a/src/remote/remote_protocol.x b/src/remote/remote_protocol.x index 9f1be6b..0f26793 100644 --- a/src/remote/remote_protocol.x +++ b/src/remote/remote_protocol.x @@ -3230,6 +3230,14 @@ struct remote_domain_set_user_password_args { unsigned int flags; }; +struct remote_domain_rename_args { + remote_nonnull_domain dom; + remote_string new_name; +}; + +struct remote_domain_rename_ret { + int rename; +}; /*----- Protocol. -----*/ @@ -5696,5 +5704,13 @@ enum remote_procedure { * @generate:both * @acl: domain:set_password */ - REMOTE_PROC_DOMAIN_SET_USER_PASSWORD = 357 + REMOTE_PROC_DOMAIN_SET_USER_PASSWORD = 357, + + /** + * @generate: both + * @acl: domain:rename + * @acl: domain:write + * @acl: domain:save + */ + REMOTE_PROC_DOMAIN_RENAME = 358 }; diff --git a/src/remote_protocol-structs b/src/remote_protocol-structs index 48c3bd8..1fdaa55 100644 --- a/src/remote_protocol-structs +++ b/src/remote_protocol-structs @@ -2684,6 +2684,13 @@ struct remote_domain_set_user_password_args { remote_string password; u_int flags; }; +struct remote_domain_rename_args { + remote_nonnull_domain dom; + remote_string new_name; +}; +struct remote_domain_rename_ret { + int rename; +}; enum remote_procedure { REMOTE_PROC_CONNECT_OPEN = 1, REMOTE_PROC_CONNECT_CLOSE = 2, @@ -3042,4 +3049,5 @@ enum remote_procedure { REMOTE_PROC_DOMAIN_ADD_IOTHREAD = 355, REMOTE_PROC_DOMAIN_DEL_IOTHREAD = 356, REMOTE_PROC_DOMAIN_SET_USER_PASSWORD = 357, + REMOTE_PROC_DOMAIN_RENAME = 358, }; -- 2.1.0

On Wed, Aug 05, 2015 at 01:59:07PM +0200, Tomas Meszaros wrote:
Also, among with this new API new ACL that restricts rename capability is invented too.
Signed-off-by: Tomas Meszaros <exo@tty.sk> --- include/libvirt/libvirt-domain.h | 2 ++ src/access/viraccessperm.c | 3 ++- src/access/viraccessperm.h | 6 ++++++ src/driver-hypervisor.h | 5 +++++ src/libvirt-domain.c | 31 +++++++++++++++++++++++++++++++ src/libvirt_public.syms | 5 +++++ src/remote/remote_driver.c | 1 + src/remote/remote_protocol.x | 18 +++++++++++++++++- src/remote_protocol-structs | 8 ++++++++ 9 files changed, 77 insertions(+), 2 deletions(-)
diff --git a/src/remote/remote_protocol.x b/src/remote/remote_protocol.x index 9f1be6b..0f26793 100644 --- a/src/remote/remote_protocol.x +++ b/src/remote/remote_protocol.x @@ -3230,6 +3230,14 @@ struct remote_domain_set_user_password_args { unsigned int flags; };
+struct remote_domain_rename_args { + remote_nonnull_domain dom; + remote_string new_name; +}; + +struct remote_domain_rename_ret { + int rename; +};
/*----- Protocol. -----*/
@@ -5696,5 +5704,13 @@ enum remote_procedure { * @generate:both * @acl: domain:set_password */ - REMOTE_PROC_DOMAIN_SET_USER_PASSWORD = 357 + REMOTE_PROC_DOMAIN_SET_USER_PASSWORD = 357, + + /** + * @generate: both + * @acl: domain:rename + * @acl: domain:write + * @acl: domain:save
When you require write + save you have already given away the keys to the kingdom. Adding a rename permission doesn't really have any benefit at that point. So I'd just get rid of the new rename permission. Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

This patch implements new virsh command, domrename. Using domrename, it will be possible to rename domain from the virsh shell by calling virRenameDomain API. It takes two arguments, current domain name and new domain name. Example: virsh # list --all Id Name State ---------------------------------------------------- - bar shut off virsh # domrename bar foo Domain successfully renamed virsh # list --all Id Name State ---------------------------------------------------- - foo shut off virsh # Signed-off-by: Tomas Meszaros <exo@tty.sk> --- tools/virsh-domain.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/virsh.pod | 7 +++++++ 2 files changed, 64 insertions(+) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index a61656f..c7e218f 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -9724,6 +9724,57 @@ cmdDomname(vshControl *ctl, const vshCmd *cmd) } /* + * "domrename" command + */ +static const vshCmdInfo info_domrename[] = { + {.name = "help", + .data = N_("rename a domain") + }, + {.name = "desc", + .data = "Rename an inactive domain." + }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_domrename[] = { + {.name = "domain", + .type = VSH_OT_DATA, + .flags = VSH_OFLAG_REQ, + .help = N_("domain name, id or uuid") + }, + {.name = "new-name", + .type = VSH_OT_DATA, + .flags = VSH_OFLAG_REQ, + .help = N_("new domain name") + }, + {.name = NULL} +}; + +static bool +cmdDomrename(vshControl *ctl, const vshCmd *cmd) +{ + virDomainPtr dom; + const char *new_name = NULL; + bool ret = false; + + if (!(dom = vshCommandOptDomain(ctl, cmd, NULL))) + return ret; + + if (vshCommandOptStringReq(ctl, cmd, "new-name", &new_name) < 0) + goto cleanup; + + if (virDomainRename(dom, new_name) < 0) + goto cleanup; + + vshPrint(ctl, "Domain successfully renamed\n"); + ret = true; + + cleanup: + virDomainFree(dom); + return false; +} + +/* * "domid" command */ static const vshCmdInfo info_domid[] = { @@ -13080,6 +13131,12 @@ const vshCmdDef domManagementCmds[] = { .info = info_domname, .flags = 0 }, + {.name = "domrename", + .handler = cmdDomrename, + .opts = opts_domrename, + .info = info_domrename, + .flags = 0 + }, {.name = "dompmsuspend", .handler = cmdDomPMSuspend, .opts = opts_dom_pm_suspend, diff --git a/tools/virsh.pod b/tools/virsh.pod index 5ee9a96..c049f8f 100644 --- a/tools/virsh.pod +++ b/tools/virsh.pod @@ -1258,6 +1258,13 @@ on both of them). Convert a domain Id (or UUID) to domain name +=item B<domrename> I<domain> I<new-name> + +Rename a domain. This command changes current domain name to the new name +specified in the second argument. + +B<Note>: Domain must be inactive and without snapshots. + =item B<domstate> I<domain> [I<--reason>] Returns state about a domain. I<--reason> tells virsh to also print -- 2.1.0

We just need to update the entry in the second hash table. Since commit 8728a56 we have two hash tables for the domain list so that we can do O(1) lookup regardless of looking up by UUID or name. Since with renaming a domain UUID does not change, we only need to update the second hash table, where domains are referenced by their name. We will call both functions from the qemuDomainRename(). Signed-off-by: Tomas Meszaros <exo@tty.sk> --- src/conf/domain_conf.c | 35 +++++++++++++++++++++++++++++++++++ src/conf/domain_conf.h | 5 +++++ src/libvirt_private.syms | 2 ++ 3 files changed, 42 insertions(+) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 6b557d1..c47ccf9 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -2848,6 +2848,41 @@ virDomainObjPtr virDomainObjListAdd(virDomainObjListPtr doms, return ret; } + +int +virDomainObjListRenameAddNew(virDomainObjListPtr doms, + virDomainObjPtr vm, + const char *name) +{ + int ret = -1; + virObjectLock(doms); + + /* Add new name into the hash table of domain names. */ + if (virHashAddEntry(doms->objsName, name, vm) < 0) + goto cleanup; + + /* Okay, this is crazy. virHashAddEntry() does not increment + * the refcounter of @vm, but virHashRemoveEntry() does + * decrement it. We need to work around it. */ + virObjectRef(vm); + + ret = 0; + cleanup: + virObjectUnlock(doms); + return ret; +} + + +int +virDomainObjListRenameRemove(virDomainObjListPtr doms, const char *name) +{ + virObjectLock(doms); + virHashRemoveEntry(doms->objsName, name); + virObjectUnlock(doms); + return 0; +} + + /* * Mark the running VM config as transient. Ensures transient hotplug * operations do not persist past shutdown. diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 0fe6b1a..719018f 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -2545,6 +2545,11 @@ virDomainObjPtr virDomainObjListAdd(virDomainObjListPtr doms, virDomainXMLOptionPtr xmlopt, unsigned int flags, virDomainDefPtr *oldDef); +int virDomainObjListRenameAddNew(virDomainObjListPtr doms, + virDomainObjPtr vm, + const char *name); +int virDomainObjListRenameRemove(virDomainObjListPtr doms, + const char *name); void virDomainObjAssignDef(virDomainObjPtr domain, virDomainDefPtr def, bool live, diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ff322d6..70ef880 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -408,6 +408,8 @@ virDomainObjListNew; virDomainObjListNumOfDomains; virDomainObjListRemove; virDomainObjListRemoveLocked; +virDomainObjListRenameAddNew; +virDomainObjListRenameRemove; virDomainObjNew; virDomainObjParseNode; virDomainObjSetDefTransient; -- 2.1.0

This should be emitted whenever a domain is renamed. Signed-off-by: Tomas Meszaros <exo@tty.sk> --- examples/object-events/event-test.c | 2 ++ include/libvirt/libvirt-domain.h | 1 + tools/virsh-domain.c | 3 ++- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/object-events/event-test.c b/examples/object-events/event-test.c index 4f17273..c62bd56 100644 --- a/examples/object-events/event-test.c +++ b/examples/object-events/event-test.c @@ -108,6 +108,8 @@ static const char *eventDetailToString(int event, int detail) { ret = "Added"; else if (detail == VIR_DOMAIN_EVENT_DEFINED_UPDATED) ret = "Updated"; + else if (detail == VIR_DOMAIN_EVENT_DEFINED_RENAMED) + ret = "Renamed"; break; case VIR_DOMAIN_EVENT_UNDEFINED: if (detail == VIR_DOMAIN_EVENT_UNDEFINED_REMOVED) diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h index 2ddc47d..d43ec27 100644 --- a/include/libvirt/libvirt-domain.h +++ b/include/libvirt/libvirt-domain.h @@ -2322,6 +2322,7 @@ typedef enum { typedef enum { VIR_DOMAIN_EVENT_DEFINED_ADDED = 0, /* Newly created config file */ VIR_DOMAIN_EVENT_DEFINED_UPDATED = 1, /* Changed config file */ + VIR_DOMAIN_EVENT_DEFINED_RENAMED = 2, /* Domain was renamed */ # ifdef VIR_ENUM_SENTINELS VIR_DOMAIN_EVENT_DEFINED_LAST diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index c7e218f..7291980 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -11763,7 +11763,8 @@ VIR_ENUM_DECL(vshDomainEventDefined) VIR_ENUM_IMPL(vshDomainEventDefined, VIR_DOMAIN_EVENT_DEFINED_LAST, N_("Added"), - N_("Updated")) + N_("Updated"), + N_("Renamed")) VIR_ENUM_DECL(vshDomainEventUndefined) VIR_ENUM_IMPL(vshDomainEventUndefined, -- 2.1.0

On Wed, Aug 05, 2015 at 01:59:10PM +0200, Tomas Meszaros wrote:
This should be emitted whenever a domain is renamed.
Signed-off-by: Tomas Meszaros <exo@tty.sk> --- examples/object-events/event-test.c | 2 ++ include/libvirt/libvirt-domain.h | 1 + tools/virsh-domain.c | 3 ++- 3 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/examples/object-events/event-test.c b/examples/object-events/event-test.c index 4f17273..c62bd56 100644 --- a/examples/object-events/event-test.c +++ b/examples/object-events/event-test.c @@ -108,6 +108,8 @@ static const char *eventDetailToString(int event, int detail) { ret = "Added"; else if (detail == VIR_DOMAIN_EVENT_DEFINED_UPDATED) ret = "Updated"; + else if (detail == VIR_DOMAIN_EVENT_DEFINED_RENAMED) + ret = "Renamed"; break; case VIR_DOMAIN_EVENT_UNDEFINED: if (detail == VIR_DOMAIN_EVENT_UNDEFINED_REMOVED) diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h index 2ddc47d..d43ec27 100644 --- a/include/libvirt/libvirt-domain.h +++ b/include/libvirt/libvirt-domain.h @@ -2322,6 +2322,7 @@ typedef enum { typedef enum { VIR_DOMAIN_EVENT_DEFINED_ADDED = 0, /* Newly created config file */ VIR_DOMAIN_EVENT_DEFINED_UPDATED = 1, /* Changed config file */ + VIR_DOMAIN_EVENT_DEFINED_RENAMED = 2, /* Domain was renamed */
# ifdef VIR_ENUM_SENTINELS VIR_DOMAIN_EVENT_DEFINED_LAST diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index c7e218f..7291980 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -11763,7 +11763,8 @@ VIR_ENUM_DECL(vshDomainEventDefined) VIR_ENUM_IMPL(vshDomainEventDefined, VIR_DOMAIN_EVENT_DEFINED_LAST, N_("Added"), - N_("Updated")) + N_("Updated"), + N_("Renamed"))
If we're going to emit a defined + renamed event for the new name, we should probably also emit an undefined + renamed event for the old one. That should ensure existing apps continue to work right Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

Currently supports only renaming inactive domains without snapshots. Signed-off-by: Tomas Meszaros <exo@tty.sk> --- src/qemu/qemu_driver.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index b9278f8..3ff04fe 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -19835,6 +19835,177 @@ qemuDomainSetUserPassword(virDomainPtr dom, } +static int qemuDomainRename(virDomainPtr dom, + const char *new_name) +{ + virQEMUDriverPtr driver = dom->conn->privateData; + virQEMUDriverConfigPtr cfg = NULL; + virDomainObjPtr vm; + virObjectEventPtr event = NULL; + int ret = -1; + int logfile = -1; + size_t i; + char ebuf[1024]; + char *timestamp; + char *rename_log_msg = NULL; + char *new_dom_name = NULL; + char *old_dom_name = NULL; + char *old_dom_cfg_file = NULL; + char *new_guest_path = NULL; + char *old_guest_path = NULL; + virDomainChrDefPtr agent = NULL; + + if (VIR_STRDUP(new_dom_name, new_name) < 0) + goto cleanup; + + if (!(vm = qemuDomObjFromDomain(dom))) + goto cleanup; + + if (virDomainRenameEnsureACL(dom->conn, vm->def) < 0) + goto cleanup; + + cfg = virQEMUDriverGetConfig(driver); + + if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0) + goto cleanup; + + if (virDomainObjIsActive(vm)) { + virReportError(VIR_ERR_OPERATION_INVALID, "%s", + _("cannot rename active domain")); + goto endjob; + } + + if (!vm->persistent) { + virReportError(VIR_ERR_OPERATION_INVALID, "%s", + _("cannot rename a transient domain")); + goto endjob; + } + + if (virDomainObjGetState(vm, NULL) != VIR_DOMAIN_SHUTOFF) { + virReportError(VIR_ERR_OPERATION_INVALID, + "%s", _("domain has to be shutoff before renaming")); + goto endjob; + } + + if (virDomainSnapshotObjListNum(vm->snapshots, NULL, 0) > 0) { + virReportError(VIR_ERR_OPERATION_INVALID, + "%s", _("cannot rename domain with snapshots")); + goto endjob; + } + + if (virAsprintf(&rename_log_msg, ": domain %s has been renamed to %s\n", + vm->def->name, new_name) < 0) { + goto endjob; + } + + if (!(old_dom_cfg_file = virDomainConfigFile(cfg->configDir, + vm->def->name))) { + goto endjob; + } + + if (virDomainObjListRenameAddNew(driver->domains, vm, new_name) < 0) + goto endjob; + + if ((logfile = qemuDomainCreateLog(driver, vm, true)) < 0) + goto rollback; + + /* Switch name in domain definition. */ + old_dom_name = vm->def->name; + vm->def->name = new_dom_name; + new_dom_name = NULL; + + /* Change guest agent path. */ + for (i = 0; i < vm->def->nchannels; i++) { + virDomainChrDefPtr channel = vm->def->channels[i]; + if (channel->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL && + channel->targetType == VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_VIRTIO && + channel->source.data.file.path && + channel->target.name) { + + if (virAsprintf(&new_guest_path, "%s/%s.%s", + cfg->channelTargetDir, + new_name, channel->target.name) < 0) + goto rollback; + + old_guest_path = channel->source.data.file.path; + channel->source.data.file.path = new_guest_path; + new_guest_path = NULL; + agent = channel; + break; + } + } + + + if (virDomainSaveConfig(cfg->configDir, vm->def) < 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; + } + + /* Remove old domain name from table. */ + virDomainObjListRenameRemove(driver->domains, old_dom_name); + + event = virDomainEventLifecycleNewFromObj(vm, + VIR_DOMAIN_EVENT_DEFINED, + VIR_DOMAIN_EVENT_DEFINED_RENAMED); + + /* Write message to the log. */ + if ((timestamp = virTimeStringNow()) != NULL) { + if (safewrite(logfile, timestamp, strlen(timestamp)) < 0 || + safewrite(logfile, rename_log_msg, + strlen(rename_log_msg)) < 0) { + VIR_WARN("Unable to write timestamp to logfile: %s", + virStrerror(errno, ebuf, sizeof(ebuf))); + } + VIR_FREE(timestamp); + } + + /* Success, domain has been renamed. */ + ret = 0; + + endjob: + qemuDomainObjEndJob(driver, vm); + + cleanup: + if (VIR_CLOSE(logfile) < 0) { + VIR_WARN("Unable to close logfile: %s", + virStrerror(errno, ebuf, sizeof(ebuf))); + } + virDomainObjEndAPI(&vm); + VIR_FREE(old_dom_cfg_file); + VIR_FREE(old_dom_name); + VIR_FREE(new_dom_name); + VIR_FREE(rename_log_msg); + VIR_FREE(new_guest_path); + VIR_FREE(old_guest_path); + if (event) + qemuDomainEventQueue(driver, event); + virObjectUnref(cfg); + return ret; + + + rollback: + if (old_dom_name) { + new_dom_name = vm->def->name; + vm->def->name = old_dom_name; + old_dom_name = NULL; + } + + if (agent) { + new_guest_path = agent->source.data.file.path; + agent->source.data.file.path = old_guest_path; + old_guest_path = NULL; + } + + virDomainObjListRenameRemove(driver->domains, new_name); + goto endjob; +} + static virHypervisorDriver qemuHypervisorDriver = { .name = QEMU_DRIVER_NAME, .connectOpen = qemuConnectOpen, /* 0.2.0 */ @@ -20042,6 +20213,7 @@ static virHypervisorDriver qemuHypervisorDriver = { .domainGetFSInfo = qemuDomainGetFSInfo, /* 1.2.11 */ .domainInterfaceAddresses = qemuDomainInterfaceAddresses, /* 1.2.14 */ .domainSetUserPassword = qemuDomainSetUserPassword, /* 1.2.16 */ + .domainRename = qemuDomainRename, /* 1.2.19 */ }; -- 2.1.0

On Wed, Aug 05, 2015 at 01:59:11PM +0200, Tomas Meszaros wrote:
Currently supports only renaming inactive domains without snapshots.
Signed-off-by: Tomas Meszaros <exo@tty.sk> --- src/qemu/qemu_driver.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+)
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index b9278f8..3ff04fe 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -19835,6 +19835,177 @@ qemuDomainSetUserPassword(virDomainPtr dom, }
+static int qemuDomainRename(virDomainPtr dom, + const char *new_name)
+ /* Change guest agent path. */ + for (i = 0; i < vm->def->nchannels; i++) { + virDomainChrDefPtr channel = vm->def->channels[i]; + if (channel->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL && + channel->targetType == VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_VIRTIO && + channel->source.data.file.path && + channel->target.name) { + + if (virAsprintf(&new_guest_path, "%s/%s.%s", + cfg->channelTargetDir, + new_name, channel->target.name) < 0) + goto rollback; + + old_guest_path = channel->source.data.file.path; + channel->source.data.file.path = new_guest_path; + new_guest_path = NULL; + agent = channel; + break; + } + }
IMHO, we should not touch this at all. It is making a (potentially invalid) assumption about the way the socket paths are named. If we go down this route of changing various aspects of device config, then people will question why channels are special cased and not renaming other things. It just will just end up as a mess of hard coded policy which works for some cases and is wrong for others. So I think we should just stick to changing the name of the VM only and not touch any other aspect of XML config. Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
participants (2)
-
Daniel P. Berrange
-
Tomas Meszaros