fix vserver start problems in 0.3 branch
authorBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Wed, 12 May 2010 17:03:24 +0000 (17:03 +0000)
committerBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Wed, 12 May 2010 17:03:24 +0000 (17:03 +0000)
python/vserver.py

index 44341a6..c05adb6 100644 (file)
@@ -278,14 +278,14 @@ class VServer:
         os.chroot(self.dir)
         os.chdir("/")
 
-    def chroot_call(self, fn, *args):
+    def chroot_call(self, fn, *args, **kwargs={}):
 
         cwd_fd = os.open(".", os.O_RDONLY)
         try:
             root_fd = os.open("/", os.O_RDONLY)
             try:
                 self.__do_chroot()
-                result = fn(*args)
+                result = fn(*args, **kwargs)
             finally:
                 os.fchdir(root_fd)
                 os.chroot(".")
@@ -450,7 +450,6 @@ class VServer:
 
 
     def start(self, runlevel = 3):
-
         if (os.fork() != 0):
             # Parent should just return.
             self.vm_running = True
@@ -458,62 +457,31 @@ class VServer:
         else:
             # child process
             try:
-                # so we don't chcontext with priv'ed fds
-                close_nonstandard_fds()
-
-                # get a new session
-                os.setsid()
+                subprocess.call("/usr/sbin/vserver %s start" % self.name, 
+                                shell=True)
 
-                # open state file to record vserver info
-                state_file = open("/var/run/vservers/%s" % self.name, "w")
-
-                # use /dev/null for stdin, /var/log/nm 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()
-                if not self.is_running():
-                    removed = self.__cleanvar()
-                else:
-                    removed = 0
-
-                log = open("/var/log/nm", "a", 0)
-                if log.fileno() != 1:
-                    os.dup2(log.fileno(), 1)
-                os.dup2(1, 2)
-
-                print >>log, ("%s: removing %s" % 
-                                (time.asctime(time.gmtime()), removed))
-                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)
+                # execute initscripts
                 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)
+                        cmd_args = [cmd[0]] + map(lambda x: x % arg_subst, cmd[1:])
+                        cmd_file = "/vservers/" + self.name + cmd[0]
+                        self.log(cmd_file)
+                        if os.path.isfile(cmd_file):
+                            self.log("executing '%s'" % " ".join(cmd_args))
+                            self.chroot_call(subprocess.call, " ".join(cmd_args), shell=True)
                         else:
-                            print >>log, "WARNING: could not run %s"%cmd[0]
+                            self.log("WARNING: could not run %s on %s" % (cmd[0], self.name))
                     except:
-                        print >>log, traceback.format_exc()
+                        self.log(traceback.format_exc())
 
             # we get here due to an exception in the top-level child process
             except Exception, ex:
                 self.log(traceback.format_exc())
             os._exit(0)
 
+
     def set_resources(self,setup=False):
 
         """ Called when vserver context is entered for first time,