+ def __update_config_file(self, filename, newvars):
+
+ # read old file, apply changes
+ f = open(filename, "r")
+ data = f.read()
+ f.close()
+ todo = newvars.copy()
+ changed = False
+ offset = 0
+ for m in self.config_var_re.finditer(data):
+ (key, val) = m.groups()
+ newval = todo.pop(key, None)
+ if newval != None:
+ data = data[:offset+m.start(2)] + str(newval) + data[offset+m.end(2):]
+ offset += len(str(newval)) - (m.end(2)-m.start(2))
+ changed = True
+ for (newkey, newval) in todo.items():
+ data += "%s=%s\n" % (newkey, newval)
+ changed = True
+
+ if not changed:
+ return
+
+ # write new file
+ newfile = filename + ".new"
+ f = open(newfile, "w")
+ f.write(data)
+ f.close()
+
+ # replace old file with new
+ os.rename(newfile, filename)
+
+ def __do_chroot(self):
+
+ os.chroot(self.dir)
+ os.chdir("/")
+
+ def chroot_call(self, fn, *args):
+
+ cwd_fd = os.open(".", os.O_RDONLY)
+ try:
+ root_fd = os.open("/", os.O_RDONLY)
+ try:
+ self.__do_chroot()
+ result = fn(*args)
+ finally:
+ os.fchdir(root_fd)
+ os.chroot(".")
+ os.fchdir(cwd_fd)
+ os.close(root_fd)
+ finally:
+ os.close(cwd_fd)
+ return result
+
+ def set_disklimit(self, block_limit):
+ # block_limit is in kB
+ if block_limit == 0:
+ try:
+ vserverimpl.unsetdlimit(self.dir, self.ctx)
+ except OSError, e:
+ print "Unexpected error with unsetdlimit for context %d" % self.ctx
+ return
+
+ if self.vm_running:
+ block_usage = vserverimpl.DLIMIT_KEEP
+ inode_usage = vserverimpl.DLIMIT_KEEP
+ else:
+ # init_disk_info() must have been called to get usage values
+ block_usage = self.disk_blocks
+ inode_usage = self.disk_inodes
+
+
+ try:
+ vserverimpl.setdlimit(self.dir,
+ self.ctx,
+ block_usage,
+ block_limit,
+ inode_usage,
+ vserverimpl.DLIMIT_INF, # inode limit
+ 2) # %age reserved for root
+ except OSError, e:
+ print "Unexpected error with setdlimit for context %d" % self.ctx
+
+
+ resources = {'VS_DISK_MAX': block_limit}
+ self.update_resources(resources)
+
+ def is_running(self):
+ return vserverimpl.isrunning(self.ctx)
+
+ def get_disklimit(self):
+
+ try:
+ (self.disk_blocks, block_limit, self.disk_inodes, inode_limit,
+ reserved) = vserverimpl.getdlimit(self.dir, self.ctx)
+ except OSError, ex:
+ if ex.errno != errno.ESRCH:
+ raise
+ # get here if no vserver disk limit has been set for xid
+ block_limit = -1
+
+ return block_limit
+
+ def set_sched_config(self, cpu_share, sched_flags):
+
+ """ Write current CPU scheduler parameters to the vserver
+ configuration file. This method does not modify the kernel CPU
+ scheduling parameters for this context. """
+
+ if cpu_share == int(self.config.get("CPULIMIT", -1)):
+ return
+ cpu_guaranteed = sched_flags & SCHED_CPU_GUARANTEED
+ cpu_config = { "CPULIMIT": cpu_share, "CPUGUARANTEED": cpu_guaranteed }
+ self.update_resources(cpu_config)
+ if self.vm_running:
+ self.set_sched(cpu_share, sched_flags)
+
+ def set_sched(self, cpu_share, sched_flags = 0):
+ """ Update kernel CPU scheduling parameters for this context. """
+ vserverimpl.setsched(self.ctx, cpu_share, sched_flags)
+
+ def get_sched(self):
+ # have no way of querying scheduler right now on a per vserver basis
+ return (-1, False)
+
+ def set_bwlimit(self, minrate = bwlimit.bwmin, maxrate = None,
+ exempt_min = None, exempt_max = None,
+ share = None, dev = "eth0"):
+
+ if minrate is None:
+ bwlimit.off(self.ctx, dev)
+ else:
+ bwlimit.on(self.ctx, dev, share,
+ minrate, maxrate, exempt_min, exempt_max)
+
+ def get_bwlimit(self, dev = "eth0"):
+
+ result = bwlimit.get(self.ctx)
+ # result of bwlimit.get is (ctx, share, minrate, maxrate)
+ if result:
+ result = result[1:]
+ return result
+
+ def open(self, filename, mode = "r", bufsize = -1):
+
+ return self.chroot_call(open, filename, mode, bufsize)
+
+ def __do_chcontext(self, state_file):
+
+ if state_file:
+ print >>state_file, "S_CONTEXT=%u" % self.ctx
+ print >>state_file, "S_PROFILE="
+ state_file.close()
+
+ if vserverimpl.chcontext(self.ctx):
+ self.set_resources()
+ vserverimpl.setup_done(self.ctx)
+
+ def __prep(self, runlevel, log):
+
+ """ Perform all the crap that the vserver script does before
+ actually executing the startup scripts. """
+
+ # remove /var/run and /var/lock/subsys files
+ # but don't remove utmp from the top-level /var/run
+ RUNDIR = "/var/run"
+ LOCKDIR = "/var/lock/subsys"
+ filter_fn = lambda fs: filter(lambda f: f != 'utmp', fs)
+ garbage = reduce((lambda (out, ff), (dir, subdirs, files):
+ (out + map((dir + "/").__add__, ff(files)),
+ lambda fs: fs)),
+ list(os.walk(RUNDIR)),
+ ([], filter_fn))[0]
+ garbage += filter(os.path.isfile, map((LOCKDIR + "/").__add__,
+ os.listdir(LOCKDIR)))
+ for f in garbage:
+ os.unlink(f)
+
+ # set the initial runlevel
+ f = open(RUNDIR + "/utmp", "w")
+ utmp.set_runlevel(f, runlevel)
+ f.close()
+
+ # mount /proc and /dev/pts
+ self.__do_mount("none", "/proc", "proc")
+ # XXX - magic mount options
+ self.__do_mount("none", "/dev/pts", "devpts", 0, "gid=5,mode=0620")
+
+ def __do_mount(self, *mount_args):
+
+ try:
+ mountimpl.mount(*mount_args)
+ except OSError, ex:
+ if ex.errno == errno.EBUSY:
+ # assume already mounted
+ return
+ raise ex
+
+ def enter(self):
+
+ state_file = open("/var/run/vservers/%s.ctx" % self.name, "w")
+ self.__do_chroot()
+ self.__do_chcontext(state_file)
+
+ def start(self, wait, runlevel = 3):
+ self.vm_running = True
+ self.rlimits_changed = False