From: Thierry Parmentelat Date: Tue, 22 Jun 2010 09:13:48 +0000 (+0000) Subject: merged from 0.4 X-Git-Tag: util-vserver-pl-0.3-31~2 X-Git-Url: http://git.onelab.eu/?p=util-vserver-pl.git;a=commitdiff_plain;h=9733cd9428af622ab00fdc0c519115bc386faf91 merged from 0.4 the initscript is now invoked by rc, which in turn invokes the slice initscript this requires NodeManager-2.0-10 --- diff --git a/python/vserver.py b/python/vserver.py index fea10c3..4724311 100644 --- a/python/vserver.py +++ b/python/vserver.py @@ -151,9 +151,6 @@ def adjust_lim(goal, curr): class VServer: - INITSCRIPTS = [('/etc/rc.vinit', 'start'), - ('/etc/rc.d/rc', '%(runlevel)d')] - def __init__(self, name, vm_id = None, vm_running = None, logfile=None): self.name = name @@ -172,12 +169,16 @@ class VServer: self.logfile = logfile # inspired from nodemanager's logger + def log_in_file (self, fd, msg): + if not msg: msg="\n" + if not msg.endswith('\n'): msg += '\n' + os.write(fd, '%s: %s' % (time.asctime(time.gmtime()), msg)) + def log(self,msg): if self.logfile: try: fd = os.open(self.logfile,os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0600) - if not msg.endswith('\n'): msg += '\n' - os.write(fd, '%s: %s' % (time.asctime(time.gmtime()), msg)) + self.log_in_file(fd,msg) os.close(fd) except: print '%s: (%s failed to open) %s'%(time.asctime(time.gmtime()),self.logfile,msg) @@ -448,37 +449,28 @@ class VServer: self.__do_chroot() self.__do_chcontext(None) - + # 2010 June 21 - Thierry + # the slice initscript now gets invoked through rc - see sliver_vs.py in nodemanager + # and, rc is triggered as part of vserver .. start + # so we don't have to worry about initscripts at all anymore here def start(self, runlevel = 3): - if (os.fork() != 0): + if os.fork() != 0: # Parent should just return. self.vm_running = True return else: - # child process + os.setsid() + # first child process: fork again + if os.fork() != 0: + os._exit(0) # Exit parent (the first child) of the second child. + # the grandson is the working one + os.chdir('/') + os.umask(0) try: - subprocess.call("/usr/sbin/vserver %s start" % self.name, - shell=True) - - # 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:]) - # add vserver name as the last command argument - cmd_args += [self.name] - 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: - self.log("WARNING: could not run %s on %s" % (cmd[0], self.name)) - except: - self.log(traceback.format_exc()) - - # we get here due to an exception in the top-level child process + # start the vserver + subprocess.call(["/usr/sbin/vserver",self.name,"start"]) + + # we get here due to an exception in the grandson process except Exception, ex: self.log(traceback.format_exc()) os._exit(0)