Example #1
0
        /// <summary>
        /// Connects to a Target.
        /// </summary>
        /// <param name="target">The Target to connect to</param>
        /// <param name="addresses">The list of addresses for the target</param>
        /// <returns>The session representing the target connection.</returns>
        public Session ConnectTo(string target, params string[] addresses)
        {
            TargetAddress[] addressObjs = new TargetAddress[addresses.Length];
            for (int i = 0; i < addresses.Length; ++i)
            {
                addressObjs[i] = TargetAddress.Parse(addresses[i]);
            }

            return ConnectTo(target, addressObjs);
        }
Example #2
0
 /// <summary>
 /// Gets the Targets available from a Portal (i.e. network entity).
 /// </summary>
 /// <param name="address">The address of the Portal</param>
 /// <returns>The list of Targets available</returns>
 /// <remarks>If you just have an IP address, use this method to discover the available Targets.</remarks>
 public TargetInfo[] GetTargets(TargetAddress address)
 {
     using (Session session = new Session(SessionType.Discovery, null, _userName, _password, new TargetAddress[] { address }))
     {
         return session.EnumerateTargets();
     }
 }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the TargetInfo class.
 /// </summary>
 /// <param name="name">The name of the Target.</param>
 /// <param name="addresses">The network addresses of the Target.</param>
 public TargetInfo(string name, TargetAddress[] addresses)
 {
     _name = name;
     _addresses = addresses;
 }
 /// <summary>
 /// Gets the Targets available from a Portal (i.e. network entity).
 /// </summary>
 /// <param name="address">The address of the Portal</param>
 /// <returns>The list of Targets available</returns>
 /// <remarks>If you just have an IP address, use this method to discover the available Targets.</remarks>
 public TargetInfo[] GetTargets(string address)
 {
     return(GetTargets(TargetAddress.Parse(address)));
 }
Example #5
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);
            }
        }
Example #6
0
        public TargetInfo[] EnumerateTargets()
        {
            TextBuffer parameters = new TextBuffer();

            parameters.Add(SendTargetsParameter, "All");

            byte[] paramBuffer = new byte[parameters.Size];
            parameters.WriteTo(paramBuffer, 0);

            TextRequest req = new TextRequest(this);

            byte[] packet = req.GetBytes(0, paramBuffer, 0, paramBuffer.Length, true);

            _stream.Write(packet, 0, packet.Length);
            _stream.Flush();

            ProtocolDataUnit pdu  = ReadPdu();
            TextResponse     resp = ParseResponse <TextResponse>(pdu);

            TextBuffer buffer = new TextBuffer();

            if (resp.TextData != null)
            {
                buffer.ReadFrom(resp.TextData, 0, resp.TextData.Length);
            }

            List <TargetInfo> targets = new List <TargetInfo>();

            string currentTarget = null;
            List <TargetAddress> currentAddresses = null;

            foreach (KeyValuePair <string, string> line in buffer.Lines)
            {
                if (currentTarget == null)
                {
                    if (line.Key != TargetNameParameter)
                    {
                        throw new InvalidProtocolException("Unexpected response parameter " + line.Key + " expected " + TargetNameParameter);
                    }

                    currentTarget    = line.Value;
                    currentAddresses = new List <TargetAddress>();
                }
                else if (line.Key == TargetNameParameter)
                {
                    targets.Add(new TargetInfo(currentTarget, currentAddresses.ToArray()));
                    currentTarget = line.Value;
                    currentAddresses.Clear();
                }
                else if (line.Key == TargetAddressParameter)
                {
                    currentAddresses.Add(TargetAddress.Parse(line.Value));
                }
            }

            if (currentTarget != null)
            {
                targets.Add(new TargetInfo(currentTarget, currentAddresses.ToArray()));
            }

            return(targets.ToArray());
        }