def forkHelper(cmd, fd, inputHandler, closeToChild): child = xPopen3(cmd, True, -1, [fd]) if closeToChild: child.tochild.close() thread = threading.Thread(target=slurp, args=(child.childerr, )) thread.start() try: try: while 1: line = child.fromchild.readline() if line == "": break else: line = line.rstrip() log.debug('%s', line) inputHandler(line, child.tochild) except IOError, exn: raise XendError('Error reading from child process for %s: %s' % (cmd, exn)) finally: child.fromchild.close() if not closeToChild: child.tochild.close() thread.join() child.childerr.close() status = child.wait() if status >> 8 == 127: raise XendError("%s failed: popen failed" % string.join(cmd)) elif status != 0: raise XendError("%s failed" % string.join(cmd))
def domain_destroy(self, domid): """Terminate domain immediately.""" dominfo = self.domain_lookup_by_name_or_id_nr(domid) if dominfo and dominfo.getDomid() == PRIV_DOMAIN: raise XendError("Cannot destroy privileged domain %s" % domid) if dominfo: val = dominfo.destroy() else: try: val = xc.domain_destroy(domid) except Exception, ex: raise XendError(str(ex))
def vnet(self, x): val = None vnetinfo = self.xvnet.vnet_get(x) if not vnetinfo: raise XendError('No such vnet ' + str(x)) val = SrvVnet(vnetinfo) return val
def diagnose(dom): global domain global domid global dompath try: domain = server.xend.domain(dom) state = sxp.child_value(domain, 'state') domid = int(sxp.child_value(domain, 'domid')) name = sxp.child_value(domain, 'name') print "Domain ID is %d." % domid print "Domain name is %s." % name if not state: raise XendError("Cannot find state") if state.find('c') != -1: print "Domain has crashed." except socket.error, exn: print "Cannot contact Xend." try: domid = int(dom) name = dom except ValueError: print \ "Without Xend, you will have to specify the domain ID, not the domain name." sys.exit(1)
def restore(xd, fd, dominfo=None, paused=False, relocating=False): try: if not os.path.isdir("/var/lib/xen"): os.makedirs("/var/lib/xen") except Exception, exn: log.exception("Can't create directory '/var/lib/xen'") raise XendError("Can't create directory '/var/lib/xen'")
def get_config_int(self, name, val=None): v = self.get_config_value(name, val) try: return int(v) except Exception: raise XendError("invalid xend config %s: expected int: %s" % (name, v))
def save(fd, dominfo, network, live, dst, checkpoint=False, node=-1): try: if not os.path.isdir("/var/lib/xen"): os.makedirs("/var/lib/xen") except Exception, exn: log.exception("Can't create directory '/var/lib/xen'") raise XendError("Can't create directory '/var/lib/xen'")
def migrate(self, deviceConfig, network, dst, step, domName): """@see DevContoller.migrate""" if network: tool = xoptions.get_external_migration_tool() if tool != '': log.info("Request to network-migrate device to %s. step=%d.", dst, step) if step == DEV_MIGRATE_TEST: """Assuming for now that everything is ok and migration with the given tool can proceed. """ return 0 else: fd = os.popen("%s -type vtpm -step %d -host %s -domname %s" % (tool, step, dst, domName), 'r') for line in fd.readlines(): mo = re.search('Error', line) if mo: raise XendError("vtpm: Fatal error in migration step %d: %s" % (step, line)) return 0 else: log.debug("External migration tool not in configuration.") return -1 return 0
def domain_configure(self, config): """Configure an existing domain. @param vmconfig: vm configuration """ # !!! raise XendError("Unsupported")
def diagnose(dom): global domain global domid global dompath try: domain = server.xend_domain(dom) state = sxp.child_value(domain, 'state') domid = int(sxp.child_value(domain, 'domid')) name = sxp.child_value(domain, 'name') dompath = '/local/domain/%d' % domid print "Domain ID is %d." % domid print "Domain name is %s." % name if not state: raise XendError("Cannot find state") if state.find('c') != -1: print "Domain has crashed." diagnose_console() diagnose_devices() except xen.xend.XendProtocol.XendError, exn: print exn
def get_config_string(self, name, val=None): try: return scf.get_string(name) except scf.error, e: if e[0] == scf.SCF_ERROR_NOT_FOUND: return val else: raise XendError("option %s: %s:%s" % (name, e[1], e[2]))
def domain_migrate(self, domid, dst, live=False, resource=0, port=0): """Start domain migration.""" dominfo = self.domain_lookup_by_name_or_id_nr(domid) if not dominfo: raise XendInvalidDomain(str(domid)) if dominfo.getDomid() == PRIV_DOMAIN: raise XendError("Cannot migrate privileged domain %i" % domid) if port == 0: port = xroot.get_xend_relocation_port() try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((dst, port)) except socket.error, err: raise XendError("can't connect: %s" % err[1])
def get_config_bool(self, name, val=None): v = string.lower(str(self.get_config_value(name, val))) if v in ['yes', 'y', '1', 'on', 'true', 't']: return True if v in ['no', 'n', '0', 'off', 'false', 'f']: return False raise XendError("invalid xend config %s: expected bool: %s" % (name, v))
def get_xend_relocation_server_ssl_cert_file(self): name = 'xend-relocation-server-ssl-cert-file' file = self.get_config_string(name) if os.path.dirname(file) == "": file = auxbin.xen_configdir() + '/' + file if not os.path.exists(file): raise XendError( "invalid xend config %s: directory '%s' does not exist" % (name, file)) return file
def get_xend_tcp_xmlrpc_server_ssl_key_file(self): name = 'xend-tcp-xmlrpc-server-ssl-key-file' file = self.get_config_string(name) if os.path.dirname(file) == "": file = auxbin.xen_configdir() + '/' + file if not os.path.exists(file): raise XendError( "invalid xend config %s: directory '%s' does not exist" % (name, file)) return file
def vnet_cmd(cmd): out = None try: try: out = file("/proc/vnet/policy", "wb") sxp.show(cmd, out) except IOError, ex: raise XendError(str(ex)) finally: if out: out.close()
def domain_cpu_bvt_get(self, domid): """Get BVT (Borrowed Virtual Time) scheduler parameters for a domain. """ dominfo = self.domain_lookup_by_name_or_id_nr(domid) if not dominfo: raise XendInvalidDomain(str(domid)) try: return xc.bvtsched_domain_get(dominfo.getDomid()) except Exception, ex: raise XendError(str(ex))
def get_vnc_x509_cert_dir(self): name = 'vnc-x509-cert-dir' vncdir = self.get_config_string(name, self.xend_vnc_x509_cert_dir) if os.path.dirname(vncdir) == "": vncdir = auxbin.xen_configdir() + '/' + vncdir if not os.path.exists(vncdir): raise XendError( "invalid xend config %s: directory '%s' does not exist" % (name, vncdir)) return vncdir
def domain_pause(self, domid): """Pause domain execution.""" try: dominfo = self.domain_lookup_by_name_or_id_nr(domid) if not dominfo: raise XendInvalidDomain(str(domid)) log.info("Domain %s (%d) paused.", dominfo.getName(), dominfo.getDomid()) return dominfo.pause() except Exception, ex: raise XendError(str(ex))
def read_exact(fd, size, errmsg): buf = '' while size != 0: readstr = os.read(fd, size) if not len(readstr): log.error("read_exact: EOF trying to read %d (buf='%s')" % \ (size, buf)) raise XendError(errmsg) size = size - len(readstr) buf = buf + readstr return buf
def op_receive(self, name, _): if self.transport: self.send_reply(["ready", name]) try: XendDomain.instance().domain_restore_fd( self.transport.sock.fileno(), relocating=True) except: self.send_error() self.close() else: log.error(name + ": no transport") raise XendError(name + ": no transport")
def domain_cpu_sedf_set(self, domid, period, slice_, latency, extratime, weight): """Set Simple EDF scheduler parameters for a domain. """ dominfo = self.domain_lookup_by_name_or_id_nr(domid) if not dominfo: raise XendInvalidDomain(str(domid)) try: return xc.sedf_domain_set(dominfo.getDomid(), period, slice_, latency, extratime, weight) except Exception, ex: raise XendError(str(ex))
def get_config_bool(self, name, val=None): try: return scf.get_bool(name) except scf.error, e: if e[0] == scf.SCF_ERROR_NOT_FOUND: if val in ['yes', 'y', '1', 'on', 'true', 't']: return True if val in ['no', 'n', '0', 'off', 'false', 'f']: return False return val else: raise XendError("option %s: %s:%s" % (name, e[1], e[2]))
def domain_pincpu(self, domid, vcpu, cpumap): """Set which cpus vcpu can use @param cpumap: string repr of list of usable cpus """ dominfo = self.domain_lookup_by_name_or_id_nr(domid) if not dominfo: raise XendInvalidDomain(str(domid)) try: return xc.vcpu_setaffinity(dominfo.getDomid(), vcpu, cpumap) except Exception, ex: raise XendError(str(ex))
def domain_maxmem_set(self, domid, mem): """Set the memory limit for a domain. @param mem: memory limit (in MiB) @return: 0 on success, -1 on error """ dominfo = self.domain_lookup_by_name_or_id_nr(domid) if not dominfo: raise XendInvalidDomain(str(domid)) maxmem = int(mem) * 1024 try: return xc.domain_setmaxmem(dominfo.getDomid(), maxmem) except Exception, ex: raise XendError(str(ex))
def domain_save(self, domid, dst): """Start saving a domain to file. @param dst: destination file """ try: dominfo = self.domain_lookup_by_name_or_id_nr(domid) if not dominfo: raise XendInvalidDomain(str(domid)) if dominfo.getDomid() == PRIV_DOMAIN: raise XendError("Cannot save privileged domain %i" % domid) fd = os.open(dst, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) try: # For now we don't support 'live checkpoint' return XendCheckpoint.save(fd, dominfo, False) finally: os.close(fd) except OSError, ex: raise XendError("can't write guest state file %s: %s" % (dst, ex[1]))
def domain_restore(self, src): """Restore a domain from file. @param src: source file """ try: fd = os.open(src, os.O_RDONLY) try: return self.domain_restore_fd(fd) finally: os.close(fd) except OSError, ex: raise XendError("can't read guest state file %s: %s" % (src, ex[1]))
def __devIsUnconstrained(self): if os.path.exists(PERMISSIVE_CONFIG_FILE): try: fin = file(PERMISSIVE_CONFIG_FILE, 'rb') try: pci_perm_dev_config = parse(fin) finally: fin.close() if pci_perm_dev_config is None: pci_perm_dev_config = [''] else: pci_perm_dev_config.insert(0, '') except Exception, ex: raise XendError("Reading config file %s: %s" % (PERMISSIVE_CONFIG_FILE, str(ex)))
def domain_cpu_bvt_set(self, domid, mcuadv, warpback, warpvalue, warpl, warpu): """Set BVT (Borrowed Virtual Time) scheduler parameters for a domain. """ dominfo = self.domain_lookup_by_name_or_id_nr(domid) if not dominfo: raise XendInvalidDomain(str(domid)) try: return xc.bvtsched_domain_set(dom=dominfo.getDomid(), mcuadv=mcuadv, warpback=warpback, warpvalue=warpvalue, warpl=warpl, warpu=warpu) except Exception, ex: raise XendError(str(ex))
def __getQuirksByID(self): if os.path.exists(QUIRK_CONFIG_FILE): try: fin = file(QUIRK_CONFIG_FILE, 'rb') try: pci_quirks_config = parse(fin) finally: fin.close() if pci_quirks_config is None: pci_quirks_config = ['xend-pci-quirks'] else: pci_quirks_config.insert(0, 'xend-pci-quirks') self.pci_quirks_config = pci_quirks_config except Exception, ex: raise XendError("Reading config file %s: %s" % (QUIRK_CONFIG_FILE, str(ex)))