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;
|
2020-05-04 01:32:53 +02:00
|
|
|
|
using System.Linq;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAPI;
|
|
|
|
|
using XenAdmin.Actions.VMActions;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Actions
|
|
|
|
|
{
|
|
|
|
|
public class ChangeMemorySettingsAction : AsyncAction
|
|
|
|
|
{
|
2022-03-17 14:03:15 +01:00
|
|
|
|
private readonly long _staticMin, _staticMax, _dynamicMin, _dynamicMax;
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
private Action<VMStartAbstractAction, Failure> _startDiagnosticForm;
|
2022-03-17 14:03:15 +01:00
|
|
|
|
private Action<VM, bool> _warningDialogHaInvalidConfig;
|
|
|
|
|
|
|
|
|
|
private readonly bool _staticChanged;
|
|
|
|
|
private readonly bool _needReboot;
|
|
|
|
|
private readonly Host _vmHost;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
public ChangeMemorySettingsAction(VM vm, string title,
|
2022-03-17 14:03:15 +01:00
|
|
|
|
long staticMin, long dynamicMin, long dynamicMax, long staticMax, Action<VM, bool> warningDialogHaInvalidConfig, Action<VMStartAbstractAction, Failure> startDiagnosticForm, bool suppressHistory)
|
2014-07-21 12:19:04 +02:00
|
|
|
|
: base(vm.Connection, title, suppressHistory)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-03-17 14:03:15 +01:00
|
|
|
|
_warningDialogHaInvalidConfig = warningDialogHaInvalidConfig;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
_startDiagnosticForm = startDiagnosticForm;
|
|
|
|
|
VM = vm;
|
2022-03-17 14:03:15 +01:00
|
|
|
|
|
|
|
|
|
_staticMin = staticMin;
|
|
|
|
|
_dynamicMin = dynamicMin;
|
|
|
|
|
_dynamicMax = dynamicMax;
|
|
|
|
|
_staticMax = staticMax;
|
|
|
|
|
|
|
|
|
|
_vmHost = VM.Home();
|
|
|
|
|
_staticChanged = staticMin != VM.memory_static_min || staticMax != VM.memory_static_max;
|
|
|
|
|
|
|
|
|
|
if (_staticChanged)
|
|
|
|
|
_needReboot = VM.power_state != vm_power_state.Halted;
|
|
|
|
|
else
|
|
|
|
|
_needReboot = VM.power_state != vm_power_state.Halted && VM.power_state != vm_power_state.Running;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
#region RBAC Dependencies
|
|
|
|
|
|
2022-03-17 14:03:15 +01:00
|
|
|
|
if (_staticChanged)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
ApiMethodsToRoleCheck.Add("vm.set_memory_limits");
|
|
|
|
|
else
|
|
|
|
|
ApiMethodsToRoleCheck.Add("vm.set_memory_dynamic_range");
|
|
|
|
|
|
2022-03-17 14:03:15 +01:00
|
|
|
|
if (_needReboot)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-03-17 14:03:15 +01:00
|
|
|
|
if (VM.allowed_operations.Contains(vm_operations.clean_shutdown))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
ApiMethodsToRoleCheck.Add("vm.clean_shutdown");
|
|
|
|
|
else
|
|
|
|
|
ApiMethodsToRoleCheck.Add("vm.hard_shutdown");
|
2022-03-17 14:03:15 +01:00
|
|
|
|
|
|
|
|
|
if (_vmHost == null)
|
|
|
|
|
ApiMethodsToRoleCheck.Add("vm.start");
|
|
|
|
|
else
|
|
|
|
|
ApiMethodsToRoleCheck.Add("vm.start_on");
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Run()
|
|
|
|
|
{
|
|
|
|
|
// If either of the static memories has changed, we need to shut down the VM
|
|
|
|
|
// before and reboot afterwards. The user has already been warned about this.
|
2022-03-17 14:03:15 +01:00
|
|
|
|
|
|
|
|
|
if (_needReboot)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-03-17 14:03:15 +01:00
|
|
|
|
AsyncAction action;
|
|
|
|
|
if (VM.allowed_operations.Contains(vm_operations.clean_shutdown))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
action = new VMCleanShutdown(VM);
|
|
|
|
|
else
|
|
|
|
|
action = new VMHardShutdown(VM);
|
2020-05-04 01:32:53 +02:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-04-05 12:52:32 +02:00
|
|
|
|
action.RunSync(Session);
|
2020-05-04 01:32:53 +02:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
if (VM.power_state == vm_power_state.Halted || VM.current_operations.Any(op =>
|
|
|
|
|
op.Value == vm_operations.clean_shutdown || op.Value == vm_operations.hard_shutdown ||
|
|
|
|
|
op.Value == vm_operations.shutdown))
|
|
|
|
|
{
|
|
|
|
|
//ignore if it got already powered off or there are other shutting down tasks in progress
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VM.Connection.WaitFor(() => VM.power_state == vm_power_state.Halted, GetCancelling);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now save the memory settings. We can't use VM.SaveChanges() for this,
|
|
|
|
|
// because we have to do the operations simultaneously or we will
|
|
|
|
|
// violate the memory ordering constraints.
|
2022-03-17 14:03:15 +01:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-03-17 14:03:15 +01:00
|
|
|
|
if (_staticChanged)
|
|
|
|
|
VM.set_memory_limits(Session, VM.opaque_ref, _staticMin, _staticMax, _dynamicMin, _dynamicMax);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
else
|
2022-03-17 14:03:15 +01:00
|
|
|
|
VM.set_memory_dynamic_range(Session, VM.opaque_ref, _dynamicMin, _dynamicMax);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2022-03-17 14:03:15 +01:00
|
|
|
|
if (_needReboot)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-03-17 14:03:15 +01:00
|
|
|
|
// boot the VM, even if we failed to change the memory settings after the shutdown
|
|
|
|
|
VMStartAbstractAction action;
|
|
|
|
|
if (_vmHost == null)
|
|
|
|
|
action = new VMStartAction(VM, _warningDialogHaInvalidConfig, _startDiagnosticForm);
|
|
|
|
|
else
|
|
|
|
|
action = new VMStartOnAction(VM, _vmHost, _warningDialogHaInvalidConfig, _startDiagnosticForm);
|
|
|
|
|
|
2022-04-05 12:52:32 +02:00
|
|
|
|
action.RunSync(Session);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 04:33:29 +02:00
|
|
|
|
Description = string.Format(Messages.ACTION_CHANGE_MEMORY_SETTINGS_DONE, VM.Name());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|