[Libvir] Add 'setvcpus', 'setmem', 'setmaxmem' to virsh

Daniel P. Berrange berrange at redhat.com
Wed Aug 16 14:26:33 UTC 2006


The attached patch adds three new commands to virsh:

    setmem          change memory allocation
    setmaxmem       change maximum memory limit
    setvcpus        change number of virtual CPUs

It also ensures then when using the 'test' hypervisor driver, it always
opens a read-write connection even as non-root. The test hypervisor does
not have any distinction between privileged / unprivileged users, so this
test for 'uuid == 0' was bogus.

Regards,
Dan.
-- 
|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-           Perl modules: http://search.cpan.org/~danberr/              -=|
|=-               Projects: http://freshmeat.net/~danielpb/               -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 
-------------- next part --------------
Index: src/virsh.c
===================================================================
RCS file: /data/cvs/libvirt/src/virsh.c,v
retrieving revision 1.33
diff -c -r1.33 virsh.c
*** src/virsh.c	7 Aug 2006 14:35:20 -0000	1.33
--- src/virsh.c	16 Aug 2006 15:18:48 -0000
***************
*** 964,969 ****
--- 964,1098 ----
  }
  
  /*
+  * "setvcpus" command
+  */
+ static vshCmdInfo info_setvcpus[] = {
+     {"syntax", "setvcpus <domain> <count>"},
+     {"help", "change number of virtual CPUs"},
+     {"desc", "Change the number of virtual CPUs active in the guest domain"},
+     {NULL, NULL}
+ };
+ 
+ static vshCmdOptDef opts_setvcpus[] = {
+     {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name, id or uuid"},
+     {"count", VSH_OT_DATA, VSH_OFLAG_REQ, "number of virtual CPUs"},
+     {NULL, 0, 0, NULL}
+ };
+ 
+ static int
+ cmdSetvcpus(vshControl * ctl, vshCmd * cmd)
+ {
+     virDomainPtr dom;
+     int count;
+     int ret = TRUE;
+ 
+     if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
+         return FALSE;
+ 
+     if (!(dom = vshCommandOptDomain(ctl, cmd, "domain", NULL)))
+         return FALSE;
+ 
+     count = vshCommandOptInt(cmd, "count", &count);
+     if (!count) {
+         virDomainFree(dom);
+         return FALSE;
+     }
+ 
+     if (virDomainSetVcpus(dom, count) != 0) {
+         ret = FALSE;
+     }
+ 
+     virDomainFree(dom);
+     return ret;
+ }
+ 
+ /*
+  * "setmemory" command
+  */
+ static vshCmdInfo info_setmem[] = {
+     {"syntax", "setmem <domain> <bytes>"},
+     {"help", "change memory allocation"},
+     {"desc", "Change the current memory allocation in the guest domain"},
+     {NULL, NULL}
+ };
+ 
+ static vshCmdOptDef opts_setmem[] = {
+     {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name, id or uuid"},
+     {"bytes", VSH_OT_DATA, VSH_OFLAG_REQ, "number of bytes of memory"},
+     {NULL, 0, 0, NULL}
+ };
+ 
+ static int
+ cmdSetmem(vshControl * ctl, vshCmd * cmd)
+ {
+     virDomainPtr dom;
+     int bytes;
+     int ret = TRUE;
+ 
+     if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
+         return FALSE;
+ 
+     if (!(dom = vshCommandOptDomain(ctl, cmd, "domain", NULL)))
+         return FALSE;
+ 
+     bytes = vshCommandOptInt(cmd, "bytes", &bytes);
+     if (!bytes) {
+         virDomainFree(dom);
+         return FALSE;
+     }
+ 
+     if (virDomainSetMemory(dom, bytes) != 0) {
+         ret = FALSE;
+     }
+ 
+     virDomainFree(dom);
+     return ret;
+ }
+ 
+ /*
+  * "setmaxmem" command
+  */
+ static vshCmdInfo info_setmaxmem[] = {
+     {"syntax", "setmaxmem <domain> <bytes>"},
+     {"help", "change maximum memory limit"},
+     {"desc", "Change the maximum memory allocation limit in the guest domain"},
+     {NULL, NULL}
+ };
+ 
+ static vshCmdOptDef opts_setmaxmem[] = {
+     {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name, id or uuid"},
+     {"bytes", VSH_OT_DATA, VSH_OFLAG_REQ, "maxmimum memory limit in bytes"},
+     {NULL, 0, 0, NULL}
+ };
+ 
+ static int
+ cmdSetmaxmem(vshControl * ctl, vshCmd * cmd)
+ {
+     virDomainPtr dom;
+     int bytes;
+     int ret = TRUE;
+ 
+     if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
+         return FALSE;
+ 
+     if (!(dom = vshCommandOptDomain(ctl, cmd, "domain", NULL)))
+         return FALSE;
+ 
+     bytes = vshCommandOptInt(cmd, "bytes", &bytes);
+     if (!bytes) {
+         virDomainFree(dom);
+         return FALSE;
+     }
+ 
+     if (virDomainSetMaxMemory(dom, bytes) != 0) {
+         ret = FALSE;
+     }
+ 
+     virDomainFree(dom);
+     return ret;
+ }
+ 
+ /*
   * "nodeinfo" command
   */
  static vshCmdInfo info_nodeinfo[] = {
***************
*** 1250,1255 ****
--- 1379,1387 ----
      {"resume", cmdResume, opts_resume, info_resume},
      {"save", cmdSave, opts_save, info_save},
      {"shutdown", cmdShutdown, opts_shutdown, info_shutdown},
+     {"setmem", cmdSetmem, opts_setmem, info_setmem},
+     {"setmaxmem", cmdSetmaxmem, opts_setmaxmem, info_setmaxmem},
+     {"setvcpus", cmdSetvcpus, opts_setvcpus, info_setvcpus},
      {"suspend", cmdSuspend, opts_suspend, info_suspend},
      {"vcpuinfo", cmdVcpuinfo, opts_vcpuinfo, info_vcpuinfo},
      {"vcpupin", cmdVcpupin, opts_vcpupin, info_vcpupin},
***************
*** 1945,1952 ****
      /* set up the library error handler */
      virSetErrorFunc(NULL, virshErrorHandler);
  
!     /* basic connection to hypervisor */
!     if (ctl->uid == 0)
          ctl->conn = virConnectOpen(ctl->name);
      else
          ctl->conn = virConnectOpenReadOnly(ctl->name);
--- 2077,2086 ----
      /* set up the library error handler */
      virSetErrorFunc(NULL, virshErrorHandler);
  
!     /* basic connection to hypervisor, for Xen connections unless
!        we're root open a read only connections. Allow 'test' HV
!        to be RW all the time though */
!     if (ctl->uid == 0 || (ctl->name && !strncmp(ctl->name, "test", 4)))
          ctl->conn = virConnectOpen(ctl->name);
      else
          ctl->conn = virConnectOpenReadOnly(ctl->name);


More information about the libvir-list mailing list