Use cgroup CPU scheduler
[util-vserver-pl.git] / python / vserver.py
index 654077f..fa58beb 100644 (file)
@@ -41,6 +41,8 @@ RLIMITS = { "NSOCK": VLIMIT_NSOCK,
             "ANON": VLIMIT_ANON,
             "SHMEM": VLIMIT_SHMEM}
 
+CPU_SHARE_MULT = 1024
+
 # add in the platform supported rlimits
 for entry in resource.__dict__.keys():
     if entry.find("RLIMIT_")==0:
@@ -200,6 +202,9 @@ class VServer:
     def set_capabilities_config(self, capabilities):
         bcaps = self.get_bcaps_from_capabilities(capabilities)
         ccaps = self.get_ccaps_from_capabilities(capabilities)
+        if len(bcaps) > 0:
+            bcaps += ","
+        bcaps += "CAP_NET_RAW"
         self.config.update('bcapabilities', bcaps)
         self.config.update('ccapabilities', ccaps)
         ret = vserverimpl.setbcaps(self.ctx, vserverimpl.text2bcaps(bcaps))
@@ -227,6 +232,7 @@ class VServer:
             vserverimpl.netadd(self.ctx, a)
 
     def set_ipaddresses_config(self, addresses):
+        return
         i = 0
         for a in addresses.split(","):
             self.config.update("interfaces/%d/ip" % i, a)
@@ -328,24 +334,27 @@ class VServer:
     def set_sched_config(self, cpu_min, cpu_share):
 
         """ Write current CPU scheduler parameters to the vserver
-        configuration file. This method does not modify the kernel CPU
-        scheduling parameters for this context. """
-
-        self.config.update('sched/fill-rate', cpu_min)
-        self.config.update('sched/fill-rate2', cpu_share)
-        if cpu_share == 0:
-            self.config.unset('sched/idle-time')
-        
+        configuration file. Currently, 'cpu_min' is not supported. """
+        self.config.update('cgroup/cpu.shares', cpu_share * CPU_SHARE_MULT)
         if self.is_running():
             self.set_sched(cpu_min, cpu_share)
 
     def set_sched(self, cpu_min, cpu_share):
-        """ Update kernel CPU scheduling parameters for this context. """
-        vserverimpl.setsched(self.ctx, cpu_min, cpu_share)
+        """ Update kernel CPU scheduling parameters for this context.
+        Currently, 'cpu_min' is not supported. """
+        try:
+            cgroup = open('/dev/cgroup/%s/cpu.shares' % name, 'w')
+            cgroup.write('%s' % (cpu_share * CPU_SHARE_MULT))
+            cgroup.close()
+        except:
+            pass
 
     def get_sched(self):
-        # have no way of querying scheduler right now on a per vserver basis
-        return (-1, False)
+        try:
+            cpu_share = int(int(self.config.get('cgroup/cpu.shares')) / CPU_SHARE_MULT)
+        except:
+            cpu_share = False
+        return (-1, cpu_share)
 
     def set_bwlimit(self, minrate = bwlimit.bwmin, maxrate = None,
                     exempt_min = None, exempt_max = None,
@@ -419,12 +428,9 @@ class VServer:
             raise ex
 
     def enter(self):
-        self.config.cache_it()
-        self.__do_chroot()
-        self.__do_chcontext(None)
+        subprocess.call("/usr/sbin/vserver %s enter" % self.name, shell=True)
 
     def start(self, runlevel = 3):
-
         if (os.fork() != 0):
             # Parent should just return.
             self.vm_running = True
@@ -432,48 +438,8 @@ class VServer:
         else:
             # child process
             try:
-                # so we don't chcontext with priv'ed fds
-                close_nonstandard_fds()
-
-                # get a new session
-                os.setsid()
-
-                # open state file to record vserver info
-                state_file = open("/var/run/vservers/%s" % self.name, "w")
-
-                # use /dev/null for stdin, /var/log/boot.log for stdout/err
-                fd = os.open("/dev/null", os.O_RDONLY)
-                if fd != 0:
-                    os.dup2(fd, 0)
-                    os.close(fd)
-                # perform pre-init cleanup
-                self.__prep(runlevel)
-
-                self.config.cache_it()
-                self.__do_chroot()
-                log = open("/var/log/boot.log", "a", 0)
-                if log.fileno() != 1:
-                    os.dup2(log.fileno(), 1)
-                os.dup2(1, 2)
-
-                print >>log, ("%s: starting the virtual server %s" %
-                              (time.asctime(time.gmtime()), self.name))
-                # execute each init script in turn
-                # XXX - we don't support all scripts that vserver script does
-                self.__do_chcontext(state_file)
-                for cmd in self.INITSCRIPTS:
-                    try:
-                        # enter vserver context
-                        arg_subst = { 'runlevel': runlevel }
-                        cmd_args = [cmd[0]] + map(lambda x: x % arg_subst,
-                                                   cmd[1:])
-                        if os.path.isfile(cmd[0]):                         
-                            print >>log, "executing '%s'" % " ".join(cmd_args)
-                            os.spawnvp(os.P_NOWAIT,cmd[0],cmd_args)
-                    except:
-                        print >>log, traceback.format_exc()
-
+                subprocess.call("/usr/sbin/vserver %s start" % self.name, 
+                                shell=True)
             # we get here due to an exception in the top-level child process
             except Exception, ex:
                 self.log(traceback.format_exc())
@@ -514,8 +480,8 @@ class VServer:
         return self.disk_blocks * 1024
 
     def stop(self, signal = signal.SIGKILL):
-        vserverimpl.killall(self.ctx, signal)
         self.vm_running = False
+        subprocess.call("/usr/sbin/vserver %s stop" % self.name, shell=True)
 
     def setname(self, slice_id):
         '''Set vcVHI_CONTEXT field in kernel to slice_id'''