|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.cloudstack.storage.motion; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import javax.inject.Inject; |
| 26 | + |
| 27 | +import org.apache.cloudstack.engine.subsystem.api.storage.DataStore; |
| 28 | +import org.apache.cloudstack.engine.subsystem.api.storage.StrategyPriority; |
| 29 | +import org.apache.cloudstack.engine.subsystem.api.storage.TemplateDataFactory; |
| 30 | +import org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo; |
| 31 | +import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo; |
| 32 | +import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; |
| 33 | + |
| 34 | +import com.cloud.agent.api.Answer; |
| 35 | +import com.cloud.agent.api.MigrateCommand; |
| 36 | +import com.cloud.agent.api.MigrateCommand.MigrateDiskInfo; |
| 37 | +import com.cloud.agent.api.storage.CreateAnswer; |
| 38 | +import com.cloud.agent.api.storage.CreateCommand; |
| 39 | +import com.cloud.agent.api.to.VirtualMachineTO; |
| 40 | +import com.cloud.host.Host; |
| 41 | +import com.cloud.hypervisor.Hypervisor.HypervisorType; |
| 42 | +import com.cloud.storage.DataStoreRole; |
| 43 | +import com.cloud.storage.DiskOfferingVO; |
| 44 | +import com.cloud.storage.Storage.StoragePoolType; |
| 45 | +import com.cloud.storage.VolumeVO; |
| 46 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 47 | +import com.cloud.vm.DiskProfile; |
| 48 | + |
| 49 | +/** |
| 50 | + * Extends {@link StorageSystemDataMotionStrategy}, allowing KVM hosts to migrate VMs with the ROOT volume on a non managed local storage pool. |
| 51 | + * As {@link StorageSystemDataMotionStrategy} is considering KVM, this implementation also migrates only from/to KVM hosts. |
| 52 | + */ |
| 53 | +public class KvmNonManagedStorageDataMotionStrategy extends StorageSystemDataMotionStrategy { |
| 54 | + |
| 55 | + @Inject |
| 56 | + private TemplateDataFactory templateDataFactory; |
| 57 | + |
| 58 | + /** |
| 59 | + * Uses the canHandle from the Super class {@link StorageSystemDataMotionStrategy}. If the storage pool is of file and the internalCanHandle from {@link StorageSystemDataMotionStrategy} CANT_HANDLE, returns the StrategyPriority.HYPERVISOR strategy priority. otherwise returns CANT_HANDLE. |
| 60 | + * Note that the super implementation (override) is called by {@link #canHandle(Map, Host, Host)} which ensures that {@link #internalCanHandle(Map)} will be executed only if the source host is KVM. |
| 61 | + */ |
| 62 | + @Override |
| 63 | + protected StrategyPriority internalCanHandle(Map<VolumeInfo, DataStore> volumeMap) { |
| 64 | + if (super.internalCanHandle(volumeMap) == StrategyPriority.CANT_HANDLE) { |
| 65 | + Set<VolumeInfo> volumeInfoSet = volumeMap.keySet(); |
| 66 | + |
| 67 | + for (VolumeInfo volumeInfo : volumeInfoSet) { |
| 68 | + StoragePoolVO storagePoolVO = _storagePoolDao.findById(volumeInfo.getPoolId()); |
| 69 | + if (storagePoolVO.getPoolType() != StoragePoolType.Filesystem && storagePoolVO.getPoolType() != StoragePoolType.NetworkFilesystem) { |
| 70 | + return StrategyPriority.CANT_HANDLE; |
| 71 | + } |
| 72 | + } |
| 73 | + return StrategyPriority.HYPERVISOR; |
| 74 | + } |
| 75 | + return StrategyPriority.CANT_HANDLE; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Configures a {@link MigrateDiskInfo} object configured for migrating a File System volume and calls rootImageProvisioning. |
| 80 | + */ |
| 81 | + @Override |
| 82 | + protected MigrateCommand.MigrateDiskInfo configureMigrateDiskInfo(VolumeInfo srcVolumeInfo, String destPath) { |
| 83 | + return new MigrateCommand.MigrateDiskInfo(srcVolumeInfo.getPath(), MigrateCommand.MigrateDiskInfo.DiskType.FILE, MigrateCommand.MigrateDiskInfo.DriverType.QCOW2, |
| 84 | + MigrateCommand.MigrateDiskInfo.Source.FILE, destPath); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Generates the volume path by appending the Volume UUID to the Libvirt destiny images path.</br> |
| 89 | + * Example: /var/lib/libvirt/images/f3d49ecc-870c-475a-89fa-fd0124420a9b |
| 90 | + */ |
| 91 | + @Override |
| 92 | + protected String generateDestPath(VirtualMachineTO vmTO, VolumeVO srcVolume, Host destHost, StoragePoolVO destStoragePool, VolumeInfo destVolumeInfo) { |
| 93 | + DiskOfferingVO diskOffering = _diskOfferingDao.findById(srcVolume.getDiskOfferingId()); |
| 94 | + DiskProfile diskProfile = new DiskProfile(destVolumeInfo, diskOffering, HypervisorType.KVM); |
| 95 | + String templateUuid = getTemplateUuid(destVolumeInfo.getTemplateId()); |
| 96 | + CreateCommand rootImageProvisioningCommand = new CreateCommand(diskProfile, templateUuid, destStoragePool, true); |
| 97 | + |
| 98 | + Answer rootImageProvisioningAnswer = _agentMgr.easySend(destHost.getId(), rootImageProvisioningCommand); |
| 99 | + |
| 100 | + if (rootImageProvisioningAnswer == null) { |
| 101 | + throw new CloudRuntimeException(String.format("Migration with storage of vm [%s] failed while provisioning root image", vmTO.getName())); |
| 102 | + } |
| 103 | + |
| 104 | + if (!rootImageProvisioningAnswer.getResult()) { |
| 105 | + throw new CloudRuntimeException(String.format("Unable to modify target volume on the host [host id:%s, name:%s]", destHost.getId(), destHost.getName())); |
| 106 | + } |
| 107 | + |
| 108 | + String libvirtDestImgsPath = null; |
| 109 | + if (rootImageProvisioningAnswer instanceof CreateAnswer) { |
| 110 | + libvirtDestImgsPath = ((CreateAnswer)rootImageProvisioningAnswer).getVolume().getName(); |
| 111 | + } |
| 112 | + // File.getAbsolutePath is used to keep the file separator as it should be and eliminate a verification to check if exists a file separator in the last character of libvirtDestImgsPath. |
| 113 | + return new File(libvirtDestImgsPath, destVolumeInfo.getUuid()).getAbsolutePath(); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Returns the template UUID with the given id. If the template ID is null, it returns null. |
| 118 | + */ |
| 119 | + protected String getTemplateUuid(Long templateId) { |
| 120 | + if (templateId == null) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + TemplateInfo templateImage = templateDataFactory.getTemplate(templateId, DataStoreRole.Image); |
| 124 | + return templateImage.getUuid(); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Sets the volume path as the volume UUID. |
| 129 | + */ |
| 130 | + @Override |
| 131 | + protected void setVolumePath(VolumeVO volume) { |
| 132 | + volume.setPath(volume.getUuid()); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Return true if the volume should be migrated. Currently only supports migrating volumes on storage pool of the type StoragePoolType.Filesystem. |
| 137 | + * This ensures that volumes on shared storage are not migrated and those on local storage pools are migrated. |
| 138 | + */ |
| 139 | + @Override |
| 140 | + protected boolean shouldMigrateVolume(StoragePoolVO sourceStoragePool, Host destHost, StoragePoolVO destStoragePool) { |
| 141 | + return sourceStoragePool.getPoolType() == StoragePoolType.Filesystem; |
| 142 | + } |
| 143 | +} |
0 commit comments