|
27 | 27 | import java.util.Map; |
28 | 28 | import java.util.UUID; |
29 | 29 |
|
| 30 | +import com.cloud.agent.api.storage.CopyVolumeAnswer; |
| 31 | +import com.cloud.agent.api.storage.CopyVolumeCommand; |
| 32 | +import com.cloud.agent.api.to.*; |
30 | 33 | import org.apache.cloudstack.storage.command.AttachAnswer; |
31 | 34 | import org.apache.cloudstack.storage.command.AttachCommand; |
32 | 35 | import org.apache.cloudstack.storage.command.CopyCmdAnswer; |
|
51 | 54 | import com.cloud.agent.api.ManageSnapshotCommand; |
52 | 55 | import com.cloud.agent.api.storage.CreatePrivateTemplateAnswer; |
53 | 56 | import com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer; |
54 | | -import com.cloud.agent.api.to.DataStoreTO; |
55 | | -import com.cloud.agent.api.to.DataTO; |
56 | | -import com.cloud.agent.api.to.DiskTO; |
57 | | -import com.cloud.agent.api.to.NfsTO; |
58 | | -import com.cloud.agent.api.to.VolumeTO; |
59 | 57 | import com.cloud.hypervisor.vmware.manager.VmwareHostService; |
60 | 58 | import com.cloud.hypervisor.vmware.manager.VmwareStorageMount; |
61 | 59 | import com.cloud.hypervisor.vmware.mo.ClusterMO; |
@@ -421,18 +419,187 @@ public Answer cloneVolumeFromBaseTemplate(CopyCommand cmd) { |
421 | 419 | return new CopyCmdAnswer(e.toString()); |
422 | 420 | } |
423 | 421 | } |
424 | | - |
| 422 | + |
| 423 | + private Pair<String, String> copyVolumeFromSecStorage(VmwareHypervisorHost hyperHost, String srcVolumePath, |
| 424 | + DatastoreMO dsMo, String secStorageUrl) throws Exception { |
| 425 | + //srcVolumePath has volumes/dc/id/uuid |
| 426 | + int index = srcVolumePath.lastIndexOf(File.separator); |
| 427 | + String volumeFolder = srcVolumePath; |
| 428 | + String volumeName = srcVolumePath.substring(index + 1); |
| 429 | + |
| 430 | + String newVolume = UUID.randomUUID().toString().replaceAll("-", ""); |
| 431 | + restoreVolumeFromSecStorage(hyperHost, dsMo, newVolume, secStorageUrl, volumeFolder, volumeName); |
| 432 | + |
| 433 | + return new Pair<String, String>(volumeFolder, newVolume); |
| 434 | + } |
| 435 | + |
| 436 | + private String deleteVolumeDirOnSecondaryStorage(String volumeDir, String secStorageUrl) throws Exception { |
| 437 | + String secondaryMountPoint = mountService.getMountPoint(secStorageUrl); |
| 438 | + String volumeMountRoot = secondaryMountPoint + File.separator + volumeDir; |
| 439 | + |
| 440 | + return deleteDir(volumeMountRoot); |
| 441 | + } |
| 442 | + |
| 443 | + private String deleteDir(String dir) { |
| 444 | + synchronized(dir.intern()) { |
| 445 | + Script command = new Script(false, "rm", _timeout, s_logger); |
| 446 | + command.add("-rf"); |
| 447 | + command.add(dir); |
| 448 | + return command.execute(); |
| 449 | + } |
| 450 | + } |
425 | 451 |
|
426 | 452 | @Override |
427 | 453 | public Answer copyVolumeFromImageCacheToPrimary(CopyCommand cmd) { |
428 | | - // TODO Auto-generated method stub |
429 | | - return null; |
430 | | - } |
| 454 | + VolumeObjectTO srcVolume = (VolumeObjectTO)cmd.getSrcTO(); |
| 455 | + VolumeObjectTO destVolume = (VolumeObjectTO)cmd.getDestTO(); |
| 456 | + VmwareContext context = hostService.getServiceContext(cmd); |
| 457 | + try { |
| 458 | + |
| 459 | + NfsTO srcStore = (NfsTO)srcVolume.getDataStore(); |
| 460 | + PrimaryDataStoreTO destStore = (PrimaryDataStoreTO)destVolume.getDataStore(); |
| 461 | + |
| 462 | + VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd); |
| 463 | + String uuid = destStore.getUuid(); |
| 464 | + |
| 465 | + ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, uuid); |
| 466 | + if (morDatastore == null) { |
| 467 | + morDatastore = hyperHost.mountDatastore( |
| 468 | + false, |
| 469 | + destStore.getHost(), 0, destStore.getPath(), |
| 470 | + destStore.getUuid().replace("-", "")); |
| 471 | + |
| 472 | + if (morDatastore == null) { |
| 473 | + throw new Exception("Unable to mount storage pool on host. storeUrl: " + destStore.getHost() + ":/" + destStore.getPath()); |
| 474 | + } |
| 475 | + } |
| 476 | + |
| 477 | + Pair<String, String> result = copyVolumeFromSecStorage( |
| 478 | + hyperHost, srcVolume.getPath(), |
| 479 | + new DatastoreMO(context, morDatastore), |
| 480 | + srcStore.getUrl()); |
| 481 | + deleteVolumeDirOnSecondaryStorage(result.first(), srcStore.getUrl()); |
| 482 | + VolumeObjectTO newVolume = new VolumeObjectTO(); |
| 483 | + newVolume.setPath(result.second()); |
| 484 | + return new CopyCmdAnswer(newVolume); |
| 485 | + } catch (Throwable t) { |
| 486 | + if (t instanceof RemoteException) { |
| 487 | + hostService.invalidateServiceContext(context); |
| 488 | + } |
| 489 | + |
| 490 | + String msg = "Unable to execute CopyVolumeCommand due to exception"; |
| 491 | + s_logger.error(msg, t); |
| 492 | + return new CopyCmdAnswer("CopyVolumeCommand failed due to exception: " + t.toString()); |
| 493 | + } |
| 494 | + |
| 495 | + } |
| 496 | + |
| 497 | + private String getVolumePathInDatastore(DatastoreMO dsMo, String volumeFileName) throws Exception { |
| 498 | + String datastoreVolumePath = dsMo.searchFileInSubFolders(volumeFileName, true); |
| 499 | + assert (datastoreVolumePath != null) : "Virtual disk file missing from datastore."; |
| 500 | + return datastoreVolumePath; |
| 501 | + } |
| 502 | + |
| 503 | + private Pair<String, String> copyVolumeToSecStorage(VmwareHostService hostService, VmwareHypervisorHost hyperHost, CopyCommand cmd, |
| 504 | + String vmName, String poolId, String volumePath, String destVolumePath, |
| 505 | + String secStorageUrl, String workerVmName) throws Exception { |
| 506 | + VirtualMachineMO workerVm=null; |
| 507 | + VirtualMachineMO vmMo=null; |
| 508 | + String exportName = UUID.randomUUID().toString(); |
| 509 | + |
| 510 | + try { |
| 511 | + ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, poolId); |
| 512 | + |
| 513 | + if (morDs == null) { |
| 514 | + String msg = "Unable to find volumes's storage pool for copy volume operation"; |
| 515 | + s_logger.error(msg); |
| 516 | + throw new Exception(msg); |
| 517 | + } |
| 518 | + |
| 519 | + vmMo = hyperHost.findVmOnHyperHost(vmName); |
| 520 | + if (vmMo == null) { |
| 521 | + // create a dummy worker vm for attaching the volume |
| 522 | + DatastoreMO dsMo = new DatastoreMO(hyperHost.getContext(), morDs); |
| 523 | + //restrict VM name to 32 chars, (else snapshot descriptor file name will be truncated to 32 chars of vm name) |
| 524 | + VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec(); |
| 525 | + vmConfig.setName(workerVmName); |
| 526 | + vmConfig.setMemoryMB((long) 4); |
| 527 | + vmConfig.setNumCPUs(1); |
| 528 | + vmConfig.setGuestId(VirtualMachineGuestOsIdentifier.OTHER_GUEST.value()); |
| 529 | + VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo(); |
| 530 | + fileInfo.setVmPathName(String.format("[%s]", dsMo.getName())); |
| 531 | + vmConfig.setFiles(fileInfo); |
| 532 | + |
| 533 | + // Scsi controller |
| 534 | + VirtualLsiLogicController scsiController = new VirtualLsiLogicController(); |
| 535 | + scsiController.setSharedBus(VirtualSCSISharing.NO_SHARING); |
| 536 | + scsiController.setBusNumber(0); |
| 537 | + scsiController.setKey(1); |
| 538 | + VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec(); |
| 539 | + scsiControllerSpec.setDevice(scsiController); |
| 540 | + scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD); |
| 541 | + vmConfig.getDeviceChange().add(scsiControllerSpec); |
| 542 | + |
| 543 | + hyperHost.createVm(vmConfig); |
| 544 | + workerVm = hyperHost.findVmOnHyperHost(workerVmName); |
| 545 | + if (workerVm == null) { |
| 546 | + String msg = "Unable to create worker VM to execute CopyVolumeCommand"; |
| 547 | + s_logger.error(msg); |
| 548 | + throw new Exception(msg); |
| 549 | + } |
| 550 | + |
| 551 | + //attach volume to worker VM |
| 552 | + String datastoreVolumePath = getVolumePathInDatastore(dsMo, volumePath + ".vmdk"); |
| 553 | + workerVm.attachDisk(new String[] { datastoreVolumePath }, morDs); |
| 554 | + vmMo = workerVm; |
| 555 | + } |
| 556 | + |
| 557 | + vmMo.createSnapshot(exportName, "Temporary snapshot for copy-volume command", false, false); |
| 558 | + |
| 559 | + exportVolumeToSecondaryStroage(vmMo, volumePath, secStorageUrl, destVolumePath, exportName, |
| 560 | + hostService.getWorkerName(hyperHost.getContext(), cmd, 1)); |
| 561 | + return new Pair<String, String>(destVolumePath, exportName); |
| 562 | + |
| 563 | + } finally { |
| 564 | + vmMo.removeSnapshot(exportName, false); |
| 565 | + if (workerVm != null) { |
| 566 | + //detach volume and destroy worker vm |
| 567 | + workerVm.detachAllDisks(); |
| 568 | + workerVm.destroy(); |
| 569 | + } |
| 570 | + } |
| 571 | + } |
431 | 572 |
|
432 | 573 | @Override |
433 | 574 | public Answer copyVolumeFromPrimaryToSecondary(CopyCommand cmd) { |
434 | | - // TODO Auto-generated method stub |
435 | | - return null; |
| 575 | + VolumeObjectTO srcVolume = (VolumeObjectTO)cmd.getSrcTO(); |
| 576 | + VolumeObjectTO destVolume = (VolumeObjectTO)cmd.getDestTO(); |
| 577 | + String vmName = srcVolume.getVmName(); |
| 578 | + |
| 579 | + VmwareContext context = hostService.getServiceContext(cmd); |
| 580 | + try { |
| 581 | + PrimaryDataStoreTO primaryStorage = (PrimaryDataStoreTO)srcVolume.getDataStore(); |
| 582 | + NfsTO destStore = (NfsTO)destVolume.getDataStore(); |
| 583 | + VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd); |
| 584 | + |
| 585 | + Pair<String, String> result; |
| 586 | + |
| 587 | + result = copyVolumeToSecStorage(hostService, |
| 588 | + hyperHost, cmd, vmName, primaryStorage.getUuid(), srcVolume.getPath(),destVolume.getPath(), |
| 589 | + destStore.getUrl(), |
| 590 | + hostService.getWorkerName(context, cmd, 0)); |
| 591 | + VolumeObjectTO newVolume = new VolumeObjectTO(); |
| 592 | + newVolume.setPath(result.first() + File.separator + result.second()); |
| 593 | + return new CopyCmdAnswer(newVolume); |
| 594 | + } catch (Throwable e) { |
| 595 | + if (e instanceof RemoteException) { |
| 596 | + hostService.invalidateServiceContext(context); |
| 597 | + } |
| 598 | + |
| 599 | + String msg = "Unable to execute CopyVolumeCommand due to exception"; |
| 600 | + s_logger.error(msg, e); |
| 601 | + return new CopyCmdAnswer("CopyVolumeCommand failed due to exception: " + e.toString()); |
| 602 | + } |
436 | 603 | } |
437 | 604 |
|
438 | 605 | private void postCreatePrivateTemplate(String installFullPath, long templateId, |
|
0 commit comments