我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用libvirt.VIR_DOMAIN_SHUTOFF。
def stop(self): self.domain.shutdown() # stop domain while self.domain.state()[0] != libvirt.VIR_DOMAIN_SHUTOFF: time.sleep(1) self.cdrom.cleanup()
def __init__(self, args): self.args = args self.userdir = args.userdir self.resources = args.resources self.credentials = args.credentials self.resources_paws_file = args.resources_paws_file self.verbose = args.verbose self.util = Util(self) self.ansible = Ansible(self.userdir) self.resources_paws = None # Libvirt domain/VM states self.states = { libvirt.VIR_DOMAIN_NOSTATE: 'no state', libvirt.VIR_DOMAIN_RUNNING: 'running', libvirt.VIR_DOMAIN_BLOCKED: 'blocked on resource', libvirt.VIR_DOMAIN_PAUSED: 'paused by user', libvirt.VIR_DOMAIN_SHUTDOWN: 'being shut down', libvirt.VIR_DOMAIN_SHUTOFF: 'shut off', libvirt.VIR_DOMAIN_CRASHED: 'crashed', }
def backup_machine_to_filesystem(self, machineid, backuppath): from shutil import make_archive if self.isCurrentStorageAction(machineid): raise Exception("Can't delete a locked machine") domain = self.connection.lookupByUUIDString(machineid) diskfiles = self._get_domain_disk_file_names(domain) if domain.state(0)[0] != libvirt.VIR_DOMAIN_SHUTOFF: domain.destroy() for diskfile in diskfiles: if os.path.exists(diskfile): try: vol = self.connection.storageVolLookupByPath(diskfile) except BaseException: continue poolpath = os.path.join(self.basepath, domain.name()) if os.path.exists(poolpath): archive_name = os.path.join( backuppath, 'vm-%04x' % machineid) root_dir = poolpath make_archive(archive_name, gztar, root_dir) return True
def delete_machine(self, id): if self.isCurrentStorageAction(id): raise Exception("Can't delete a locked machine") domain = self._get_domain(id) if domain: if domain.state(0)[0] != libvirt.VIR_DOMAIN_SHUTOFF: domain.destroy() domain.undefineFlags( libvirt.VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA) poolpath = os.path.join(self.basepath, id) try: diskpool = self.connection.storagePoolLookupByName(id) for vol in diskpool.listAllVolumes(): vol.delete() diskpool.destroy() except BaseException: pass if os.path.exists(poolpath): shutil.rmtree(poolpath) return True
def shutdown(self, id): if self.isCurrentStorageAction(id): raise Exception("Can't stop a locked machine") domain = self._get_domain(id) if domain.state(0)[0] in [libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED]: return True return domain.shutdown() == 0
def isCurrentStorageAction(self, domainid): domain = self._get_domain(domainid) if not domain: return False # at this moment we suppose the machine is following the default naming # of disks if domain.state()[0] not in [libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED]: status = domain.blockJobInfo('vda', 0) if 'cur' in status: return True # check also that there is no qemu-img job running if self.isLocked(domainid): return True return False
def _clone(self, id, name, clonefrom): domain = self.connection.lookupByUUIDString(id) domainconfig = domain.XMLDesc() name = '%s_%s.qcow2' % (name, time.time()) destination_path = os.path.join(self.templatepath, name) if domain.state()[0] in [ libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED, libvirt.VIR_DOMAIN_PAUSED] or not self._isRootVolume( domain, clonefrom): if not self.isLocked(id): lock = self._lockDomain(id) if lock != LOCKCREATED: raise Exception('Failed to create lock: %s' % str(lock)) else: raise Exception("Can't perform this action on a locked domain") q2 = Qcow2(clonefrom) try: q2.export(destination_path) finally: if self.isLocked(id): self._unlockDomain(id) else: domain.undefine() try: domain.blockRebase(clonefrom, destination_path, 0, libvirt.VIR_DOMAIN_BLOCK_REBASE_COPY) rebasedone = False while not rebasedone: rebasedone = self._block_job_info(domain, clonefrom) domain.blockJobAbort(clonefrom, 0) except BaseException: self.connection.defineXML(domainconfig) raise self.connection.defineXML(domainconfig) return destination_path
def guest_state_report(guest): try: _uuid = guest.UUIDString() state, maxmem, mem, ncpu, cputime = guest.info() # state ????? # http://libvirt.org/docs/libvirt-appdev-guide-python/en-US/html/libvirt_application_development_guide_using_python-Guest_Domains-Information-State.html # http://stackoverflow.com/questions/4986076/alternative-to-virsh-libvirt log = u' '.join([u'?', guest.name(), u', UUID', _uuid, u'??????']) if state == libvirt.VIR_DOMAIN_RUNNING: log += u' Running?' guest_event_emit.running(uuid=_uuid) elif state == libvirt.VIR_DOMAIN_BLOCKED: log += u' Blocked?' guest_event_emit.blocked(uuid=_uuid) elif state == libvirt.VIR_DOMAIN_PAUSED: log += u' Paused?' guest_event_emit.paused(uuid=_uuid) elif state == libvirt.VIR_DOMAIN_SHUTDOWN: log += u' Shutdown?' guest_event_emit.shutdown(uuid=_uuid) elif state == libvirt.VIR_DOMAIN_SHUTOFF: log += u' Shutoff?' guest_event_emit.shutoff(uuid=_uuid) elif state == libvirt.VIR_DOMAIN_CRASHED: log += u' Crashed?' guest_event_emit.crashed(uuid=_uuid) elif state == libvirt.VIR_DOMAIN_PMSUSPENDED: log += u' PM_Suspended?' guest_event_emit.pm_suspended(uuid=_uuid) else: log += u' NO_State?' guest_event_emit.no_state(uuid=_uuid) logger.info(log) log_emit.info(log) except Exception as e: logger.error(e.message) log_emit.error(e.message)