[libvirt] [PATCH] virsh: add --start option to the define command

Doug Goldstein cardoe at cardoe.com
Mon Feb 25 03:01:00 UTC 2013


Adds the --start option to the define command to simplify the often used
case of virsh "define dom.xml; start dom" or virsh define dom.xml &&
virsh start dom.

This is just a rebased version of:
https://www.redhat.com/archives/libvir-list/2013-January/msg00490.html

There's a competing patchset available at:
https://www.redhat.com/archives/libvir-list/2013-February/msg01298.html
---
 tools/virsh-domain.c | 23 +++++++++++++++++++++--
 tools/virsh.pod      |  5 +++--
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 96dd4fa..6d62197 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -6602,6 +6602,10 @@ static const vshCmdOptDef opts_define[] = {
      .flags = VSH_OFLAG_REQ,
      .help = N_("file containing an XML domain description")
     },
+    {.name = "start",
+     .type = VSH_OT_BOOL,
+     .help = N("start the domain after definition")
+    },
     {.name = NULL}
 };
 
@@ -6612,6 +6616,7 @@ cmdDefine(vshControl *ctl, const vshCmd *cmd)
     const char *from = NULL;
     bool ret = true;
     char *buffer;
+    bool start = false;
 
     if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
         return false;
@@ -6619,17 +6624,31 @@ cmdDefine(vshControl *ctl, const vshCmd *cmd)
     if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0)
         return false;
 
+    start = vshCommandOptBool(cmd, "start");
+
     dom = virDomainDefineXML(ctl->conn, buffer);
     VIR_FREE(buffer);
 
     if (dom != NULL) {
         vshPrint(ctl, _("Domain %s defined from %s\n"),
                  virDomainGetName(dom), from);
-        virDomainFree(dom);
     } else {
         vshError(ctl, _("Failed to define domain from %s"), from);
-        ret = false;
+        return false;
     }
+
+    /* Start the domain if the user requested it and it was defined */
+    if (start) {
+        if (virDomainCreate(dom) < 0) {
+            vshError(ctl, _("Failed to start domain %s, which was "
+                        "successfully defined."), virDomainGetName(dom));
+            ret = false;
+        } else {
+            vshPrint(ctl, _("Domain %s started\n"), virDomainGetName(dom));
+        }
+    }
+
+    virDomainFree(dom);
     return ret;
 }
 
diff --git a/tools/virsh.pod b/tools/virsh.pod
index a5d8fe6..660331c 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -555,11 +555,12 @@ B<Example>
  vi domain.xml (or make changes with your other text editor)
  virsh create domain.xml
 
-=item B<define> I<FILE>
+=item B<define> I<FILE> [I<--start>]
 
 Define a domain from an XML <file>. The domain definition is registered
 but not started.  If domain is already running, the changes will take
-effect on the next boot.
+effect on the next boot. If I<--start> is requested, start the domain
+after defining it.
 
 =item B<desc> I<domain> [[I<--live>] [I<--config>] |
               [I<--current>]] [I<--title>] [I<--edit>] [I<--new-desc>
-- 
1.7.12.4




More information about the libvir-list mailing list