2023-01-24 15:29:31 +01:00
|
|
|
|
/* Copyright (c) Cloud Software Group, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
*
|
|
|
|
|
* 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.Windows.Forms;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using XenAdmin.Controls;
|
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
using XenAdmin.Actions;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Dialogs
|
|
|
|
|
{
|
|
|
|
|
public partial class NewDiskDialog : XenDialogBase
|
|
|
|
|
{
|
2019-01-13 01:39:12 +01:00
|
|
|
|
#region Private fields
|
|
|
|
|
|
2023-02-22 03:19:47 +01:00
|
|
|
|
private readonly VM _vm;
|
|
|
|
|
private readonly VDI _diskTemplate;
|
|
|
|
|
private readonly IEnumerable<VDI> _vdiNamesInUse = new List<VDI>();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-01-13 01:39:12 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
2019-09-04 03:03:44 +02:00
|
|
|
|
public NewDiskDialog(IXenConnection connection, SR sr)
|
|
|
|
|
: base(connection ?? throw new ArgumentNullException(nameof(connection)))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2020-03-30 00:49:06 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
NameTextBox.Text = GetDefaultVDIName();
|
2019-09-04 03:03:44 +02:00
|
|
|
|
diskSpinner1.Populate();
|
2022-11-29 23:59:01 +01:00
|
|
|
|
srPicker.Populate(SrPicker.SRPickerType.InstallFromTemplate, connection, null, sr, new[] { NewDisk() });
|
|
|
|
|
buttonRescan.Enabled = srPicker.CanBeScanned;
|
2020-03-30 00:49:06 +02:00
|
|
|
|
UpdateErrorsAndButtons();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 03:03:44 +02:00
|
|
|
|
public NewDiskDialog(IXenConnection connection, VM vm, Host affinity,
|
|
|
|
|
SrPicker.SRPickerType pickerUsage = SrPicker.SRPickerType.VM, VDI diskTemplate = null,
|
|
|
|
|
bool canResize = true, long minSize = 0, IEnumerable<VDI> vdiNamesInUse = null)
|
|
|
|
|
: base(connection ?? throw new ArgumentNullException(nameof(connection)))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-09-04 03:03:44 +02:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
2023-02-22 03:19:47 +01:00
|
|
|
|
_vm = vm;
|
|
|
|
|
_vdiNamesInUse = vdiNamesInUse ?? new List<VDI>();
|
2020-03-30 00:49:06 +02:00
|
|
|
|
diskSpinner1.CanResize = canResize;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-09-04 03:03:44 +02:00
|
|
|
|
if (diskTemplate == null)
|
|
|
|
|
{
|
|
|
|
|
NameTextBox.Text = GetDefaultVDIName();
|
|
|
|
|
diskSpinner1.Populate(minSize: minSize);
|
2022-11-29 23:59:01 +01:00
|
|
|
|
srPicker.Populate(pickerUsage, connection, affinity, null, new[] { NewDisk() });
|
|
|
|
|
buttonRescan.Enabled = srPicker.CanBeScanned;
|
2020-03-30 00:49:06 +02:00
|
|
|
|
UpdateErrorsAndButtons();
|
2019-09-04 03:03:44 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-02-22 03:19:47 +01:00
|
|
|
|
_diskTemplate = diskTemplate;
|
|
|
|
|
NameTextBox.Text = _diskTemplate.Name();
|
|
|
|
|
DescriptionTextBox.Text = _diskTemplate.Description();
|
2019-09-04 03:03:44 +02:00
|
|
|
|
Text = Messages.EDIT_DISK;
|
|
|
|
|
OkButton.Text = Messages.OK;
|
2023-02-22 03:19:47 +01:00
|
|
|
|
diskSpinner1.Populate(_diskTemplate.virtual_size, minSize);
|
|
|
|
|
srPicker.Populate(pickerUsage, connection, affinity, connection.Resolve(_diskTemplate.SR), new[] { NewDisk() });
|
2022-11-29 23:59:01 +01:00
|
|
|
|
buttonRescan.Enabled = srPicker.CanBeScanned;
|
2020-03-30 00:49:06 +02:00
|
|
|
|
UpdateErrorsAndButtons();
|
2019-09-04 03:03:44 +02:00
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-13 01:39:12 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-02-22 02:10:30 +01:00
|
|
|
|
public VDI Disk { get; private set; }
|
|
|
|
|
|
|
|
|
|
public VBD Device { get; private set; }
|
|
|
|
|
|
2019-09-04 03:03:44 +02:00
|
|
|
|
public bool DontCreateVDI { get; set; }
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-02-22 03:19:47 +01:00
|
|
|
|
internal override string HelpName => _diskTemplate == null ? "NewDiskDialog" : "EditNewDiskDialog";
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
private string GetDefaultVDIName()
|
|
|
|
|
{
|
|
|
|
|
List<string> usedNames = new List<string>();
|
2023-02-22 03:19:47 +01:00
|
|
|
|
foreach (VDI v in connection.Cache.VDIs.Concat(_vdiNamesInUse))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
usedNames.Add(v.Name());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
return Helpers.MakeUniqueName(Messages.DEFAULT_VDI_NAME, usedNames);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-13 01:39:12 +01:00
|
|
|
|
private void srListBox_SelectedIndexChanged(object sender, EventArgs e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-09-04 03:03:44 +02:00
|
|
|
|
UpdateErrorsAndButtons();
|
2015-06-18 16:03:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 23:59:01 +01:00
|
|
|
|
private void srPicker_CanBeScannedChanged()
|
|
|
|
|
{
|
|
|
|
|
buttonRescan.Enabled = srPicker.CanBeScanned;
|
|
|
|
|
UpdateErrorsAndButtons();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonRescan_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
srPicker.ScanSRs();
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
private void OkButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2022-11-15 14:58:22 +01:00
|
|
|
|
if (srPicker.SR == null || NameTextBox.Text == "" || !connection.IsConnected)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
|
2023-02-22 02:10:30 +01:00
|
|
|
|
Disk = NewDisk();
|
|
|
|
|
Device = NewDevice();
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (DontCreateVDI)
|
|
|
|
|
{
|
|
|
|
|
DialogResult = DialogResult.OK;
|
|
|
|
|
Close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-22 03:08:51 +01:00
|
|
|
|
|
|
|
|
|
SR sr = srPicker.SR;
|
|
|
|
|
|
2023-02-22 03:19:47 +01:00
|
|
|
|
if (!sr.shared && _vm != null && _vm.HaPriorityIsRestart())
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-04-22 15:47:03 +02:00
|
|
|
|
using (var dlg = new WarningDialog(Messages.NEW_SR_DIALOG_ATTACH_NON_SHARED_DISK_HA,
|
|
|
|
|
ThreeButtonDialog.ButtonYes, ThreeButtonDialog.ButtonNo))
|
2016-06-20 11:49:12 +02:00
|
|
|
|
{
|
2023-02-22 03:08:51 +01:00
|
|
|
|
if (dlg.ShowDialog(this) != DialogResult.Yes)
|
|
|
|
|
return;
|
2016-06-20 11:49:12 +02:00
|
|
|
|
}
|
2023-02-22 03:08:51 +01:00
|
|
|
|
|
2023-02-22 03:19:47 +01:00
|
|
|
|
new HAUnprotectVMAction(_vm).RunSync(_vm.Connection.Session);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 03:19:47 +01:00
|
|
|
|
if (_vm != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-02-23 16:19:22 +01:00
|
|
|
|
//note that this action alters the Device
|
2023-02-22 03:19:47 +01:00
|
|
|
|
var action = new CreateDiskAction(Disk, Device, _vm);
|
2019-07-09 14:51:32 +02:00
|
|
|
|
using (var dialog = new ActionProgressDialog(action, ProgressBarStyle.Blocks))
|
|
|
|
|
dialog.ShowDialog();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (!action.Succeeded)
|
|
|
|
|
return;
|
2023-02-22 03:08:51 +01:00
|
|
|
|
|
|
|
|
|
// Now try to plug the VBD.
|
2023-02-22 03:19:47 +01:00
|
|
|
|
var plugAction = new VbdSaveAndPlugAction(_vm, Device, Disk.Name(), _vm.Connection.Session, false);
|
2023-02-22 03:08:51 +01:00
|
|
|
|
plugAction.ShowUserInstruction += PlugAction_ShowUserInstruction;
|
|
|
|
|
plugAction.RunAsync();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-02-22 03:08:51 +01:00
|
|
|
|
var action = new CreateDiskAction(Disk);
|
2019-07-09 14:51:32 +02:00
|
|
|
|
using (var dialog = new ActionProgressDialog(action, ProgressBarStyle.Marquee))
|
|
|
|
|
dialog.ShowDialog();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (!action.Succeeded)
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-22 03:08:51 +01:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
DialogResult = DialogResult.OK;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 17:03:09 +02:00
|
|
|
|
private void PlugAction_ShowUserInstruction(string message)
|
|
|
|
|
{
|
|
|
|
|
Program.Invoke(Program.MainWindow, () =>
|
|
|
|
|
{
|
|
|
|
|
if (!Program.RunInAutomatedTestMode)
|
|
|
|
|
{
|
2020-04-22 15:47:03 +02:00
|
|
|
|
using (var dlg = new InformationDialog(message))
|
2019-08-16 17:03:09 +02:00
|
|
|
|
dlg.ShowDialog(Program.MainWindow);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 02:10:30 +01:00
|
|
|
|
private VDI NewDisk()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-10-11 10:26:54 +02:00
|
|
|
|
VDI vdi = new VDI
|
|
|
|
|
{
|
|
|
|
|
Connection = connection,
|
2023-02-22 03:19:47 +01:00
|
|
|
|
read_only = _diskTemplate?.read_only ?? false,
|
2022-11-15 14:58:22 +01:00
|
|
|
|
SR = srPicker.SR == null ? new XenRef<SR>(Helper.NullOpaqueRef) : new XenRef<SR>(srPicker.SR),
|
2022-10-11 10:26:54 +02:00
|
|
|
|
virtual_size = diskSpinner1.SelectedSize,
|
|
|
|
|
name_label = NameTextBox.Text,
|
|
|
|
|
name_description = DescriptionTextBox.Text,
|
2023-02-22 03:19:47 +01:00
|
|
|
|
sharable = _diskTemplate?.sharable ?? false,
|
|
|
|
|
type = _diskTemplate?.type ?? vdi_type.user
|
2022-10-11 10:26:54 +02:00
|
|
|
|
};
|
2023-02-22 03:19:47 +01:00
|
|
|
|
vdi.SetVmHint(_vm != null ? _vm.uuid : "");
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return vdi;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 02:10:30 +01:00
|
|
|
|
private VBD NewDevice()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
VBD vbd = new VBD();
|
|
|
|
|
vbd.Connection = connection;
|
|
|
|
|
vbd.device = "";
|
|
|
|
|
vbd.empty = false;
|
2023-02-22 02:10:30 +01:00
|
|
|
|
vbd.type = vbd_type.Disk;
|
|
|
|
|
vbd.mode = vbd_mode.RW;
|
2017-09-05 03:15:38 +02:00
|
|
|
|
vbd.SetIsOwner(true);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
vbd.unpluggable = true;
|
|
|
|
|
return vbd;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void NameTextBox_TextChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2019-09-04 03:03:44 +02:00
|
|
|
|
UpdateErrorsAndButtons();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 03:03:44 +02:00
|
|
|
|
private void diskSpinner1_SelectedSizeChanged()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-11-15 14:58:22 +01:00
|
|
|
|
srPicker.UpdateDisks(NewDisk());
|
2019-09-04 03:03:44 +02:00
|
|
|
|
UpdateErrorsAndButtons();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 03:03:44 +02:00
|
|
|
|
private void UpdateErrorsAndButtons()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
// Ordering is important here, we want to show the most relevant message
|
2019-09-04 03:03:44 +02:00
|
|
|
|
// The error should be shown only for size errors
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2019-09-04 03:03:44 +02:00
|
|
|
|
if (!diskSpinner1.IsSizeValid)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
OkButton.Enabled = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-09-04 03:03:44 +02:00
|
|
|
|
|
2022-11-29 23:59:01 +01:00
|
|
|
|
bool allDisabled = true;
|
|
|
|
|
bool anyScanning = false;
|
|
|
|
|
|
|
|
|
|
foreach (SrPickerItem item in srPicker.Items)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (item.Enabled)
|
|
|
|
|
{
|
|
|
|
|
allDisabled = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (item.Scanning)
|
|
|
|
|
anyScanning = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (allDisabled)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
OkButton.Enabled = false;
|
2022-11-29 23:59:01 +01:00
|
|
|
|
diskSpinner1.SetError(anyScanning ? null : Messages.NO_VALID_DISK_LOCATION);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-09-04 03:03:44 +02:00
|
|
|
|
|
2022-11-15 14:58:22 +01:00
|
|
|
|
if (srPicker.SR == null) //enabled SR exists but the user selects a disabled one
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
OkButton.Enabled = false;
|
2019-09-04 03:03:44 +02:00
|
|
|
|
diskSpinner1.SetError(null);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-09-04 03:03:44 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (string.IsNullOrEmpty(NameTextBox.Text.Trim()))
|
|
|
|
|
{
|
|
|
|
|
OkButton.Enabled = false;
|
2019-09-04 03:03:44 +02:00
|
|
|
|
diskSpinner1.SetError(null);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OkButton.Enabled = true;
|
2019-09-04 03:03:44 +02:00
|
|
|
|
diskSpinner1.SetError(null);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CloseButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
DialogResult = DialogResult.Cancel;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|