diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java index 3b806e9ef02..201659d3e87 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java @@ -37,20 +37,6 @@ import java.util.UUID; import javax.naming.ConfigurationException; -import com.cloud.hypervisor.Hypervisor; -import org.apache.commons.io.FileUtils; -import org.apache.log4j.Logger; -import org.libvirt.Connect; -import org.libvirt.Domain; -import org.libvirt.DomainInfo; -import org.libvirt.DomainSnapshot; -import org.libvirt.LibvirtException; - -import com.ceph.rados.IoCTX; -import com.ceph.rados.Rados; -import com.ceph.rbd.Rbd; -import com.ceph.rbd.RbdImage; - import org.apache.cloudstack.storage.command.AttachAnswer; import org.apache.cloudstack.storage.command.AttachCommand; import org.apache.cloudstack.storage.command.CopyCmdAnswer; @@ -72,7 +58,18 @@ import org.apache.cloudstack.utils.qemu.QemuImg; import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat; import org.apache.cloudstack.utils.qemu.QemuImgException; import org.apache.cloudstack.utils.qemu.QemuImgFile; +import org.apache.commons.io.FileUtils; +import org.apache.log4j.Logger; +import org.libvirt.Connect; +import org.libvirt.Domain; +import org.libvirt.DomainInfo; +import org.libvirt.DomainSnapshot; +import org.libvirt.LibvirtException; +import com.ceph.rados.IoCTX; +import com.ceph.rados.Rados; +import com.ceph.rbd.Rbd; +import com.ceph.rbd.RbdImage; import com.cloud.agent.api.Answer; import com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer; import com.cloud.agent.api.to.DataObjectType; @@ -82,6 +79,7 @@ import com.cloud.agent.api.to.DiskTO; import com.cloud.agent.api.to.NfsTO; import com.cloud.agent.api.to.S3TO; import com.cloud.exception.InternalErrorException; +import com.cloud.hypervisor.Hypervisor; import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; import com.cloud.hypervisor.kvm.resource.LibvirtConnection; import com.cloud.hypervisor.kvm.resource.LibvirtDomainXMLParser; @@ -110,7 +108,7 @@ public class KVMStorageProcessor implements StorageProcessor { private String _manageSnapshotPath; private int _cmdsTimeout; - public KVMStorageProcessor(KVMStoragePoolManager storagePoolMgr, LibvirtComputingResource resource) { + public KVMStorageProcessor(final KVMStoragePoolManager storagePoolMgr, final LibvirtComputingResource resource) { this.storagePoolMgr = storagePoolMgr; this.resource = resource; } @@ -119,7 +117,7 @@ public class KVMStorageProcessor implements StorageProcessor { return "scripts/storage/qcow2"; } - public boolean configure(String name, Map params) throws ConfigurationException { + public boolean configure(final String name, final Map params) throws ConfigurationException { storageLayer = new JavaStorageLayer(); storageLayer.configure("StorageLayer", params); @@ -138,34 +136,34 @@ public class KVMStorageProcessor implements StorageProcessor { throw new ConfigurationException("Unable to find the managesnapshot.sh"); } - String value = (String)params.get("cmds.timeout"); + final String value = (String)params.get("cmds.timeout"); _cmdsTimeout = NumbersUtil.parseInt(value, 7200) * 1000; return true; } @Override - public SnapshotAndCopyAnswer snapshotAndCopy(SnapshotAndCopyCommand cmd) { + public SnapshotAndCopyAnswer snapshotAndCopy(final SnapshotAndCopyCommand cmd) { s_logger.info("'SnapshotAndCopyAnswer snapshotAndCopy(SnapshotAndCopyCommand)' not currently used for KVMStorageProcessor"); return new SnapshotAndCopyAnswer(); } @Override - public Answer copyTemplateToPrimaryStorage(CopyCommand cmd) { - DataTO srcData = cmd.getSrcTO(); - DataTO destData = cmd.getDestTO(); - TemplateObjectTO template = (TemplateObjectTO)srcData; - DataStoreTO imageStore = template.getDataStore(); - PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)destData.getDataStore(); + public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) { + final DataTO srcData = cmd.getSrcTO(); + final DataTO destData = cmd.getDestTO(); + final TemplateObjectTO template = (TemplateObjectTO)srcData; + final DataStoreTO imageStore = template.getDataStore(); + final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)destData.getDataStore(); if (!(imageStore instanceof NfsTO)) { return new CopyCmdAnswer("unsupported protocol"); } - NfsTO nfsImageStore = (NfsTO)imageStore; - String tmplturl = nfsImageStore.getUrl() + File.separator + template.getPath(); - int index = tmplturl.lastIndexOf("/"); - String mountpoint = tmplturl.substring(0, index); + final NfsTO nfsImageStore = (NfsTO)imageStore; + final String tmplturl = nfsImageStore.getUrl() + File.separator + template.getPath(); + final int index = tmplturl.lastIndexOf("/"); + final String mountpoint = tmplturl.substring(0, index); String tmpltname = null; if (index < tmplturl.length() - 1) { tmpltname = tmplturl.substring(index + 1); @@ -179,11 +177,11 @@ public class KVMStorageProcessor implements StorageProcessor { /* Get template vol */ if (tmpltname == null) { secondaryPool.refresh(); - List disks = secondaryPool.listPhysicalDisks(); + final List disks = secondaryPool.listPhysicalDisks(); if (disks == null || disks.isEmpty()) { return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: " + secondaryPool.getUuid()); } - for (KVMPhysicalDisk disk : disks) { + for (final KVMPhysicalDisk disk : disks) { if (disk.getName().endsWith("qcow2")) { tmplVol = disk; break; @@ -199,11 +197,11 @@ public class KVMStorageProcessor implements StorageProcessor { /* Copy volume to primary storage */ s_logger.debug("Copying template to primary storage, template format is " + tmplVol.getFormat() ); - KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid()); + final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid()); KVMPhysicalDisk primaryVol = null; if (destData instanceof VolumeObjectTO) { - VolumeObjectTO volume = (VolumeObjectTO)destData; + final VolumeObjectTO volume = (VolumeObjectTO)destData; // pass along volume's target size if it's bigger than template's size, for storage types that copy template rather than cloning on deploy if (volume.getSize() != null && volume.getSize() > tmplVol.getVirtualSize()) { s_logger.debug("Using configured size of " + volume.getSize()); @@ -214,7 +212,7 @@ public class KVMStorageProcessor implements StorageProcessor { } primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, volume.getUuid(), primaryPool, cmd.getWaitInMillSeconds()); } else if (destData instanceof TemplateObjectTO) { - TemplateObjectTO destTempl = (TemplateObjectTO)destData; + final TemplateObjectTO destTempl = (TemplateObjectTO)destData; primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, destTempl.getUuid(), primaryPool, cmd.getWaitInMillSeconds()); } else { primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, cmd.getWaitInMillSeconds()); @@ -226,7 +224,7 @@ public class KVMStorageProcessor implements StorageProcessor { * */ if (destData.getObjectType() == DataObjectType.TEMPLATE) { - TemplateObjectTO newTemplate = new TemplateObjectTO(); + final TemplateObjectTO newTemplate = new TemplateObjectTO(); newTemplate.setPath(primaryVol.getName()); newTemplate.setSize(primaryVol.getSize()); if (primaryPool.getType() == StoragePoolType.RBD) { @@ -236,34 +234,34 @@ public class KVMStorageProcessor implements StorageProcessor { } data = newTemplate; } else if (destData.getObjectType() == DataObjectType.VOLUME) { - VolumeObjectTO volumeObjectTO = new VolumeObjectTO(); + final VolumeObjectTO volumeObjectTO = new VolumeObjectTO(); volumeObjectTO.setPath(primaryVol.getName()); volumeObjectTO.setSize(primaryVol.getSize()); - if (primaryVol.getFormat() == PhysicalDiskFormat.RAW) + if (primaryVol.getFormat() == PhysicalDiskFormat.RAW) { volumeObjectTO.setFormat(ImageFormat.RAW); - else if (primaryVol.getFormat() == PhysicalDiskFormat.QCOW2) { + } else if (primaryVol.getFormat() == PhysicalDiskFormat.QCOW2) { volumeObjectTO.setFormat(ImageFormat.QCOW2); } data = volumeObjectTO; } return new CopyCmdAnswer(data); - } catch (CloudRuntimeException e) { + } catch (final CloudRuntimeException e) { return new CopyCmdAnswer(e.toString()); } finally { try { if (secondaryPool != null) { secondaryPool.delete(); } - } catch(Exception e) { + } catch(final Exception e) { s_logger.debug("Failed to clean up secondary storage", e); } } } // this is much like PrimaryStorageDownloadCommand, but keeping it separate. copies template direct to root disk - private KVMPhysicalDisk templateToPrimaryDownload(String templateUrl, KVMStoragePool primaryPool, String volUuid, Long size, int timeout) { - int index = templateUrl.lastIndexOf("/"); - String mountpoint = templateUrl.substring(0, index); + private KVMPhysicalDisk templateToPrimaryDownload(final String templateUrl, final KVMStoragePool primaryPool, final String volUuid, final Long size, final int timeout) { + final int index = templateUrl.lastIndexOf("/"); + final String mountpoint = templateUrl.substring(0, index); String templateName = null; if (index < templateUrl.length() - 1) { templateName = templateUrl.substring(index + 1); @@ -276,12 +274,12 @@ public class KVMStorageProcessor implements StorageProcessor { /* Get template vol */ if (templateName == null) { secondaryPool.refresh(); - List disks = secondaryPool.listPhysicalDisks(); + final List disks = secondaryPool.listPhysicalDisks(); if (disks == null || disks.isEmpty()) { s_logger.error("Failed to get volumes from pool: " + secondaryPool.getUuid()); return null; } - for (KVMPhysicalDisk disk : disks) { + for (final KVMPhysicalDisk disk : disks) { if (disk.getName().endsWith("qcow2")) { templateVol = disk; break; @@ -305,9 +303,9 @@ public class KVMStorageProcessor implements StorageProcessor { s_logger.debug("Using templates disk size of " + templateVol.getVirtualSize() + "since size passed was " + size); } - KVMPhysicalDisk primaryVol = storagePoolMgr.copyPhysicalDisk(templateVol, volUuid, primaryPool, timeout); + final KVMPhysicalDisk primaryVol = storagePoolMgr.copyPhysicalDisk(templateVol, volUuid, primaryPool, timeout); return primaryVol; - } catch (CloudRuntimeException e) { + } catch (final CloudRuntimeException e) { s_logger.error("Failed to download template to primary storage", e); return null; } finally { @@ -318,13 +316,13 @@ public class KVMStorageProcessor implements StorageProcessor { } @Override - public Answer cloneVolumeFromBaseTemplate(CopyCommand cmd) { - DataTO srcData = cmd.getSrcTO(); - DataTO destData = cmd.getDestTO(); - TemplateObjectTO template = (TemplateObjectTO)srcData; - DataStoreTO imageStore = template.getDataStore(); - VolumeObjectTO volume = (VolumeObjectTO)destData; - PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)volume.getDataStore(); + public Answer cloneVolumeFromBaseTemplate(final CopyCommand cmd) { + final DataTO srcData = cmd.getSrcTO(); + final DataTO destData = cmd.getDestTO(); + final TemplateObjectTO template = (TemplateObjectTO)srcData; + final DataStoreTO imageStore = template.getDataStore(); + final VolumeObjectTO volume = (VolumeObjectTO)destData; + final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)volume.getDataStore(); KVMPhysicalDisk BaseVol = null; KVMStoragePool primaryPool = null; KVMPhysicalDisk vol = null; @@ -350,7 +348,7 @@ public class KVMStorageProcessor implements StorageProcessor { return new CopyCmdAnswer(" Can't create storage volume on storage pool"); } - VolumeObjectTO newVol = new VolumeObjectTO(); + final VolumeObjectTO newVol = new VolumeObjectTO(); newVol.setPath(vol.getName()); newVol.setSize(volume.getSize()); @@ -363,59 +361,59 @@ public class KVMStorageProcessor implements StorageProcessor { } return new CopyCmdAnswer(newVol); - } catch (CloudRuntimeException e) { + } catch (final CloudRuntimeException e) { s_logger.debug("Failed to create volume: ", e); return new CopyCmdAnswer(e.toString()); } } @Override - public Answer copyVolumeFromImageCacheToPrimary(CopyCommand cmd) { - DataTO srcData = cmd.getSrcTO(); - DataTO destData = cmd.getDestTO(); - DataStoreTO srcStore = srcData.getDataStore(); - DataStoreTO destStore = destData.getDataStore(); - VolumeObjectTO srcVol = (VolumeObjectTO)srcData; - ImageFormat srcFormat = srcVol.getFormat(); - PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)destStore; + public Answer copyVolumeFromImageCacheToPrimary(final CopyCommand cmd) { + final DataTO srcData = cmd.getSrcTO(); + final DataTO destData = cmd.getDestTO(); + final DataStoreTO srcStore = srcData.getDataStore(); + final DataStoreTO destStore = destData.getDataStore(); + final VolumeObjectTO srcVol = (VolumeObjectTO)srcData; + final ImageFormat srcFormat = srcVol.getFormat(); + final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)destStore; if (!(srcStore instanceof NfsTO)) { return new CopyCmdAnswer("can only handle nfs storage"); } - NfsTO nfsStore = (NfsTO)srcStore; - String srcVolumePath = srcData.getPath(); - String secondaryStorageUrl = nfsStore.getUrl(); + final NfsTO nfsStore = (NfsTO)srcStore; + final String srcVolumePath = srcData.getPath(); + final String secondaryStorageUrl = nfsStore.getUrl(); KVMStoragePool secondaryStoragePool = null; KVMStoragePool primaryPool = null; try { try { primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid()); - } catch (CloudRuntimeException e) { + } catch (final CloudRuntimeException e) { if (e.getMessage().contains("not found")) { primaryPool = - storagePoolMgr.createStoragePool(primaryStore.getUuid(), primaryStore.getHost(), primaryStore.getPort(), primaryStore.getPath(), null, - primaryStore.getPoolType()); + storagePoolMgr.createStoragePool(primaryStore.getUuid(), primaryStore.getHost(), primaryStore.getPort(), primaryStore.getPath(), null, + primaryStore.getPoolType()); } else { return new CopyCmdAnswer(e.getMessage()); } } - String volumeName = UUID.randomUUID().toString(); + final String volumeName = UUID.randomUUID().toString(); - int index = srcVolumePath.lastIndexOf(File.separator); - String volumeDir = srcVolumePath.substring(0, index); + final int index = srcVolumePath.lastIndexOf(File.separator); + final String volumeDir = srcVolumePath.substring(0, index); String srcVolumeName = srcVolumePath.substring(index + 1); secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl + File.separator + volumeDir); if (!srcVolumeName.endsWith(".qcow2") && srcFormat == ImageFormat.QCOW2) { srcVolumeName = srcVolumeName + ".qcow2"; } - KVMPhysicalDisk volume = secondaryStoragePool.getPhysicalDisk(srcVolumeName); + final KVMPhysicalDisk volume = secondaryStoragePool.getPhysicalDisk(srcVolumeName); volume.setFormat(PhysicalDiskFormat.valueOf(srcFormat.toString())); - KVMPhysicalDisk newDisk = storagePoolMgr.copyPhysicalDisk(volume, volumeName, primaryPool, cmd.getWaitInMillSeconds()); - VolumeObjectTO newVol = new VolumeObjectTO(); + final KVMPhysicalDisk newDisk = storagePoolMgr.copyPhysicalDisk(volume, volumeName, primaryPool, cmd.getWaitInMillSeconds()); + final VolumeObjectTO newVol = new VolumeObjectTO(); newVol.setFormat(ImageFormat.valueOf(newDisk.getFormat().toString().toUpperCase())); newVol.setPath(volumeName); return new CopyCmdAnswer(newVol); - } catch (CloudRuntimeException e) { + } catch (final CloudRuntimeException e) { s_logger.debug("Failed to ccopyVolumeFromImageCacheToPrimary: ", e); return new CopyCmdAnswer(e.toString()); } finally { @@ -426,30 +424,30 @@ public class KVMStorageProcessor implements StorageProcessor { } @Override - public Answer copyVolumeFromPrimaryToSecondary(CopyCommand cmd) { - DataTO srcData = cmd.getSrcTO(); - DataTO destData = cmd.getDestTO(); - VolumeObjectTO srcVol = (VolumeObjectTO)srcData; - VolumeObjectTO destVol = (VolumeObjectTO)destData; - ImageFormat srcFormat = srcVol.getFormat(); - ImageFormat destFormat = destVol.getFormat(); - DataStoreTO srcStore = srcData.getDataStore(); - DataStoreTO destStore = destData.getDataStore(); - PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)srcStore; + public Answer copyVolumeFromPrimaryToSecondary(final CopyCommand cmd) { + final DataTO srcData = cmd.getSrcTO(); + final DataTO destData = cmd.getDestTO(); + final VolumeObjectTO srcVol = (VolumeObjectTO)srcData; + final VolumeObjectTO destVol = (VolumeObjectTO)destData; + final ImageFormat srcFormat = srcVol.getFormat(); + final ImageFormat destFormat = destVol.getFormat(); + final DataStoreTO srcStore = srcData.getDataStore(); + final DataStoreTO destStore = destData.getDataStore(); + final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)srcStore; if (!(destStore instanceof NfsTO)) { return new CopyCmdAnswer("can only handle nfs storage"); } - NfsTO nfsStore = (NfsTO)destStore; - String srcVolumePath = srcData.getPath(); - String destVolumePath = destData.getPath(); - String secondaryStorageUrl = nfsStore.getUrl(); + final NfsTO nfsStore = (NfsTO)destStore; + final String srcVolumePath = srcData.getPath(); + final String destVolumePath = destData.getPath(); + final String secondaryStorageUrl = nfsStore.getUrl(); KVMStoragePool secondaryStoragePool = null; try { - String volumeName = UUID.randomUUID().toString(); + final String volumeName = UUID.randomUUID().toString(); - String destVolumeName = volumeName + "." + destFormat.getFileExtension(); - KVMPhysicalDisk volume = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), srcVolumePath); + final String destVolumeName = volumeName + "." + destFormat.getFileExtension(); + final KVMPhysicalDisk volume = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), srcVolumePath); volume.setFormat(PhysicalDiskFormat.valueOf(srcFormat.toString())); secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl); @@ -457,11 +455,11 @@ public class KVMStorageProcessor implements StorageProcessor { storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid()); secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl + File.separator + destVolumePath); storagePoolMgr.copyPhysicalDisk(volume, destVolumeName, secondaryStoragePool, cmd.getWaitInMillSeconds()); - VolumeObjectTO newVol = new VolumeObjectTO(); + final VolumeObjectTO newVol = new VolumeObjectTO(); newVol.setPath(destVolumePath + File.separator + destVolumeName); newVol.setFormat(destFormat); return new CopyCmdAnswer(newVol); - } catch (CloudRuntimeException e) { + } catch (final CloudRuntimeException e) { s_logger.debug("Failed to copyVolumeFromPrimaryToSecondary: ", e); return new CopyCmdAnswer(e.toString()); } finally { @@ -472,41 +470,41 @@ public class KVMStorageProcessor implements StorageProcessor { } @Override - public Answer createTemplateFromVolume(CopyCommand cmd) { - DataTO srcData = cmd.getSrcTO(); - DataTO destData = cmd.getDestTO(); - int wait = cmd.getWaitInMillSeconds(); - TemplateObjectTO template = (TemplateObjectTO)destData; - DataStoreTO imageStore = template.getDataStore(); - VolumeObjectTO volume = (VolumeObjectTO)srcData; - PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)volume.getDataStore(); + public Answer createTemplateFromVolume(final CopyCommand cmd) { + final DataTO srcData = cmd.getSrcTO(); + final DataTO destData = cmd.getDestTO(); + final int wait = cmd.getWaitInMillSeconds(); + final TemplateObjectTO template = (TemplateObjectTO)destData; + final DataStoreTO imageStore = template.getDataStore(); + final VolumeObjectTO volume = (VolumeObjectTO)srcData; + final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)volume.getDataStore(); if (!(imageStore instanceof NfsTO)) { return new CopyCmdAnswer("unsupported protocol"); } - NfsTO nfsImageStore = (NfsTO)imageStore; + final NfsTO nfsImageStore = (NfsTO)imageStore; KVMStoragePool secondaryStorage = null; KVMStoragePool primary = null; try { - String templateFolder = template.getPath(); + final String templateFolder = template.getPath(); secondaryStorage = storagePoolMgr.getStoragePoolByURI(nfsImageStore.getUrl()); primary = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid()); - KVMPhysicalDisk disk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volume.getPath()); - String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateFolder; + final KVMPhysicalDisk disk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volume.getPath()); + final String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateFolder; storageLayer.mkdirs(tmpltPath); - String templateName = UUID.randomUUID().toString(); + final String templateName = UUID.randomUUID().toString(); if (primary.getType() != StoragePoolType.RBD) { - Script command = new Script(_createTmplPath, wait, s_logger); + final Script command = new Script(_createTmplPath, wait, s_logger); command.add("-f", disk.getPath()); command.add("-t", tmpltPath); command.add("-n", templateName + ".qcow2"); - String result = command.execute(); + final String result = command.execute(); if (result != null) { s_logger.debug("failed to create template: " + result); @@ -515,60 +513,60 @@ public class KVMStorageProcessor implements StorageProcessor { } else { s_logger.debug("Converting RBD disk " + disk.getPath() + " into template " + templateName); - QemuImgFile srcFile = - new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(primary.getSourceHost(), primary.getSourcePort(), primary.getAuthUserName(), - primary.getAuthSecret(), disk.getPath())); + final QemuImgFile srcFile = + new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(primary.getSourceHost(), primary.getSourcePort(), primary.getAuthUserName(), + primary.getAuthSecret(), disk.getPath())); srcFile.setFormat(PhysicalDiskFormat.RAW); - QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + templateName + ".qcow2"); + final QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + templateName + ".qcow2"); destFile.setFormat(PhysicalDiskFormat.QCOW2); - QemuImg q = new QemuImg(cmd.getWaitInMillSeconds()); + final QemuImg q = new QemuImg(cmd.getWaitInMillSeconds()); try { q.convert(srcFile, destFile); - } catch (QemuImgException e) { + } catch (final QemuImgException e) { s_logger.error("Failed to create new template while converting " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + - e.getMessage()); + e.getMessage()); } - File templateProp = new File(tmpltPath + "/template.properties"); + final File templateProp = new File(tmpltPath + "/template.properties"); if (!templateProp.exists()) { templateProp.createNewFile(); } String templateContent = "filename=" + templateName + ".qcow2" + System.getProperty("line.separator"); - DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy"); - Date date = new Date(); + final DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy"); + final Date date = new Date(); templateContent += "snapshot.name=" + dateFormat.format(date) + System.getProperty("line.separator"); - FileOutputStream templFo = new FileOutputStream(templateProp); + final FileOutputStream templFo = new FileOutputStream(templateProp); templFo.write(templateContent.getBytes()); templFo.flush(); templFo.close(); } - Map params = new HashMap(); + final Map params = new HashMap(); params.put(StorageLayer.InstanceConfigKey, storageLayer); - Processor qcow2Processor = new QCOW2Processor(); + final Processor qcow2Processor = new QCOW2Processor(); qcow2Processor.configure("QCOW2 Processor", params); - FormatInfo info = qcow2Processor.process(tmpltPath, null, templateName); + final FormatInfo info = qcow2Processor.process(tmpltPath, null, templateName); - TemplateLocation loc = new TemplateLocation(storageLayer, tmpltPath); + final TemplateLocation loc = new TemplateLocation(storageLayer, tmpltPath); loc.create(1, true, templateName); loc.addFormat(info); loc.save(); - TemplateObjectTO newTemplate = new TemplateObjectTO(); + final TemplateObjectTO newTemplate = new TemplateObjectTO(); newTemplate.setPath(templateFolder + File.separator + templateName + ".qcow2"); newTemplate.setSize(info.virtualSize); newTemplate.setPhysicalSize(info.size); newTemplate.setFormat(ImageFormat.QCOW2); newTemplate.setName(templateName); return new CopyCmdAnswer(newTemplate); - } catch (Exception e) { + } catch (final Exception e) { s_logger.debug("Failed to createTemplateFromVolume: ", e); return new CopyCmdAnswer(e.toString()); } finally { @@ -579,15 +577,15 @@ public class KVMStorageProcessor implements StorageProcessor { } @Override - public Answer createTemplateFromSnapshot(CopyCommand cmd) { + public Answer createTemplateFromSnapshot(final CopyCommand cmd) { return null; //To change body of implemented methods use File | Settings | File Templates. } - protected String copyToS3(File srcFile, S3TO destStore, String destPath) throws InterruptedException { + protected String copyToS3(final File srcFile, final S3TO destStore, final String destPath) throws InterruptedException { final String bucket = destStore.getBucketName(); - long srcSize = srcFile.length(); - String key = destPath + S3Utils.SEPARATOR + srcFile.getName(); + final long srcSize = srcFile.length(); + final String key = destPath + S3Utils.SEPARATOR + srcFile.getName(); if (!destStore.getSingleUpload(srcSize)) { mputFile(destStore, srcFile, bucket, key); } else { @@ -596,15 +594,15 @@ public class KVMStorageProcessor implements StorageProcessor { return key; } - protected Answer copyToObjectStore(CopyCommand cmd) { - DataTO srcData = cmd.getSrcTO(); - DataTO destData = cmd.getDestTO(); - DataStoreTO imageStore = destData.getDataStore(); - NfsTO srcStore = (NfsTO)srcData.getDataStore(); - String srcPath = srcData.getPath(); - int index = srcPath.lastIndexOf(File.separator); - String srcSnapshotDir = srcPath.substring(0, index); - String srcFileName = srcPath.substring(index + 1); + protected Answer copyToObjectStore(final CopyCommand cmd) { + final DataTO srcData = cmd.getSrcTO(); + final DataTO destData = cmd.getDestTO(); + final DataStoreTO imageStore = destData.getDataStore(); + final NfsTO srcStore = (NfsTO)srcData.getDataStore(); + final String srcPath = srcData.getPath(); + final int index = srcPath.lastIndexOf(File.separator); + final String srcSnapshotDir = srcPath.substring(0, index); + final String srcFileName = srcPath.substring(index + 1); KVMStoragePool srcStorePool = null; File srcFile = null; try { @@ -622,10 +620,10 @@ public class KVMStorageProcessor implements StorageProcessor { } else { return new CopyCmdAnswer("Unsupported protocol"); } - SnapshotObjectTO newSnapshot = new SnapshotObjectTO(); + final SnapshotObjectTO newSnapshot = new SnapshotObjectTO(); newSnapshot.setPath(destPath); return new CopyCmdAnswer(newSnapshot); - } catch (Exception e) { + } catch (final Exception e) { s_logger.error("failed to upload" + srcPath, e); return new CopyCmdAnswer("failed to upload" + srcPath + e.toString()); } finally { @@ -636,55 +634,55 @@ public class KVMStorageProcessor implements StorageProcessor { if (srcStorePool != null) { srcStorePool.delete(); } - } catch (Exception e) { + } catch (final Exception e) { s_logger.debug("Failed to clean up:", e); } } } - protected Answer backupSnapshotForObjectStore(CopyCommand cmd) { - DataTO destData = cmd.getDestTO(); - DataStoreTO imageStore = destData.getDataStore(); - DataTO cacheData = cmd.getCacheTO(); + protected Answer backupSnapshotForObjectStore(final CopyCommand cmd) { + final DataTO destData = cmd.getDestTO(); + final DataStoreTO imageStore = destData.getDataStore(); + final DataTO cacheData = cmd.getCacheTO(); if (cacheData == null) { return new CopyCmdAnswer("Failed to copy to object store without cache store"); } - DataStoreTO cacheStore = cacheData.getDataStore(); + final DataStoreTO cacheStore = cacheData.getDataStore(); ((SnapshotObjectTO)destData).setDataStore(cacheStore); - CopyCmdAnswer answer = (CopyCmdAnswer)backupSnapshot(cmd); + final CopyCmdAnswer answer = (CopyCmdAnswer)backupSnapshot(cmd); if (!answer.getResult()) { return answer; } - SnapshotObjectTO snapshotOnCacheStore = (SnapshotObjectTO)answer.getNewData(); + final SnapshotObjectTO snapshotOnCacheStore = (SnapshotObjectTO)answer.getNewData(); snapshotOnCacheStore.setDataStore(cacheStore); ((SnapshotObjectTO)destData).setDataStore(imageStore); - CopyCommand newCpyCmd = new CopyCommand(snapshotOnCacheStore, destData, cmd.getWaitInMillSeconds(), cmd.executeInSequence()); + final CopyCommand newCpyCmd = new CopyCommand(snapshotOnCacheStore, destData, cmd.getWaitInMillSeconds(), cmd.executeInSequence()); return copyToObjectStore(newCpyCmd); } @Override - public Answer backupSnapshot(CopyCommand cmd) { - DataTO srcData = cmd.getSrcTO(); - DataTO destData = cmd.getDestTO(); - SnapshotObjectTO snapshot = (SnapshotObjectTO)srcData; - PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)snapshot.getDataStore(); - SnapshotObjectTO destSnapshot = (SnapshotObjectTO)destData; - DataStoreTO imageStore = destData.getDataStore(); + public Answer backupSnapshot(final CopyCommand cmd) { + final DataTO srcData = cmd.getSrcTO(); + final DataTO destData = cmd.getDestTO(); + final SnapshotObjectTO snapshot = (SnapshotObjectTO)srcData; + final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)snapshot.getDataStore(); + final SnapshotObjectTO destSnapshot = (SnapshotObjectTO)destData; + final DataStoreTO imageStore = destData.getDataStore(); if (!(imageStore instanceof NfsTO)) { return backupSnapshotForObjectStore(cmd); } - NfsTO nfsImageStore = (NfsTO)imageStore; + final NfsTO nfsImageStore = (NfsTO)imageStore; - String secondaryStoragePoolUrl = nfsImageStore.getUrl(); + final String secondaryStoragePoolUrl = nfsImageStore.getUrl(); // NOTE: snapshot name is encoded in snapshot path - int index = snapshot.getPath().lastIndexOf("/"); + final int index = snapshot.getPath().lastIndexOf("/"); - String snapshotName = snapshot.getPath().substring(index + 1); - String volumePath = snapshot.getVolume().getPath(); + final String snapshotName = snapshot.getPath().substring(index + 1); + final String volumePath = snapshot.getVolume().getPath(); String snapshotDestPath = null; String snapshotRelPath = null; - String vmName = snapshot.getVmName(); + final String vmName = snapshot.getVmName(); KVMStoragePool secondaryStoragePool = null; Connect conn = null; KVMPhysicalDisk snapshotDisk = null; @@ -694,7 +692,7 @@ public class KVMStorageProcessor implements StorageProcessor { secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl); - String ssPmountPath = secondaryStoragePool.getLocalPath(); + final String ssPmountPath = secondaryStoragePool.getLocalPath(); snapshotRelPath = destSnapshot.getPath(); snapshotDestPath = ssPmountPath + File.separator + snapshotRelPath; @@ -709,69 +707,69 @@ public class KVMStorageProcessor implements StorageProcessor { * This reduces the amount of time and storage it takes to back up a snapshot dramatically */ if (primaryPool.getType() == StoragePoolType.RBD) { - String rbdSnapshot = snapshotDisk.getPath() + "@" + snapshotName; - String snapshotFile = snapshotDestPath + "/" + snapshotName; + final String rbdSnapshot = snapshotDisk.getPath() + "@" + snapshotName; + final String snapshotFile = snapshotDestPath + "/" + snapshotName; try { s_logger.debug("Attempting to backup RBD snapshot " + rbdSnapshot); - File snapDir = new File(snapshotDestPath); + final File snapDir = new File(snapshotDestPath); s_logger.debug("Attempting to create " + snapDir.getAbsolutePath() + " recursively for snapshot storage"); FileUtils.forceMkdir(snapDir); - QemuImgFile srcFile = - new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(primaryPool.getSourceHost(), primaryPool.getSourcePort(), primaryPool.getAuthUserName(), - primaryPool.getAuthSecret(), rbdSnapshot)); + final QemuImgFile srcFile = + new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(primaryPool.getSourceHost(), primaryPool.getSourcePort(), primaryPool.getAuthUserName(), + primaryPool.getAuthSecret(), rbdSnapshot)); srcFile.setFormat(PhysicalDiskFormat.RAW); - QemuImgFile destFile = new QemuImgFile(snapshotFile); + final QemuImgFile destFile = new QemuImgFile(snapshotFile); destFile.setFormat(snapshotDisk.getFormat()); s_logger.debug("Backing up RBD snapshot " + rbdSnapshot + " to " + snapshotFile); - QemuImg q = new QemuImg(cmd.getWaitInMillSeconds()); + final QemuImg q = new QemuImg(cmd.getWaitInMillSeconds()); q.convert(srcFile, destFile); - File snapFile = new File(snapshotFile); + final File snapFile = new File(snapshotFile); if(snapFile.exists()) { size = snapFile.length(); } s_logger.debug("Finished backing up RBD snapshot " + rbdSnapshot + " to " + snapshotFile + " Snapshot size: " + size); - } catch (FileNotFoundException e) { + } catch (final FileNotFoundException e) { s_logger.error("Failed to open " + snapshotDestPath + ". The error was: " + e.getMessage()); return new CopyCmdAnswer(e.toString()); - } catch (IOException e) { + } catch (final IOException e) { s_logger.error("Failed to create " + snapshotDestPath + ". The error was: " + e.getMessage()); return new CopyCmdAnswer(e.toString()); - } catch (QemuImgException e) { + } catch (final QemuImgException e) { s_logger.error("Failed to backup the RBD snapshot from " + rbdSnapshot + - " to " + snapshotFile + " the error was: " + e.getMessage()); + " to " + snapshotFile + " the error was: " + e.getMessage()); return new CopyCmdAnswer(e.toString()); } } else { - Script command = new Script(_manageSnapshotPath, cmd.getWaitInMillSeconds(), s_logger); + final Script command = new Script(_manageSnapshotPath, cmd.getWaitInMillSeconds(), s_logger); command.add("-b", snapshotDisk.getPath()); command.add("-n", snapshotName); command.add("-p", snapshotDestPath); command.add("-t", snapshotName); - String result = command.execute(); + final String result = command.execute(); if (result != null) { s_logger.debug("Failed to backup snaptshot: " + result); return new CopyCmdAnswer(result); } - File snapFile = new File(snapshotDestPath + "/" + snapshotName); + final File snapFile = new File(snapshotDestPath + "/" + snapshotName); if(snapFile.exists()){ size = snapFile.length(); } } - SnapshotObjectTO newSnapshot = new SnapshotObjectTO(); + final SnapshotObjectTO newSnapshot = new SnapshotObjectTO(); newSnapshot.setPath(snapshotRelPath + File.separator + snapshotName); newSnapshot.setPhysicalSize(size); return new CopyCmdAnswer(newSnapshot); - } catch (LibvirtException e) { + } catch (final LibvirtException e) { s_logger.debug("Failed to backup snapshot: ", e); return new CopyCmdAnswer(e.toString()); - } catch (CloudRuntimeException e) { + } catch (final CloudRuntimeException e) { s_logger.debug("Failed to backup snapshot: ", e); return new CopyCmdAnswer(e.toString()); } finally { @@ -783,15 +781,15 @@ public class KVMStorageProcessor implements StorageProcessor { try { vm = resource.getDomain(conn, vmName); state = vm.getInfo().state; - } catch (LibvirtException e) { + } catch (final LibvirtException e) { s_logger.trace("Ignoring libvirt error.", e); } } - KVMStoragePool primaryStorage = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), + final KVMStoragePool primaryStorage = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid()); if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryStorage.isExternalSnapshot()) { - DomainSnapshot snap = vm.snapshotLookupByName(snapshotName); + final DomainSnapshot snap = vm.snapshotLookupByName(snapshotName); snap.delete(0); /* @@ -805,17 +803,17 @@ public class KVMStorageProcessor implements StorageProcessor { } } else { if (primaryPool.getType() != StoragePoolType.RBD) { - Script command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger); + final Script command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger); command.add("-d", snapshotDisk.getPath()); command.add("-n", snapshotName); - String result = command.execute(); + final String result = command.execute(); if (result != null) { s_logger.debug("Failed to delete snapshot on primary: " + result); // return new CopyCmdAnswer("Failed to backup snapshot: " + result); } } } - } catch (Exception ex) { + } catch (final Exception ex) { s_logger.debug("Failed to delete snapshots on primary", ex); } @@ -823,36 +821,36 @@ public class KVMStorageProcessor implements StorageProcessor { if (secondaryStoragePool != null) { secondaryStoragePool.delete(); } - } catch (Exception ex) { + } catch (final Exception ex) { s_logger.debug("Failed to delete secondary storage", ex); } } } - protected synchronized String attachOrDetachISO(Connect conn, String vmName, String isoPath, boolean isAttach) throws LibvirtException, URISyntaxException, - InternalErrorException { + protected synchronized String attachOrDetachISO(final Connect conn, final String vmName, String isoPath, final boolean isAttach) throws LibvirtException, URISyntaxException, + InternalErrorException { String isoXml = null; if (isoPath != null && isAttach) { - int index = isoPath.lastIndexOf("/"); - String path = isoPath.substring(0, index); - String name = isoPath.substring(index + 1); - KVMStoragePool secondaryPool = storagePoolMgr.getStoragePoolByURI(path); - KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name); + final int index = isoPath.lastIndexOf("/"); + final String path = isoPath.substring(0, index); + final String name = isoPath.substring(index + 1); + final KVMStoragePool secondaryPool = storagePoolMgr.getStoragePoolByURI(path); + final KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name); isoPath = isoVol.getPath(); - DiskDef iso = new DiskDef(); + final DiskDef iso = new DiskDef(); iso.defISODisk(isoPath); isoXml = iso.toString(); } else { - DiskDef iso = new DiskDef(); + final DiskDef iso = new DiskDef(); iso.defISODisk(null); isoXml = iso.toString(); } - List disks = resource.getDisks(conn, vmName); - String result = attachOrDetachDevice(conn, true, vmName, isoXml); + final List disks = resource.getDisks(conn, vmName); + final String result = attachOrDetachDevice(conn, true, vmName, isoXml); if (result == null && !isAttach) { - for (DiskDef disk : disks) { + for (final DiskDef disk : disks) { if (disk.getDeviceType() == DiskDef.deviceType.CDROM) { resource.cleanupDisk(disk); } @@ -863,22 +861,22 @@ public class KVMStorageProcessor implements StorageProcessor { } @Override - public Answer attachIso(AttachCommand cmd) { - DiskTO disk = cmd.getDisk(); - TemplateObjectTO isoTO = (TemplateObjectTO)disk.getData(); - DataStoreTO store = isoTO.getDataStore(); + public Answer attachIso(final AttachCommand cmd) { + final DiskTO disk = cmd.getDisk(); + final TemplateObjectTO isoTO = (TemplateObjectTO)disk.getData(); + final DataStoreTO store = isoTO.getDataStore(); if (!(store instanceof NfsTO)) { return new AttachAnswer("unsupported protocol"); } - NfsTO nfsStore = (NfsTO)store; + final NfsTO nfsStore = (NfsTO)store; try { - Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName()); + final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName()); attachOrDetachISO(conn, cmd.getVmName(), nfsStore.getUrl() + File.separator + isoTO.getPath(), true); - } catch (LibvirtException e) { + } catch (final LibvirtException e) { return new Answer(cmd, false, e.toString()); - } catch (URISyntaxException e) { + } catch (final URISyntaxException e) { return new Answer(cmd, false, e.toString()); - } catch (InternalErrorException e) { + } catch (final InternalErrorException e) { return new Answer(cmd, false, e.toString()); } @@ -886,29 +884,29 @@ public class KVMStorageProcessor implements StorageProcessor { } @Override - public Answer dettachIso(DettachCommand cmd) { - DiskTO disk = cmd.getDisk(); - TemplateObjectTO isoTO = (TemplateObjectTO)disk.getData(); - DataStoreTO store = isoTO.getDataStore(); + public Answer dettachIso(final DettachCommand cmd) { + final DiskTO disk = cmd.getDisk(); + final TemplateObjectTO isoTO = (TemplateObjectTO)disk.getData(); + final DataStoreTO store = isoTO.getDataStore(); if (!(store instanceof NfsTO)) { return new AttachAnswer("unsupported protocol"); } - NfsTO nfsStore = (NfsTO)store; + final NfsTO nfsStore = (NfsTO)store; try { - Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName()); + final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName()); attachOrDetachISO(conn, cmd.getVmName(), nfsStore.getUrl() + File.separator + isoTO.getPath(), false); - } catch (LibvirtException e) { + } catch (final LibvirtException e) { return new Answer(cmd, false, e.toString()); - } catch (URISyntaxException e) { + } catch (final URISyntaxException e) { return new Answer(cmd, false, e.toString()); - } catch (InternalErrorException e) { + } catch (final InternalErrorException e) { return new Answer(cmd, false, e.toString()); } return new Answer(cmd); } - protected synchronized String attachOrDetachDevice(Connect conn, boolean attach, String vmName, String xml) throws LibvirtException, InternalErrorException { + protected synchronized String attachOrDetachDevice(final Connect conn, final boolean attach, final String vmName, final String xml) throws LibvirtException, InternalErrorException { Domain dm = null; try { dm = conn.domainLookupByName(vmName); @@ -920,7 +918,7 @@ public class KVMStorageProcessor implements StorageProcessor { s_logger.debug("Detaching device: " + xml); dm.detachDevice(xml); } - } catch (LibvirtException e) { + } catch (final LibvirtException e) { if (attach) { s_logger.warn("Failed to attach device to " + vmName + ": " + e.getMessage()); } else { @@ -931,7 +929,7 @@ public class KVMStorageProcessor implements StorageProcessor { if (dm != null) { try { dm.free(); - } catch (LibvirtException l) { + } catch (final LibvirtException l) { s_logger.trace("Ignoring libvirt error.", l); } } @@ -940,23 +938,23 @@ public class KVMStorageProcessor implements StorageProcessor { return null; } - protected synchronized String attachOrDetachDisk(Connect conn, boolean attach, String vmName, KVMPhysicalDisk attachingDisk, int devId) throws LibvirtException, - InternalErrorException { + protected synchronized String attachOrDetachDisk(final Connect conn, final boolean attach, final String vmName, final KVMPhysicalDisk attachingDisk, final int devId) throws LibvirtException, + InternalErrorException { List disks = null; Domain dm = null; DiskDef diskdef = null; - KVMStoragePool attachingPool = attachingDisk.getPool(); + final KVMStoragePool attachingPool = attachingDisk.getPool(); try { if (!attach) { dm = conn.domainLookupByName(vmName); - LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser(); - String xml = dm.getXMLDesc(0); + final LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser(); + final String xml = dm.getXMLDesc(0); parser.parseDomainXML(xml); disks = parser.getDisks(); if (attachingPool.getType() == StoragePoolType.RBD) { if (resource.getHypervisorType() == Hypervisor.HypervisorType.LXC) { - String device = resource.mapRbdDevice(attachingDisk); + final String device = resource.mapRbdDevice(attachingDisk); if (device != null) { s_logger.debug("RBD device on host is: "+device); attachingDisk.setPath(device); @@ -964,8 +962,8 @@ public class KVMStorageProcessor implements StorageProcessor { } } - for (DiskDef disk : disks) { - String file = disk.getDiskPath(); + for (final DiskDef disk : disks) { + final String file = disk.getDiskPath(); if (file != null && file.equalsIgnoreCase(attachingDisk.getPath())) { diskdef = disk; break; @@ -979,7 +977,7 @@ public class KVMStorageProcessor implements StorageProcessor { if (attachingPool.getType() == StoragePoolType.RBD) { if(resource.getHypervisorType() == Hypervisor.HypervisorType.LXC){ // For LXC, map image to host and then attach to Vm - String device = resource.mapRbdDevice(attachingDisk); + final String device = resource.mapRbdDevice(attachingDisk); if (device != null) { s_logger.debug("RBD device on host is: "+device); diskdef.defBlockBasedDisk(device, devId, DiskDef.diskBus.VIRTIO); @@ -991,11 +989,11 @@ public class KVMStorageProcessor implements StorageProcessor { attachingPool.getUuid(), devId, DiskDef.diskBus.VIRTIO, diskProtocol.RBD, DiskDef.diskFmtType.RAW); } } else if (attachingPool.getType() == StoragePoolType.Gluster) { - String mountpoint = attachingPool.getLocalPath(); - String path = attachingDisk.getPath(); - String glusterVolume = attachingPool.getSourceDir().replace("/", ""); + final String mountpoint = attachingPool.getLocalPath(); + final String path = attachingDisk.getPath(); + final String glusterVolume = attachingPool.getSourceDir().replace("/", ""); diskdef.defNetworkBasedDisk(glusterVolume + path.replace(mountpoint, ""), attachingPool.getSourceHost(), attachingPool.getSourcePort(), null, - null, devId, DiskDef.diskBus.VIRTIO, diskProtocol.GLUSTER, DiskDef.diskFmtType.QCOW2); + null, devId, DiskDef.diskBus.VIRTIO, diskProtocol.GLUSTER, DiskDef.diskFmtType.QCOW2); } else if (attachingDisk.getFormat() == PhysicalDiskFormat.QCOW2) { diskdef.defFileBasedDisk(attachingDisk.getPath(), devId, DiskDef.diskBus.VIRTIO, DiskDef.diskFmtType.QCOW2); } else if (attachingDisk.getFormat() == PhysicalDiskFormat.RAW) { @@ -1003,7 +1001,7 @@ public class KVMStorageProcessor implements StorageProcessor { } } - String xml = diskdef.toString(); + final String xml = diskdef.toString(); return attachOrDetachDevice(conn, attach, vmName, xml); } finally { if (dm != null) { @@ -1013,60 +1011,60 @@ public class KVMStorageProcessor implements StorageProcessor { } @Override - public Answer attachVolume(AttachCommand cmd) { - DiskTO disk = cmd.getDisk(); - VolumeObjectTO vol = (VolumeObjectTO)disk.getData(); - PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)vol.getDataStore(); - String vmName = cmd.getVmName(); + 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(); try { - Connect conn = LibvirtConnection.getConnectionByVmName(vmName); + final Connect conn = LibvirtConnection.getConnectionByVmName(vmName); storagePoolMgr.connectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath(), disk.getDetails()); - KVMPhysicalDisk phyDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath()); + final KVMPhysicalDisk phyDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath()); attachOrDetachDisk(conn, true, vmName, phyDisk, disk.getDiskSeq().intValue()); return new AttachAnswer(disk); - } catch (LibvirtException e) { + } 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 (InternalErrorException e) { + } catch (final InternalErrorException e) { s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e); return new AttachAnswer(e.toString()); } } @Override - public Answer dettachVolume(DettachCommand cmd) { - DiskTO disk = cmd.getDisk(); - VolumeObjectTO vol = (VolumeObjectTO)disk.getData(); - PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)vol.getDataStore(); - String vmName = cmd.getVmName(); + public Answer dettachVolume(final DettachCommand cmd) { + final DiskTO disk = cmd.getDisk(); + final VolumeObjectTO vol = (VolumeObjectTO)disk.getData(); + final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)vol.getDataStore(); + final String vmName = cmd.getVmName(); try { - Connect conn = LibvirtConnection.getConnectionByVmName(vmName); + final Connect conn = LibvirtConnection.getConnectionByVmName(vmName); - KVMPhysicalDisk phyDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath()); + final KVMPhysicalDisk phyDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath()); attachOrDetachDisk(conn, false, vmName, phyDisk, disk.getDiskSeq().intValue()); storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath()); return new DettachAnswer(disk); - } catch (LibvirtException e) { + } catch (final LibvirtException e) { s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e); return new DettachAnswer(e.toString()); - } catch (InternalErrorException e) { + } catch (final InternalErrorException e) { s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e); return new DettachAnswer(e.toString()); } } @Override - public Answer createVolume(CreateObjectCommand cmd) { - VolumeObjectTO volume = (VolumeObjectTO)cmd.getData(); - PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)volume.getDataStore(); + public Answer createVolume(final CreateObjectCommand cmd) { + final VolumeObjectTO volume = (VolumeObjectTO)cmd.getData(); + final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)volume.getDataStore(); KVMStoragePool primaryPool = null; KVMPhysicalDisk vol = null; @@ -1081,9 +1079,9 @@ public class KVMStorageProcessor implements StorageProcessor { format = PhysicalDiskFormat.valueOf(volume.getFormat().toString().toUpperCase()); } vol = primaryPool.createPhysicalDisk(volume.getUuid(), format, - volume.getProvisioningType(), disksize); + volume.getProvisioningType(), disksize); - VolumeObjectTO newVol = new VolumeObjectTO(); + final VolumeObjectTO newVol = new VolumeObjectTO(); if(vol != null) { newVol.setPath(vol.getName()); } @@ -1091,46 +1089,46 @@ public class KVMStorageProcessor implements StorageProcessor { newVol.setFormat(ImageFormat.valueOf(format.toString().toUpperCase())); return new CreateObjectAnswer(newVol); - } catch (Exception e) { + } catch (final Exception e) { s_logger.debug("Failed to create volume: ", e); return new CreateObjectAnswer(e.toString()); } } protected static final MessageFormat SnapshotXML = new MessageFormat(" " + " {0}" + " " - + " {1}" + " " + " "); + + " {1}" + " " + " "); @Override - public Answer createSnapshot(CreateObjectCommand cmd) { - SnapshotObjectTO snapshotTO = (SnapshotObjectTO)cmd.getData(); - PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)snapshotTO.getDataStore(); - VolumeObjectTO volume = snapshotTO.getVolume(); - String snapshotName = UUID.randomUUID().toString(); - String vmName = volume.getVmName(); + public Answer createSnapshot(final CreateObjectCommand cmd) { + final SnapshotObjectTO snapshotTO = (SnapshotObjectTO)cmd.getData(); + final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)snapshotTO.getDataStore(); + final VolumeObjectTO volume = snapshotTO.getVolume(); + final String snapshotName = UUID.randomUUID().toString(); + final String vmName = volume.getVmName(); try { - Connect conn = LibvirtConnection.getConnectionByVmName(vmName); + final Connect conn = LibvirtConnection.getConnectionByVmName(vmName); DomainInfo.DomainState state = null; Domain vm = null; if (vmName != null) { try { vm = resource.getDomain(conn, vmName); state = vm.getInfo().state; - } catch (LibvirtException e) { + } catch (final LibvirtException e) { s_logger.trace("Ignoring libvirt error.", e); } } - KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid()); + final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid()); - KVMPhysicalDisk disk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volume.getPath()); + final KVMPhysicalDisk disk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volume.getPath()); if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryPool.isExternalSnapshot()) { - String vmUuid = vm.getUUIDString(); - Object[] args = new Object[] {snapshotName, vmUuid}; - String snapshot = SnapshotXML.format(args); + final String vmUuid = vm.getUUIDString(); + final Object[] args = new Object[] {snapshotName, vmUuid}; + final String snapshot = SnapshotXML.format(args); - long start = System.currentTimeMillis(); + final long start = System.currentTimeMillis(); vm.snapshotCreateXML(snapshot); - long total = (System.currentTimeMillis() - start)/1000; + final long total = (System.currentTimeMillis() - start)/1000; s_logger.debug("snapshot takes " + total + " seconds to finish"); /* @@ -1158,23 +1156,23 @@ public class KVMStorageProcessor implements StorageProcessor { */ if (primaryPool.getType() == StoragePoolType.RBD) { try { - Rados r = new Rados(primaryPool.getAuthUserName()); + final Rados r = new Rados(primaryPool.getAuthUserName()); r.confSet("mon_host", primaryPool.getSourceHost() + ":" + primaryPool.getSourcePort()); r.confSet("key", primaryPool.getAuthSecret()); r.confSet("client_mount_timeout", "30"); r.connect(); s_logger.debug("Succesfully connected to Ceph cluster at " + r.confGet("mon_host")); - IoCTX io = r.ioCtxCreate(primaryPool.getSourceDir()); - Rbd rbd = new Rbd(io); - RbdImage image = rbd.open(disk.getName()); + final IoCTX io = r.ioCtxCreate(primaryPool.getSourceDir()); + final Rbd rbd = new Rbd(io); + final RbdImage image = rbd.open(disk.getName()); s_logger.debug("Attempting to create RBD snapshot " + disk.getName() + "@" + snapshotName); image.snapCreate(snapshotName); rbd.close(image); r.ioCtxDestroy(io); - } catch (Exception e) { + } catch (final Exception e) { s_logger.error("A RBD snapshot operation on " + disk.getName() + " failed. The error was: " + e.getMessage()); } } else { @@ -1182,7 +1180,7 @@ public class KVMStorageProcessor implements StorageProcessor { final Script command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger); command.add("-c", disk.getPath()); command.add("-n", snapshotName); - String result = command.execute(); + final String result = command.execute(); if (result != null) { s_logger.debug("Failed to manage snapshot: " + result); return new CreateObjectAnswer("Failed to manage snapshot: " + result); @@ -1190,58 +1188,58 @@ public class KVMStorageProcessor implements StorageProcessor { } } - SnapshotObjectTO newSnapshot = new SnapshotObjectTO(); + final SnapshotObjectTO newSnapshot = new SnapshotObjectTO(); // NOTE: sort of hack, we'd better just put snapshtoName newSnapshot.setPath(disk.getPath() + File.separator + snapshotName); return new CreateObjectAnswer(newSnapshot); - } catch (LibvirtException e) { + } catch (final LibvirtException e) { s_logger.debug("Failed to manage snapshot: ", e); return new CreateObjectAnswer("Failed to manage snapshot: " + e.toString()); } } @Override - public Answer deleteVolume(DeleteCommand cmd) { - VolumeObjectTO vol = (VolumeObjectTO)cmd.getData(); - PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)vol.getDataStore(); + public Answer deleteVolume(final DeleteCommand cmd) { + final VolumeObjectTO vol = (VolumeObjectTO)cmd.getData(); + final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)vol.getDataStore(); try { - KVMStoragePool pool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid()); + final KVMStoragePool pool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid()); try { pool.getPhysicalDisk(vol.getPath()); - } catch (Exception e) { + } catch (final Exception e) { s_logger.debug("can't find volume: " + vol.getPath() + ", return true"); return new Answer(null); } pool.deletePhysicalDisk(vol.getPath(), vol.getFormat()); return new Answer(null); - } catch (CloudRuntimeException e) { + } catch (final CloudRuntimeException e) { s_logger.debug("Failed to delete volume: ", e); return new Answer(null, false, e.toString()); } } @Override - public Answer createVolumeFromSnapshot(CopyCommand cmd) { + public Answer createVolumeFromSnapshot(final CopyCommand cmd) { try { - DataTO srcData = cmd.getSrcTO(); - SnapshotObjectTO snapshot = (SnapshotObjectTO)srcData; - DataTO destData = cmd.getDestTO(); - PrimaryDataStoreTO pool = (PrimaryDataStoreTO)destData.getDataStore(); - DataStoreTO imageStore = srcData.getDataStore(); - VolumeObjectTO volume = snapshot.getVolume(); + final DataTO srcData = cmd.getSrcTO(); + final SnapshotObjectTO snapshot = (SnapshotObjectTO)srcData; + final DataTO destData = cmd.getDestTO(); + final PrimaryDataStoreTO pool = (PrimaryDataStoreTO)destData.getDataStore(); + final DataStoreTO imageStore = srcData.getDataStore(); + final VolumeObjectTO volume = snapshot.getVolume(); if (!(imageStore instanceof NfsTO)) { return new CopyCmdAnswer("unsupported protocol"); } - NfsTO nfsImageStore = (NfsTO)imageStore; + final NfsTO nfsImageStore = (NfsTO)imageStore; - String snapshotFullPath = snapshot.getPath(); - int index = snapshotFullPath.lastIndexOf("/"); - String snapshotPath = snapshotFullPath.substring(0, index); - String snapshotName = snapshotFullPath.substring(index + 1); - KVMStoragePool secondaryPool = storagePoolMgr.getStoragePoolByURI(nfsImageStore.getUrl() + File.separator + snapshotPath); - KVMPhysicalDisk snapshotDisk = secondaryPool.getPhysicalDisk(snapshotName); + final String snapshotFullPath = snapshot.getPath(); + final int index = snapshotFullPath.lastIndexOf("/"); + final String snapshotPath = snapshotFullPath.substring(0, index); + final String snapshotName = snapshotFullPath.substring(index + 1); + final KVMStoragePool secondaryPool = storagePoolMgr.getStoragePoolByURI(nfsImageStore.getUrl() + File.separator + snapshotPath); + final KVMPhysicalDisk snapshotDisk = secondaryPool.getPhysicalDisk(snapshotName); if (volume.getFormat() == ImageFormat.RAW) { snapshotDisk.setFormat(PhysicalDiskFormat.RAW); @@ -1249,34 +1247,34 @@ public class KVMStorageProcessor implements StorageProcessor { snapshotDisk.setFormat(PhysicalDiskFormat.QCOW2); } - String primaryUuid = pool.getUuid(); - KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(pool.getPoolType(), primaryUuid); - String volUuid = UUID.randomUUID().toString(); - KVMPhysicalDisk disk = storagePoolMgr.copyPhysicalDisk(snapshotDisk, volUuid, primaryPool, cmd.getWaitInMillSeconds()); - VolumeObjectTO newVol = new VolumeObjectTO(); + final String primaryUuid = pool.getUuid(); + final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(pool.getPoolType(), primaryUuid); + final String volUuid = UUID.randomUUID().toString(); + final KVMPhysicalDisk disk = storagePoolMgr.copyPhysicalDisk(snapshotDisk, volUuid, primaryPool, cmd.getWaitInMillSeconds()); + final VolumeObjectTO newVol = new VolumeObjectTO(); newVol.setPath(disk.getName()); newVol.setSize(disk.getVirtualSize()); newVol.setFormat(ImageFormat.valueOf(disk.getFormat().toString().toUpperCase())); return new CopyCmdAnswer(newVol); - } catch (CloudRuntimeException e) { + } catch (final CloudRuntimeException e) { s_logger.debug("Failed to createVolumeFromSnapshot: ", e); return new CopyCmdAnswer(e.toString()); } } @Override - public Answer deleteSnapshot(DeleteCommand cmd) { + public Answer deleteSnapshot(final DeleteCommand cmd) { return new Answer(cmd); } @Override - public Answer introduceObject(IntroduceObjectCmd cmd) { + public Answer introduceObject(final IntroduceObjectCmd cmd) { return new Answer(cmd, false, "not implememented yet"); } @Override - public Answer forgetObject(ForgetObjectCmd cmd) { + public Answer forgetObject(final ForgetObjectCmd cmd) { return new Answer(cmd, false, "not implememented yet"); }