Added set_dlimit and get_dlimit to be used by NM to enforce disk limits.
[util-vserver.git] / python / vserver.py
index 39d15f2..2d72bd0 100644 (file)
@@ -12,7 +12,7 @@ import mountimpl
 import linuxcaps
 import passfdimpl
 import utmp
-import vserverimpl
+import vserverimpl, vduimpl
 
 from util_vserver_vars import *
 
@@ -81,6 +81,24 @@ class VServer:
 
         return os.chroot("%s/%s" % (VROOTDIR, self.name))
 
+    def set_dlimit(self, blocktotal):
+        path = "%s/%s" % (VROOTDIR, self.name)
+        inodes, blockcount, size = vduimpl.vdu(path)
+        vserverimpl.setdlimit(path, self.ctx, blockcount>>1, blocktotal, inodes, -1, 2)
+
+    def get_dlimit(self):
+        path = "%s/%s" % (VROOTDIR, 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 open(self, filename, mode = "r", bufsize = -1):
 
         (sendsock, recvsock) = passfdimpl.socketpair()
@@ -193,7 +211,7 @@ class VServer:
         self.__do_chroot()
         self.__do_chcontext(state_file)
 
-    def start(self, runlevel = 3):
+    def start(self, wait, runlevel = 3):
 
         child_pid = os.fork()
         if child_pid == 0:
@@ -223,18 +241,19 @@ class VServer:
                 # XXX - we don't support all scripts that vserver script does
                 cmd_pid = 0
                 for cmd in self.INITSCRIPTS + [None]:
-                    # don't bother waiting for last command to terminate
-                    if cmd == None:
-                        os._exit(0)
-
-                    # wait for previous command to terminate
-                    if cmd_pid:
+                    # wait for previous command to terminate, unless it
+                    # is the last one and the caller has specified to wait
+                    if cmd_pid and (cmd != None or wait):
                         try:
                             os.waitpid(cmd_pid, 0)
                         except:
                             print >>log, "error waiting for %s:" % cmd_pid
                             traceback.print_exc()
 
+                    # end of list
+                    if cmd == None:
+                        os._exit(0)
+
                     # fork and exec next command
                     cmd_pid = os.fork()
                     if cmd_pid == 0: