1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#!/usr/bin/env ruby
require 'trollop'
require 'rbvmomi'
require 'rbvmomi/trollop'
VIM = RbVmomi::VIM
opts = Trollop.options do
banner <<-EOS
Create a VM.
Usage:
create_vm.rb [options]
VIM connection options:
EOS
rbvmomi_connection_opts
text <<-EOS
VM location options:
EOS
rbvmomi_datacenter_opt
text <<-EOS
Other options:
EOS
end
Trollop.die("must specify host") unless opts[:host]
vm_name = ARGV[0] or abort "must specify VM name"
vim = VIM.connect opts
dc = vim.serviceInstance.find_datacenter(opts[:datacenter]) or abort "datacenter not found"
vmFolder = dc.vmFolder
hosts = dc.hostFolder.children
rp = hosts.first.resourcePool
vm_cfg = {
:name => vm_name,
:guestId => 'otherGuest',
:files => { :vmPathName => '[datastore1]' },
:numCPUs => 1,
:memoryMB => 128,
:deviceChange => [
{
:operation => :add,
:device => VIM.VirtualLsiLogicController(
:key => 1000,
:busNumber => 0,
:sharedBus => :noSharing
)
}, {
:operation => :add,
:fileOperation => :create,
:device => VIM.VirtualDisk(
:key => 0,
:backing => VIM.VirtualDiskFlatVer2BackingInfo(
:fileName => '[datastore1]',
:diskMode => :persistent,
:thinProvisioned => true
),
:controllerKey => 1000,
:unitNumber => 0,
:capacityInKB => 4000000
)
}, {
:operation => :add,
:device => VIM.VirtualE1000(
:key => 0,
:deviceInfo => {
:label => 'Network Adapter 1',
:summary => 'VM Network'
},
:backing => VIM.VirtualEthernetCardNetworkBackingInfo(
:deviceName => 'VM Network'
),
:addressType => 'generated'
)
}
],
:extraConfig => [
{
:key => 'bios.bootOrder',
:value => 'ethernet0'
}
]
}
vmFolder.CreateVM_Task(:config => vm_cfg, :pool => rp).wait_for_completion
|