/// <summary> /// /// </summary> /// <param name="session"></param> /// <param name="vm"></param> /// <param name="vbd"></param> /// <param name="userdevice"></param> /// <param name="plug"></param> /// <returns>True if it warned the user, so you don't warn twice</returns> private static void SetUserDevice(Session session, VM vm, VBD vbd, String userdevice, bool plug) { //Program.AssertOffEventThread(); if (vm.power_state == vm_power_state.Running && vbd.currently_attached && vbd.allowed_operations.Contains(vbd_operations.unplug)) { VBD.unplug(session, vbd.opaque_ref); } VBD.set_userdevice(session, vbd.opaque_ref, userdevice); if (plug && vbd.allowed_operations.Contains(vbd_operations.plug) && vm.power_state == vm_power_state.Running) { VBD.plug(session, vbd.opaque_ref); } }
private AsyncAction getDeactivateVBDAction(VBD vbd) { VDI vdi = vbd.Connection.Resolve <VDI>(vbd.VDI); VM vm = vbd.Connection.Resolve <VM>(vbd.VM); String title = String.Format(Messages.ACTION_DISK_DEACTIVATING_TITLE, vdi.Name, vm.Name); String startDesc = Messages.ACTION_DISK_DEACTIVATING; String endDesc = Messages.ACTION_DISK_DEACTIVATED; AsyncAction action = new DelegatedAsyncAction(vbd.Connection, title, startDesc, endDesc, session => VBD.unplug(session, vbd.opaque_ref), "vbd.unplug"); action.VM = vm; return(action); }
protected override void Run() { Description = string.Format(Messages.ACTION_MOVING_VDI, Helpers.GetName(vdi)); PercentComplete = 10; log.DebugFormat("Moving VDI '{0}'", Helpers.GetName(vdi)); RelatedTask = VDI.async_copy(Session, vdi.opaque_ref, SR.opaque_ref); PollToCompletion(PercentComplete, 60); VDI newVdi = Connection.WaitForCache(new XenRef <VDI>(Result)); // if the original is a suspend VDI, link the suspended VM to the new VDI if (vdi.type == vdi_type.suspend) { var suspendedVm = (from vm in Connection.Cache.VMs let suspendVdi = Connection.Resolve(vm.suspend_VDI) where suspendVdi != null && suspendVdi.uuid == vdi.uuid select vm).FirstOrDefault(); if (suspendedVm != null) { VM.set_suspend_VDI(Session, suspendedVm.opaque_ref, newVdi.opaque_ref); } } PercentComplete = 60; var newVbds = new List <VBD>(); foreach (var vbdRef in vdi.VBDs) { var oldVbd = Connection.Resolve(vbdRef); if (oldVbd == null) { continue; } var newVbd = new VBD { userdevice = oldVbd.userdevice, bootable = oldVbd.bootable, mode = oldVbd.mode, type = oldVbd.type, unpluggable = oldVbd.unpluggable, other_config = oldVbd.other_config, VDI = new XenRef <VDI>(newVdi.opaque_ref), VM = new XenRef <VM>(oldVbd.VM) }; newVbd.SetIsOwner(oldVbd.GetIsOwner()); newVbds.Add(newVbd); try { if (oldVbd.currently_attached && oldVbd.allowed_operations.Contains(vbd_operations.unplug)) { VBD.unplug(Session, vbdRef); } } finally { if (!oldVbd.currently_attached) { VBD.destroy(Session, vbdRef); } } } PercentComplete = 80; VDI.destroy(Session, vdi.opaque_ref); foreach (var newVbd in newVbds) { Connection.WaitForCache(VBD.create(Session, newVbd)); } PercentComplete = 100; Description = Messages.MOVED; log.DebugFormat("Moved VDI '{0}'", Helpers.GetName(vdi)); }
internal static void DestroyVM(Session session, VM vm) { Exception caught = null; log.DebugFormat("Destroying VM '{0}'", vm.Name); if (vm.snapshots.Count > 0) { log.Debug("Destroying snapshots"); foreach (VM snapshot in vm.Connection.ResolveAll(vm.snapshots)) { DestroyVM(session, snapshot); } log.Debug("Snapshots destroyed"); } log.Debug("Unplugging and destroying VBDs"); foreach (VBD vbd in vm.Connection.ResolveAll <VBD>(vm.VBDs)) { try { if (vbd.currently_attached && vbd.allowed_operations.Contains(vbd_operations.unplug)) { VBD.unplug(session, vbd.opaque_ref); } } finally { if (!vbd.currently_attached) { VBD.destroy(session, vbd.opaque_ref); } } } log.Debug("VBDs unplugged and destroyed"); log.Debug("Unplugging and destroying VIFs"); foreach (XenAPI.VIF vif in vm.Connection.ResolveAll <XenAPI.VIF>(vm.VIFs)) { try { if (vif.currently_attached && vif.allowed_operations.Contains(XenAPI.vif_operations.unplug)) { XenAPI.VIF.unplug(session, vif.opaque_ref); } } finally { if (!vif.currently_attached) { XenAPI.VIF.destroy(session, vif.opaque_ref); } } } log.Debug("VIFs unplugged and destroyed"); log.Debug("Destroying VM"); try { VM.destroy(session, vm.opaque_ref); } catch (Exception e) { if (!e.Message.StartsWith("Object has been deleted")) { log.Error(e); throw; } } log.DebugFormat("VM '{0}' destroyed", vm.Name); if (caught != null) { throw caught; } }