Fix to implement chcontext so that NM's vserver start works correctly.
[util-vserver.git] / python / vserver.py
index 666ef00..2fb1799 100644 (file)
@@ -13,6 +13,7 @@ import linuxcaps
 import passfdimpl
 import utmp
 import vserverimpl, vduimpl
+import cpulimit, bwlimit
 
 from util_vserver_vars import *
 
@@ -42,6 +43,11 @@ FLAGS_HIDEINFO = 32
 FLAGS_ULIMIT = 64
 FLAGS_NAMESPACE = 128
 
+# default values for new vserver scheduler
+SCHED_TOKENS_MIN = 50
+SCHED_TOKENS_MAX = 100
+SCHED_TOKENS = 100
+SCHED_INTERVAL = 1000
 
               
 class VServer:
@@ -79,10 +85,10 @@ class VServer:
 
     def __do_chroot(self):
 
-        return os.chroot("%s/%s" % (VROOTDIR, self.name))
+        return os.chroot("%s/%s" % (DEFAULT_VSERVERDIR, self.name))
 
-    def set_dlimit(self, blocktotal):
-        path = "%s/%s" % (VROOTDIR, self.name)
+    def set_disklimit(self, blocktotal):
+        path = "%s/%s" % (DEFAULT_VSERVERDIR, self.name)
         inodes, blockcount, size = vduimpl.vdu(path)
         blockcount = blockcount >> 1
 
@@ -93,8 +99,8 @@ class VServer:
             # should raise some error value
             print "block limit (%d) ignored for vserver %s" %(blocktotal,self.name)
 
-    def get_dlimit(self):
-        path = "%s/%s" % (VROOTDIR, self.name)
+    def get_disklimit(self):
+        path = "%s/%s" % (DEFAULT_VSERVERDIR, self.name)
         try:
             blocksused, blocktotal, inodesused, inodestotal, reserved = \
                         vserverimpl.getdlimit(path,self.ctx)
@@ -106,6 +112,74 @@ class VServer:
 
         return blocktotal
 
+    def set_sched(self, shares = 32, besteffort = True):
+        # for the old CKRM scheduler
+        if cpulimit.checkckrm() is True:
+            cpulimit.cpuinit()
+            cpulimit.vs2ckrm_on(self.name)
+            try:
+                cpulimit.cpulimit(self.name,shares)
+            except OSError, ex:
+                if ex.errno == 22:
+                    print "invalid shares argument"
+                    # should re-raise exception?!
+
+        # for the new vserver scheduler
+        else:
+            global SCHED_TOKENS_MIN, SCHED_TOKENS_MAX, SCHED_TOKENS, SCHED_INTERVAL
+            tokensmin = SCHED_TOKENS_MIN
+            tokensmax = SCHED_TOKENS_MAX
+            tokens    = SCHED_TOKENS
+            interval  = SCHED_INTERVAL
+            fillrate = shares
+
+            if besteffort is True:
+                cpuguaranteed = 0
+            else:
+                cpuguaranteed = 1
+
+            try:
+                vserverimpl.setsched(self.ctx,fillrate,interval,tokens,tokensmin,tokensmax,cpuguaranteed)
+            except OSError, ex:
+                if ex.errno == 22:
+                    print "kernel does not support vserver scheduler"
+                else:
+                    raise ex
+
+    def get_sched(self):
+        # have no way of querying scheduler right now on a per vserver basis
+        return (-1, False)
+
+    def set_memlimit(self, limit):
+        ret = vserverimpl.setrlimit(self.ctx,5,limit)
+        return ret
+
+    def get_memlimit(self):
+        ret = vserverimpl.getrlimit(self.ctx,5)
+        return ret
+    
+    def set_tasklimit(self, limit):
+        ret = vserverimpl.setrlimit(self.ctx,6,limit)
+        return ret
+
+    def get_tasklimit(self):
+        ret = vserverimpl.getrlimit(self.ctx,6)
+        return ret
+
+    def set_bwlimit(self, eth, limit, cap, minrate, maxrate):
+        if cap == "-1":
+            bwlimit.off(self.ctx,eth)
+        else:
+            bwlimit.on(self.ctx, eth, limit, cap, minrate, maxrate)
+
+    def get_bwlimit(self, eth):
+        # not implemented yet
+        bwlimit = -1
+        cap = "unknown"
+        minrate = "unknown"
+        maxrate = "unknown"
+        return (bwlimit, cap, minrate, maxrate)
+        
     def open(self, filename, mode = "r", bufsize = -1):
 
         (sendsock, recvsock) = passfdimpl.socketpair()
@@ -165,7 +239,8 @@ class VServer:
 
     def __do_chcontext(self, state_file = None):
 
-        vserverimpl.chcontext(self.ctx, self.remove_caps)
+        vserverimpl.chcontext(self.ctx)
+
         if not state_file:
             return
         print >>state_file, "S_CONTEXT=%d" % self.ctx