1 From bf945ee97b72d3b0c4fc2da04530f5294f529d66 Mon Sep 17 00:00:00 2001
2 From: Eric Blake <eblake@redhat.com>
3 Date: Wed, 29 Sep 2010 15:20:23 -0600
4 Subject: [PATCH 08/15] vcpu: add virsh support
6 * tools/virsh.c (cmdSetvcpus): Add new flags. Let invalid
7 commands through to driver, to ease testing of hypervisor argument
9 (cmdMaxvcpus, cmdVcpucount): New commands.
10 (commands): Add new commands.
11 * tools/virsh.pod (setvcpus, vcpucount, maxvcpus): Document new
14 tools/virsh.c | 247 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
15 tools/virsh.pod | 38 ++++++++-
16 2 files changed, 262 insertions(+), 23 deletions(-)
18 diff --git a/tools/virsh.c b/tools/virsh.c
19 index 4f8c495..7fb7fbd 100644
22 @@ -2281,10 +2281,216 @@ cmdFreecell(vshControl *ctl, const vshCmd *cmd)
26 + * "maxvcpus" command
28 +static const vshCmdInfo info_maxvcpus[] = {
29 + {"help", N_("connection vcpu maximum")},
30 + {"desc", N_("Show maximum number of virtual CPUs for guests on this connection.")},
34 +static const vshCmdOptDef opts_maxvcpus[] = {
35 + {"type", VSH_OT_STRING, 0, N_("domain type")},
40 +cmdMaxvcpus(vshControl *ctl, const vshCmd *cmd)
45 + type = vshCommandOptString(cmd, "type", NULL);
47 + if (!vshConnectionUsability(ctl, ctl->conn))
50 + vcpus = virConnectGetMaxVcpus(ctl->conn, type);
53 + vshPrint(ctl, "%d\n", vcpus);
59 + * "vcpucount" command
61 +static const vshCmdInfo info_vcpucount[] = {
62 + {"help", N_("domain vcpu counts")},
63 + {"desc", N_("Returns the number of virtual CPUs used by the domain.")},
67 +static const vshCmdOptDef opts_vcpucount[] = {
68 + {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
69 + {"maximum", VSH_OT_BOOL, 0, N_("get maximum cap on vcpus")},
70 + {"current", VSH_OT_BOOL, 0, N_("get current vcpu usage")},
71 + {"config", VSH_OT_BOOL, 0, N_("get value to be used on next boot")},
72 + {"live", VSH_OT_BOOL, 0, N_("get value from running domain")},
77 +cmdVcpucount(vshControl *ctl, const vshCmd *cmd)
81 + int maximum = vshCommandOptBool(cmd, "maximum");
82 + int current = vshCommandOptBool(cmd, "current");
83 + int config = vshCommandOptBool(cmd, "config");
84 + int live = vshCommandOptBool(cmd, "live");
85 + bool all = maximum + current + config + live == 0;
88 + if (maximum && current) {
90 + _("--maximum and --current cannot both be specified"));
93 + if (config && live) {
95 + _("--config and --live cannot both be specified"));
98 + /* We want one of each pair of mutually exclusive options; that
99 + * is, use of flags requires exactly two options. */
100 + if (maximum + current + config + live == 1) {
102 + _("when using --%s, either --%s or --%s must be specified"),
103 + (maximum ? "maximum" : current ? "current"
104 + : config ? "config" : "live"),
105 + maximum + current ? "config" : "maximum",
106 + maximum + current ? "live" : "current");
110 + if (!vshConnectionUsability(ctl, ctl->conn))
113 + if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
116 + /* In all cases, try the new API first; if it fails because we are
117 + * talking to an older client, try a fallback API before giving
119 + if (all || (maximum && config)) {
120 + count = virDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_MAXIMUM |
121 + VIR_DOMAIN_VCPU_CONFIG));
122 + if (count < 0 && (last_error->code == VIR_ERR_NO_SUPPORT
123 + || last_error->code == VIR_ERR_INVALID_ARG)) {
125 + char *xml = virDomainGetXMLDesc(dom, VIR_DOMAIN_XML_INACTIVE);
126 + if (xml && (tmp = strstr(xml, "<vcpu"))) {
127 + tmp = strchr(tmp, '>');
128 + if (!tmp || virStrToLong_i(tmp + 1, &tmp, 10, &count) < 0)
135 + virshReportError(ctl);
138 + vshPrint(ctl, "%-12s %-12s %3d\n", _("maximum"), _("config"),
141 + vshPrint(ctl, "%d\n", count);
143 + virFreeError(last_error);
147 + if (all || (maximum && live)) {
148 + count = virDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_MAXIMUM |
149 + VIR_DOMAIN_VCPU_LIVE));
150 + if (count < 0 && (last_error->code == VIR_ERR_NO_SUPPORT
151 + || last_error->code == VIR_ERR_INVALID_ARG)) {
152 + count = virDomainGetMaxVcpus(dom);
156 + virshReportError(ctl);
159 + vshPrint(ctl, "%-12s %-12s %3d\n", _("maximum"), _("live"),
162 + vshPrint(ctl, "%d\n", count);
164 + virFreeError(last_error);
168 + if (all || (current && config)) {
169 + count = virDomainGetVcpusFlags(dom, VIR_DOMAIN_VCPU_CONFIG);
170 + if (count < 0 && (last_error->code == VIR_ERR_NO_SUPPORT
171 + || last_error->code == VIR_ERR_INVALID_ARG)) {
173 + char *xml = virDomainGetXMLDesc(dom, VIR_DOMAIN_XML_INACTIVE);
174 + if (xml && (tmp = strstr(xml, "<vcpu"))) {
175 + end = strchr(tmp, '>');
178 + tmp = strstr(tmp, "current=");
182 + tmp += strlen("current=");
183 + tmp += *tmp == '\'' || *tmp == '"';
186 + if (!tmp || virStrToLong_i(tmp, &tmp, 10, &count) < 0)
193 + virshReportError(ctl);
196 + vshPrint(ctl, "%-12s %-12s %3d\n", _("current"), _("config"),
199 + vshPrint(ctl, "%d\n", count);
201 + virFreeError(last_error);
205 + if (all || (current && live)) {
206 + count = virDomainGetVcpusFlags(dom, VIR_DOMAIN_VCPU_LIVE);
207 + if (count < 0 && (last_error->code == VIR_ERR_NO_SUPPORT
208 + || last_error->code == VIR_ERR_INVALID_ARG)) {
209 + virDomainInfo info;
210 + if (virDomainGetInfo(dom, &info) == 0)
211 + count = info.nrVirtCpu;
215 + virshReportError(ctl);
218 + vshPrint(ctl, "%-12s %-12s %3d\n", _("current"), _("live"),
221 + vshPrint(ctl, "%d\n", count);
223 + virFreeError(last_error);
227 + virDomainFree(dom);
234 static const vshCmdInfo info_vcpuinfo[] = {
235 - {"help", N_("domain vcpu information")},
236 + {"help", N_("detailed domain vcpu information")},
237 {"desc", N_("Returns basic information about the domain virtual CPUs.")},
240 @@ -2514,6 +2720,9 @@ static const vshCmdInfo info_setvcpus[] = {
241 static const vshCmdOptDef opts_setvcpus[] = {
242 {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
243 {"count", VSH_OT_DATA, VSH_OFLAG_REQ, N_("number of virtual CPUs")},
244 + {"maximum", VSH_OT_BOOL, 0, N_("set maximum limit on next boot")},
245 + {"config", VSH_OT_BOOL, 0, N_("affect next boot")},
246 + {"live", VSH_OT_BOOL, 0, N_("affect running domain")},
250 @@ -2522,8 +2731,13 @@ cmdSetvcpus(vshControl *ctl, const vshCmd *cmd)
256 + int maximum = vshCommandOptBool(cmd, "maximum");
257 + int config = vshCommandOptBool(cmd, "config");
258 + int live = vshCommandOptBool(cmd, "live");
259 + int flags = ((maximum ? VIR_DOMAIN_VCPU_MAXIMUM : 0) |
260 + (config ? VIR_DOMAIN_VCPU_CONFIG : 0) |
261 + (live ? VIR_DOMAIN_VCPU_LIVE : 0));
263 if (!vshConnectionUsability(ctl, ctl->conn))
265 @@ -2532,26 +2746,15 @@ cmdSetvcpus(vshControl *ctl, const vshCmd *cmd)
268 count = vshCommandOptInt(cmd, "count", &count);
270 - vshError(ctl, "%s", _("Invalid number of virtual CPUs."));
271 - virDomainFree(dom);
275 - maxcpu = virDomainGetMaxVcpus(dom);
277 - virDomainFree(dom);
281 - if (count > maxcpu) {
282 - vshError(ctl, "%s", _("Too many virtual CPUs."));
283 - virDomainFree(dom);
287 - if (virDomainSetVcpus(dom, count) != 0) {
290 + if (virDomainSetVcpus(dom, count) != 0) {
294 + if (virDomainSetVcpusFlags(dom, count, flags) < 0) {
300 @@ -9642,6 +9845,7 @@ static const vshCmdDef commands[] = {
301 {"freecell", cmdFreecell, opts_freecell, info_freecell},
302 {"hostname", cmdHostname, NULL, info_hostname},
303 {"list", cmdList, opts_list, info_list},
304 + {"maxvcpus", cmdMaxvcpus, opts_maxvcpus, info_maxvcpus},
305 {"migrate", cmdMigrate, opts_migrate, info_migrate},
306 {"migrate-setmaxdowntime", cmdMigrateSetMaxDowntime, opts_migrate_setmaxdowntime, info_migrate_setmaxdowntime},
308 @@ -9748,6 +9952,7 @@ static const vshCmdDef commands[] = {
309 {"vol-name", cmdVolName, opts_vol_name, info_vol_name},
310 {"vol-key", cmdVolKey, opts_vol_key, info_vol_key},
312 + {"vcpucount", cmdVcpucount, opts_vcpucount, info_vcpucount},
313 {"vcpuinfo", cmdVcpuinfo, opts_vcpuinfo, info_vcpuinfo},
314 {"vcpupin", cmdVcpupin, opts_vcpupin, info_vcpupin},
315 {"version", cmdVersion, NULL, info_version},
316 diff --git a/tools/virsh.pod b/tools/virsh.pod
317 index 943a563..dbcc680 100644
318 --- a/tools/virsh.pod
319 +++ b/tools/virsh.pod
320 @@ -443,7 +443,14 @@ Remove the managed save file for a domain if it exists. The next time the
321 domain is started it will not restore to its previous state but instead will
324 -=item B<migrate> optional I<--live> I<--suspend> I<domain-id> I<desturi> I<migrateuri>
325 +=item B<maxvcpus> optional I<type>
327 +Provide the maximum number of virtual CPUs supported for a guest VM on
328 +this connection. If provided, the I<type> parameter must be a valid
329 +type attribute for the <domain> element of XML.
331 +=item B<migrate> optional I<--live> I<--suspend> I<domain-id> I<desturi>
334 Migrate domain to another host. Add --live for live migration; --suspend
335 leaves the domain paused on the destination host. The I<desturi> is the
336 @@ -521,7 +528,8 @@ Displays the domain memory parameters.
338 Allows you to set the domain memory parameters. LXC and QEMU/KVM supports these parameters.
340 -=item B<setvcpus> I<domain-id> I<count>
341 +=item B<setvcpus> I<domain-id> I<count> optional I<--maximum> I<--config>
344 Change the number of virtual CPUs active in the guest domain. Note that
345 I<count> may be limited by host, hypervisor or limit coming from the
346 @@ -530,6 +538,17 @@ original description of domain.
347 For Xen, you can only adjust the virtual CPUs of a running domain if
348 the domain is paravirtualized.
350 +If I<--config> is specified, the change will only affect the next
351 +boot of a domain. If I<--live> is specified, the domain must be
352 +running, and the change takes place immediately. Both flags may be
353 +specified, if supported by the hypervisor. If neither flag is given,
354 +then I<--live> is implied and it is up to the hypervisor whether
355 +I<--config> is also implied.
357 +If I<--maximum> is specified, then you must use I<--config> and
358 +avoid I<--live>; this flag controls the maximum limit of vcpus that
359 +can be hot-plugged the next time the domain is booted.
361 =item B<shutdown> I<domain-id>
363 Gracefully shuts down a domain. This coordinates with the domain OS
364 @@ -568,6 +587,21 @@ is not available the processes will provide an exit code of 1.
365 Undefine the configuration for an inactive domain. Since it's not running
366 the domain name or UUID must be used as the I<domain-id>.
368 +=item B<vcpucount> I<domain-id> optional I<--maximum> I<--current>
369 +I<--config> I<--live>
371 +Print information about the virtual cpu counts of the given
372 +I<domain-id>. If no flags are specified, all possible counts are
373 +listed in a table; otherwise, the output is limited to just the
374 +numeric value requested.
376 +I<--maximum> requests information on the maximum cap of vcpus that a
377 +domain can add via B<setvcpus>, while I<--current> shows the current
378 +usage; these two flags cannot both be specified. I<--config>
379 +requests information regarding the next time the domain will be
380 +booted, while I<--live> requires a running domain and lists current
381 +values; these two flags cannot both be specified.
383 =item B<vcpuinfo> I<domain-id>
385 Returns basic information about the domain virtual CPUs, like the number of