Last active
August 29, 2015 14:10
-
-
Save hartsock/3287cca2b8797f944b1e to your computer and use it in GitHub Desktop.
A very rough sample that demonstrates mounting a disk on a VM.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
si = connect.SmartConnect(host='vcsa', | |
user='my_user', | |
pwd='my_password') | |
content = si.RetrieveContent() | |
datacenters = [entity for entity in content.rootFolder.childEntity | |
if hasattr(entity, 'vmFolder')] | |
dc = None | |
for datacenter in datacenters: | |
if datacenter.name == 'MyDatacenter': | |
dc = datacenter | |
search = content.searchIndex | |
vm = search.FindByDatastorePath(dc, '[MyStorage] virtual_machine/virtual_machine.vmx') | |
# the details we will need to make the disk: | |
disk_path = '[storage0] disks/some_disk_file.vmdk' | |
capacity = 16 * 1024 * 1024 | |
controller = None | |
devices = vm.config.hardware.device | |
for device in devices: | |
if 'SCSI' in device.deviceInfo.label: | |
controller = device | |
virtual_disk = vim.vm.device.VirtualDisk() | |
# https://github.com/vmware/pyvmomi/blob/master | |
# /docs/vim/vm/device/VirtualDisk/FlatVer2BackingInfo.rst | |
virtual_disk.backing = \ | |
vim.vm.device.VirtualDisk.FlatVer2BackingInfo() | |
# https://github.com/vmware/pyvmomi/blob/master | |
# /docs/vim/vm/device/VirtualDiskOption/ | |
# DiskMode.rst#independent_persistent | |
virtual_disk.backing.diskMode = \ | |
vim.vm.device.VirtualDiskOption.DiskMode.persistent | |
virtual_disk.backing.thinProvisioned = False | |
virtual_disk.backing.eagerlyScrub = True | |
virtual_disk.backing.fileName = disk_path | |
virtual_disk.connectable = vim.vm.device.VirtualDevice.ConnectInfo() | |
virtual_disk.connectable.startConnected = True | |
virtual_disk.connectable.allowGuestControl = False | |
virtual_disk.connectable.connected = True | |
virtual_disk.key = -100 | |
virtual_disk.controllerKey = controller.key | |
virtual_disk.unitNumber = len(controller.device) | |
virtual_disk.capacityInKB = capacity | |
device_spec = vim.vm.device.VirtualDiskSpec() | |
device_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add | |
device_spec.device = virtual_disk | |
spec = vim.vm.ConfigSpec() | |
spec.deviceChange = [device_spec] | |
task = vm.ReconfigVM_Task(spec) | |
while task.info.state in [vim.TaskInfo.State.queued, | |
vim.TaskInfo.State.running]: | |
print(".") | |
print("done.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a suggestion from an off line review:
Anyone seriously looking to use this script in a production environment should consider that.