Hi,
I am trying to accomplish a pretty simple task, and it should be easy enough, but I cannot get it to work. I hope someone inhere will be able to figure out why.
I want to move all disks in a VM (Except for the first OS disk: Harddisk 1) to a second Para Virtual Controller (Controller 1) The controller is already added to the machine.
I have the following code, that runs successfully, but it does not accomplish the job, and I cannot figure out why. The environment is vSphere 6, and the vRO is version 7.0
Best Regards
Brian
var runtime = vm.config
var hardware = runtime.hardware;
var devices = hardware.device;
var foundController = false;
var correctControllerType = false;
var disks = [];
var deviceConfigSpecs = new VcVirtualMachineConfigSpec();
deviceConfigSpecs.deviceChange = new Array();
System.log("deviceChange: " + deviceConfigSpecs.deviceChange);
//System.log("Number of Devices: " + devices.length);
for (var i in devices) {
if (((devices[i].deviceInfo.label).indexOf("Hard disk 1") == -1) && ((devices[i].deviceInfo.label).indexOf("Hard disk") > -1)) {
System.log(i + " " + "deviceInfo Label: " + i + " - " + devices[i].deviceInfo.label);
System.log(i + " " + "Backing: " + i + " - " + devices[i].backing);
System.log(i + " " + "ControllerKey: " + i + " - " + devices[i].controllerKey);
disks.push(devices[i]);
}
}
if (disks.length > 0) { // Multiple disks found
for (var i in devices) {
if ( devices[i].deviceInfo.label == "SCSI controller 1") {
var controllerType = devices[i].deviceInfo.summary;
if (controllerType.indexOf("VMware paravirtual SCSI") > -1) {
System.log("Found Correct Controller for Disks");
var deviceControllerKey = devices[i].key;
}
}
}
if (deviceControllerKey) {
var unit = 1;
for (var i in disks) {
System.log("Checking Controller Key: " + deviceControllerKey + " for Disk: " + disks[i].deviceInfo.label + " with key: " + disks[i].controllerKey);
if (true) { // (disk[i].controllerKey != deviceControllerKey) {
var deviceConfigSpec = new VcVirtualDeviceConfigSpec();
deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.edit;
deviceConfigSpec.device = new VcVirtualDisk;
deviceConfigSpec.device = disks[i];
deviceConfigSpec.device.controllerKey = deviceControllerKey;
deviceConfigSpec.device.unitNumber = unit++;
deviceConfigSpecs.deviceChange.push(deviceConfigSpec);
System.log("Settings Controller Key: " + deviceControllerKey + " for Disk: " + disks[i].deviceInfo.label + " - unit: " + unit);
}
}
task = vm.reconfigVM_Task( deviceConfigSpecs );
}
}
Hi,
Try the following code. It is almost verbatim copy of yours, with only differences on lines 11,12,52,53 and 57. What is changed it that the array property deviceChange of variable deviceConfigSpecs is not modified directly, but the spec values are accumulated in a separate array variable and at the end this array is assigned at once.
If I remember correctly there could be some issues with direct modification of properties of type Array.
var runtime = vm.config
var hardware = runtime.hardware;
var devices = hardware.device;
var foundController = false;
var correctControllerType = false;
var disks = [];
var deviceConfigSpecs = new VcVirtualMachineConfigSpec();
//deviceConfigSpecs.deviceChange = new Array();
var devChange = new Array();
System.log("deviceChange: " + deviceConfigSpecs.deviceChange);
//System.log("Number of Devices: " + devices.length);
for (var i in devices) {
if (((devices[i].deviceInfo.label).indexOf("Hard disk 1") == -1) && ((devices[i].deviceInfo.label).indexOf("Hard disk") > -1)) {
System.log(i + " " + "deviceInfo Label: " + i + " - " + devices[i].deviceInfo.label);
System.log(i + " " + "Backing: " + i + " - " + devices[i].backing);
System.log(i + " " + "ControllerKey: " + i + " - " + devices[i].controllerKey);
disks.push(devices[i]);
}
}
if (disks.length > 0) { // Multiple disks found
for (var i in devices) {
if ( devices[i].deviceInfo.label == "SCSI controller 1") {
var controllerType = devices[i].deviceInfo.summary;
if (controllerType.indexOf("VMware paravirtual SCSI") > -1) {
System.log("Found Correct Controller for Disks");
var deviceControllerKey = devices[i].key;
}
}
}
if (deviceControllerKey) {
var unit = 1;
for (var i in disks) {
System.log("Checking Controller Key: " + deviceControllerKey + " for Disk: " + disks[i].deviceInfo.label + " with key: " + disks[i].controllerKey);
if (true) { // (disk[i].controllerKey != deviceControllerKey) {
var deviceConfigSpec = new VcVirtualDeviceConfigSpec();
deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.edit;
deviceConfigSpec.device = new VcVirtualDisk;
deviceConfigSpec.device = disks[i];
deviceConfigSpec.device.controllerKey = deviceControllerKey;
deviceConfigSpec.device.unitNumber = unit++;
//deviceConfigSpecs.deviceChange.push(deviceConfigSpec);
devChange.push(deviceConfigSpec);
System.log("Settings Controller Key: " + deviceControllerKey + " for Disk: " + disks[i].deviceInfo.label + " - unit: " + unit);
}
}
deviceConfigSpecs.deviceChange = devChange;
task = vm.reconfigVM_Task( deviceConfigSpecs );
}
}
It cannot be this hard..?
Hi,
Try the following code. It is almost verbatim copy of yours, with only differences on lines 11,12,52,53 and 57. What is changed it that the array property deviceChange of variable deviceConfigSpecs is not modified directly, but the spec values are accumulated in a separate array variable and at the end this array is assigned at once.
If I remember correctly there could be some issues with direct modification of properties of type Array.
var runtime = vm.config
var hardware = runtime.hardware;
var devices = hardware.device;
var foundController = false;
var correctControllerType = false;
var disks = [];
var deviceConfigSpecs = new VcVirtualMachineConfigSpec();
//deviceConfigSpecs.deviceChange = new Array();
var devChange = new Array();
System.log("deviceChange: " + deviceConfigSpecs.deviceChange);
//System.log("Number of Devices: " + devices.length);
for (var i in devices) {
if (((devices[i].deviceInfo.label).indexOf("Hard disk 1") == -1) && ((devices[i].deviceInfo.label).indexOf("Hard disk") > -1)) {
System.log(i + " " + "deviceInfo Label: " + i + " - " + devices[i].deviceInfo.label);
System.log(i + " " + "Backing: " + i + " - " + devices[i].backing);
System.log(i + " " + "ControllerKey: " + i + " - " + devices[i].controllerKey);
disks.push(devices[i]);
}
}
if (disks.length > 0) { // Multiple disks found
for (var i in devices) {
if ( devices[i].deviceInfo.label == "SCSI controller 1") {
var controllerType = devices[i].deviceInfo.summary;
if (controllerType.indexOf("VMware paravirtual SCSI") > -1) {
System.log("Found Correct Controller for Disks");
var deviceControllerKey = devices[i].key;
}
}
}
if (deviceControllerKey) {
var unit = 1;
for (var i in disks) {
System.log("Checking Controller Key: " + deviceControllerKey + " for Disk: " + disks[i].deviceInfo.label + " with key: " + disks[i].controllerKey);
if (true) { // (disk[i].controllerKey != deviceControllerKey) {
var deviceConfigSpec = new VcVirtualDeviceConfigSpec();
deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.edit;
deviceConfigSpec.device = new VcVirtualDisk;
deviceConfigSpec.device = disks[i];
deviceConfigSpec.device.controllerKey = deviceControllerKey;
deviceConfigSpec.device.unitNumber = unit++;
//deviceConfigSpecs.deviceChange.push(deviceConfigSpec);
devChange.push(deviceConfigSpec);
System.log("Settings Controller Key: " + deviceControllerKey + " for Disk: " + disks[i].deviceInfo.label + " - unit: " + unit);
}
}
deviceConfigSpecs.deviceChange = devChange;
task = vm.reconfigVM_Task( deviceConfigSpecs );
}
}
Thank you. It worked. I did not not about that.
It is a good day when you learn something new.