def __init__(self, conn: libvirt.virConnect, domain_name: str): self.dom = None try: self.dom = conn.lookupByName(domain_name) self.block = Block(self.dom) except Exception as e: print("makine bulunamadi, makineyi kontrol ediniz...", str(e.args)) exit(1)
def _machineOperation(virt_conn: libvirt.virConnect, domain_name: str, operation: str, force: bool = False) -> [int, bool]: """ Do not export/import this function """ try: vm = virt_conn.lookupByName(domain_name) except libvirt.libvirtError: raise Exception("No VM found with the provided name") ops = { 'stop': { 'function': vm.shutdown, 'state_list': [MACHINE_STATE_SHUTDOWN, MACHINE_STATE_SHUTOFF], }, 'start': { 'function': vm.create, 'state_list': [ MACHINE_STATE_RUNNING, ], }, } if operation == "stop" and force: ops['stop']['function'] = vm.destroy if operation not in ops.keys(): raise Exception( f"Unknown operation. Available operations: {ops.keys()}") state = vm.state()[0] if state not in ops[operation]['state_list']: _ = ops[operation]['function']() sleep(1) new_state = vm.state()[0] if new_state not in ops[operation]['state_list']: sleep(5) new_state = vm.state()[0] if new_state not in ops[operation]['state_list']: raise Exception( f"VM refused to perform the {operation} action") return [new_state, False] elif state in ops[operation]['state_list']: # Machine was already in state return [state, True] raise Exception("VM is blocked")
def machineStopped(virt_conn: libvirt.virConnect, domain_name: str): virt_conn = reassureConnection(virt_conn) try: vm = virt_conn.lookupByName(domain_name) except libvirt.libvirtError: raise Exception("No VM found with the provided name") state = vm.state()[0] if state in [MACHINE_STATE_SHUTDOWN, MACHINE_STATE_SHUTOFF]: return True else: return False
def revertSnapshot(virt_conn: libvirt.virConnect, domain_name: str, snapshot_name: str) -> bool: """ Revert a snapshot of shutoff VM """ virt_conn = reassureConnection(virt_conn) vm = virt_conn.lookupByName(domain_name) snapshot = vm.snapshotLookupByName(snapshot_name) # Make sure VM is shutoff _, _ = stopMachine(virt_conn, domain_name) try: vm.revertToSnapshot(snapshot) except libvirt.libvirtError: return False return True
def createSnapshot(virt_conn: libvirt.virConnect, domain_name: str, snapshot_name: str) -> bool: """ Make a snapshot of shutoff VM """ virt_conn = reassureConnection(virt_conn) # Make sure VM is shutoff _, _ = stopMachine(virt_conn, domain_name) vm = virt_conn.lookupByName(domain_name) try: vm.snapshotCreateXML( SNAPSHOT_XML_TEMPLATE.format(snapshot_name=snapshot_name)) except libvirt.libvirtError: return False return True