Skip to content

Instantly share code, notes, and snippets.

@warroyo
Last active November 8, 2019 07:45
Show Gist options
  • Save warroyo/4887300cbb3bec2034650202a65fb906 to your computer and use it in GitHub Desktop.
Save warroyo/4887300cbb3bec2034650202a65fb906 to your computer and use it in GitHub Desktop.
plugin for knife-vsphere to add extend disk, extra disk, and memory reservation during clone
require 'rbvmomi'
class KnifeVspherePlugin
##
# example json data
# disk_num will default to 1
# datastore will default to the first disks datastore
# pass the below json as a string using the --cplugin-data option
# {"memory_reservation":200, "extend_disk":{"new_size_gb":11,"disk_num":1},"extra_disk":{"size":10,"datastore":"somedatastore"}}
##
attr_accessor :data # rather than defining a data= method
def customize_clone_spec(src_config, clone_spec)
unless data.nil?
json_data = JSON.parse(data)
#add memory reservation if requested
if json_data.key?('memory_reservation')
puts "Memory Reservation #{json_data['memory_reservation']}"
clone_spec.config.memoryAllocation = {"reservation"=> json_data['memory_reservation']}
end
#extend 1st disk or a disk by number
if json_data.key?('extend_disk')
disk_num= json_data['extend_disk']['disk_num'] || 1
new_disk_size = json_data['extend_disk']['new_size_gb']
vmdk_size_kb = new_disk_size.to_i * 1024 * 1024
src_config.hardware.device.each do |device|
if device.class == RbVmomi::VIM::VirtualDisk
if device.deviceInfo.label == "Hard disk #{disk_num}"
puts "Disk #{disk_num} found !"
puts "New disk size #{new_disk_size} GB"
device.capacityInKB=vmdk_size_kb
extend_disk_spec = RbVmomi::VIM::VirtualDeviceConfigSpec(
device: device,
operation: RbVmomi::VIM::VirtualDeviceConfigSpecOperation('edit')
)
clone_spec.config.deviceChange << extend_disk_spec
end
end
end
end
#add another disk if requested
if json_data.key?('extra_disk')
#get number of disks on template that is being cloned
disks =src_config.hardware.device.select do |device|
device.is_a? RbVmomi::VIM::VirtualDisk
end
disk_number = disks.length+1
#use same datatsore as first disk if not specified
datastore = json_data['extra_disk']['datastore'] || src_config.datastoreUrl[0].name
size = json_data['extra_disk']['size'].to_i * 1024 * 1024
puts "Extra disk size #{json_data['extra_disk']['size']} GB "
newdisk = RbVmomi::VIM::VirtualDeviceConfigSpec(
operation: 'add',
fileOperation: :create,
device: RbVmomi::VIM.VirtualDisk(
key: disk_number,
backing: RbVmomi::VIM.VirtualDiskFlatVer2BackingInfo(
fileName: "[#{datastore}]",
diskMode: "persistent",
thinProvisioned: false,
),
controllerKey: 1000,
unitNumber: disk_number,
capacityInKB: size,
)
)
clone_spec.config.deviceChange << newdisk
end
end
#dump clone_spec for debugging
Chef::Log.debug(YAML::dump(clone_spec))
clone_spec
end
end
{
"memory_reservation": 200,
"extend_disk": {
"new_size_gb": 11,
"disk_num": 1
},
"extra_disk": {
"size": 10,
"datastore": "somedatastore"
}
}
@yangpeng-chn
Copy link

Seems like new_size_gb is the total size after extending the disk. Changed the value to a larger one and it worked.
If the disk is 30GB, we have to use a number that is greater than 30. e.g.
--cplugin-data '{"extend_disk":{"new_size_gb":40,"disk_num":1},"extra_disk":{"size":15,"datastore":"Verify_003"}}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment