def save_vm(vm_name, hostname='.'): conn = create_virt_conn(hostname) vm = get_vm(vm_name, hostname) # get the current state of the VM current_state = get_vm_state(vm_name, hostname) print current_state if current_state == VM_STATE_RUNNING: job_path, requested_state = vm.RequestStateChange(VM_STATE_SAVED) elif current_state == VM_STATE_SAVED: return True else: return False if requested_state == STATE_CODE_STARTED: if utils.job_completed(job_path): print vm_name, "saved." return True else: print vm_name, "failed to save." else: print "cannot change", vm_name, "to saved." return False
def add_kvp_item(kvp_key, kvp_value, vm_name, hostname='.'): # http://blogs.msdn.com/b/taylorb/archive/2008/07/06/hyper-v-wmi-kvp-exchange-aka-data-exchange-adding-new-items-from-parent-host.aspx conn = create_virt_conn(hostname) vm = get_vm(vm_name, hostname) kvps = vm.associators("Msvm_SystemDevice", wmi_result_class="Msvm_KvpExchangeComponent") vmms = get_vmms(hostname) data_item = conn.Msvm_KvpExchangeDataItem.new() data_item.Name = kvp_key data_item.Data = kvp_value data_item.Source = 0 job_path, ret_val = vmms.AddKvpItems(TargetSystem=vm.path_(), DataItems=[data_item.GetText_(1)]) if ret_val == STATE_CODE_STARTED: if utils.job_completed(job_path): print vm_name, " KVP item added." return True else: print vm_name, "failed to add KVP item, code:", ret_val else: print "Cannot add KVP item" return False
def remove_kvp_item(kvp_key, vm_name, hostname='.'): conn = create_virt_conn(hostname) vm = get_vm(vm_name, hostname) kvps = vm.associators("Msvm_SystemDevice", wmi_result_class="Msvm_KvpExchangeComponent") vmms = get_vmms(hostname) data_item = conn.Msvm_KvpExchangeDataItem.new() data_item.Name = kvp_key data_item.Data = '' data_item.Source = 0 job_path, ret_val = vmms.RemoveKvpItems(TargetSystem=vm.path_(), DataItems=[data_item.GetText_(1)]) if ret_val == STATE_CODE_STARTED: if utils.job_completed(job_path): print vm_name, " KVP item removed." return True else: print vm_name, "failed to remove KVP item, code:", ret_val else: print "Cannot remove KVP item" return False
def revert_to_snapshot(snapshot_name, vm_name, hostname='.'): conn = create_virt_conn(hostname) snapshot_service = get_snapshot_service(hostname) vm = get_vm(vm_name, hostname) snapshots = vm.associators(wmi_result_class="Msvm_VirtualSystemSettingData") for s in snapshots: if s.ElementName == snapshot_name: # snapshot found, reverting... snapshot = s print 'Reverting to snapshot "%s"' % snapshot_name job_path, ret = snapshot_service.ApplySnapshot(Snapshot=snapshot.path_()) if ret == STATE_CODE_STARTED: if utils.job_completed(job_path): print 'VM "%s" reverted to snapshot "%s"' % (vm_name, snapshot_name) else: print 'VM "%s" failed to revert to snapshot "%s"' % (vm_name, snapshot_name) # return now since we found the correct snapshot object return True else: continue return False
def revert_to_snapshot(snapshot_name, vm_name, hostname='.'): conn = create_virt_conn(hostname) snapshot_service = get_snapshot_service(hostname) vm = get_vm(vm_name, hostname) snapshots = vm.associators( wmi_result_class="Msvm_VirtualSystemSettingData") for s in snapshots: if s.ElementName == snapshot_name: # snapshot found, reverting... snapshot = s print 'Reverting to snapshot "%s"' % snapshot_name job_path, ret = snapshot_service.ApplySnapshot( Snapshot=snapshot.path_()) if ret == STATE_CODE_STARTED: if utils.job_completed(job_path): print 'VM "%s" reverted to snapshot "%s"' % (vm_name, snapshot_name) else: print 'VM "%s" failed to revert to snapshot "%s"' % ( vm_name, snapshot_name) # return now since we found the correct snapshot object return True else: continue return False
def stop_vm(vm_name, hostname='.'): conn = create_virt_conn(hostname) vm = get_vm(vm_name, hostname) current_state = get_vm_state(vm_name, hostname) print current_state if current_state != VM_STATE_OFF: job_path, ret = vm.RequestStateChange(VM_STATE_OFF) if ret == STATE_CODE_STARTED: if utils.job_completed(job_path): print vm_name, "stopped." return True else: print vm_name, "failed to stop." else: print "Cannot turn off VM" return False