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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

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


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]