|
44 | 44 | import com.cloud.agent.api.Answer; |
45 | 45 | import com.cloud.agent.api.BackupSnapshotAnswer; |
46 | 46 | import com.cloud.agent.api.Command; |
| 47 | +import com.cloud.agent.api.CreateVolumeFromSnapshotAnswer; |
47 | 48 | import com.cloud.agent.api.ManageSnapshotAnswer; |
48 | 49 | import com.cloud.agent.api.ManageSnapshotCommand; |
49 | 50 | import com.cloud.agent.api.storage.CreatePrivateTemplateAnswer; |
@@ -980,6 +981,7 @@ public Answer createVolume(CreateObjectCommand cmd) { |
980 | 981 |
|
981 | 982 | VolumeObjectTO newVol = new VolumeObjectTO(); |
982 | 983 | newVol.setPath(volumeUuid); |
| 984 | + newVol.setSize(volume.getSize() / (1024L * 1024L)); |
983 | 985 | return new CreateObjectAnswer(newVol); |
984 | 986 | } finally { |
985 | 987 | s_logger.info("Destroy dummy VM after volume creation"); |
@@ -1154,4 +1156,120 @@ public Answer deleteVolume(DeleteCommand cmd) { |
1154 | 1156 | return new Answer(cmd, false, msg); |
1155 | 1157 | } |
1156 | 1158 | } |
| 1159 | + |
| 1160 | + private Long restoreVolumeFromSecStorage(VmwareHypervisorHost hyperHost, DatastoreMO primaryDsMo, String newVolumeName, |
| 1161 | + String secStorageUrl, String secStorageDir, String backupName) throws Exception { |
| 1162 | + |
| 1163 | + String secondaryMountPoint = mountService.getMountPoint(secStorageUrl); |
| 1164 | + String srcOVAFileName = secondaryMountPoint + "/" + secStorageDir + "/" |
| 1165 | + + backupName + "." + ImageFormat.OVA.getFileExtension(); |
| 1166 | + String snapshotDir = ""; |
| 1167 | + if (backupName.contains("/")){ |
| 1168 | + snapshotDir = backupName.split("/")[0]; |
| 1169 | + } |
| 1170 | + |
| 1171 | + File ovafile = new File(srcOVAFileName); |
| 1172 | + String srcOVFFileName = secondaryMountPoint + "/" + secStorageDir + "/" |
| 1173 | + + backupName + ".ovf"; |
| 1174 | + File ovfFile = new File(srcOVFFileName); |
| 1175 | + // String srcFileName = getOVFFilePath(srcOVAFileName); |
| 1176 | + if (!ovfFile.exists()) { |
| 1177 | + srcOVFFileName = getOVFFilePath(srcOVAFileName); |
| 1178 | + if(srcOVFFileName == null && ovafile.exists() ) { // volss: ova file exists; o/w can't do tar |
| 1179 | + Script command = new Script("tar", 0, s_logger); |
| 1180 | + command.add("--no-same-owner"); |
| 1181 | + command.add("-xf", srcOVAFileName); |
| 1182 | + command.setWorkDir(secondaryMountPoint + "/" + secStorageDir + "/" + snapshotDir); |
| 1183 | + s_logger.info("Executing command: " + command.toString()); |
| 1184 | + String result = command.execute(); |
| 1185 | + if(result != null) { |
| 1186 | + String msg = "Unable to unpack snapshot OVA file at: " + srcOVAFileName; |
| 1187 | + s_logger.error(msg); |
| 1188 | + throw new Exception(msg); |
| 1189 | + } |
| 1190 | + } else { |
| 1191 | + String msg = "Unable to find snapshot OVA file at: " + srcOVAFileName; |
| 1192 | + s_logger.error(msg); |
| 1193 | + throw new Exception(msg); |
| 1194 | + } |
| 1195 | + |
| 1196 | + srcOVFFileName = getOVFFilePath(srcOVAFileName); |
| 1197 | + } |
| 1198 | + if(srcOVFFileName == null) { |
| 1199 | + String msg = "Unable to locate OVF file in template package directory: " + srcOVAFileName; |
| 1200 | + s_logger.error(msg); |
| 1201 | + throw new Exception(msg); |
| 1202 | + } |
| 1203 | + |
| 1204 | + VirtualMachineMO clonedVm = null; |
| 1205 | + try { |
| 1206 | + hyperHost.importVmFromOVF(srcOVFFileName, newVolumeName, primaryDsMo, "thin"); |
| 1207 | + clonedVm = hyperHost.findVmOnHyperHost(newVolumeName); |
| 1208 | + if(clonedVm == null) |
| 1209 | + throw new Exception("Unable to create container VM for volume creation"); |
| 1210 | + |
| 1211 | + clonedVm.moveAllVmDiskFiles(primaryDsMo, "", false); |
| 1212 | + clonedVm.detachAllDisks(); |
| 1213 | + return _storage.getSize(srcOVFFileName); |
| 1214 | + } finally { |
| 1215 | + if(clonedVm != null) { |
| 1216 | + clonedVm.detachAllDisks(); |
| 1217 | + clonedVm.destroy(); |
| 1218 | + } |
| 1219 | + } |
| 1220 | + } |
| 1221 | + |
| 1222 | + @Override |
| 1223 | + public Answer createVolumeFromSnapshot(CopyCommand cmd) { |
| 1224 | + DataTO srcData = cmd.getSrcTO(); |
| 1225 | + SnapshotObjectTO snapshot = (SnapshotObjectTO)srcData; |
| 1226 | + DataTO destData = cmd.getDestTO(); |
| 1227 | + PrimaryDataStoreTO pool = (PrimaryDataStoreTO)destData.getDataStore(); |
| 1228 | + DataStoreTO imageStore = srcData.getDataStore(); |
| 1229 | + |
| 1230 | + |
| 1231 | + if (!(imageStore instanceof NfsTO)) { |
| 1232 | + return new CopyCmdAnswer("unsupported protocol"); |
| 1233 | + } |
| 1234 | + |
| 1235 | + NfsTO nfsImageStore = (NfsTO)imageStore; |
| 1236 | + String primaryStorageNameLabel = pool.getUuid(); |
| 1237 | + |
| 1238 | + String secondaryStorageUrl = nfsImageStore.getUrl(); |
| 1239 | + String backedUpSnapshotUuid = snapshot.getPath(); |
| 1240 | + int index = backedUpSnapshotUuid.lastIndexOf(File.separator); |
| 1241 | + String backupPath = backedUpSnapshotUuid.substring(0, index); |
| 1242 | + backedUpSnapshotUuid = backedUpSnapshotUuid.substring(index + 1); |
| 1243 | + String details = null; |
| 1244 | + String newVolumeName = UUID.randomUUID().toString().replaceAll("-", ""); |
| 1245 | + |
| 1246 | + VmwareContext context = hostService.getServiceContext(cmd); |
| 1247 | + try { |
| 1248 | + VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd); |
| 1249 | + ManagedObjectReference morPrimaryDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, |
| 1250 | + primaryStorageNameLabel); |
| 1251 | + if (morPrimaryDs == null) { |
| 1252 | + String msg = "Unable to find datastore: " + primaryStorageNameLabel; |
| 1253 | + s_logger.error(msg); |
| 1254 | + throw new Exception(msg); |
| 1255 | + } |
| 1256 | + |
| 1257 | + DatastoreMO primaryDsMo = new DatastoreMO(hyperHost.getContext(), morPrimaryDs); |
| 1258 | + Long size = restoreVolumeFromSecStorage(hyperHost, primaryDsMo, |
| 1259 | + newVolumeName, secondaryStorageUrl, backupPath, backedUpSnapshotUuid); |
| 1260 | + |
| 1261 | + VolumeObjectTO newVol = new VolumeObjectTO(); |
| 1262 | + newVol.setPath(newVolumeName); |
| 1263 | + newVol.setSize(size); |
| 1264 | + return new CopyCmdAnswer(newVol); |
| 1265 | + } catch (Throwable e) { |
| 1266 | + if (e instanceof RemoteException) { |
| 1267 | + hostService.invalidateServiceContext(context); |
| 1268 | + } |
| 1269 | + |
| 1270 | + s_logger.error("Unexpecpted exception ", e); |
| 1271 | + details = "CreateVolumeFromSnapshotCommand exception: " + StringUtils.getExceptionStackInfo(e); |
| 1272 | + } |
| 1273 | + return new CopyCmdAnswer(details); |
| 1274 | + } |
1157 | 1275 | } |
0 commit comments