Re: [libvirt] [PATCH] virsh: Implement command to rename domain

2014-09-02 Thread Michal Privoznik

On 01.09.2014 10:31, Philipp Hahn wrote:

Hello Tomas,

On 01.09.2014 01:51, Tomas Meszaros wrote:

I've recently worked with rather large number of virtual machines
and needed to rename all domains. I couldn't find better way how
to rename domain other than:

 virsh dumpxml domain  domain.xml
 (change domain name in domain.xml)
 virsh undefine domain
 virsh define domain.xml


1: ^^^



This is rather pain to do every time I want to rename domain.
I think there should be simple way to change domain name.


This has been requested in the past already (even by me ;-)
Renaming is not that simple, as there are several more things to do:
1. Rename log files (this was somehow controversial last time it was
discussed, especially combined with external programs like logrotate)


I don't find this troublesome. I mean, it's desired for a domain A to 
have logs in A.log, and for domain B in B.log. Doesn't matter if A was 
renamed to B. If that's the case I'd expect something like the following 
to be the last line in A.log:


  Domain A was renamed to B.

This is what will happen after [1] anyway (without the nice message in 
A.log).



2. Fix domain config for suspended VMs.
3. Keep existing snapshots
3.1 Fix domain config in snapshots.


These two can be a reason to refuse renaming until the time we have 
appropriate design.




Especially the last thing does very bad things if you revert a renamed
VM, as the UUID is then no longer unique.



Having said that, I think we need an libvirt API instead of pure virsh 
implementation. It'll be usable for layered products then too.


Michal

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH] virsh: Implement command to rename domain

2014-09-01 Thread Philipp Hahn
Hello Tomas,

On 01.09.2014 01:51, Tomas Meszaros wrote:
 I've recently worked with rather large number of virtual machines
 and needed to rename all domains. I couldn't find better way how
 to rename domain other than:
 
 virsh dumpxml domain  domain.xml
 (change domain name in domain.xml)
 virsh undefine domain
 virsh define domain.xml
 
 This is rather pain to do every time I want to rename domain.
 I think there should be simple way to change domain name.

This has been requested in the past already (even by me ;-)
Renaming is not that simple, as there are several more things to do:
1. Rename log files (this was somehow controversial last time it was
discussed, especially combined with external programs like logrotate)
2. Fix domain config for suspended VMs.
3. Keep existing snapshots
3.1 Fix domain config in snapshots.

Especially the last thing does very bad things if you revert a renamed
VM, as the UUID is then no longer unique.

Philipp sorry, no patch Hahn

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH] virsh: Implement command to rename domain

2014-09-01 Thread Tomas Meszaros
On 01/09/14 at 10:31am, Philipp Hahn wrote:
 This has been requested in the past already (even by me ;-)
 Renaming is not that simple, as there are several more things to do:
 1. Rename log files (this was somehow controversial last time it was
 discussed, especially combined with external programs like logrotate)
 2. Fix domain config for suspended VMs.
 3. Keep existing snapshots
 3.1 Fix domain config in snapshots.
 
 Especially the last thing does very bad things if you revert a renamed
 VM, as the UUID is then no longer unique.
 
 Philipp sorry, no patch Hahn

Oh... so I will have to stick with the old way...
Anyway, thank you for clarification Philipp.


Tomas

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH] virsh: Implement command to rename domain

2014-08-31 Thread Tomas Meszaros
I've recently worked with rather large number of virtual machines
and needed to rename all domains. I couldn't find better way how
to rename domain other than:

virsh dumpxml domain  domain.xml
(change domain name in domain.xml)
virsh undefine domain
virsh define domain.xml

This is rather pain to do every time I want to rename domain.
I think there should be simple way to change domain name.

So, I decided to implement new command which will basically perform
all actions listed above. When running:

virsh rename foo bar

domain foo will be renamed to bar.

Command rename is implemented using cmdUndefine options with addition
of option name-name.

I included opts_undefine into the opts_rename because I couldn't find
any other way how to call cmdUndefine directly from the cmdRename and
not getting vshCommandOpt assertion fails.

In order to hide undefine options, I added VSH_OFLAG_HIDDEN flag to
the Command Option Flags, so options flagged as hidden wont show up
in help and autocompletion results.

But it is still possible to run rename command with undefine flags:

virsh rename foo bar --managed-save

I would like to call cmdUndefine from cmdRename and not having to
use undefine options as a part of rename options but I just don't
know how to do it at this point.
---
 tools/virsh-domain.c | 133 +++
 tools/virsh.c|  10 
 tools/virsh.h|   2 +
 tools/virsh.pod  |  12 +
 4 files changed, 157 insertions(+)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index c75cd73..cd8c663 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -5103,6 +5103,133 @@ cmdReboot(vshControl *ctl, const vshCmd *cmd)
 }
 
 /*
+ * rename command
+ */
+static const vshCmdInfo info_rename[] = {
+{.name = help,
+ .data = N_(rename a domain)
+},
+{.name = desc,
+ .data = N_(Change domain name.)
+},
+{.name = NULL}
+};
+
+static const vshCmdOptDef opts_rename[] = {
+{.name = domain,
+ .type = VSH_OT_DATA,
+ .flags = VSH_OFLAG_REQ,
+ .help = N_(domain name or uuid)
+},
+{.name = new-name,
+ .type = VSH_OT_DATA,
+ .flags = VSH_OFLAG_REQ,
+ .help = N_(new domain name)
+},
+{.name = managed-save,
+ .type = VSH_OT_BOOL,
+ .flags = VSH_OFLAG_HIDDEN,
+},
+{.name = storage,
+ .type = VSH_OT_DATA,
+ .flags = VSH_OFLAG_HIDDEN,
+},
+{.name = remove-all-storage,
+ .type = VSH_OT_BOOL,
+ .flags = VSH_OFLAG_HIDDEN,
+},
+{.name = wipe-storage,
+ .type = VSH_OT_BOOL,
+ .flags = VSH_OFLAG_HIDDEN,
+},
+{.name = snapshots-metadata,
+ .type = VSH_OT_BOOL,
+ .flags = VSH_OFLAG_HIDDEN,
+},
+{.name = NULL}
+};
+
+static bool
+cmdRename(vshControl *ctl, const vshCmd *cmd)
+{
+virDomainPtr dom = NULL;
+virDomainPtr new_dom = NULL;
+bool ret = false;
+int dom_state;
+int xml_size;
+char *dom_xml = NULL;
+const char *new_name = NULL;
+xmlDocPtr doc = NULL;
+xmlChar *new_dom_xml = NULL;
+xmlNodePtr name_node = NULL;
+xmlXPathObjectPtr obj = NULL;
+xmlXPathContextPtr ctxt = NULL;
+
+if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
+return false;
+
+if (virDomainGetState(dom, dom_state, NULL, 0)  0) {
+vshError(ctl, %s, _(Failed to get domain state));
+goto cleanup;
+}
+
+if (dom_state != VIR_DOMAIN_SHUTOFF) {
+vshError(ctl, _(Please shutdown domain before renaming));
+goto cleanup;
+}
+
+if (vshCommandOptString(cmd, new-name, new_name) = 0) {
+vshError(ctl, _(Failed to parse --new-name parameter));
+goto cleanup;
+}
+
+if (!(dom_xml = virDomainGetXMLDesc(dom, 0)))
+goto cleanup;
+if (!(doc = virXMLParseStringCtxt(dom_xml, NULL, ctxt)))
+goto cleanup;
+
+obj = xmlXPathEval(BAD_CAST /domain/name, ctxt);
+if (obj == NULL || obj-type != XPATH_NODESET || obj-nodesetval == NULL ||
+obj-nodesetval-nodeNr == 0 || obj-nodesetval-nodeTab == NULL) {
+vshError(ctl, _(Failed to extract domain name));
+goto cleanup;
+}
+
+if (!(name_node = obj-nodesetval-nodeTab[0]-children))
+goto cleanup;
+
+xmlNodeSetContent(name_node, BAD_CAST new_name);
+xmlDocDumpMemory(doc, new_dom_xml, xml_size);
+if (new_dom_xml == NULL || xml_size = 0) {
+vshError(ctl, _(Failed to format new XML for domain %s), new_name);
+goto cleanup;
+}
+
+if (!cmdUndefine(ctl, cmd))
+goto cleanup;
+
+if (!(new_dom = virDomainDefineXML(ctl-conn, (char *)new_dom_xml))) {
+vshError(ctl, _(Failed to define domain %s), new_name);
+goto cleanup;
+}
+
+vshPrint(ctl, _(Domain %s has been renamed to %s\n),
+ virDomainGetName(dom), virDomainGetName(new_dom));
+ret = true;
+
+ cleanup:
+virDomainFree(dom);
+if (new_dom)
+virDomainFree(new_dom);
+