Skip to content

Commit

Permalink
Merge pull request #839 from Autosde/411361_vmware_disk_size
Browse files Browse the repository at this point in the history
add a customizable disk_size field to vm post-provision
  • Loading branch information
agrare committed Dec 8, 2022
2 parents 84e6290 + ec33f2e commit db73578
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 31 deletions.
Expand Up @@ -5,11 +5,37 @@ module ManageIQ::Providers::Vmware::InfraManager::Provision::Configuration
include_concern 'Network'
include_concern 'Disk'

def reconfigure_hardware_on_destination?
def reconfigure_container_on_destination?
# Do we need to perform a post-clone hardware reconfigure on the new VM?
[:cpu_limit, :memory_limit, :cpu_reserve, :memory_reserve].any? do |k|
return false unless options.key?(k)
destination.send(k) != options[k]
end
end

def reconfigure_disk_on_destination?
return false unless options.key?(:allocated_disk_storage)

# TODO: short-term fix to only enable :allocated_disk_storage in machines that have a single hard disk
# in the long-term it should be able to deal with multiple hard disks
if vm.hardware.disks.where(:device_type => "disk").length != 1
_log.info("custom disk size is currently only supported for machines that have a single hard disk")
return false
end

default_size = vm.hardware.disks.find_by(:device_type => "disk").size / (1024**3)
get_option(:allocated_disk_storage).to_f > default_size
end

def reconfigure_hardware
config_spec = VimHash.new("VirtualMachineConfigSpec") do |vmcs|
set_cpu_and_memory_allocation(vmcs) if reconfigure_container_on_destination?
set_disk_allocation(vm, vmcs) if reconfigure_disk_on_destination?
end
return if config_spec.empty?

_log.info("Calling VM reconfiguration")
dump_obj(config_spec, "#{_log.prefix} Post-create Config spec: ", $log, :info)
vm.spec_reconfigure(config_spec)
end
end
Expand Up @@ -107,25 +107,19 @@ def get_next_device_idx
@new_device_idx -= 1
end

def set_cpu_and_memory_allocation(vm)
config_spec = VimHash.new("VirtualMachineConfigSpec") do |vmcs|
vmcs.cpuAllocation = VimHash.new("ResourceAllocationInfo") do |rai|
set_spec_option(rai, :limit, :cpu_limit, nil, :to_i)
set_spec_option(rai, :reservation, :cpu_reserve, nil, :to_i)
end

vmcs.memoryAllocation = VimHash.new("ResourceAllocationInfo") do |rai|
set_spec_option(rai, :limit, :memory_limit, nil, :to_i)
set_spec_option(rai, :reservation, :memory_reserve, nil, :to_i)
end
def set_cpu_and_memory_allocation(vmcs)
vmcs.cpuAllocation = VimHash.new("ResourceAllocationInfo") do |rai|
set_spec_option(rai, :limit, :cpu_limit, nil, :to_i)
set_spec_option(rai, :reservation, :cpu_reserve, nil, :to_i)
end

# Only explicitly disable #MemoryReservationLockedToMax if the memory reserve
# is less than the total vm memory
vmcs.memoryReservationLockedToMax = false if get_option(:memory_reserve).to_i < get_option(:vm_memory).to_i
vmcs.memoryAllocation = VimHash.new("ResourceAllocationInfo") do |rai|
set_spec_option(rai, :limit, :memory_limit, nil, :to_i)
set_spec_option(rai, :reservation, :memory_reserve, nil, :to_i)
end

_log.info("Calling VM reconfiguration")
dump_obj(config_spec, "#{_log.prefix} Post-create Config spec: ", $log, :info)
vm.spec_reconfigure(config_spec)
# Only explicitly disable #MemoryReservationLockedToMax if the memory reserve
# is less than the total vm memory
vmcs.memoryReservationLockedToMax = false if get_option(:memory_reserve).to_i < get_option(:vm_memory).to_i
end
end
Expand Up @@ -105,6 +105,23 @@ def add_disk(vmcs, disk, controller, new_dev_key)
end
end

def set_disk_allocation(vm, vmcs)
disk = vm.hardware.disks.find_by(:device_type => "disk")
vm.with_provider_object do |vim_obj|
device = vim_obj.getDeviceByLabel(disk.device_name)
new_capacity_in_kb = (get_option(:allocated_disk_storage).to_f * 1.megabyte).to_i
add_device_config_spec(vmcs, VirtualDeviceConfigSpecOperation::Edit) do |vdcs|
vdcs.device = VimHash.new("VirtualDisk") do |dev|
dev.key = device.key
dev.capacityInKB = new_capacity_in_kb
dev.controllerKey = device.controllerKey
dev.unitNumber = device.unitNumber
dev.backing = device.backing
end
end
end
end

private

def devices
Expand Down
Expand Up @@ -72,7 +72,7 @@ def customize_destination
_log.info("Post-processing #{destination_type} id: [#{destination.id}], name: [#{dest_name}]")
update_and_notify_parent(:message => "Starting New #{destination_type} Customization")

set_cpu_and_memory_allocation(destination) if reconfigure_hardware_on_destination?
reconfigure_hardware
signal :autostart_destination
end

Expand Down
Expand Up @@ -3,8 +3,7 @@ def customize_destination
_log.info("Post-processing #{destination_type} id: [#{destination.id}], name: [#{dest_name}]")
update_and_notify_parent(:message => "Starting New #{destination_type} Customization")

set_cpu_and_memory_allocation(destination) if reconfigure_hardware_on_destination?

reconfigure_hardware
signal :create_pxe_configuration_file
end

Expand Down
Expand Up @@ -615,6 +615,13 @@
:display: :edit
:default: unchanged
:data_type: :string
:allocated_disk_storage:
:description: Allocated Disk Storage (GB)
:required: false
:display: :edit
:data_type: :string
:notes: (default taken from template, custom can only be larger)
:notes_display: :show
:cpu_limit:
:description: CPU (MHz)
:required: false
Expand Down
Expand Up @@ -652,6 +652,13 @@
:display: :edit
:default: unchanged
:data_type: :string
:allocated_disk_storage:
:description: Allocated Disk Storage (GB)
:required: false
:display: :edit
:data_type: :string
:notes: (default taken from template, custom can only be larger)
:notes_display: :show
:cpu_limit:
:description: CPU (MHz)
:required: false
Expand Down
Expand Up @@ -661,6 +661,13 @@
:display: :edit
:default: unchanged
:data_type: :string
:allocated_disk_storage:
:description: Allocated Disk Storage (GB)
:required: false
:display: :edit
:data_type: :string
:notes: (default taken from template, custom can only be larger)
:notes_display: :show
:cpu_limit:
:description: CPU (MHz)
:required: false
Expand Down
7 changes: 7 additions & 0 deletions content/miq_dialogs/miq_provision_vmware_dialogs_ovf.yaml
Expand Up @@ -677,6 +677,13 @@
:display: :edit
:default: unchanged
:data_type: :string
:allocated_disk_storage:
:description: Allocated Disk Storage (GB)
:required: false
:display: :edit
:data_type: :string
:notes: (default taken from template, custom can only be larger)
:notes_display: :show
:cpu_limit:
:description: CPU (MHz)
:required: false
Expand Down
Expand Up @@ -693,6 +693,13 @@
:display: :edit
:default: unchanged
:data_type: :string
:allocated_disk_storage:
:description: Allocated Disk Storage (GB)
:required: false
:display: :edit
:data_type: :string
:notes: (default taken from template, custom can only be larger)
:notes_display: :show
:cpu_limit:
:description: CPU (MHz)
:required: false
Expand Down
Expand Up @@ -605,6 +605,13 @@
:display: :edit
:default: unchanged
:data_type: :string
:allocated_disk_storage:
:description: Allocated Disk Storage (GB)
:required: false
:display: :edit
:data_type: :string
:notes: (default taken from template, custom can only be larger)
:notes_display: :show
:cpu_limit:
:description: CPU (MHz)
:required: false
Expand Down
Expand Up @@ -7,11 +7,12 @@
@admin = FactoryBot.create(:user_admin)
@target_vm_name = 'clone test'
@options = {
:pass => 1,
:vm_name => @target_vm_name,
:number_of_vms => 1,
:cpu_limit => -1,
:cpu_reserve => 0
:pass => 1,
:vm_name => @target_vm_name,
:number_of_vms => 1,
:cpu_limit => -1,
:cpu_reserve => 0,
:allocated_disk_storage => 16
}
end

Expand Down Expand Up @@ -76,12 +77,20 @@
end
end

it "should detect when a reconfigure_hardware_on_destination call is required" do
target_vm = FactoryBot.create(:vm_vmware, :name => "target_vm1", :location => "abc/def.vmx", :cpu_limit => @vm_prov.options[:cpu_limit])
@vm_prov.destination = target_vm
expect(@vm_prov.reconfigure_hardware_on_destination?).to eq(false)
it "should detect when reconfigure container or disk calls are required" do
target_vm1 = FactoryBot.create(:vm_vmware, :name => "target_vm1", :location => "abc/def.vmx", :cpu_limit => @vm_prov.options[:cpu_limit],
:hardware => FactoryBot.create(:hardware, :disks => FactoryBot.create(:disks, [:device_type => "disk", :size => 16 * 1.gigabyte])))
target_vm2 = FactoryBot.create(:vm_vmware, :name => "target_vm1", :location => "abc/def.vmx", :cpu_limit => @vm_prov.options[:cpu_limit],
:hardware => FactoryBot.create(:hardware, :disks => FactoryBot.create(:disks, [{:device_type => "disk", :size => 16 * 1.gigabyte},
{:device_type => "disk", :size => 16 * 1.gigabyte}])))
@vm_prov.destination = target_vm1
expect(@vm_prov.reconfigure_container_on_destination?).to eq(false)
@vm_prov.options[:cpu_limit] = 100
expect(@vm_prov.reconfigure_hardware_on_destination?).to eq(true)
expect(@vm_prov.reconfigure_container_on_destination?).to eq(true)
@vm_prov.options[:allocated_disk_storage] = 20
expect(@vm_prov.reconfigure_disk_on_destination?).to eq(true)
@vm_prov.destination = target_vm2
expect(@vm_prov.reconfigure_disk_on_destination?).to eq(false)
end

it "should delete unneeded network cards" do
Expand Down

0 comments on commit db73578

Please sign in to comment.