+ self.rlimits_changed = False
+ self.dir = "%s/%s" % (vserverimpl.VSERVER_BASEDIR, name)
+ if not (os.path.isdir(self.dir) and
+ os.access(self.dir, os.R_OK | os.W_OK | os.X_OK)):
+ raise NoSuchVServer, "no such vserver: " + name
+ self.config = VServerConfig(name, "/etc/vservers/%s" % name)
+ self.remove_caps = ~vserverimpl.CAP_SAFE;
+ if vm_id == None:
+ vm_id = int(self.config.get('context'))
+ self.ctx = vm_id
+ if vm_running == None:
+ vm_running = self.is_running()
+ self.vm_running = vm_running
+
+ def have_limits_changed(self):
+ return self.rlimits_changed
+
+ def set_rlimit_limit(self,type,hard,soft,minimum):
+ """Generic set resource limit function for vserver"""
+ global RLIMITS
+ changed = False
+ try:
+ old_hard, old_soft, old_minimum = self.get_rlimit_limit(type)
+ if old_hard != VC_LIM_KEEP and old_hard <> hard: changed = True
+ if old_soft != VC_LIM_KEEP and old_soft <> soft: changed = True
+ if old_minimum != VC_LIM_KEEP and old_minimum <> minimum: changed = True
+ self.rlimits_changed = self.rlimits_changed or changed
+ except OSError, e:
+ if self.is_running(): print "Unexpected error with getrlimit for running context %d" % self.ctx
+
+ resource_type = RLIMITS[type]
+ try:
+ ret = vserverimpl.setrlimit(self.ctx,resource_type,hard,soft,minimum)
+ except OSError, e:
+ if self.is_running(): print "Unexpected error with setrlimit for running context %d" % self.ctx
+
+ def set_rlimit_config(self,type,hard,soft,minimum):
+ """Generic set resource limit function for vserver"""
+ if hard <> VC_LIM_KEEP:
+ self.config.update('rlimits/%s.hard' % type.lower(), hard)
+ if soft <> VC_LIM_KEEP:
+ self.config.update('rlimits/%s.soft' % type.lower(), soft)
+ if minimum <> VC_LIM_KEEP:
+ self.config.update('rlimits/%s.min' % type.lower(), minimum)
+ self.set_rlimit_limit(type,hard,soft,minimum)
+
+ def get_rlimit_limit(self,type):
+ """Generic get resource configuration function for vserver"""
+ global RLIMITS
+ resource_type = RLIMITS[type]
+ try:
+ ret = vserverimpl.getrlimit(self.ctx,resource_type)
+ except OSError, e:
+ print "Unexpected error with getrlimit for context %d" % self.ctx
+ ret = self.get_rlimit_config(type)
+ return ret
+
+ def get_rlimit_config(self,type):
+ """Generic get resource configuration function for vserver"""
+ hard = int(self.config.get("rlimits/%s.hard"%type.lower(),VC_LIM_KEEP))
+ soft = int(self.config.get("rlimits/%s.soft"%type.lower(),VC_LIM_KEEP))
+ minimum = int(self.config.get("rlimits/%s.min"%type.lower(),VC_LIM_KEEP))
+ return (hard,soft,minimum)
+
+ def set_capabilities(self, capabilities):
+ return vserverimpl.setbcaps(self.ctx, vserverimpl.text2bcaps(capabilities))
+
+ def set_capabilities_config(self, capabilities):
+ self.config.update('bcapabilities', capabilities)
+ self.set_capabilities(capabilities)
+
+ def get_capabilities(self):
+ return vserverimpl.bcaps2text(vserverimpl.getbcaps(self.ctx))
+
+ def get_capabilities_config(self):
+ return self.config.get('bcapabilities', '')
+
+ def set_ipaddresses(self, addresses):
+ vserverimpl.netremove(self.ctx, "all")
+ for a in addresses.split(","):
+ vserverimpl.netadd(self.ctx, a)
+
+ def set_ipaddresses_config(self, addresses):
+ i = 0
+ for a in addresses.split(","):
+ self.config.update("interfaces/%d/ip" % i, a)
+ i += 1
+ while self.config.unset("interfaces/%d/ip" % i):
+ i += 1
+ self.set_ipaddresses(addresses)
+
+ def get_ipaddresses_config(self):
+ i = 0
+ ret = []
+ while True:
+ r = self.config.get("interfaces/%d/ip" % i, '')
+ if r == '':
+ break
+ ret += [r]
+ i += 1
+ return ",".join(ret)
+
+ def get_ipaddresses(self):
+ # No clean way to do this right now.
+ return None
+
+ def __do_chroot(self):
+ self.config.cache_it()
+ 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
+
+
+ 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")
+ vserverimpl.setrunlevel(f, runlevel)