[libvirt] [PATCH 1/2] virsh: Add options for several commands which change domain config

Osier Yang jyang at redhat.com
Tue Jun 21 05:37:31 UTC 2011


All of the following 7 commands just provide one option (--persistent)
for user to specify how to affect the domain:
    attach-device
    detach-device
    attach-disk
    detach-disk
    attach-interface
    detach-interface
    update-device

However, All of the APIs the above 7 commands use: virDomainAttachDeviceFlags,
virDomainDetachDeviceFlags, and virDomainUpdateDeviceFlags support following
3 flags:
    VIR_DOMAIN_AFFECT_CURRENT
    VIR_DOMAIN_AFFECT_CONFIG
    VIR_DOMAIN_AFFECT_LIVE

This patch add two new options (--live, --current), and changes
"--persistent" into "--config", just as other similar commands,
e.g. "schedinfo", "vcpupin".

And since the APIs are designed as: If no flag is specified, behaviour
is different depending on hypervisor, so virsh shouldn't do things like:

    if (virDomainIsActive(dom) == 1)
        flags |= VIR_DOMAIN_AFFECT_LIVE;

This patch removes codes like this, leave the determination for underly
hypervisor driver.
---
 tools/virsh.c |  242 ++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 178 insertions(+), 64 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index abc4614..4cd2e1a 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -9738,7 +9738,9 @@ static const vshCmdInfo info_attach_device[] = {
 static const vshCmdOptDef opts_attach_device[] = {
     {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
     {"file",   VSH_OT_DATA, VSH_OFLAG_REQ, N_("XML file")},
-    {"persistent", VSH_OT_BOOL, 0, N_("persist device attachment")},
+    {"config", VSH_OT_BOOL, 0, N_("affect next boot")},
+    {"live", VSH_OT_BOOL, 0, N_("affect running domain")},
+    {"current", VSH_OT_BOOL, 0, N_("affect current domain")},
     {NULL, 0, 0, NULL}
 };
 
@@ -9749,7 +9751,26 @@ cmdAttachDevice(vshControl *ctl, const vshCmd *cmd)
     const char *from = NULL;
     char *buffer;
     int ret;
-    unsigned int flags;
+    int flags = 0;
+
+    int current = vshCommandOptBool(cmd, "current");
+    int config = vshCommandOptBool(cmd, "config");
+    int live = vshCommandOptBool(cmd, "live");
+
+    if (current) {
+        if (live || config) {
+            vshError(ctl, "%s", _("--current must be specified exclusively"));
+            return false;
+        }
+        flags = VIR_DOMAIN_AFFECT_CURRENT;
+    } else {
+        if (config)
+            flags |= VIR_DOMAIN_AFFECT_CONFIG;
+        if (live)
+            flags |= VIR_DOMAIN_AFFECT_LIVE;
+        if (!config && !live)
+            flags = -1;
+    }
 
     if (!vshConnectionUsability(ctl, ctl->conn))
         return false;
@@ -9768,14 +9789,10 @@ cmdAttachDevice(vshControl *ctl, const vshCmd *cmd)
         return false;
     }
 
-    if (vshCommandOptBool(cmd, "persistent")) {
-        flags = VIR_DOMAIN_AFFECT_CONFIG;
-        if (virDomainIsActive(dom) == 1)
-           flags |= VIR_DOMAIN_AFFECT_LIVE;
-        ret = virDomainAttachDeviceFlags(dom, buffer, flags);
-    } else {
+    if (flags == -1)
         ret = virDomainAttachDevice(dom, buffer);
-    }
+    else
+        ret = virDomainAttachDeviceFlags(dom, buffer, flags);
     VIR_FREE(buffer);
 
     if (ret < 0) {
@@ -9803,7 +9820,9 @@ static const vshCmdInfo info_detach_device[] = {
 static const vshCmdOptDef opts_detach_device[] = {
     {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
     {"file",   VSH_OT_DATA, VSH_OFLAG_REQ, N_("XML file")},
-    {"persistent", VSH_OT_BOOL, 0, N_("persist device detachment")},
+    {"config", VSH_OT_BOOL, 0, N_("affect next boot")},
+    {"live", VSH_OT_BOOL, 0, N_("affect running domain")},
+    {"current", VSH_OT_BOOL, 0, N_("affect current domain")},
     {NULL, 0, 0, NULL}
 };
 
@@ -9814,7 +9833,26 @@ cmdDetachDevice(vshControl *ctl, const vshCmd *cmd)
     const char *from = NULL;
     char *buffer;
     int ret;
-    unsigned int flags;
+    int flags = 0;
+
+    int current = vshCommandOptBool(cmd, "current");
+    int config = vshCommandOptBool(cmd, "config");
+    int live = vshCommandOptBool(cmd, "live");
+
+    if (current) {
+        if (live || config) {
+            vshError(ctl, "%s", _("--current must be specified exclusively"));
+            return false;
+        }
+        flags = VIR_DOMAIN_AFFECT_CURRENT;
+    } else {
+        if (config)
+            flags |= VIR_DOMAIN_AFFECT_CONFIG;
+        if (live)
+            flags |= VIR_DOMAIN_AFFECT_LIVE;
+        if (!config && !live)
+            flags = -1;
+    }
 
     if (!vshConnectionUsability(ctl, ctl->conn))
         return false;
@@ -9833,14 +9871,10 @@ cmdDetachDevice(vshControl *ctl, const vshCmd *cmd)
         return false;
     }
 
-    if (vshCommandOptBool(cmd, "persistent")) {
-        flags = VIR_DOMAIN_AFFECT_CONFIG;
-        if (virDomainIsActive(dom) == 1)
-           flags |= VIR_DOMAIN_AFFECT_LIVE;
-        ret = virDomainDetachDeviceFlags(dom, buffer, flags);
-    } else {
+    if (flags == -1)
         ret = virDomainDetachDevice(dom, buffer);
-    }
+    else
+        ret = virDomainDetachDeviceFlags(dom, buffer, flags);
     VIR_FREE(buffer);
 
     if (ret < 0) {
@@ -9868,8 +9902,10 @@ static const vshCmdInfo info_update_device[] = {
 static const vshCmdOptDef opts_update_device[] = {
     {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
     {"file",   VSH_OT_DATA, VSH_OFLAG_REQ, N_("XML file")},
-    {"persistent", VSH_OT_BOOL, 0, N_("persist device update")},
     {"force",  VSH_OT_BOOL, 0, N_("force device update")},
+    {"config", VSH_OT_BOOL, 0, N_("affect next boot")},
+    {"live", VSH_OT_BOOL, 0, N_("affect running domain")},
+    {"current", VSH_OT_BOOL, 0, N_("affect current domain")},
     {NULL, 0, 0, NULL}
 };
 
@@ -9880,7 +9916,24 @@ cmdUpdateDevice(vshControl *ctl, const vshCmd *cmd)
     const char *from = NULL;
     char *buffer;
     int ret;
-    unsigned int flags;
+    unsigned int flags = 0;
+
+    int current = vshCommandOptBool(cmd, "current");
+    int config = vshCommandOptBool(cmd, "config");
+    int live = vshCommandOptBool(cmd, "live");
+
+    if (current) {
+        if (live || config) {
+            vshError(ctl, "%s", _("--current must be specified exclusively"));
+            return false;
+        }
+        flags = VIR_DOMAIN_AFFECT_CURRENT;
+    } else {
+        if (config)
+            flags |= VIR_DOMAIN_AFFECT_CONFIG;
+        if (live)
+            flags |= VIR_DOMAIN_AFFECT_LIVE;
+    }
 
     if (!vshConnectionUsability(ctl, ctl->conn))
         return false;
@@ -9899,14 +9952,6 @@ cmdUpdateDevice(vshControl *ctl, const vshCmd *cmd)
         return false;
     }
 
-    if (vshCommandOptBool(cmd, "persistent")) {
-        flags = VIR_DOMAIN_AFFECT_CONFIG;
-        if (virDomainIsActive(dom) == 1)
-           flags |= VIR_DOMAIN_AFFECT_LIVE;
-    } else {
-        flags = VIR_DOMAIN_AFFECT_LIVE;
-    }
-
     if (vshCommandOptBool(cmd, "force"))
         flags |= VIR_DOMAIN_DEVICE_MODIFY_FORCE;
 
@@ -9943,7 +9988,9 @@ static const vshCmdOptDef opts_attach_interface[] = {
     {"mac",    VSH_OT_DATA, 0, N_("MAC address")},
     {"script", VSH_OT_DATA, 0, N_("script used to bridge network interface")},
     {"model", VSH_OT_DATA, 0, N_("model type")},
-    {"persistent", VSH_OT_BOOL, 0, N_("persist interface attachment")},
+    {"config", VSH_OT_BOOL, 0, N_("affect next boot")},
+    {"live", VSH_OT_BOOL, 0, N_("affect running domain")},
+    {"current", VSH_OT_BOOL, 0, N_("affect current domain")},
     {NULL, 0, 0, NULL}
 };
 
@@ -9952,13 +9999,32 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd)
 {
     virDomainPtr dom = NULL;
     const char *mac = NULL, *target = NULL, *script = NULL,
-                *type = NULL, *source = NULL, *model = NULL;
+               *type = NULL, *source = NULL, *model = NULL;
     int typ;
     int ret;
     bool functionReturn = false;
-    unsigned int flags;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     char *xml;
+    int flags = 0;
+
+    int current = vshCommandOptBool(cmd, "current");
+    int config = vshCommandOptBool(cmd, "config");
+    int live = vshCommandOptBool(cmd, "live");
+
+    if (current) {
+        if (live || config) {
+            vshError(ctl, "%s", _("--current must be specified exclusively"));
+            return false;
+        }
+        flags = VIR_DOMAIN_AFFECT_CURRENT;
+    } else {
+        if (config)
+            flags |= VIR_DOMAIN_AFFECT_CONFIG;
+        if (live)
+            flags |= VIR_DOMAIN_AFFECT_LIVE;
+        if (!config && !live)
+            flags = -1;
+    }
 
     if (!vshConnectionUsability(ctl, ctl->conn))
         goto cleanup;
@@ -10015,15 +10081,10 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd)
 
     xml = virBufferContentAndReset(&buf);
 
-    if (vshCommandOptBool(cmd, "persistent")) {
-        flags = VIR_DOMAIN_AFFECT_CONFIG;
-        if (virDomainIsActive(dom) == 1)
-            flags |= VIR_DOMAIN_AFFECT_LIVE;
-        ret = virDomainAttachDeviceFlags(dom, xml, flags);
-    } else {
+    if (flags == -1)
         ret = virDomainAttachDevice(dom, xml);
-    }
-
+    else
+        ret = virDomainAttachDeviceFlags(dom, xml, flags);
     VIR_FREE(xml);
 
     if (ret != 0) {
@@ -10053,7 +10114,9 @@ static const vshCmdOptDef opts_detach_interface[] = {
     {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
     {"type",   VSH_OT_DATA, VSH_OFLAG_REQ, N_("network interface type")},
     {"mac",    VSH_OT_STRING, 0, N_("MAC address")},
-    {"persistent", VSH_OT_BOOL, 0, N_("persist interface detachment")},
+    {"config", VSH_OT_BOOL, 0, N_("affect next boot")},
+    {"live", VSH_OT_BOOL, 0, N_("affect running domain")},
+    {"current", VSH_OT_BOOL, 0, N_("affect current domain")},
     {NULL, 0, 0, NULL}
 };
 
@@ -10072,7 +10135,26 @@ cmdDetachInterface(vshControl *ctl, const vshCmd *cmd)
     int i = 0, diff_mac;
     int ret;
     int functionReturn = false;
-    unsigned int flags;
+    int flags = 0;
+
+    int current = vshCommandOptBool(cmd, "current");
+    int config = vshCommandOptBool(cmd, "config");
+    int live = vshCommandOptBool(cmd, "live");
+
+    if (current) {
+        if (live || config) {
+            vshError(ctl, "%s", _("--current must be specified exclusively"));
+            return false;
+        }
+        flags = VIR_DOMAIN_AFFECT_CURRENT;
+    } else {
+        if (config)
+            flags |= VIR_DOMAIN_AFFECT_CONFIG;
+        if (live)
+            flags |= VIR_DOMAIN_AFFECT_LIVE;
+        if (!config && !live)
+            flags = -1;
+    }
 
     if (!vshConnectionUsability(ctl, ctl->conn))
         goto cleanup;
@@ -10154,15 +10236,12 @@ cmdDetachInterface(vshControl *ctl, const vshCmd *cmd)
         goto cleanup;
     }
 
-    if (vshCommandOptBool(cmd, "persistent")) {
-        flags = VIR_DOMAIN_AFFECT_CONFIG;
-        if (virDomainIsActive(dom) == 1)
-            flags |= VIR_DOMAIN_AFFECT_LIVE;
+    if (flags == -1) {
+        ret = virDomainDetachDevice(dom, (char *)xmlBufferContent(xml_buf));
+    } else {
         ret = virDomainDetachDeviceFlags(dom,
                                          (char *)xmlBufferContent(xml_buf),
                                          flags);
-    } else {
-        ret = virDomainDetachDevice(dom, (char *)xmlBufferContent(xml_buf));
     }
 
     if (ret != 0) {
@@ -10201,8 +10280,10 @@ static const vshCmdOptDef opts_attach_disk[] = {
     {"subdriver", VSH_OT_STRING, 0, N_("subdriver of disk device")},
     {"type",    VSH_OT_STRING, 0, N_("target device type")},
     {"mode",    VSH_OT_STRING, 0, N_("mode of device reading and writing")},
-    {"persistent", VSH_OT_BOOL, 0, N_("persist disk attachment")},
     {"sourcetype", VSH_OT_STRING, 0, N_("type of source (block|file)")},
+    {"config", VSH_OT_BOOL, 0, N_("affect next boot")},
+    {"live", VSH_OT_BOOL, 0, N_("affect running domain")},
+    {"current", VSH_OT_BOOL, 0, N_("affect current domain")},
     {NULL, 0, 0, NULL}
 };
 
@@ -10214,10 +10295,29 @@ cmdAttachDisk(vshControl *ctl, const vshCmd *cmd)
                 *subdriver = NULL, *type = NULL, *mode = NULL;
     bool isFile = false, functionReturn = false;
     int ret;
-    unsigned int flags;
     const char *stype = NULL;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     char *xml;
+    int flags = 0;
+
+    int current = vshCommandOptBool(cmd, "current");
+    int config = vshCommandOptBool(cmd, "config");
+    int live = vshCommandOptBool(cmd, "live");
+
+    if (current) {
+        if (live || config) {
+            vshError(ctl, "%s", _("--current must be specified exclusively"));
+            return false;
+        }
+        flags = VIR_DOMAIN_AFFECT_CURRENT;
+    } else {
+        if (config)
+            flags |= VIR_DOMAIN_AFFECT_CONFIG;
+        if (live)
+            flags |= VIR_DOMAIN_AFFECT_LIVE;
+        if (!config && !live)
+            flags = -1;
+    }
 
     if (!vshConnectionUsability(ctl, ctl->conn))
         goto cleanup;
@@ -10292,14 +10392,10 @@ cmdAttachDisk(vshControl *ctl, const vshCmd *cmd)
 
     xml = virBufferContentAndReset(&buf);
 
-    if (vshCommandOptBool(cmd, "persistent")) {
-        flags = VIR_DOMAIN_AFFECT_CONFIG;
-        if (virDomainIsActive(dom) == 1)
-            flags |= VIR_DOMAIN_AFFECT_LIVE;
-        ret = virDomainAttachDeviceFlags(dom, xml, flags);
-    } else {
+    if (flags == -1)
         ret = virDomainAttachDevice(dom, xml);
-    }
+    else
+        ret = virDomainAttachDeviceFlags(dom, xml, flags);
 
     VIR_FREE(xml);
 
@@ -10329,7 +10425,9 @@ static const vshCmdInfo info_detach_disk[] = {
 static const vshCmdOptDef opts_detach_disk[] = {
     {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
     {"target", VSH_OT_DATA, VSH_OFLAG_REQ, N_("target of disk device")},
-    {"persistent", VSH_OT_BOOL, 0, N_("persist disk detachment")},
+    {"config", VSH_OT_BOOL, 0, N_("affect next boot")},
+    {"live", VSH_OT_BOOL, 0, N_("affect running domain")},
+    {"current", VSH_OT_BOOL, 0, N_("affect current domain")},
     {NULL, 0, 0, NULL}
 };
 
@@ -10347,7 +10445,26 @@ cmdDetachDisk(vshControl *ctl, const vshCmd *cmd)
     int i = 0, diff_tgt;
     int ret;
     bool functionReturn = false;
-    unsigned int flags;
+    int flags = 0;
+
+    int current = vshCommandOptBool(cmd, "current");
+    int config = vshCommandOptBool(cmd, "config");
+    int live = vshCommandOptBool(cmd, "live");
+
+    if (current) {
+        if (live || config) {
+            vshError(ctl, "%s", _("--current must be specified exclusively"));
+            return false;
+        }
+        flags = VIR_DOMAIN_AFFECT_CURRENT;
+    } else {
+        if (config)
+            flags |= VIR_DOMAIN_AFFECT_CONFIG;
+        if (live)
+            flags |= VIR_DOMAIN_AFFECT_LIVE;
+        if (!config && !live)
+            flags = -1;
+    }
 
     if (!vshConnectionUsability(ctl, ctl->conn))
         goto cleanup;
@@ -10414,15 +10531,12 @@ cmdDetachDisk(vshControl *ctl, const vshCmd *cmd)
         goto cleanup;
     }
 
-    if (vshCommandOptBool(cmd, "persistent")) {
-        flags = VIR_DOMAIN_AFFECT_CONFIG;
-        if (virDomainIsActive(dom) == 1)
-            flags |= VIR_DOMAIN_AFFECT_LIVE;
+    if (flags == -1) {
+        ret = virDomainDetachDevice(dom, (char *)xmlBufferContent(xml_buf));
+    } else {
         ret = virDomainDetachDeviceFlags(dom,
                                          (char *)xmlBufferContent(xml_buf),
                                          flags);
-    } else {
-        ret = virDomainDetachDevice(dom, (char *)xmlBufferContent(xml_buf));
     }
 
     if (ret != 0) {
-- 
1.7.4




More information about the libvir-list mailing list