}
// if the VM is not running and we're not dealing with managed storage, just return success (nothing to do here)
// this should probably never actually happen
if (vmNotRunning && !cmd.isManaged()) {
return new DettachAnswer(disk);
}
if (!vmNotRunning) {
/* For HVM guest, if no pv driver installed, no attach/detach */
boolean isHVM = vm.getPVBootloader(conn).equalsIgnoreCase("");
VMGuestMetrics vgm = vm.getGuestMetrics(conn);
boolean pvDrvInstalled = false;
if (!this.hypervisorResource.isRefNull(vgm) && vgm.getPVDriversUpToDate(conn)) {
pvDrvInstalled = true;
}
if (isHVM && !pvDrvInstalled) {
s_logger.warn(": You attempted an operation on a VM which requires PV drivers to be installed but the drivers were not detected");
return new DettachAnswer("You attempted an operation that requires PV drivers to be installed on the VM. Please install them by inserting xen-pv-drv.iso.");
}
VDI vdi = this.hypervisorResource.mount(conn, null, null, data.getPath());
// Look up all VBDs for this VDI
Set<VBD> vbds = vdi.getVBDs(conn);
// Detach each VBD from its VM, and then destroy it
for (VBD vbd : vbds) {
VBD.Record vbdr = vbd.getRecord(conn);
if (vbdr.currentlyAttached) {
vbd.unplug(conn);
}
vbd.destroy(conn);
}
// Update the VDI's label to be "detached"
vdi.setNameLabel(conn, "detached");
this.hypervisorResource.umount(conn, vdi);
}
if (cmd.isManaged()) {
hypervisorResource.handleSrAndVdiDetach(cmd.get_iScsiName(), conn);
}
return new DettachAnswer(disk);
} catch (Exception e) {
s_logger.warn("Failed dettach volume: " + data.getPath());
return new DettachAnswer("Failed dettach volume: " + data.getPath() + ", due to " + e.toString());
}
}