REQUESTED

detachISO from a running virtualmachine

Overview

trying to detachiso from running vm using java sdk but not able to.
Able to get runtimeinfo object and able get runtime power state and config details.
But when question object is returning null.Able to see question window from vsphere client but not from sdk

Here is the sample code for reference ;

String labelName="CD/DVD Drive 1";
String dsName = "Local_ex05";
List<VirtualDevice> deviceArr = ((ArrayOfVirtualDevice)entityProps(vimPort, serviceContent,clonevmMor,new String[]{"config.hardware.device"}).get("config.hardware.device")).getVirtualDevice();

VirtualDevice cdRom = null;
for (VirtualDevice device : deviceArr) {
if (device instanceof VirtualCdrom) {
Description info = device.getDeviceInfo();
if (info != null) {
if (info.getLabel().equalsIgnoreCase(labelName)) {
cdRom = device;
break;
}
}
}
}

VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);

VirtualCdromRemotePassthroughBackingInfo backingInfo = new VirtualCdromRemotePassthroughBackingInfo();
backingInfo.setDeviceName("");

cdRom.setBacking(backingInfo);
deviceConfigSpec.setDevice(cdRom);

List<VirtualDeviceConfigSpec> deviceConfigSpecArr = new ArrayList<VirtualDeviceConfigSpec>();
deviceConfigSpecArr.add(deviceConfigSpec);

VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec();
configSpec.getDeviceChange().addAll(deviceConfigSpecArr);
ManagedObjectReference editcdtask = vimPort.reconfigVMTask(clonevmMor, configSpec);

Map<String,Object> vmsumArr = entityProps(vimPort, serviceContent,clonevmMor,new String[]{"summary"});
final VirtualMachineSummary vmsum = (VirtualMachineSummary)vmsumArr.get("summary");
System.out.println("power state : "+ vmsum.getRuntime().getPowerState());
Thread.sleep(10000);
System.out.println("vm Name :"+vmsum.getConfig().getName());
final Boolean[] flags = {false};

ExecutorService executorService = Executors.newSingleThreadExecutor();

Future<?> future = executorService.submit(new Runnable() {
@Override
public void run() {
System.out.println("inside run");
VirtualMachineRuntimeInfo runtimeInfo = vmsum.getRuntime();
VirtualMachineQuestionInfo question = runtimeInfo.getQuestion();
if (question != null) {
if (question.getMessage() != null) {
for (VirtualMachineMessage msg : question.getMessage()) {
if ("msg.cdromdisconnect.locked".equalsIgnoreCase(msg.getId())) {
System.out.println("Found that VM has a pending question that we need to answer programmatically, question id: " + msg.getId() +
", for safe operation we will automatically decline it");
// vmMo.answerVM(question.getId(), "1");
break;
}
}
} else if (question.getText() != null) {
String text = question.getText();
System.out.println("Message : "+question.getText());
String msgId;
String msgText;
String[] tokens = text.split(":");
msgId = tokens[0];
msgText = tokens[1];
if ("msg.cdromdisconnect.locked".equalsIgnoreCase(msgId)) {
System.out.println("Found that VM has a pending question that we need to answer programmatically, question id: " + question.getId() +
". Message id : " + msgId + ". Message text : " + msgText + ", for safe operation we will automatically decline it.");
// vmMo.answerVM(question.getId(), "1");
}
}

}

}
});

future.get();

Sign in to be able to add comments.

Comments 2


doskiran 2 years ago
Actually it’s a VI client popup window (Remote device disconnect) to disconnect ISO image on a local disk and also it is completely blocks the VI client until answer (click OK manually). As for me this popup window is not same as ” VirtualMachineQuestionInfo" because vmQuestionInfo always comes in the VM summary page, this we can proven by open one more sessions for the same server using VI client this popup will not be exist and also detachISO, but vmQuestionInfo in the VM summary page should always exist in any new VI client session . (https://drive.google.com/file/d/0B1XPsxC5cz4qR0Z5dzduNkk4XzQ/view).

Usually for VM runtime questions use some timer to get the result. Eg:,

for (int i = 0; i < 60000); i++) {
if (vm.getRuntime().getQuestion() != null) {
message = true;
break;
} else {
Thread.sleep(1000);
}
}
doskiran 2 years ago
I think it needs some VI client sdk to handle this client popup window.

Usually its best practice to wait for some specified time until VM summary message pops. Eg:,
for (int i = 0; i < 10; i++) {
if (vm.getRuntime().getQuestion() != null) {
message = true;
break;
} else {
Thread.sleep(10*1000);
}
}
;