more fixes
[util-vserver.git] / python / vserver.py
index 64cc681..b2fe876 100644 (file)
@@ -12,7 +12,8 @@ import mountimpl
 import linuxcaps
 import passfdimpl
 import utmp
-import vserverimpl
+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,8 +85,101 @@ 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_disklimit(self, blocktotal):
+        path = "%s/%s" % (DEFAULT_VSERVERDIR, self.name)
+        inodes, blockcount, size = vduimpl.vdu(path)
+        blockcount = blockcount >> 1
+
+        if blocktotal > blockcount:
+            vserverimpl.setdlimit(path, self.ctx, blockcount>>1, \
+                                  blocktotal, inodes, -1, 2)
+        else:
+            # should raise some error value
+            print "block limit (%d) ignored for vserver %s" %(blocktotal,self.name)
+
+    def get_disklimit(self):
+        path = "%s/%s" % (DEFAULT_VSERVERDIR, self.name)
+        try:
+            blocksused, blocktotal, inodesused, inodestotal, reserved = \
+                        vserverimpl.getdlimit(path,self.ctx)
+        except OSError, ex:
+            if ex.errno == 3:
+                # get here if no vserver disk limit has been set for xid
+                # set blockused to -1 to indicate no limit
+                blocktotal = -1
+
+        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()
@@ -140,7 +239,11 @@ class VServer:
 
     def __do_chcontext(self, state_file = None):
 
-        vserverimpl.chcontext(self.ctx, self.remove_caps)
+        vserverimpl.create(self.ctx)
+        vserverimpl.flags(self.ctx)
+        self.set_sched()
+        vserverimpl.enter(self.ctx)
+
         if not state_file:
             return
         print >>state_file, "S_CONTEXT=%d" % self.ctx