1 # Copyright 2005 Princeton University
15 import vserverimpl, vduimpl
16 import cpulimit, bwlimit
21 # these are the flags taken from the kernel linux/vserver/legacy.h
24 FLAGS_SCHED = 2 # XXX - defined in util-vserver/src/chcontext.c
36 INITSCRIPTS = [('/etc/rc.vinit', 'start'),
37 ('/etc/rc.d/rc', '%(runlevel)d')]
39 def __init__(self, name, vm_id, vm_running = False, resources = {}):
42 self.config_file = "/etc/vservers/%s.conf" % name
43 self.dir = "%s/%s" % (vserverimpl.VSERVER_BASEDIR, name)
44 if not (os.path.isdir(self.dir) and
45 os.access(self.dir, os.R_OK | os.W_OK | os.X_OK)):
46 raise Exception, "no such vserver: " + name
48 for config_file in ["/etc/vservers.conf", self.config_file]:
50 self.config.update(self.__read_config_file(config_file))
52 if ex.errno != errno.ENOENT:
54 self.remove_caps = ~vserverimpl.CAP_SAFE;
56 self.vm_running = vm_running
57 self.resources = resources
59 config_var_re = re.compile(r"^ *([A-Z_]+)=(.*)\n?$", re.MULTILINE)
61 def __read_config_file(self, filename):
63 f = open(filename, "r")
67 for m in self.config_var_re.finditer(data):
68 (key, val) = m.groups()
69 config[key] = val.strip('"')
72 def __update_config_file(self, filename, newvars):
74 # read old file, apply changes
75 f = open(filename, "r")
80 for m in self.config_var_re.finditer(data):
81 (key, val) = m.groups()
82 newval = todo.pop(key, None)
84 data = data[:m.start(2)] + str(newval) + data[m.end(2):]
86 for (newkey, newval) in todo.items():
87 data += "%s=%s\n" % (newkey, newval)
94 newfile = filename + ".new"
95 f = open(newfile, "w")
99 # 'copy' original file, rename new to original
100 backup = filename + ".old"
104 if ex.errno != errno.ENOENT:
106 os.link(filename, backup)
107 os.rename(newfile, filename)
109 def __do_chroot(self):
114 def set_disklimit(self, block_limit):
116 # block_limit is in kB
118 vserverimpl.unsetdlimit(self.dir, self.ctx)
122 block_usage = vserverimpl.DLIMIT_KEEP
123 inode_usage = vserverimpl.DLIMIT_KEEP
125 # init_disk_info() must have been called to get usage values
126 block_usage = self.disk_blocks
127 inode_usage = self.disk_inodes
129 vserverimpl.setdlimit(self.dir,
134 vserverimpl.DLIMIT_INF, # inode limit
135 2) # %age reserved for root
137 def get_disklimit(self):
140 (self.disk_blocks, block_limit, self.disk_inodes, inode_limit,
141 reserved) = vserverimpl.getdlimit(self.dir, self.ctx)
143 if ex.errno != errno.ESRCH:
145 # get here if no vserver disk limit has been set for xid
150 def set_sched(self, cpu_share):
152 if cpu_share == int(self.config.get("CPULIMIT", -1)):
154 # XXX - don't want to have to deal with nm_ flags here
155 cpu_guaranteed = int(self.resources.get("nm_sched_flags",
156 None) == "guaranteed")
157 cpu_config = { "CPULIMIT": cpu_share, "CPUGUARANTEED": cpu_guaranteed }
158 self.__update_config_file(self.config_file, cpu_config)
160 # caller must ensure cpu_share is consistent with self.resources
161 vserverimpl.setsched(self.ctx, self.resources)
164 # have no way of querying scheduler right now on a per vserver basis
167 def set_memlimit(self, limit):
168 ret = vserverimpl.setrlimit(self.ctx,5,limit)
171 def get_memlimit(self):
172 ret = vserverimpl.getrlimit(self.ctx,5)
175 def set_tasklimit(self, limit):
176 ret = vserverimpl.setrlimit(self.ctx,6,limit)
179 def get_tasklimit(self):
180 ret = vserverimpl.getrlimit(self.ctx,6)
183 def set_bwlimit(self, share, minrate = 1, maxrate = None, dev = "eth0"):
186 bwlimit.on(self.ctx, dev, share, minrate, maxrate)
188 bwlimit.off(self.ctx, dev)
190 def get_bwlimit(self, eth):
191 # not implemented yet
196 return (bwlimit, cap, minrate, maxrate)
198 def open(self, filename, mode = "r", bufsize = -1):
200 (sendsock, recvsock) = passfdimpl.socketpair()
201 child_pid = os.fork()
206 f = open(filename, mode)
207 passfdimpl.sendmsg(f.fileno(), sendsock)
209 except EnvironmentError, ex:
210 (result, errmsg) = (ex.errno, ex.strerror)
211 except Exception, ex:
212 (result, errmsg) = (255, str(ex))
213 os.write(sendsock, errmsg)
218 # XXX - need this since a lambda can't raise an exception
223 throw = lambda : __throw(Exception(errmsg))
226 (pid, status) = os.waitpid(child_pid, 0)
227 if os.WIFEXITED(status):
228 result = os.WEXITSTATUS(status)
230 errmsg = os.strerror(result)
231 throw = lambda : __throw(IOError(result, errmsg))
233 errmsg = "unexpected exception in child"
236 errmsg = "child killed"
239 if ex.errno != errno.EINTR:
242 fcntl.fcntl(recvsock, fcntl.F_SETFL, os.O_NONBLOCK)
244 (fd, errmsg) = passfdimpl.recvmsg(recvsock)
246 if ex.errno != errno.EAGAIN:
247 throw = lambda : __throw(ex)
253 return os.fdopen(fd, mode, bufsize)
255 def __do_chcontext(self, state_file):
258 print >>state_file, "S_CONTEXT=%u" % self.ctx
259 print >>state_file, "S_PROFILE="
262 vserverimpl.chcontext(self.ctx, self.resources)
264 def __prep(self, runlevel, log):
266 """ Perform all the crap that the vserver script does before
267 actually executing the startup scripts. """
269 # remove /var/run and /var/lock/subsys files
270 # but don't remove utmp from the top-level /var/run
272 LOCKDIR = "/var/lock/subsys"
273 filter_fn = lambda fs: filter(lambda f: f != 'utmp', fs)
274 garbage = reduce((lambda (out, ff), (dir, subdirs, files):
275 (out + map((dir + "/").__add__, ff(files)),
277 list(os.walk(RUNDIR)),
279 garbage += filter(os.path.isfile, map((LOCKDIR + "/").__add__,
280 os.listdir(LOCKDIR)))
284 # set the initial runlevel
285 f = open(RUNDIR + "/utmp", "w")
286 utmp.set_runlevel(f, runlevel)
289 # mount /proc and /dev/pts
290 self.__do_mount("none", "/proc", "proc")
291 # XXX - magic mount options
292 self.__do_mount("none", "/dev/pts", "devpts", 0, "gid=5,mode=0620")
294 def __do_mount(self, *mount_args):
297 mountimpl.mount(*mount_args)
299 if ex.errno == errno.EBUSY:
300 # assume already mounted
306 state_file = open("/var/run/vservers/%s.ctx" % self.name, "w")
308 self.__do_chcontext(state_file)
310 def start(self, wait, runlevel = 3):
312 self.vm_running = True
314 child_pid = os.fork()
321 # open state file to record vserver info
322 state_file = open("/var/run/vservers/%s.ctx" % self.name, "w")
324 # use /dev/null for stdin, /var/log/boot.log for stdout/err
327 os.open("/dev/null", os.O_RDONLY)
329 log = open("/var/log/boot.log", "w", 0)
332 print >>log, ("%s: starting the virtual server %s" %
333 (time.asctime(time.gmtime()), self.name))
335 # perform pre-init cleanup
336 self.__prep(runlevel, log)
338 # execute each init script in turn
339 # XXX - we don't support all scripts that vserver script does
342 for cmd in self.INITSCRIPTS + [None]:
343 # wait for previous command to terminate, unless it
344 # is the last one and the caller has specified to wait
345 if cmd_pid and (cmd != None or wait):
347 os.waitpid(cmd_pid, 0)
349 print >>log, "error waiting for %s:" % cmd_pid
350 traceback.print_exc()
356 # fork and exec next command
360 # enter vserver context
361 self.__do_chcontext(state_file)
362 arg_subst = { 'runlevel': runlevel }
363 cmd_args = [cmd[0]] + map(lambda x: x % arg_subst,
365 print >>log, "executing '%s'" % " ".join(cmd_args)
366 os.execl(cmd[0], *cmd_args)
368 traceback.print_exc()
371 # don't want to write state_file multiple times
374 # we get here due to an exception in the top-level child process
375 except Exception, ex:
376 traceback.print_exc()
382 def update_resources(self, resources):
384 self.config.update(resources)
386 # write new values to configuration file
387 self.__update_config_file(self.config_file, resources)
389 def init_disk_info(self):
391 (self.disk_inodes, self.disk_blocks, size) = vduimpl.vdu(self.dir)
395 def stop(self, signal = signal.SIGKILL):
397 vserverimpl.killall(self.ctx, signal)
398 self.vm_running = False