StorageFilerTO spool = cmd.getPool();
if ( currentSize == newSize) {
// nothing to do
s_logger.info("No need to resize volume: current size " + currentSize + " is same as new size " + newSize);
return new ResizeVolumeAnswer(cmd, true, "success", currentSize);
}
try {
KVMStoragePool pool = _storagePoolMgr.getStoragePool(spool.getType(), spool.getUuid());
KVMPhysicalDisk vol = pool.getPhysicalDisk(volid);
String path = vol.getPath();
String type = getResizeScriptType(pool, vol);
if (type == null) {
return new ResizeVolumeAnswer(cmd, false, "Unsupported volume format: pool type '" + pool.getType() + "' and volume format '" + vol.getFormat() + "'");
} else if (type.equals("QCOW2") && shrinkOk) {
return new ResizeVolumeAnswer(cmd, false, "Unable to shrink volumes of type " + type);
}
s_logger.debug("Resizing volume: " + path + "," + currentSize + "," + newSize + "," + type + "," + vmInstanceName + "," + shrinkOk);
/* libvirt doesn't support resizing (C)LVM devices, so we have to do that via a Bash script */
if (pool.getType() != StoragePoolType.CLVM) {
s_logger.debug("Volume " + path + " can be resized by libvirt. Asking libvirt to resize the volume.");
try {
Connect conn = LibvirtConnection.getConnection();
StorageVol v = conn.storageVolLookupByPath(path);
int flags = 0;
if (conn.getLibVirVersion() > 1001000 && vol.getFormat() == PhysicalDiskFormat.RAW) {
flags = 1;
}
if (shrinkOk) {
flags = 4;
}
v.resize(newSize, flags);
} catch (LibvirtException e) {
return new ResizeVolumeAnswer(cmd, false, e.toString());
}
} else {
s_logger.debug("Volume " + path + " is of the type LVM and can not be resized using libvirt. Invoking resize script.");
final Script resizecmd = new Script(_resizeVolumePath, _cmdsTimeout, s_logger);
resizecmd.add("-s", String.valueOf(newSize));
resizecmd.add("-c", String.valueOf(currentSize));
resizecmd.add("-p", path);
resizecmd.add("-t", type);
resizecmd.add("-r", String.valueOf(shrinkOk));
resizecmd.add("-v", vmInstanceName);
String result = resizecmd.execute();
if (result != null) {
return new ResizeVolumeAnswer(cmd, false, result);
}
}
/* fetch new size as seen from libvirt, don't want to assume anything */
pool = _storagePoolMgr.getStoragePool(spool.getType(), spool.getUuid());
long finalSize = pool.getPhysicalDisk(volid).getVirtualSize();
s_logger.debug("after resize, size reports as " + finalSize + ", requested " + newSize);
return new ResizeVolumeAnswer(cmd, true, "success", finalSize);
} catch (CloudRuntimeException e) {
String error = "failed to resize volume: " + e;
s_logger.debug(error);
return new ResizeVolumeAnswer(cmd, false, error);
}
}