Example #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);
            }
        }
Example #2
0
        public DiskStream Connect(XenAPI.Session xenSession, string vdiuuid, bool read_only)
        {
            StartiScsiTarget(xenSession, vdiuuid, read_only);

            string ipaddress, port, targetGroupTag, username, password;

            if (!TryParsePluginRecordFor(_pluginrecord, "ip", out ipaddress))
            {
                throw new Exception(Messages.ISCSI_ERROR_NO_IPADDRESS);
            }

            TryParsePluginRecordFor(_pluginrecord, "port", out port);
            int ipport = Convert.ToInt32(port);

            TryParsePluginRecordFor(_pluginrecord, "isci_lun", out targetGroupTag);
            TryParsePluginRecordFor(_pluginrecord, "username", out username);
            TryParsePluginRecordFor(_pluginrecord, "password", out password);

            Initiator initiator = new Initiator();

            if (username != null && password != null)
            {
                initiator.SetCredentials(username, password);
            }

            int  iSCSIConnectRetry = Properties.Settings.Default.iSCSIConnectRetry;
            bool iSCSIConnected    = false;

            while (!iSCSIConnected && iSCSIConnectRetry > 0)
            {
                if (Cancel)
                {
                    throw new OperationCanceledException();
                }

                try
                {
                    log.DebugFormat("Connecting virtual disk {0}... ", vdiuuid);
                    TargetAddress ta      = new TargetAddress(ipaddress, ipport, targetGroupTag);
                    TargetInfo[]  targets = initiator.GetTargets(ta);
                    log.InfoFormat("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($"Failed to connect to VDI {vdiuuid} over iSCSI.", ex);
                    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)
                {
                    string idx;
                    TryParsePluginRecordFor(_pluginrecord, "iscsi_lun", out idx);
                    long lunIdx = Convert.ToInt32(idx);
                    lun = luns[lunIdx].Lun;
                }

                log.InfoFormat("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 e)
            {
                log.Error("Could not determine LUN", e);
                throw;
            }

            log.InfoFormat("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 DiskStream(_iscsisession, lun, (read_only ? FileAccess.Read : FileAccess.ReadWrite)));
            }
            catch (Exception ex)
            {
                log.Error("Failed to open virtual disk.", ex);
                throw new Exception(Messages.ISCSI_ERROR_CANNOT_OPEN_DISK, ex);
            }
        }
Example #3
0
        protected override void DoRun()
        {
            Initiator initiator = new Initiator();

            if (!string.IsNullOrEmpty(UserName))
            {
                initiator.SetCredentials(UserName, Password);
            }

            bool foundTargets = false;

            try
            {
                foreach (var target in initiator.GetTargets(_portalAddress.Value))
                {
                    foundTargets = true;
                    Console.WriteLine("Target: " + target);

                    if (Verbose)
                    {
                        Console.WriteLine("  Name: " + target.Name);
                        foreach (var addr in target.Addresses)
                        {
                            Console.WriteLine("  Address: " + addr + "  <" + addr.ToUri() + ">");
                        }
                        Console.WriteLine();
                    }

                    using (Session s = initiator.ConnectTo(target))
                    {
                        foreach (var lun in s.GetLuns())
                        {
                            Console.WriteLine(lun.DeviceType + ": ");
                            string[] uris = lun.GetUris();
                            if (uris.Length > 1)
                            {
                                for (int i = 0; i < uris.Length; ++i)
                                {
                                    Console.WriteLine("  URI[" + i + "]: " + uris[i]);
                                }
                            }
                            else if (uris.Length > 0)
                            {
                                Console.WriteLine("  URI: " + uris[0]);
                            }

                            if (Verbose)
                            {
                                Console.WriteLine("  LUN: " + lun.Lun.ToString("x16", CultureInfo.InvariantCulture));
                                Console.WriteLine("  Device Type: " + lun.DeviceType);
                                Console.WriteLine("  Removeable: " + (lun.Removable ? "Yes" : "No"));
                                Console.WriteLine("  Vendor: " + lun.VendorId);
                                Console.WriteLine("  Product: " + lun.ProductId);
                                Console.WriteLine("  Revision: " + lun.ProductRevision);
                                Console.WriteLine();
                            }
                        }
                    }
                }

                if (!foundTargets)
                {
                    Console.WriteLine("No targets found");
                }
            }
            catch (LoginException)
            {
                Console.WriteLine("ERROR: Need credentials, or the credentials specified were invalid");
            }
        }