📄 xenddomain.py
字号:
if self.is_domain_managed(dom): self._managed_config_remove(dom.get_uuid()) del self.managed_domains[dom.get_uuid()] except ValueError: log.warn("Domain is not registered: %s" % dom.get_uuid()) def _managed_domain_register(self, dom): self.managed_domains[dom.get_uuid()] = dom def is_domain_managed(self, dom = None): return (dom.get_uuid() in self.managed_domains) # End of Managed Domain Access # -------------------------------------------------------------------- def _running_domains(self): """Get table of domains indexed by id from xc. @requires: Expects to be protected by domains_lock. @rtype: list of dicts @return: A list of dicts representing the running domains. """ try: return xc.domain_getinfo() except RuntimeError, e: log.exception("Unable to get domain information.") return {} def _setDom0CPUCount(self): """Sets the number of VCPUs dom0 has. Retreived from the Xend configuration, L{XendOptions}. @requires: Expects to be protected by domains_lock. @rtype: None """ dom0 = self.privilegedDomain() # get max number of vcpus to use for dom0 from config target = int(xoptions.get_dom0_vcpus()) log.debug("number of vcpus to use is %d", target) # target == 0 means use all processors if target > 0: dom0.setVCpuCount(target) def _refresh(self, refresh_shutdown = True): """Refresh the domain list. Needs to be called when either xenstore has changed or when a method requires up to date information (like uptime, cputime stats). Expects to be protected by the domains_lock. @rtype: None """ txn = xstransact() try: self._refreshTxn(txn, refresh_shutdown) txn.commit() except: txn.abort() raise def _refreshTxn(self, transaction, refresh_shutdown): running = self._running_domains() # Add domains that are not already tracked but running in Xen, # and update domain state for those that are running and tracked. for dom in running: domid = dom['domid'] if domid in self.domains: self.domains[domid].update(dom, refresh_shutdown, transaction) elif domid not in self.domains and dom['dying'] != 1: try: new_dom = XendDomainInfo.recreate(dom, False) except VmError: log.exception("Unable to recreate domain") try: xc.domain_destroy(domid) except: log.exception("Hard destruction of domain failed: %d" % domid) # update information for all running domains # - like cpu_time, status, dying, etc. # remove domains that are not running from active domain list. # The list might have changed by now, because the update call may # cause new domains to be added, if the domain has rebooted. We get # the list again. running = self._running_domains() running_domids = [d['domid'] for d in running if d['dying'] != 1] for domid, dom in self.domains.items(): if domid not in running_domids and domid != DOM0_ID: self._remove_domain(dom, domid) def add_domain(self, info): """Add a domain to the list of running domains @requires: Expects to be protected by the domains_lock. @param info: XendDomainInfo of a domain to be added. @type info: XendDomainInfo """ log.debug("Adding Domain: %s" % info.getDomid()) self.domains[info.getDomid()] = info # update the managed domains with a new XendDomainInfo object # if we are keeping track of it. if info.get_uuid() in self.managed_domains: self._managed_domain_register(info) def remove_domain(self, info, domid = None): """Remove the domain from the list of running domains, taking the domains_lock first. """ self.domains_lock.acquire() try: self._remove_domain(info, domid) finally: self.domains_lock.release() def _remove_domain(self, info, domid = None): """Remove the domain from the list of running domains @requires: Expects to be protected by the domains_lock. @param info: XendDomainInfo of a domain to be removed. @type info: XendDomainInfo """ if info: if domid == None: domid = info.getDomid() if info._stateGet() != DOM_STATE_HALTED: info.cleanupDomain() if domid in self.domains: del self.domains[domid] info.destroy_xapi_device_instances() else: log.warning("Attempted to remove non-existent domain.") def restore_(self, config): """Create a domain as part of the restore process. This is called only from L{XendCheckpoint}. A restore request comes into XendDomain through L{domain_restore} or L{domain_restore_fd}. That request is forwarded immediately to XendCheckpoint which, when it is ready, will call this method. It is necessary to come through here rather than go directly to L{XendDomainInfo.restore} because we need to serialise the domain creation process, but cannot lock domain_restore_fd as a whole, otherwise we will deadlock waiting for the old domain to die. @param config: Configuration of domain to restore @type config: SXP Object (eg. list of lists) """ self.domains_lock.acquire() try: dominfo = XendDomainInfo.restore(config) return dominfo finally: self.domains_lock.release() def domain_lookup(self, domid): """Look up given I{domid} in the list of managed and running domains. @note: Will cause a refresh before lookup up domains, for a version that does not need to re-read xenstore use L{domain_lookup_nr}. @param domid: Domain ID or Domain Name. @type domid: int or string @return: Found domain. @rtype: XendDomainInfo @raise XendInvalidDomain: If domain is not found. """ self.domains_lock.acquire() try: self._refresh(refresh_shutdown = False) dom = self.domain_lookup_nr(domid) if not dom: raise XendInvalidDomain(str(domid)) return dom finally: self.domains_lock.release() def domain_lookup_nr(self, domid): """Look up given I{domid} in the list of managed and running domains. @param domid: Domain ID or Domain Name. @type domid: int or string @return: Found domain. @rtype: XendDomainInfo or None """ self.domains_lock.acquire() try: # lookup by name match = [dom for dom in self.domains.values() \ if dom.getName() == domid] if match: return match[0] match = [dom for dom in self.managed_domains.values() \ if dom.getName() == domid] if match: return match[0] # lookup by id try: if int(domid) in self.domains: return self.domains[int(domid)] except ValueError: pass # lookup by uuid for running domains match = [dom for dom in self.domains.values() \ if dom.get_uuid() == domid] if match: return match[0] # lookup by uuid for inactive managed domains if domid in self.managed_domains: return self.managed_domains[domid] return None finally: self.domains_lock.release() def privilegedDomain(self): """ Get the XendDomainInfo of a dom0 @rtype: XendDomainInfo """ self.domains_lock.acquire() try: return self.domains[DOM0_ID] finally: self.domains_lock.release() def autostart_domains(self): """ Autostart managed domains that are marked as such. """ need_starting = [] self.domains_lock.acquire() try: for dom_uuid, dom in self.managed_domains.items(): if dom and dom._stateGet() == DOM_STATE_HALTED: on_xend_start = dom.info.get('on_xend_start', 'ignore') auto_power_on = dom.info.get('auto_power_on', False) should_start = (on_xend_start == 'start') or auto_power_on if should_start: need_starting.append(dom_uuid) finally: self.domains_lock.release() for dom_uuid in need_starting: self.domain_start(dom_uuid, False) def cleanup_domains(self): """Clean up domains that are marked as autostop. Should be called when Xend goes down. This is currently called from L{xen.xend.servers.XMLRPCServer}. """ log.debug('cleanup_domains') self.domains_lock.acquire() try: for dom in self.domains.values(): if dom.getName() == DOM0_NAME: continue try: if dom._stateGet() == DOM_STATE_RUNNING: shutdownAction = dom.info.get('on_xend_stop', 'ignore') if shutdownAction == 'shutdown': log.debug('Shutting down domain: %s' % dom.getName()) dom.shutdown("poweroff") elif shutdownAction == 'suspend': self.domain_suspend(dom.getName()) else: log.debug('Domain %s continues to run.' % dom.getName()) except: log.exception('Domain %s failed to %s.' % \ (dom.getName(), shutdownAction)) finally: self.domains_lock.release() # ---------------------------------------------------------------- # Xen API def set_allow_new_domains(self, allow_new_domains): self._allow_new_domains = allow_new_domains def allow_new_domains(self): return self._allow_new_domains def get_domain_refs(self): result = [] try: self.domains_lock.acquire() result = [d.get_uuid() for d in self.domains.values()] for d in self.managed_domains.keys(): if d not in result: result.append(d) return result finally: self.domains_lock.release() def get_all_vms(self): self.domains_lock.acquire() try: result = self.domains.values() result += [x for x in self.managed_domains.values() if x not in result] return result finally: self.domains_lock.release() def get_vm_by_uuid(self, vm_uuid): self.domains_lock.acquire() try: for dom in self.domains.values(): if dom.get_uuid() == vm_uuid: return dom if vm_uuid in self.managed_domains: return self.managed_domains[vm_uuid] return None finally: self.domains_lock.release() def get_vm_with_dev_uuid(self, klass, dev_uuid):
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -