Esempio n. 1
0
        public XenOvfTransport.DiskStream Connect(XenAPI.Session xenSession, string vdiuuid, bool read_only)
        {
            int iSCSIConnectRetry = Properties.Settings.Default.iSCSIConnectRetry;
            bool iSCSIConnected = false;
            StartiScsiTarget(xenSession, vdiuuid, read_only);
            string ipaddress = ParsePluginRecordFor("ip");
            int ipport = Convert.ToInt32(ParsePluginRecordFor("port"));
            string targetGroupTag = ParsePluginRecordFor("isci_lun");
            if (ipaddress == null)
            {
                throw new NullReferenceException(Messages.ISCSI_ERROR_NO_IPADDRESS);
            }
            string username = ParsePluginRecordFor("username");
            string password = ParsePluginRecordFor("password");

            Initiator initiator = new Initiator();
            if (username != null && password != null)
                initiator.SetCredentials(username, password);
            while (!iSCSIConnected && iSCSIConnectRetry > 0)
            {
                if (Cancel)
                    throw new OperationCanceledException();

                try
                {
                    Log.Debug(Messages.FILES_TRANSPORT_SETUP, vdiuuid);
                    TargetAddress ta = new TargetAddress(ipaddress, ipport, targetGroupTag);
                    TargetInfo[] targets = initiator.GetTargets(ta);
                    Log.Info("iSCSI.Connect found {0} targets, connecting to: {1}", targets.Length, targets[0].Name);
                    _iscsisession = initiator.ConnectTo(targets[0]);
                    iSCSIConnected = true;
                }
                catch (Exception ex)
                {
                    Log.Error("{0} {1}", Messages.ISCSI_ERROR, ex.Message);
                    Thread.Sleep(new TimeSpan(0, 0, 5));
                    iSCSIConnectRetry--;
                }
            }

            if (!iSCSIConnected)
            {
                throw new Exception(Messages.ISCSI_ERROR);
            }

            long lun = 0;
            try
            {
                LunInfo[] luns = _iscsisession.GetLuns();
                if (_newscsi)
                {
                    long lunIdx = Convert.ToInt32(ParsePluginRecordFor("iscsi_lun"));
                    lun = luns[lunIdx].Lun;
                }
                Log.Info("iSCSI.Connect found {0} luns, looking for block storage.", luns.Length);
                foreach (LunInfo iLun in luns)
                {
                    if (iLun.DeviceType == LunClass.BlockStorage)
                    {
                        if (_newscsi && iLun.Lun == lun) { break; }
                        lun = iLun.Lun;
                        break;
                    }
                }
            }
            catch (Exception)
            {
                Log.Error("Could not determin LUN");
                throw;
            }
            Log.Info("iSCSI.Connect, found on lun: {0}", lun);
            try
            {
                iDisk = _iscsisession.OpenDisk(lun);
                // Use our own DiskStream class to workaround a bug in DiscUtils.DiskStream.
                return new XenOvfTransport.DiskStream(_iscsisession, lun, (read_only ? FileAccess.Read : FileAccess.ReadWrite));
            }
            catch (Exception ex)
            {
                Log.Error("{0} {1}", Messages.ISCSI_ERROR_CANNOT_OPEN_DISK, ex.Message);
                throw new Exception(Messages.ISCSI_ERROR_CANNOT_OPEN_DISK, ex);
            }
        }
Esempio n. 2
0
 public void Disconnect(XenAPI.Session xenSession)
 {
     try
     {
         if (iDisk != null)
             iDisk.Dispose();
         iDisk = null;
     }
     catch (Exception exn)
     {
         Log.Warning("Exception when disposing iDisk", exn);
     }
     try
     {
         if (_iscsisession != null)
             _iscsisession.Dispose();
         _iscsisession = null;
     }
     catch (Exception exn)
     {
         Log.Warning("Exception when disposing iscsisession", exn);
     }
     StopiScsiTarget(xenSession);
 }
Esempio n. 3
0
        public void Disconnect(XenAPI.Session xenSession)
        {
            try
            {
                if (iDisk != null)
                    iDisk.Dispose();
                iDisk = null;
            }
            catch (Exception exn)
            {
                log.DebugFormat("Failed to dispose iDisk: {0}. Continuing.", exn);
            }

            try
            {
                if (_iscsisession != null)
                    _iscsisession.Dispose();
                _iscsisession = null;
            }
            catch (Exception exn)
            {
                log.DebugFormat("Failed to dispose iScsiSession: {0}. Continuing.", exn);
            }

            StopiScsiTarget(xenSession);
        }