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"
}
}
@maheshawagh
Copy link

I am also looking for similar requirement where I have to add extra disk post provisioning the VM. Could you please add steps on how to use this plugin?

@explody
Copy link

explody commented Jan 21, 2017

Save the ruby to a file somewhere, use like this:

knife vsphere clone vm <clone arguments> --cplugin /path/to/knife-vsphere-plugin.rb --cplugin-data '{"memory_reservation":200, "extend_disk":{"new_size_gb":11,"disk_num":1},"extra_disk":{"size":10,"datastore":"somedatastore"}}'

@BhargaviAllada
Copy link

Hi, Do we have a separate knife vSphere command to add extra disk to the VM post provisioning ?

@bby-bishopclark
Copy link

Hi, Do we have a separate knife vSphere command to add extra disk to the VM post provisioning ?

as per https://github.com/chef/knife-vsphere PR #35 in 2013 (chef/knife-vsphere#35 by Brian Flad)

knife vsphere vm vmdk add VMNAME DISKSIZE_GB

@yangpeng-chn
Copy link

Hi there, can anyone tell me why I am facing this error?

$ knife vsphere vm clone EXAMPLE \
> --ccpu 2 \
> --cdomain xxx \
> --cgw xxx \
> --chostname example \
> --cips xxx/24 \
> --cdnsips xxx \
> --cmacs xxx \
> --cram 4 \
> --ctz Asia/Tokyo \
> --cvlan TYO_VLAN392 \
> --datastorecluster xxx \
> --dest-folder xxx \
> --resource-pool xxx \
> --start \
> --cplugin ./cookbooks/sv_os_config/recipes/knife-vsphere-plugin.rb \
> --cplugin-data '{"extend_disk":{"new_size_gb":10,"disk_num":1},"extra_disk":{"size":15,"datastore":"Verify_003"}}' \
> --template xxx -f xxx
Disk 1 found !
New disk size 10 GB
Extra disk size 15 GB
Cloning template xxx to new VM EXAMPLE
ERROR: RbVmomi::Fault: InvalidArgument: A specified parameter was not correct: spec.deviceChange.device

If I pass the --cplugin-data as follow, it works, so it seems the extend_disk doesn't work in my environment.

--cplugin-data '{"extra_disk":{"size":15,"datastore":"Verify_003"}}'

Any feedback is highly appreciated.

@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