diff --git a/ezmomi/ezmomi.py b/ezmomi/ezmomi.py index 53a8a79..0d0b79a 100644 --- a/ezmomi/ezmomi.py +++ b/ezmomi/ezmomi.py @@ -356,6 +356,8 @@ def clone(self): clonespec.powerOn = True clonespec.template = False + self.addDisks(template_vm, clonespec) + if self.debug: self.print_debug("CloneSpec", clonespec) @@ -382,6 +384,54 @@ def clone(self): if self.config['mail']: self.send_email() + def addDisks(self, vm, spec): + # get all disks on the VM, set unit_number to the last taken + unit_number = 0 + controller = None + # XXX more than one SCSI controller? + for dev in vm.config.hardware.device: + if hasattr(dev.backing, 'fileName'): + unit_number = max(unit_number, int(dev.unitNumber)) + if isinstance(dev, vim.vm.device.VirtualSCSIController): + controller = dev + + dev_changes = [] + for key, disk_spec in enumerate(self.config['disks']): + disk_size_str, disk_type = disk_spec.partition(",")[::2] + new_disk_kb = int(disk_size_str) * 1024 * 1024 + if new_disk_kb <= 0: + # ignore + continue + + unit_number += 1 + # unit_number 7 reserved for scsi controller + if unit_number == 7: + unit_number += 1 + if unit_number >= 16: + raise "too many disks" + + if self.debug: + self.print_debug("disk size %s[GB]" % key, disk_size_str) + self.print_debug("disk unit_number %d" % key, unit_number) + self.print_debug("controller %d" % key, controller) + + # add disk here + disk_spec = vim.vm.device.VirtualDeviceSpec() + disk_spec.fileOperation = "create" + disk_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add + disk_spec.device = vim.vm.device.VirtualDisk() + disk_spec.device.backing = \ + vim.vm.device.VirtualDisk.FlatVer2BackingInfo() + # XXX + if disk_type == 'thin': + disk_spec.device.backing.thinProvisioned = True + disk_spec.device.backing.diskMode = 'persistent' + disk_spec.device.unitNumber = unit_number + disk_spec.device.capacityInKB = new_disk_kb + disk_spec.device.controllerKey = controller.key + dev_changes.append(disk_spec) + spec.config.deviceChange += dev_changes + def destroy(self): tasks = list() diff --git a/ezmomi/params.py b/ezmomi/params.py index ae2f5e3..9786642 100644 --- a/ezmomi/params.py +++ b/ezmomi/params.py @@ -202,6 +202,15 @@ def arg_setup(): type=float, help="Memory in GB" ) + clone_parser.add_argument( + "--disks", + required=False, + default="", + type=str, + help="Additional disks in GB, e.g. 16,thin 32; " + "adds two disks, 16GB (thin provisioned) and 32GB (default)", + nargs="+", + ) clone_parser.add_argument( "--domain", type=str,