Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to add VMDK to VM #35

Merged
merged 1 commit into from
Apr 25, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ VM Operations:
-Power on/off
-Clone (with optional chef bootstrap and run list)
-Delete
-VMDK addition
Clone-specific customization options (for linux guests):
-Destination folder
-CPU core count
Expand Down Expand Up @@ -129,6 +130,13 @@ Arguments are optional, and allow for redirection in Linux and Solaris.
--exec-passwd PASSWD - The password for the user executing as.
--exec-dir DIRECTORY - Optional: Working directory to execute in. Will default to $HOME of user.

== knife vsphere vm vmdk add VMNAME SIZE

Adds VMDK to VM.

Optional arguments

--vmdk-type TYPE - VMDK type, "thick" or "thin", defaults to "thin"

= LICENSE:

Expand Down
6 changes: 6 additions & 0 deletions lib/chef/knife/BaseVsphereCommand.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ def find_datastore(dsName)
baseEntity.find { |f| f.info.name == dsName } or abort "no such datastore #{dsName}"
end

def find_device(vm,deviceName)
vm.config.hardware.device.each do |device|
return device if device.deviceInfo.label == deviceName
end
nil
end

def find_all_in_folder(folder, type)
if folder.instance_of?(RbVmomi::VIM::ClusterComputeResource)
Expand Down
101 changes: 101 additions & 0 deletions lib/chef/knife/vsphere_vm_vmdk_add.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#
# Author:: Brian Flad (<bflad417@gmail.com>)
# License:: Apache License, Version 2.0
#
require 'chef/knife'
require 'chef/knife/BaseVsphereCommand'

# Lists all known virtual machines in the configured datacenter
class Chef::Knife::VsphereVmVmdkAdd < Chef::Knife::BaseVsphereCommand

banner "knife vsphere vm vmdk add"

get_common_options

option :vmdk_type,
:long => "--vmdk-type TYPE",
:description => "Type of VMDK"
$default[:vmdk_type] = "thin"

def run
$stdout.sync = true

vmname = @name_args[0]
if vmname.nil?
show_usage
ui.fatal("You must specify a virtual machine name")
exit 1
end

size = @name_args[1]
if size.nil?
ui.fatal "You need a VMDK size!"
show_usage
exit 1
end

vim = get_vim_connection
vdm = vim.serviceContent.virtualDiskManager
vm = get_vm(vmname)

vmdk_datastore = vm.datastore[0]
vmdk_fileName = "#{vmname}/#{vmname}_1.vmdk"
vmdk_name = "[#{vmdk_datastore.name}] #{vmdk_fileName}"
vmdk_size_kb = size.to_i * 1024 * 1024
vmdk_type = get_config(:vmdk_type)
vmdk_type = "preallocated" if vmdk_type == "thick"

vmdk_spec = RbVmomi::VIM::FileBackedVirtualDiskSpec(
:adapterType => "lsiLogic",
:capacityKb => vmdk_size_kb,
:diskType => vmdk_type
)

ui.info "Creating VMDK"
ui.info "#{ui.color "Capacity:", :cyan} #{size} GB"
ui.info "#{ui.color "Disk:", :cyan} #{vmdk_name}"

if get_config(:noop)
ui.info "#{ui.color "Skipping disk creation process because --noop specified.", :red}"
else
vdm.CreateVirtualDisk_Task(
:datacenter => get_datacenter,
:name => vmdk_name,
:spec => vmdk_spec
).wait_for_completion
end

ui.info "Attaching VMDK to #{vmname}"

controller = find_device(vm,"SCSI controller 0")

vmdk_backing = RbVmomi::VIM::VirtualDiskFlatVer2BackingInfo(
:datastore => vmdk_datastore,
:diskMode => "persistent",
:fileName => vmdk_name,
)

device = RbVmomi::VIM::VirtualDisk(
:backing => vmdk_backing,
:capacityInKB => vmdk_size_kb,
:controllerKey => controller.key,
:key => -1,
:unitNumber => controller.device.size + 1
)

device_config_spec = RbVmomi::VIM::VirtualDeviceConfigSpec(
:device => device,
:operation => RbVmomi::VIM::VirtualDeviceConfigSpecOperation("add")
)

vm_config_spec = RbVmomi::VIM::VirtualMachineConfigSpec(
:deviceChange => [device_config_spec]
)

if get_config(:noop)
ui.info "#{ui.color "Skipping disk attaching process because --noop specified.", :red}"
else
vm.ReconfigVM_Task(:spec => vm_config_spec).wait_for_completion
end
end
end