daemon: Move TLS initialization to virInitialize
[libvirt/zwu.git] / docs / api_extension / 0007-add-virsh-support.patch
blob8c5494e04dc474863e20497f1fa35a236edd432a
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
8 validation.
9 (cmdMaxvcpus, cmdVcpucount): New commands.
10 (commands): Add new commands.
11 * tools/virsh.pod (setvcpus, vcpucount, maxvcpus): Document new
12 behavior.
13 ---
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
20 --- a/tools/virsh.c
21 +++ b/tools/virsh.c
22 @@ -2281,10 +2281,216 @@ cmdFreecell(vshControl *ctl, const vshCmd *cmd)
26 + * "maxvcpus" command
27 + */
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.")},
31 + {NULL, NULL}
32 +};
34 +static const vshCmdOptDef opts_maxvcpus[] = {
35 + {"type", VSH_OT_STRING, 0, N_("domain type")},
36 + {NULL, 0, 0, NULL}
37 +};
39 +static int
40 +cmdMaxvcpus(vshControl *ctl, const vshCmd *cmd)
42 + char *type;
43 + int vcpus;
45 + type = vshCommandOptString(cmd, "type", NULL);
47 + if (!vshConnectionUsability(ctl, ctl->conn))
48 + return FALSE;
50 + vcpus = virConnectGetMaxVcpus(ctl->conn, type);
51 + if (vcpus < 0)
52 + return FALSE;
53 + vshPrint(ctl, "%d\n", vcpus);
55 + return TRUE;
58 +/*
59 + * "vcpucount" command
60 + */
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.")},
64 + {NULL, NULL}
65 +};
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")},
73 + {NULL, 0, 0, NULL}
74 +};
76 +static int
77 +cmdVcpucount(vshControl *ctl, const vshCmd *cmd)
79 + virDomainPtr dom;
80 + int ret = TRUE;
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;
86 + int count;
88 + if (maximum && current) {
89 + vshError(ctl, "%s",
90 + _("--maximum and --current cannot both be specified"));
91 + return FALSE;
92 + }
93 + if (config && live) {
94 + vshError(ctl, "%s",
95 + _("--config and --live cannot both be specified"));
96 + return FALSE;
97 + }
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) {
101 + vshError(ctl,
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");
107 + return FALSE;
110 + if (!vshConnectionUsability(ctl, ctl->conn))
111 + return FALSE;
113 + if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
114 + return FALSE;
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
118 + * up. */
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)) {
124 + char *tmp;
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)
129 + count = -1;
131 + VIR_FREE(xml);
134 + if (count < 0) {
135 + virshReportError(ctl);
136 + ret = FALSE;
137 + } else if (all) {
138 + vshPrint(ctl, "%-12s %-12s %3d\n", _("maximum"), _("config"),
139 + count);
140 + } else {
141 + vshPrint(ctl, "%d\n", count);
143 + virFreeError(last_error);
144 + last_error = NULL;
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);
155 + if (count < 0) {
156 + virshReportError(ctl);
157 + ret = FALSE;
158 + } else if (all) {
159 + vshPrint(ctl, "%-12s %-12s %3d\n", _("maximum"), _("live"),
160 + count);
161 + } else {
162 + vshPrint(ctl, "%d\n", count);
164 + virFreeError(last_error);
165 + last_error = NULL;
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)) {
172 + char *tmp, *end;
173 + char *xml = virDomainGetXMLDesc(dom, VIR_DOMAIN_XML_INACTIVE);
174 + if (xml && (tmp = strstr(xml, "<vcpu"))) {
175 + end = strchr(tmp, '>');
176 + if (end) {
177 + *end = '\0';
178 + tmp = strstr(tmp, "current=");
179 + if (!tmp)
180 + tmp = end + 1;
181 + else {
182 + tmp += strlen("current=");
183 + tmp += *tmp == '\'' || *tmp == '"';
186 + if (!tmp || virStrToLong_i(tmp, &tmp, 10, &count) < 0)
187 + count = -1;
189 + VIR_FREE(xml);
192 + if (count < 0) {
193 + virshReportError(ctl);
194 + ret = FALSE;
195 + } else if (all) {
196 + vshPrint(ctl, "%-12s %-12s %3d\n", _("current"), _("config"),
197 + count);
198 + } else {
199 + vshPrint(ctl, "%d\n", count);
201 + virFreeError(last_error);
202 + last_error = NULL;
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;
214 + if (count < 0) {
215 + virshReportError(ctl);
216 + ret = FALSE;
217 + } else if (all) {
218 + vshPrint(ctl, "%-12s %-12s %3d\n", _("current"), _("live"),
219 + count);
220 + } else {
221 + vshPrint(ctl, "%d\n", count);
223 + virFreeError(last_error);
224 + last_error = NULL;
227 + virDomainFree(dom);
228 + return ret;
232 * "vcpuinfo" command
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.")},
238 {NULL, NULL}
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")},
247 {NULL, 0, 0, NULL}
250 @@ -2522,8 +2731,13 @@ cmdSetvcpus(vshControl *ctl, const vshCmd *cmd)
252 virDomainPtr dom;
253 int count;
254 - int maxcpu;
255 int ret = TRUE;
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))
264 return FALSE;
265 @@ -2532,26 +2746,15 @@ cmdSetvcpus(vshControl *ctl, const vshCmd *cmd)
266 return FALSE;
268 count = vshCommandOptInt(cmd, "count", &count);
269 - if (count <= 0) {
270 - vshError(ctl, "%s", _("Invalid number of virtual CPUs."));
271 - virDomainFree(dom);
272 - return FALSE;
275 - maxcpu = virDomainGetMaxVcpus(dom);
276 - if (maxcpu <= 0) {
277 - virDomainFree(dom);
278 - return FALSE;
281 - if (count > maxcpu) {
282 - vshError(ctl, "%s", _("Too many virtual CPUs."));
283 - virDomainFree(dom);
284 - return FALSE;
287 - if (virDomainSetVcpus(dom, count) != 0) {
288 - ret = FALSE;
289 + if (!flags) {
290 + if (virDomainSetVcpus(dom, count) != 0) {
291 + ret = FALSE;
293 + } else {
294 + if (virDomainSetVcpusFlags(dom, count, flags) < 0) {
295 + ret = FALSE;
299 virDomainFree(dom);
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
322 do a full boot.
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>
332 +I<migrateuri>
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>
342 +I<--live>
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
387 1.7.2.3