如果手动克隆时候士有选项直接可以定义克隆后的磁盘空间大小
但是不知道在python调用时候在哪操作?是否可以这样操作?
#!/usr/bin/env python # -*-coding: UTF-8 -*- import sys import traceback from pyVmomi import vim from tools import cli, service_instance, pchelper from add_nic_to_vm import add_nic def wait_for_task(task): """ wait for a vCenter task to finish """ task_done = False while not task_done: if task.info.state == 'success': return task.info.result if task.info.state == 'error': print("there was an error") print(task.info.error) task_done = True def change_disk_size(self, vm_obj, vm_disk): """ :param vm_obj: 虚拟机对象(模板对象也适用) :param vm_disk: 硬盘大小(单位:G) :return: disk_change 配置列表 """ virtual_disk_devices = [] virtual_disk_device = None virtual_scsi_controller = None # Find the disk device for dev in vm_obj.config.hardware.device: if isinstance(dev, vim.vm.device.VirtualSCSIController): virtual_scsi_controller = dev if isinstance(dev, vim.vm.device.VirtualDisk): virtual_disk_devices.append(dev) for dev in virtual_disk_devices: if dev.controllerKey == virtual_scsi_controller.key: virtual_disk_device = dev new_disk_kb = int(vm_disk) * 1024 * 1024 virtual_disk_spec = vim.vm.device.VirtualDeviceSpec() virtual_disk_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit virtual_disk_spec.device = virtual_disk_device virtual_disk_spec.device.capacityInKB = new_disk_kb disk_changes = [] disk_changes.append(virtual_disk_spec) print(virtual_disk_spec) return disk_changes def clone_vm(content, template, vm_name, datastore_name, exsi_host, num_cpu, size_mem, ip_address ,subnet_mask, gateway, prot_group, folder, annotation): #网络参数 destination_host = pchelper.get_obj(content, [vim.HostSystem], exsi_host) adapter_map = vim.vm.customization.AdapterMapping() adapter_map.adapter = vim.vm.customization.IPSettings() adapter_map.adapter.ip = vim.vm.customization.FixedIp() adapter_map.adapter.ip.ipAddress = ip_address adapter_map.adapter.subnetMask = subnet_mask adapter_map.adapter.gateway = gateway # 设备参数 vmconf = vim.vm.ConfigSpec() vmconf.numCPUs = int(num_cpu) vmconf.memoryMB = int(size_mem) * 1024 vmconf.cpuHotAddEnabled = True vmconf.memoryHotAddEnabled = True vmconf.annotation = annotation device_change = [] disk_change = change_disk_size(content, template, '40') if type(disk_change) is list: device_change.extend(disk_change) else: return {'message': disk_change, 'result': False} vmconf.deviceChange = device_change global_ip = vim.vm.customization.GlobalIPSettings() ident = vim.vm.customization.LinuxPrep( hostName = vim.vm.customization.FixedName(name="localhost") ) custom_spec = vim.vm.customization.Specification(nicSettingMap=[adapter_map], globalIPSettings=global_ip, identity=ident) datastore = pchelper.get_obj(content, [vim.Datastore], datastore_name) if folder: folder = pchelper.search_for_obj(content, [vim.Folder], folder) else: folder = datacenter.vmFolder relospec = vim.vm.RelocateSpec() relospec.datastore = datastore relospec.host = destination_host relospec.pool = destination_host.parent.resourcePool clonespec = vim.vm.CloneSpec() clonespec.location = relospec clonespec.powerOn = True clonespec.config = vmconf clonespec.customization = custom_spec print('开始克隆') wait_for_task(template.Clone(folder=folder, name=vm_name, spec=clonespec)) print('结束克隆') def main(): args={} args["host"] = '192. args["user"] = 'admin' args["password"] = 'Aa63' args["port"] = '443' args["disable_ssl_verification"] = 'False' args["vm_name"] = '222' args["cpu"] = '1' args["mem"] = '2' args["port_group"] = 'VM Network' args["exsi_host"] = '192.168.10.134' args["datastore_name"] = 'datastore1' args["template"] = 'stand1' args["ipaddress"] = '192.168.10.199' args["subnetMask"] = '255.255.255.0' args["gateway"] = '192.168.10.191' args["annotation"] = '测试sample' args["folder"] = 'test' #建立连接 si = service_instance.connect(args) content = si.RetrieveContent() #检查模板是否存在 template = pchelper.search_for_obj(content,[vim.VirtualMachine],args["template"]) if template is None: raise RuntimeError("未找到模板{}".format(args["template"])) sys.exit(1) vm = pchelper.search_for_obj(content, [vim.VirtualMachine], args["vm_name"]) if vm is None: clone_vm(content, template, args["vm_name"], args["datastore_name"], args["exsi_host"], args["cpu"], args["mem"], args["ipaddress"], args["subnetMask"], args["gateway"], args["port_group"], args["folder"], args["annotation"]) # 开始程序 if __name__ == "__main__": main()
我本身是做运维的,不太懂python,东拼西凑的脚本加上自己的理解,勉强实现了功能,没有完善,但是功能可用,可以调整 处理器核数 内存大小 硬盘大小(只能调比模板大)