+ 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
+
+
+ self.config.update('dlimits/0/space_total', block_limit)
+
+ 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 sched_flags & SCHED_CPU_GUARANTEED:
+ cpu_guaranteed = cpu_share
+ else:
+ cpu_guaranteed = 0
+ self.config.update('sched/fill-rate2', cpu_share)
+ self.config.update('sched/fill-rate', cpu_guaranteed)
+
+ 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, "%u" % self.ctx
+ state_file.close()
+
+ if vserverimpl.chcontext(self.ctx, vserverimpl.text2bcaps(self.get_capabilities_config())):
+ 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)))
+ if False:
+ 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):
+ self.__do_chroot()
+ self.__do_chcontext(None)
+
+ def start(self, wait, runlevel = 3):
+ self.vm_running = True
+ self.rlimits_changed = False