public override void Connect(Uri uri, string username, string password) { _lunInfo = LunInfo.ParseUri(uri.OriginalString); Initiator initiator = new Initiator(); initiator.SetCredentials(username, password); _session = initiator.ConnectTo(_lunInfo.Target); }
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"); } }
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); } }