Ejemplo n.º 1
0
        protected override void Run()
        {
            Description = Messages.ACTION_PREPARING;

            // move the progress bar above 0, it's more reassuring to see than a blank bar as we copy the first disk
            PercentComplete += 10;
            int halfstep = 90 / (VM.VBDs.Count * 2);

            var exceptions = new List <Exception>();

            foreach (var vbdRef in VM.VBDs)
            {
                if (Cancelling)
                {
                    throw new CancelledException();
                }

                var oldVBD = Connection.Resolve(vbdRef);

                if (oldVBD == null || !oldVBD.GetIsOwner())
                {
                    continue;
                }

                if (_storageMapping == null ||
                    !_storageMapping.TryGetValue(oldVBD.VDI.opaque_ref, out SR sr) || sr == null)
                {
                    continue;
                }

                var curVdi = Connection.Resolve(oldVBD.VDI);
                if (curVdi == null)
                {
                    continue;
                }

                if (curVdi.SR.opaque_ref == sr.opaque_ref)
                {
                    continue;
                }

                Description = string.Format(Messages.ACTION_MOVING_VDI_TO_SR,
                                            Helpers.GetName(curVdi), Helpers.GetName(sr));

                RelatedTask = VDI.async_copy(Session, oldVBD.VDI.opaque_ref, sr.opaque_ref);
                PollToCompletion(PercentComplete, PercentComplete + halfstep);
                var newVDI = Connection.WaitForCache(new XenRef <VDI>(Result));

                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>(VM.opaque_ref)
                };
                newVBD.SetIsOwner(oldVBD.GetIsOwner());

                try
                {
                    VDI.destroy(Session, oldVBD.VDI.opaque_ref);
                }
                catch (Exception e)
                {
                    log.ErrorFormat("Failed to destroy old VDI {0}", oldVBD.VDI.opaque_ref);
                    exceptions.Add(e);
                }

                Connection.WaitForCache(VBD.create(Session, newVBD));

                PercentComplete += halfstep;
            }

            if (SR != null)
            {
                VM.set_suspend_SR(Session, VM.opaque_ref, SR.opaque_ref);
            }

            if (exceptions.Count > 0)
            {
                throw new Exception(Messages.ACTION_VM_MOVING_VDI_DESTROY_FAILURE);
            }

            Description = Messages.MOVED;
        }
Ejemplo n.º 2
0
        protected override void Run()
        {
            this.Description = Messages.ACTION_VM_MOVING;
            try
            {
                var vbds     = Connection.ResolveAll(VM.VBDs);
                int halfstep = (int)(90 / (vbds.Count * 2));
                // move the progress bar above 0, it's more reassuring to see than a blank bar as we copy the first disk
                PercentComplete += 10;
                Exception exn = null;

                foreach (VBD oldVBD in vbds)
                {
                    if (!oldVBD.IsOwner)
                    {
                        continue;
                    }

                    var curVdi = Connection.Resolve(oldVBD.VDI);
                    if (curVdi == null || curVdi.SR.opaque_ref == this.SR.opaque_ref)
                    {
                        continue;
                    }

                    RelatedTask = XenAPI.VDI.async_copy(Session, oldVBD.VDI.opaque_ref, this.SR.opaque_ref);
                    PollToCompletion(PercentComplete, PercentComplete + halfstep);
                    var newVDI = Connection.WaitForCache(new XenRef <VDI>(Result));

                    var newVBD = new VBD
                    {
                        IsOwner      = oldVBD.IsOwner,
                        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>(VM.opaque_ref)
                    };

                    VBD vbd = oldVBD;
                    BestEffort(ref exn, () => VDI.destroy(Session, vbd.VDI.opaque_ref));
                    Connection.WaitForCache <VBD>(VBD.create(Session, newVBD));

                    PercentComplete += halfstep;
                }

                if (Helpers.BostonOrGreater(Connection))
                {
                    VM.set_suspend_SR(Session, VM.opaque_ref, SR.opaque_ref);
                }

                if (exn != null)
                {
                    throw exn;
                }
            }
            catch (CancelledException)
            {
                this.Description = string.Format(Messages.MOVE_CANCELLED, VM.Name);
                throw;
            }
            this.Description = Messages.MOVED;
        }
Ejemplo n.º 3
0
        private void insertAEISO(string vmRef, string aeFileName)
        {
            HTTPHelper.progressInfo     = "获取AE ISO库";
            HTTPHelper.progressPercent += 1;
            SROper srOper        = new SROper();
            string aeIsoRepoUuid = srOper.getAeISORepoUuid();

            XenRef <VBD>         cdVBDRef = null;
            VM                   vm       = VM.get_record(ConnectManager.session, vmRef);
            List <XenRef <VBD> > vbdRefs  = vm.VBDs;

            foreach (XenRef <VBD> vbdRef in vbdRefs)
            {
                VBD vbd = VBD.get_record(ConnectManager.session, vbdRef);
                if (vbd_type.CD.Equals(vbd.type))
                {
                    cdVBDRef = vbdRef;
                    try {
                        VBD.eject(ConnectManager.session, vbdRef);
                    }
                    catch (Exception ex) {
                        log.InfoFormat("设置VBD eject, error=[{0}]", ex.Message);
                    }
                    break;
                }
            }

            if (cdVBDRef == null)
            {
                string[] userdevices = VM.get_allowed_VBD_devices(ConnectManager.session, vmRef);
                if (userdevices != null && userdevices.Length > 0)
                {
                    VBD vbd = new VBD();
                    vbd.VM         = new XenRef <VM>(vmRef);
                    vbd.type       = vbd_type.CD;
                    vbd.mode       = vbd_mode.RO;
                    vbd.userdevice = userdevices[0];
                    vbd.empty      = true;
                    cdVBDRef       = VBD.create(ConnectManager.session, vbd);
                    log.InfoFormat("WinCenter VM没有虚拟光驱,新建一个VBD成功");
                }
                else
                {
                    log.InfoFormat("可供使用的VBD标识符列表数为0,不能创建VBD");
                }
            }

            XenRef <VDI> vdiRef   = srOper.getVdiRef(aeIsoRepoUuid, aeFileName);
            VBD          cdVBD    = VBD.get_record(ConnectManager.session, cdVBDRef);
            bool         isInsert = VBD.get_allowed_operations(ConnectManager.session, cdVBDRef).Contains(vbd_operations.insert);

            if (isInsert)
            {
                log.InfoFormat("加载AE ISO文件");
                VBD.insert(ConnectManager.session, cdVBDRef, vdiRef);
                HTTPHelper.progressInfo     = "加载AE ISO文件";
                HTTPHelper.progressPercent += 1;
            }
            else
            {
                XenRef <VDI> cdVDIRef = cdVBD.VDI;
                string       name     = VDI.get_name_label(ConnectManager.session, cdVDIRef);
                log.InfoFormat("WinCenter VM已插入镜像[{0}],不能插入AE镜像", name);
            }
        }
Ejemplo n.º 4
0
        protected override void Run()
        {
            Description     = string.Format(Messages.ACTION_MOVING_VDI_STATUS, 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.COMPLETED;
            log.DebugFormat("Moved VDI '{0}'", Helpers.GetName(vdi));
        }