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

Doug Goldstein cardoe at cardoe.com
Tue Jan 8 21:36:05 UTC 2013


I often find myself doing virsh "define blah.xml; start blah". I figured
adding this would be a easier^Hlazier way to do it.
---
 tools/virsh-domain.c |   20 ++++++++++++++++++--
 1 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index e91939c..1a3af34 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -5597,6 +5597,7 @@ static const vshCmdInfo info_define[] = {
 
 static const vshCmdOptDef opts_define[] = {
     {"file", VSH_OT_DATA, VSH_OFLAG_REQ, N_("file containing an XML domain description")},
+    {"start", VSH_OT_BOOL, 0, N_("start the domain after creation")},
     {NULL, 0, 0, NULL}
 };
 
@@ -5607,6 +5608,7 @@ cmdDefine(vshControl *ctl, const vshCmd *cmd)
     const char *from = NULL;
     bool ret = true;
     char *buffer;
+    bool start;
 
     if (vshCommandOptString(cmd, "file", &from) <= 0)
         return false;
@@ -5614,17 +5616,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;
 }
 
-- 
1.7.8.6




More information about the libvir-list mailing list