2017-01-16 20:59:50 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
|
* that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer.
|
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer in the documentation and/or other
|
|
|
|
|
* materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using XenAdmin;
|
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAPI
|
|
|
|
|
{
|
|
|
|
|
partial class PBD
|
|
|
|
|
{
|
2013-12-12 12:59:00 +01:00
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
private static bool WaitForPlug(Session session, string pbdOpaqueRef)
|
|
|
|
|
{
|
|
|
|
|
int timeout = 120; //Wait 2 min for PBD to plug
|
|
|
|
|
|
|
|
|
|
while (timeout > 0)
|
|
|
|
|
{
|
|
|
|
|
if (PBD.get_currently_attached(session, pbdOpaqueRef))
|
|
|
|
|
return true;
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
timeout--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-27 02:24:56 +02:00
|
|
|
|
public static void CheckPlugPBDsForVMs(IXenConnection connection, List<XenRef<VM>> vmRefs, bool ignoreFailure = false)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-06-27 02:24:56 +02:00
|
|
|
|
var pbds = new List<PBD>();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2018-06-27 02:24:56 +02:00
|
|
|
|
foreach (XenRef<VM> vmRef in vmRefs)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-06-27 02:24:56 +02:00
|
|
|
|
VM vm = connection.TryResolveWithTimeout(vmRef);
|
|
|
|
|
|
|
|
|
|
foreach (var vbdRef in vm.VBDs)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-06-27 02:24:56 +02:00
|
|
|
|
var vbd = connection.Resolve(vbdRef);
|
|
|
|
|
if (vbd == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
VDI vdi = connection.Resolve(vbd.VDI);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (vdi == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2018-06-27 02:24:56 +02:00
|
|
|
|
SR sr = connection.Resolve(vdi.SR);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (sr == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2018-06-27 02:24:56 +02:00
|
|
|
|
foreach (var pbdRef in sr.PBDs)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-06-27 02:24:56 +02:00
|
|
|
|
var pbd = connection.Resolve(pbdRef);
|
|
|
|
|
if (pbd != null && !pbds.Contains(pbd))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
pbds.Add(pbd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (PBD pbd in pbds)
|
|
|
|
|
{
|
|
|
|
|
Session session = pbd.Connection.DuplicateSession();
|
|
|
|
|
|
2017-09-03 04:33:29 +02:00
|
|
|
|
log.DebugFormat("Waiting for PBDs {0} to become plugged", pbd.Name());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
// Wait 2 min for PBD to become plugged
|
|
|
|
|
if (WaitForPlug(session, pbd.opaque_ref))
|
|
|
|
|
continue;
|
|
|
|
|
|
2018-06-27 02:24:56 +02:00
|
|
|
|
// if it's still unplugged, try plugging it - this will probably
|
2013-06-24 13:41:48 +02:00
|
|
|
|
// fail, but at least we'll get a better error message.
|
2013-12-12 12:59:00 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
log.DebugFormat("Plugging PBD {0}", pbd.Name());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
plug(session, pbd.opaque_ref);
|
2013-12-12 12:59:00 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
log.Debug(string.Format("Error plugging PBD {0}", pbd.Name()), e);
|
2013-12-12 12:59:00 +01:00
|
|
|
|
|
2018-06-27 02:24:56 +02:00
|
|
|
|
if (!ignoreFailure)
|
2013-12-12 12:59:00 +01:00
|
|
|
|
throw;
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 04:33:29 +02:00
|
|
|
|
public bool MultipathActive()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
|
|
|
|
|
return BoolKey(other_config, "multipathed");
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The number of iSCSI sessions in use, as advertised by the SR backend, or -1 if the value is missing.
|
|
|
|
|
/// </summary>
|
2017-09-03 04:33:29 +02:00
|
|
|
|
public int ISCSISessions()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
return IntKey(other_config, "iscsi_sessions", -1);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static readonly Regex multipathCountRegex = new Regex(@"\[(\d+)L?,\s(\d+)L?");
|
|
|
|
|
|
|
|
|
|
public static bool ParsePathCounts(String input, out int currentPaths, out int maxPaths)
|
|
|
|
|
{
|
|
|
|
|
currentPaths = 0;
|
|
|
|
|
maxPaths = 0;
|
|
|
|
|
|
|
|
|
|
Match match = multipathCountRegex.Match(input);
|
|
|
|
|
|
|
|
|
|
return match.Success && match.Groups.Count == 3 &&
|
|
|
|
|
int.TryParse(match.Groups[1].Value, out currentPaths) &&
|
|
|
|
|
int.TryParse(match.Groups[2].Value, out maxPaths);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The status of the PBD as a friendly string (unplugged, host down, connected)
|
|
|
|
|
/// </summary>
|
2017-09-03 04:33:29 +02:00
|
|
|
|
public string StatusString()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
if (!currently_attached)
|
|
|
|
|
return Messages.REPAIR_SR_DIALOG_UNPLUGGED;
|
|
|
|
|
|
|
|
|
|
Host h = Connection.Resolve(host);
|
|
|
|
|
if (h == null)
|
|
|
|
|
return Messages.REPAIR_SR_DIALOG_UNPLUGGED;
|
|
|
|
|
|
|
|
|
|
if (!h.IsLive())
|
|
|
|
|
return Messages.HOST_NOT_LIVE;
|
|
|
|
|
|
|
|
|
|
return Messages.CONNECTED;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public StorageLinkCredentials GetStorageLinkCredentials()
|
|
|
|
|
{
|
|
|
|
|
var deviceConfig = new Dictionary<string, string>(device_config);
|
|
|
|
|
|
|
|
|
|
SR sr = Connection.Resolve<SR>(SR);
|
|
|
|
|
|
2015-10-26 17:01:55 +01:00
|
|
|
|
if (sr != null && sr.type == "cslg")
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
string host, username, passwordSecret;
|
|
|
|
|
|
|
|
|
|
deviceConfig.TryGetValue("target", out host);
|
|
|
|
|
deviceConfig.TryGetValue("username", out username);
|
|
|
|
|
deviceConfig.TryGetValue("password_secret", out passwordSecret);
|
|
|
|
|
|
|
|
|
|
return new StorageLinkCredentials(Connection, host, username, null, passwordSecret);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|