CLOUDSTACK-2972: due to change on master, using name instead of uuid to find vm name, so break object_store branch

This commit is contained in:
Edison Su 2013-06-14 17:35:53 -07:00
parent c2da4eac89
commit 619ec12f61

View File

@ -88,39 +88,35 @@ public class KVMStorageProcessor implements StorageProcessor {
private String _createTmplPath; private String _createTmplPath;
private String _manageSnapshotPath; private String _manageSnapshotPath;
private int _cmdsTimeout; private int _cmdsTimeout;
public KVMStorageProcessor(KVMStoragePoolManager storagePoolMgr, LibvirtComputingResource resource) { public KVMStorageProcessor(KVMStoragePoolManager storagePoolMgr, LibvirtComputingResource resource) {
this.storagePoolMgr = storagePoolMgr; this.storagePoolMgr = storagePoolMgr;
this.resource = resource; this.resource = resource;
} }
protected String getDefaultStorageScriptsDir() { protected String getDefaultStorageScriptsDir() {
return "scripts/storage/qcow2"; return "scripts/storage/qcow2";
} }
public boolean configure(String name, Map<String, Object> params) public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
throws ConfigurationException {
storageLayer = new JavaStorageLayer(); storageLayer = new JavaStorageLayer();
storageLayer.configure("StorageLayer", params); storageLayer.configure("StorageLayer", params);
String storageScriptsDir = (String) params.get("storage.scripts.dir"); String storageScriptsDir = (String) params.get("storage.scripts.dir");
if (storageScriptsDir == null) { if (storageScriptsDir == null) {
storageScriptsDir = getDefaultStorageScriptsDir(); storageScriptsDir = getDefaultStorageScriptsDir();
} }
_createTmplPath = Script _createTmplPath = Script.findScript(storageScriptsDir, "createtmplt.sh");
.findScript(storageScriptsDir, "createtmplt.sh");
if (_createTmplPath == null) { if (_createTmplPath == null) {
throw new ConfigurationException( throw new ConfigurationException("Unable to find the createtmplt.sh");
"Unable to find the createtmplt.sh");
} }
_manageSnapshotPath = Script.findScript(storageScriptsDir, _manageSnapshotPath = Script.findScript(storageScriptsDir, "managesnapshot.sh");
"managesnapshot.sh");
if (_manageSnapshotPath == null) { if (_manageSnapshotPath == null) {
throw new ConfigurationException( throw new ConfigurationException("Unable to find the managesnapshot.sh");
"Unable to find the managesnapshot.sh");
} }
String value = (String) params.get("cmds.timeout"); String value = (String) params.get("cmds.timeout");
_cmdsTimeout = NumbersUtil.parseInt(value, 7200) * 1000; _cmdsTimeout = NumbersUtil.parseInt(value, 7200) * 1000;
return true; return true;
@ -128,18 +124,18 @@ public class KVMStorageProcessor implements StorageProcessor {
@Override @Override
public Answer copyTemplateToPrimaryStorage(CopyCommand cmd) { public Answer copyTemplateToPrimaryStorage(CopyCommand cmd) {
DataTO srcData = cmd.getSrcTO(); DataTO srcData = cmd.getSrcTO();
DataTO destData = cmd.getDestTO(); DataTO destData = cmd.getDestTO();
TemplateObjectTO template = (TemplateObjectTO)srcData; TemplateObjectTO template = (TemplateObjectTO) srcData;
DataStoreTO imageStore = template.getDataStore(); DataStoreTO imageStore = template.getDataStore();
TemplateObjectTO volume = (TemplateObjectTO)destData; TemplateObjectTO volume = (TemplateObjectTO) destData;
PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)volume.getDataStore(); PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
if (!(imageStore instanceof NfsTO)) { if (!(imageStore instanceof NfsTO)) {
return new CopyCmdAnswer("unsupported protocol"); return new CopyCmdAnswer("unsupported protocol");
} }
NfsTO nfsImageStore = (NfsTO)imageStore; NfsTO nfsImageStore = (NfsTO) imageStore;
String tmplturl = nfsImageStore.getUrl() + File.separator + template.getPath(); String tmplturl = nfsImageStore.getUrl() + File.separator + template.getPath();
int index = tmplturl.lastIndexOf("/"); int index = tmplturl.lastIndexOf("/");
String mountpoint = tmplturl.substring(0, index); String mountpoint = tmplturl.substring(0, index);
@ -158,9 +154,8 @@ public class KVMStorageProcessor implements StorageProcessor {
secondaryPool.refresh(); secondaryPool.refresh();
List<KVMPhysicalDisk> disks = secondaryPool.listPhysicalDisks(); List<KVMPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
if (disks == null || disks.isEmpty()) { if (disks == null || disks.isEmpty()) {
return new PrimaryStorageDownloadAnswer( return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: "
"Failed to get volumes from pool: " + secondaryPool.getUuid());
+ secondaryPool.getUuid());
} }
for (KVMPhysicalDisk disk : disks) { for (KVMPhysicalDisk disk : disks) {
if (disk.getName().endsWith("qcow2")) { if (disk.getName().endsWith("qcow2")) {
@ -169,21 +164,19 @@ public class KVMStorageProcessor implements StorageProcessor {
} }
} }
if (tmplVol == null) { if (tmplVol == null) {
return new PrimaryStorageDownloadAnswer( return new PrimaryStorageDownloadAnswer("Failed to get template from pool: "
"Failed to get template from pool: " + secondaryPool.getUuid());
+ secondaryPool.getUuid());
} }
} else { } else {
tmplVol = secondaryPool.getPhysicalDisk(tmpltname); tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
} }
/* Copy volume to primary storage */ /* Copy volume to primary storage */
KVMStoragePool primaryPool = storagePoolMgr.getStoragePool( KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(),
primaryStore.getPoolType(),
primaryStore.getUuid()); primaryStore.getUuid());
KVMPhysicalDisk primaryVol = storagePoolMgr.copyPhysicalDisk( KVMPhysicalDisk primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(),
tmplVol, UUID.randomUUID().toString(), primaryPool); primaryPool);
TemplateObjectTO newTemplate = new TemplateObjectTO(); TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(primaryVol.getName()); newTemplate.setPath(primaryVol.getName());
@ -197,7 +190,7 @@ public class KVMStorageProcessor implements StorageProcessor {
} }
} }
} }
// this is much like PrimaryStorageDownloadCommand, but keeping it separate // this is much like PrimaryStorageDownloadCommand, but keeping it separate
private KVMPhysicalDisk templateToPrimaryDownload(String templateUrl, KVMStoragePool primaryPool) { private KVMPhysicalDisk templateToPrimaryDownload(String templateUrl, KVMStoragePool primaryPool) {
int index = templateUrl.lastIndexOf("/"); int index = templateUrl.lastIndexOf("/");
@ -235,10 +228,11 @@ public class KVMStorageProcessor implements StorageProcessor {
/* Copy volume to primary storage */ /* Copy volume to primary storage */
KVMPhysicalDisk primaryVol = storagePoolMgr.copyPhysicalDisk(templateVol, UUID.randomUUID().toString(), primaryPool); KVMPhysicalDisk primaryVol = storagePoolMgr.copyPhysicalDisk(templateVol, UUID.randomUUID().toString(),
primaryPool);
return primaryVol; return primaryVol;
} catch (CloudRuntimeException e) { } catch (CloudRuntimeException e) {
s_logger.error("Failed to download template to primary storage",e); s_logger.error("Failed to download template to primary storage", e);
return null; return null;
} finally { } finally {
if (secondaryPool != null) { if (secondaryPool != null) {
@ -249,38 +243,35 @@ public class KVMStorageProcessor implements StorageProcessor {
@Override @Override
public Answer cloneVolumeFromBaseTemplate(CopyCommand cmd) { public Answer cloneVolumeFromBaseTemplate(CopyCommand cmd) {
DataTO srcData = cmd.getSrcTO(); DataTO srcData = cmd.getSrcTO();
DataTO destData = cmd.getDestTO(); DataTO destData = cmd.getDestTO();
TemplateObjectTO template = (TemplateObjectTO)srcData; TemplateObjectTO template = (TemplateObjectTO) srcData;
DataStoreTO imageStore = template.getDataStore(); DataStoreTO imageStore = template.getDataStore();
VolumeObjectTO volume = (VolumeObjectTO)destData; VolumeObjectTO volume = (VolumeObjectTO) destData;
PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)volume.getDataStore(); PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
KVMPhysicalDisk BaseVol = null; KVMPhysicalDisk BaseVol = null;
KVMStoragePool primaryPool = null; KVMStoragePool primaryPool = null;
KVMPhysicalDisk vol = null; KVMPhysicalDisk vol = null;
try { try {
primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
primaryStore.getUuid());
String templatePath = template.getPath(); String templatePath = template.getPath();
if(primaryPool.getType() == StoragePoolType.CLVM) { if (primaryPool.getType() == StoragePoolType.CLVM) {
vol = templateToPrimaryDownload(templatePath, primaryPool); vol = templateToPrimaryDownload(templatePath, primaryPool);
} else { } else {
BaseVol = primaryPool.getPhysicalDisk(templatePath); BaseVol = primaryPool.getPhysicalDisk(templatePath);
vol = storagePoolMgr.createDiskFromTemplate(BaseVol, UUID vol = storagePoolMgr.createDiskFromTemplate(BaseVol, UUID.randomUUID().toString(), primaryPool);
.randomUUID().toString(), primaryPool);
} }
if (vol == null) { if (vol == null) {
return new CopyCmdAnswer( return new CopyCmdAnswer(" Can't create storage volume on storage pool");
" Can't create storage volume on storage pool");
} }
VolumeObjectTO newVol = new VolumeObjectTO(); VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(vol.getName()); newVol.setPath(vol.getName());
newVol.setSize(vol.getSize()); newVol.setSize(vol.getSize());
return new CopyCmdAnswer(newVol); return new CopyCmdAnswer(newVol);
} catch (CloudRuntimeException e) { } catch (CloudRuntimeException e) {
s_logger.debug("Failed to create volume: " + e.toString()); s_logger.debug("Failed to create volume: " + e.toString());
@ -301,45 +292,39 @@ public class KVMStorageProcessor implements StorageProcessor {
@Override @Override
public Answer createTemplateFromVolume(CopyCommand cmd) { public Answer createTemplateFromVolume(CopyCommand cmd) {
DataTO srcData = cmd.getSrcTO(); DataTO srcData = cmd.getSrcTO();
DataTO destData = cmd.getDestTO(); DataTO destData = cmd.getDestTO();
int wait = cmd.getWait(); int wait = cmd.getWait();
TemplateObjectTO template = (TemplateObjectTO)destData; TemplateObjectTO template = (TemplateObjectTO) destData;
DataStoreTO imageStore = template.getDataStore(); DataStoreTO imageStore = template.getDataStore();
VolumeObjectTO volume = (VolumeObjectTO)srcData; VolumeObjectTO volume = (VolumeObjectTO) srcData;
PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)volume.getDataStore(); PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
if (!(imageStore instanceof NfsTO)) { if (!(imageStore instanceof NfsTO)) {
return new CopyCmdAnswer("unsupported protocol"); return new CopyCmdAnswer("unsupported protocol");
} }
NfsTO nfsImageStore = (NfsTO)imageStore; NfsTO nfsImageStore = (NfsTO) imageStore;
KVMStoragePool secondaryStorage = null; KVMStoragePool secondaryStorage = null;
KVMStoragePool primary = null; KVMStoragePool primary = null;
try { try {
String templateFolder = template.getPath(); String templateFolder = template.getPath();
secondaryStorage = storagePoolMgr.getStoragePoolByURI( secondaryStorage = storagePoolMgr.getStoragePoolByURI(nfsImageStore.getUrl());
nfsImageStore.getUrl());
try { try {
primary = storagePoolMgr.getStoragePool( primary = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
primaryStore.getPoolType(),
primaryStore.getUuid());
} catch (CloudRuntimeException e) { } catch (CloudRuntimeException e) {
if (e.getMessage().contains("not found")) { if (e.getMessage().contains("not found")) {
primary = storagePoolMgr.createStoragePool(primaryStore.getUuid(), primary = storagePoolMgr.createStoragePool(primaryStore.getUuid(), primaryStore.getHost(),
primaryStore.getHost(), primaryStore.getPort(), primaryStore.getPort(), primaryStore.getPath(), null, primaryStore.getPoolType());
primaryStore.getPath(), null,
primaryStore.getPoolType());
} else { } else {
return new CopyCmdAnswer(e.getMessage()); return new CopyCmdAnswer(e.getMessage());
} }
} }
KVMPhysicalDisk disk = primary.getPhysicalDisk(volume.getPath()); KVMPhysicalDisk disk = primary.getPhysicalDisk(volume.getPath());
String tmpltPath = secondaryStorage.getLocalPath() + File.separator String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateFolder;
+ templateFolder;
this.storageLayer.mkdirs(tmpltPath); this.storageLayer.mkdirs(tmpltPath);
String templateName = UUID.randomUUID().toString(); String templateName = UUID.randomUUID().toString();
@ -359,10 +344,7 @@ public class KVMStorageProcessor implements StorageProcessor {
s_logger.debug("Converting RBD disk " + disk.getPath() + " into template " + templateName); s_logger.debug("Converting RBD disk " + disk.getPath() + " into template " + templateName);
QemuImgFile srcFile = new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(primary.getSourceHost(), QemuImgFile srcFile = new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(primary.getSourceHost(),
primary.getSourcePort(), primary.getSourcePort(), primary.getAuthUserName(), primary.getAuthSecret(), disk.getPath()));
primary.getAuthUserName(),
primary.getAuthSecret(),
disk.getPath()));
srcFile.setFormat(PhysicalDiskFormat.RAW); srcFile.setFormat(PhysicalDiskFormat.RAW);
QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + templateName + ".qcow2"); QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + templateName + ".qcow2");
@ -372,8 +354,8 @@ public class KVMStorageProcessor implements StorageProcessor {
try { try {
q.convert(srcFile, destFile); q.convert(srcFile, destFile);
} catch (QemuImgException e) { } catch (QemuImgException e) {
s_logger.error("Failed to create new template while converting " s_logger.error("Failed to create new template while converting " + srcFile.getFileName() + " to "
+ srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage()); + destFile.getFileName() + " the error was: " + e.getMessage());
} }
File templateProp = new File(tmpltPath + "/template.properties"); File templateProp = new File(tmpltPath + "/template.properties");
@ -399,8 +381,7 @@ public class KVMStorageProcessor implements StorageProcessor {
qcow2Processor.configure("QCOW2 Processor", params); qcow2Processor.configure("QCOW2 Processor", params);
FormatInfo info = qcow2Processor.process(tmpltPath, null, FormatInfo info = qcow2Processor.process(tmpltPath, null, templateName);
templateName);
TemplateLocation loc = new TemplateLocation(this.storageLayer, tmpltPath); TemplateLocation loc = new TemplateLocation(this.storageLayer, tmpltPath);
loc.create(1, true, templateName); loc.create(1, true, templateName);
@ -413,8 +394,8 @@ public class KVMStorageProcessor implements StorageProcessor {
newTemplate.setFormat(ImageFormat.QCOW2); newTemplate.setFormat(ImageFormat.QCOW2);
return new CopyCmdAnswer(newTemplate); return new CopyCmdAnswer(newTemplate);
} catch (Exception e) { } catch (Exception e) {
s_logger.debug("Failed to create template from volume: " + e.toString()); s_logger.debug("Failed to create template from volume: " + e.toString());
return new CopyCmdAnswer(e.toString()); return new CopyCmdAnswer(e.toString());
} finally { } finally {
if (secondaryStorage != null) { if (secondaryStorage != null) {
secondaryStorage.delete(); secondaryStorage.delete();
@ -424,23 +405,23 @@ public class KVMStorageProcessor implements StorageProcessor {
@Override @Override
public Answer backupSnasphot(CopyCommand cmd) { public Answer backupSnasphot(CopyCommand cmd) {
DataTO srcData = cmd.getSrcTO(); DataTO srcData = cmd.getSrcTO();
DataTO destData = cmd.getDestTO(); DataTO destData = cmd.getDestTO();
int wait = cmd.getWait(); int wait = cmd.getWait();
SnapshotObjectTO snapshot = (SnapshotObjectTO)srcData; SnapshotObjectTO snapshot = (SnapshotObjectTO) srcData;
PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)snapshot.getDataStore(); PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) snapshot.getDataStore();
SnapshotObjectTO destSnapshot = (SnapshotObjectTO)destData; SnapshotObjectTO destSnapshot = (SnapshotObjectTO) destData;
DataStoreTO imageStore = destData.getDataStore(); DataStoreTO imageStore = destData.getDataStore();
if (!(imageStore instanceof NfsTO)) { if (!(imageStore instanceof NfsTO)) {
return new CopyCmdAnswer("unsupported protocol"); return new CopyCmdAnswer("unsupported protocol");
} }
NfsTO nfsImageStore = (NfsTO)imageStore; NfsTO nfsImageStore = (NfsTO) imageStore;
String secondaryStoragePoolUrl = nfsImageStore.getUrl(); String secondaryStoragePoolUrl = nfsImageStore.getUrl();
//NOTE: snapshot name is encoded in snapshot path // NOTE: snapshot name is encoded in snapshot path
int index = snapshot.getPath().lastIndexOf("/"); int index = snapshot.getPath().lastIndexOf("/");
String snapshotName = snapshot.getPath().substring(index + 1); String snapshotName = snapshot.getPath().substring(index + 1);
String volumePath = snapshot.getVolume().getPath(); String volumePath = snapshot.getVolume().getPath();
String snapshotDestPath = null; String snapshotDestPath = null;
@ -450,19 +431,16 @@ public class KVMStorageProcessor implements StorageProcessor {
try { try {
Connect conn = LibvirtConnection.getConnectionByVmName(vmName); Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
secondaryStoragePool = storagePoolMgr.getStoragePoolByURI( secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
secondaryStoragePoolUrl);
String ssPmountPath = secondaryStoragePool.getLocalPath(); String ssPmountPath = secondaryStoragePool.getLocalPath();
snapshotRelPath = destSnapshot.getPath(); snapshotRelPath = destSnapshot.getPath();
snapshotDestPath = ssPmountPath + File.separator + snapshotRelPath; snapshotDestPath = ssPmountPath + File.separator + snapshotRelPath;
KVMStoragePool primaryPool = storagePoolMgr.getStoragePool( KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(),
primaryStore.getPoolType(),
primaryStore.getUuid()); primaryStore.getUuid());
KVMPhysicalDisk snapshotDisk = primaryPool.getPhysicalDisk(volumePath); KVMPhysicalDisk snapshotDisk = primaryPool.getPhysicalDisk(volumePath);
Script command = new Script(_manageSnapshotPath, _cmdsTimeout, Script command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
s_logger);
command.add("-b", snapshotDisk.getPath()); command.add("-b", snapshotDisk.getPath());
command.add("-n", snapshotName); command.add("-n", snapshotName);
command.add("-p", snapshotDestPath); command.add("-p", snapshotDestPath);
@ -485,11 +463,9 @@ public class KVMStorageProcessor implements StorageProcessor {
} }
} }
KVMStoragePool primaryStorage = storagePoolMgr.getStoragePool( KVMStoragePool primaryStorage = storagePoolMgr.getStoragePool(primaryStore.getPoolType(),
primaryStore.getPoolType(),
primaryStore.getUuid()); primaryStore.getUuid());
if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryStorage.isExternalSnapshot()) {
&& !primaryStorage.isExternalSnapshot()) {
DomainSnapshot snap = vm.snapshotLookupByName(snapshotName); DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
snap.delete(0); snap.delete(0);
@ -503,21 +479,18 @@ public class KVMStorageProcessor implements StorageProcessor {
vm.resume(); vm.resume();
} }
} else { } else {
command = new Script(_manageSnapshotPath, _cmdsTimeout, command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
s_logger);
command.add("-d", snapshotDisk.getPath()); command.add("-d", snapshotDisk.getPath());
command.add("-n", snapshotName); command.add("-n", snapshotName);
result = command.execute(); result = command.execute();
if (result != null) { if (result != null) {
s_logger.debug("Failed to backup snapshot: " + result); s_logger.debug("Failed to backup snapshot: " + result);
return new CopyCmdAnswer( return new CopyCmdAnswer("Failed to backup snapshot: " + result);
"Failed to backup snapshot: " + result);
} }
} }
SnapshotObjectTO newSnapshot = new SnapshotObjectTO(); SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
newSnapshot.setPath(snapshotRelPath newSnapshot.setPath(snapshotRelPath + File.separator + snapshotName);
+ File.separator + snapshotName);
return new CopyCmdAnswer(newSnapshot); return new CopyCmdAnswer(newSnapshot);
} catch (LibvirtException e) { } catch (LibvirtException e) {
s_logger.debug("Failed to backup snapshot: " + e.toString()); s_logger.debug("Failed to backup snapshot: " + e.toString());
@ -532,16 +505,14 @@ public class KVMStorageProcessor implements StorageProcessor {
} }
} }
protected synchronized String attachOrDetachISO(Connect conn, protected synchronized String attachOrDetachISO(Connect conn, String vmName, String isoPath, boolean isAttach)
String vmName, String isoPath, boolean isAttach) throws LibvirtException, URISyntaxException, InternalErrorException {
throws LibvirtException, URISyntaxException, InternalErrorException {
String isoXml = null; String isoXml = null;
if (isoPath != null && isAttach) { if (isoPath != null && isAttach) {
int index = isoPath.lastIndexOf("/"); int index = isoPath.lastIndexOf("/");
String path = isoPath.substring(0, index); String path = isoPath.substring(0, index);
String name = isoPath.substring(index + 1); String name = isoPath.substring(index + 1);
KVMStoragePool secondaryPool = storagePoolMgr.getStoragePoolByURI( KVMStoragePool secondaryPool = storagePoolMgr.getStoragePoolByURI(path);
path);
KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name); KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
isoPath = isoVol.getPath(); isoPath = isoVol.getPath();
@ -566,19 +537,19 @@ public class KVMStorageProcessor implements StorageProcessor {
} }
return result; return result;
} }
@Override @Override
public Answer attachIso(AttachCommand cmd) { public Answer attachIso(AttachCommand cmd) {
DiskTO disk = cmd.getDisk(); DiskTO disk = cmd.getDisk();
TemplateObjectTO isoTO = (TemplateObjectTO)disk.getData(); TemplateObjectTO isoTO = (TemplateObjectTO) disk.getData();
DataStoreTO store = isoTO.getDataStore(); DataStoreTO store = isoTO.getDataStore();
if (!(store instanceof NfsTO)) { if (!(store instanceof NfsTO)) {
return new AttachAnswer("unsupported protocol"); return new AttachAnswer("unsupported protocol");
} }
NfsTO nfsStore = (NfsTO)store; NfsTO nfsStore = (NfsTO) store;
try { try {
Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName()); Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName());
attachOrDetachISO(conn, cmd.getVmName(), nfsStore.getUrl() + File.separator + isoTO.getPath(), attachOrDetachISO(conn, cmd.getVmName(), nfsStore.getUrl() + File.separator + isoTO.getPath(), true);
true);
} catch (LibvirtException e) { } catch (LibvirtException e) {
return new Answer(cmd, false, e.toString()); return new Answer(cmd, false, e.toString());
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
@ -589,14 +560,12 @@ public class KVMStorageProcessor implements StorageProcessor {
return new Answer(cmd); return new Answer(cmd);
} }
protected synchronized String attachOrDetachDevice(Connect conn, protected synchronized String attachOrDetachDevice(Connect conn, boolean attach, String vmName, String xml)
boolean attach, String vmName, String xml) throws LibvirtException, throws LibvirtException, InternalErrorException {
InternalErrorException {
Domain dm = null; Domain dm = null;
try { try {
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes((vmName dm = conn.domainLookupByName(vmName);
.getBytes())));
if (attach) { if (attach) {
s_logger.debug("Attaching device: " + xml); s_logger.debug("Attaching device: " + xml);
@ -607,11 +576,9 @@ public class KVMStorageProcessor implements StorageProcessor {
} }
} catch (LibvirtException e) { } catch (LibvirtException e) {
if (attach) { if (attach) {
s_logger.warn("Failed to attach device to " + vmName + ": " s_logger.warn("Failed to attach device to " + vmName + ": " + e.getMessage());
+ e.getMessage());
} else { } else {
s_logger.warn("Failed to detach device from " + vmName + ": " s_logger.warn("Failed to detach device from " + vmName + ": " + e.getMessage());
+ e.getMessage());
} }
throw e; throw e;
} finally { } finally {
@ -626,17 +593,15 @@ public class KVMStorageProcessor implements StorageProcessor {
return null; return null;
} }
protected synchronized String attachOrDetachDisk(Connect conn, protected synchronized String attachOrDetachDisk(Connect conn, boolean attach, String vmName,
boolean attach, String vmName, KVMPhysicalDisk attachingDisk, KVMPhysicalDisk attachingDisk, int devId) throws LibvirtException, InternalErrorException {
int devId) throws LibvirtException, InternalErrorException {
List<DiskDef> disks = null; List<DiskDef> disks = null;
Domain dm = null; Domain dm = null;
DiskDef diskdef = null; DiskDef diskdef = null;
try { try {
if (!attach) { if (!attach) {
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName dm = conn.domainLookupByName(vmName);
.getBytes()));
LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser(); LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
String xml = dm.getXMLDesc(0); String xml = dm.getXMLDesc(0);
parser.parseDomainXML(xml); parser.parseDomainXML(xml);
@ -644,25 +609,21 @@ public class KVMStorageProcessor implements StorageProcessor {
for (DiskDef disk : disks) { for (DiskDef disk : disks) {
String file = disk.getDiskPath(); String file = disk.getDiskPath();
if (file != null if (file != null && file.equalsIgnoreCase(attachingDisk.getPath())) {
&& file.equalsIgnoreCase(attachingDisk.getPath())) {
diskdef = disk; diskdef = disk;
break; break;
} }
} }
if (diskdef == null) { if (diskdef == null) {
throw new InternalErrorException("disk: " throw new InternalErrorException("disk: " + attachingDisk.getPath() + " is not attached before");
+ attachingDisk.getPath()
+ " is not attached before");
} }
} else { } else {
diskdef = new DiskDef(); diskdef = new DiskDef();
if (attachingDisk.getFormat() == PhysicalDiskFormat.QCOW2) { if (attachingDisk.getFormat() == PhysicalDiskFormat.QCOW2) {
diskdef.defFileBasedDisk(attachingDisk.getPath(), devId, diskdef.defFileBasedDisk(attachingDisk.getPath(), devId, DiskDef.diskBus.VIRTIO,
DiskDef.diskBus.VIRTIO, DiskDef.diskFmtType.QCOW2); DiskDef.diskFmtType.QCOW2);
} else if (attachingDisk.getFormat() == PhysicalDiskFormat.RAW) { } else if (attachingDisk.getFormat() == PhysicalDiskFormat.RAW) {
diskdef.defBlockBasedDisk(attachingDisk.getPath(), devId, diskdef.defBlockBasedDisk(attachingDisk.getPath(), devId, DiskDef.diskBus.VIRTIO);
DiskDef.diskBus.VIRTIO);
} }
} }
@ -677,20 +638,17 @@ public class KVMStorageProcessor implements StorageProcessor {
@Override @Override
public Answer attachVolume(AttachCommand cmd) { public Answer attachVolume(AttachCommand cmd) {
DiskTO disk = cmd.getDisk(); DiskTO disk = cmd.getDisk();
VolumeObjectTO vol = (VolumeObjectTO)disk.getData(); VolumeObjectTO vol = (VolumeObjectTO) disk.getData();
PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)vol.getDataStore(); PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) vol.getDataStore();
String vmName = cmd.getVmName(); String vmName = cmd.getVmName();
try { try {
Connect conn = LibvirtConnection.getConnectionByVmName(vmName); Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
KVMStoragePool primary = storagePoolMgr.getStoragePool( KVMStoragePool primary = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
primaryStore.getPoolType(),
primaryStore.getUuid());
KVMPhysicalDisk phyDisk = primary.getPhysicalDisk(vol.getPath()); KVMPhysicalDisk phyDisk = primary.getPhysicalDisk(vol.getPath());
attachOrDetachDisk(conn, true, vmName, phyDisk, attachOrDetachDisk(conn, true, vmName, phyDisk, disk.getDiskSeq().intValue());
disk.getDiskSeq().intValue());
return new AttachAnswer(disk); return new AttachAnswer(disk);
} catch (LibvirtException e) { } catch (LibvirtException e) {
s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to " + e.toString()); s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to " + e.toString());
@ -709,20 +667,17 @@ public class KVMStorageProcessor implements StorageProcessor {
@Override @Override
public Answer dettachVolume(DettachCommand cmd) { public Answer dettachVolume(DettachCommand cmd) {
DiskTO disk = cmd.getDisk(); DiskTO disk = cmd.getDisk();
VolumeObjectTO vol = (VolumeObjectTO)disk.getData(); VolumeObjectTO vol = (VolumeObjectTO) disk.getData();
PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)vol.getDataStore(); PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) vol.getDataStore();
String vmName = cmd.getVmName(); String vmName = cmd.getVmName();
try { try {
Connect conn = LibvirtConnection.getConnectionByVmName(vmName); Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
KVMStoragePool primary = storagePoolMgr.getStoragePool( KVMStoragePool primary = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
primaryStore.getPoolType(),
primaryStore.getUuid());
KVMPhysicalDisk phyDisk = primary.getPhysicalDisk(vol.getPath()); KVMPhysicalDisk phyDisk = primary.getPhysicalDisk(vol.getPath());
attachOrDetachDisk(conn, false, vmName, phyDisk, attachOrDetachDisk(conn, false, vmName, phyDisk, disk.getDiskSeq().intValue());
disk.getDiskSeq().intValue());
return new DettachAnswer(disk); return new DettachAnswer(disk);
} catch (LibvirtException e) { } catch (LibvirtException e) {
s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to " + e.toString()); s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to " + e.toString());
@ -735,39 +690,35 @@ public class KVMStorageProcessor implements StorageProcessor {
@Override @Override
public Answer createVolume(CreateObjectCommand cmd) { public Answer createVolume(CreateObjectCommand cmd) {
VolumeObjectTO volume = (VolumeObjectTO)cmd.getData(); VolumeObjectTO volume = (VolumeObjectTO) cmd.getData();
PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)volume.getDataStore(); PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
KVMStoragePool primaryPool = null; KVMStoragePool primaryPool = null;
KVMPhysicalDisk vol = null; KVMPhysicalDisk vol = null;
long disksize; long disksize;
try { try {
primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
primaryStore.getUuid());
disksize = volume.getSize(); disksize = volume.getSize();
vol = primaryPool.createPhysicalDisk(UUID.randomUUID() vol = primaryPool.createPhysicalDisk(UUID.randomUUID().toString(), disksize);
.toString(), disksize);
VolumeObjectTO newVol = new VolumeObjectTO(); VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(vol.getName()); newVol.setPath(vol.getName());
return new CreateObjectAnswer(newVol); return new CreateObjectAnswer(newVol);
} catch (Exception e) { } catch (Exception e) {
s_logger.debug("Failed to create volume: " + e.toString()); s_logger.debug("Failed to create volume: " + e.toString());
return new CreateObjectAnswer(e.toString()); return new CreateObjectAnswer(e.toString());
} }
} }
protected static MessageFormat SnapshotXML = new MessageFormat( protected static MessageFormat SnapshotXML = new MessageFormat(" <domainsnapshot>" + " <name>{0}</name>"
" <domainsnapshot>" + " <name>{0}</name>" + " <domain>" + " <domain>" + " <uuid>{1}</uuid>" + " </domain>" + " </domainsnapshot>");
+ " <uuid>{1}</uuid>" + " </domain>"
+ " </domainsnapshot>");
@Override @Override
public Answer createSnapshot(CreateObjectCommand cmd) { public Answer createSnapshot(CreateObjectCommand cmd) {
SnapshotObjectTO snapshotTO = (SnapshotObjectTO)cmd.getData(); SnapshotObjectTO snapshotTO = (SnapshotObjectTO) cmd.getData();
PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)snapshotTO.getDataStore(); PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) snapshotTO.getDataStore();
VolumeObjectTO volume = snapshotTO.getVolume(); VolumeObjectTO volume = snapshotTO.getVolume();
String snapshotName = UUID.randomUUID().toString(); String snapshotName = UUID.randomUUID().toString();
String vmName = volume.getVmName(); String vmName = volume.getVmName();
@ -784,24 +735,21 @@ public class KVMStorageProcessor implements StorageProcessor {
} }
} }
KVMStoragePool primaryPool = storagePoolMgr.getStoragePool( KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(),
primaryStore.getPoolType(),
primaryStore.getUuid()); primaryStore.getUuid());
if (primaryPool.getType() == StoragePoolType.RBD) { if (primaryPool.getType() == StoragePoolType.RBD) {
s_logger.debug("Snapshots are not supported on RBD volumes"); s_logger.debug("Snapshots are not supported on RBD volumes");
return new CreateObjectAnswer( return new CreateObjectAnswer("Snapshots are not supported on RBD volumes");
"Snapshots are not supported on RBD volumes");
} }
KVMPhysicalDisk disk = primaryPool.getPhysicalDisk(volume.getPath()); KVMPhysicalDisk disk = primaryPool.getPhysicalDisk(volume.getPath());
if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryPool.isExternalSnapshot()) {
&& !primaryPool.isExternalSnapshot()) {
String vmUuid = vm.getUUIDString(); String vmUuid = vm.getUUIDString();
Object[] args = new Object[] { snapshotName, vmUuid }; Object[] args = new Object[] { snapshotName, vmUuid };
String snapshot = SnapshotXML.format(args); String snapshot = SnapshotXML.format(args);
s_logger.debug(snapshot); s_logger.debug(snapshot);
vm.snapshotCreateXML(snapshot); vm.snapshotCreateXML(snapshot);
/* /*
* libvirt on RHEL6 doesn't handle resume event emitted from * libvirt on RHEL6 doesn't handle resume event emitted from
@ -815,40 +763,35 @@ public class KVMStorageProcessor implements StorageProcessor {
} else { } else {
/* VM is not running, create a snapshot by ourself */ /* VM is not running, create a snapshot by ourself */
final Script command = new Script(_manageSnapshotPath, final Script command = new Script(_manageSnapshotPath, this._cmdsTimeout, s_logger);
this._cmdsTimeout, s_logger);
command.add("-c", disk.getPath()); command.add("-c", disk.getPath());
command.add("-n", snapshotName); command.add("-n", snapshotName);
String result = command.execute(); String result = command.execute();
if (result != null) { if (result != null) {
s_logger.debug("Failed to manage snapshot: " + result); s_logger.debug("Failed to manage snapshot: " + result);
return new CreateObjectAnswer( return new CreateObjectAnswer("Failed to manage snapshot: " + result);
"Failed to manage snapshot: " + result);
} }
} }
SnapshotObjectTO newSnapshot = new SnapshotObjectTO(); SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
//NOTE: sort of hack, we'd better just put snapshtoName // NOTE: sort of hack, we'd better just put snapshtoName
newSnapshot.setPath(disk.getPath() + File.separator + snapshotName); newSnapshot.setPath(disk.getPath() + File.separator + snapshotName);
return new CreateObjectAnswer(newSnapshot); return new CreateObjectAnswer(newSnapshot);
} catch (LibvirtException e) { } catch (LibvirtException e) {
s_logger.debug("Failed to manage snapshot: " + e.toString()); s_logger.debug("Failed to manage snapshot: " + e.toString());
return new CreateObjectAnswer( return new CreateObjectAnswer("Failed to manage snapshot: " + e.toString());
"Failed to manage snapshot: " + e.toString());
} }
} }
@Override @Override
public Answer deleteVolume(DeleteCommand cmd) { public Answer deleteVolume(DeleteCommand cmd) {
VolumeObjectTO vol = (VolumeObjectTO)cmd.getData(); VolumeObjectTO vol = (VolumeObjectTO) cmd.getData();
PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)vol.getDataStore(); PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) vol.getDataStore();
try { try {
KVMStoragePool pool = storagePoolMgr.getStoragePool( KVMStoragePool pool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
primaryStore.getPoolType(),
primaryStore.getUuid());
try { try {
pool.getPhysicalDisk(vol.getPath()); pool.getPhysicalDisk(vol.getPath());
} catch(Exception e) { } catch (Exception e) {
s_logger.debug("can't find volume: " + vol.getPath() + ", return true"); s_logger.debug("can't find volume: " + vol.getPath() + ", return true");
return new Answer(null); return new Answer(null);
} }
@ -862,48 +805,42 @@ public class KVMStorageProcessor implements StorageProcessor {
@Override @Override
public Answer createVolumeFromSnapshot(CopyCommand cmd) { public Answer createVolumeFromSnapshot(CopyCommand cmd) {
try { try {
DataTO srcData = cmd.getSrcTO(); DataTO srcData = cmd.getSrcTO();
SnapshotObjectTO snapshot = (SnapshotObjectTO)srcData; SnapshotObjectTO snapshot = (SnapshotObjectTO) srcData;
DataTO destData = cmd.getDestTO(); DataTO destData = cmd.getDestTO();
PrimaryDataStoreTO pool = (PrimaryDataStoreTO)destData.getDataStore(); PrimaryDataStoreTO pool = (PrimaryDataStoreTO) destData.getDataStore();
DataStoreTO imageStore = srcData.getDataStore(); DataStoreTO imageStore = srcData.getDataStore();
if (!(imageStore instanceof NfsTO)) {
return new CopyCmdAnswer("unsupported protocol");
}
if (!(imageStore instanceof NfsTO)) { NfsTO nfsImageStore = (NfsTO) imageStore;
return new CopyCmdAnswer("unsupported protocol");
}
NfsTO nfsImageStore = (NfsTO)imageStore; String snapshotPath = snapshot.getPath();
int index = snapshotPath.lastIndexOf("/");
snapshotPath = snapshotPath.substring(0, index);
String snapshotName = snapshotPath.substring(index + 1);
KVMStoragePool secondaryPool = storagePoolMgr.getStoragePoolByURI(nfsImageStore.getUrl() + File.separator
+ snapshotPath);
KVMPhysicalDisk snapshotDisk = secondaryPool.getPhysicalDisk(snapshotName);
String snapshotPath = snapshot.getPath(); String primaryUuid = pool.getUuid();
int index = snapshotPath.lastIndexOf("/"); KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(pool.getPoolType(), primaryUuid);
snapshotPath = snapshotPath.substring(0, index); String volUuid = UUID.randomUUID().toString();
String snapshotName = snapshotPath.substring(index + 1); KVMPhysicalDisk disk = storagePoolMgr.copyPhysicalDisk(snapshotDisk, volUuid, primaryPool);
KVMStoragePool secondaryPool = storagePoolMgr.getStoragePoolByURI( VolumeObjectTO newVol = new VolumeObjectTO();
nfsImageStore.getUrl() + File.separator + snapshotPath); newVol.setPath(disk.getName());
KVMPhysicalDisk snapshotDisk = secondaryPool.getPhysicalDisk(snapshotName); newVol.setSize(disk.getVirtualSize());
return new CopyCmdAnswer(newVol);
String primaryUuid = pool.getUuid(); } catch (CloudRuntimeException e) {
KVMStoragePool primaryPool = storagePoolMgr return new CopyCmdAnswer(e.toString());
.getStoragePool(pool.getPoolType(), }
primaryUuid);
String volUuid = UUID.randomUUID().toString();
KVMPhysicalDisk disk = storagePoolMgr.copyPhysicalDisk(snapshotDisk,
volUuid, primaryPool);
VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(disk.getName());
newVol.setSize(disk.getVirtualSize());
return new CopyCmdAnswer(
newVol);
} catch (CloudRuntimeException e) {
return new CopyCmdAnswer(e.toString()
);
}
} }
@Override @Override
public Answer deleteSnapshot(DeleteCommand cmd) { public Answer deleteSnapshot(DeleteCommand cmd) {
return new Answer(cmd); return new Answer(cmd);
} }
} }