protected Answer execute(AttachCommand cmd) { DiskTO disk = cmd.getDisk(); if (disk.getType() == Volume.Type.ISO) { return processor.attachIso(cmd); } else { return processor.attachVolume(cmd); } }
volumeToAttach.getDiskOfferingId()); AttachCommand cmd = new AttachCommand(disk, vm.getInstanceName()); controllerInfo.put(VmDetailConstants.ROOT_DISK_CONTROLLER, vm.getDetail(VmDetailConstants.ROOT_DISK_CONTROLLER)); controllerInfo.put(VmDetailConstants.DATA_DISK_CONTROLLER, vm.getDetail(VmDetailConstants.DATA_DISK_CONTROLLER)); cmd.setControllerInfo(controllerInfo); s_logger.debug("Attach volume id:" + volumeToAttach.getId() + " on VM id:" + vm.getId() + " has controller info:" + controllerInfo);
private boolean attachISOToVM(long vmId, long isoId, boolean attach) { UserVmVO vm = _userVmDao.findById(vmId); if (vm == null) { return false; } else if (vm.getState() != State.Running) { return true; } // prepare ISO ready to mount on hypervisor resource level TemplateInfo tmplt = prepareIso(isoId, vm.getDataCenterId(), vm.getHostId(), null); if (tmplt == null) { s_logger.error("Failed to prepare ISO ready to mount on hypervisor resource level"); throw new CloudRuntimeException("Failed to prepare ISO ready to mount on hypervisor resource level"); } String vmName = vm.getInstanceName(); HostVO host = _hostDao.findById(vm.getHostId()); if (host == null) { s_logger.warn("Host: " + vm.getHostId() + " does not exist"); return false; } DataTO isoTO = tmplt.getTO(); DiskTO disk = new DiskTO(isoTO, null, null, Volume.Type.ISO); Command cmd = null; if (attach) { cmd = new AttachCommand(disk, vmName); } else { cmd = new DettachCommand(disk, vmName); } Answer a = _agentMgr.easySend(vm.getHostId(), cmd); return (a != null && a.getResult()); }
@Override public Answer attachIso(final AttachCommand cmd) { final DiskTO disk = cmd.getDisk(); final TemplateObjectTO isoTO = (TemplateObjectTO)disk.getData(); final DataStoreTO store = isoTO.getDataStore(); try { String dataStoreUrl = getDataStoreUrlFromStore(store); final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName()); attachOrDetachISO(conn, cmd.getVmName(), dataStoreUrl + File.separator + isoTO.getPath(), true); } catch (final LibvirtException e) { return new Answer(cmd, false, e.toString()); } catch (final URISyntaxException e) { return new Answer(cmd, false, e.toString()); } catch (final InternalErrorException e) { return new Answer(cmd, false, e.toString()); } catch (final InvalidParameterValueException e) { return new Answer(cmd, false, e.toString()); } return new Answer(cmd); }
@Override public Answer attachVolume(final AttachCommand cmd) { final DiskTO disk = cmd.getDisk(); final VolumeObjectTO vol = (VolumeObjectTO)disk.getData(); final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)vol.getDataStore(); final String vmName = cmd.getVmName(); final String serial = resource.diskUuidToSerial(vol.getUuid()); try { final Connect conn = LibvirtConnection.getConnectionByVmName(vmName); storagePoolMgr.connectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath(), disk.getDetails()); final KVMPhysicalDisk phyDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath()); attachOrDetachDisk(conn, true, vmName, phyDisk, disk.getDiskSeq().intValue(), serial, vol.getBytesReadRate(), vol.getBytesWriteRate(), vol.getIopsReadRate(), vol.getIopsWriteRate()); return new AttachAnswer(disk); } catch (final LibvirtException e) { s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e); storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath()); return new AttachAnswer(e.toString()); } catch (final InternalErrorException e) { s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e); return new AttachAnswer(e.toString()); } }