Skip to content

Commit c9575e0

Browse files
committed
kvm: remove the partial RBD image when a template copy fails
When qemu-img convert into an RBD pool failed or timed out, the half-written image was left behind. Every later copy of the same template then failed with "rbd create: File exists", so deployments from that template broke on the pool until the image was removed by hand. - check whether the destination RBD image exists before converting, and refuse to overwrite it, as it may be a complete image used by clones - remove the image again if the copy created it and then failed - do not print the Ceph key in the "Failed to convert" error
1 parent dfff32e commit c9575e0

2 files changed

Lines changed: 88 additions & 3 deletions

File tree

‎plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java‎

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,58 @@ public boolean deletePhysicalDisk(String uuid, KVMStoragePool pool, Storage.Imag
13191319
}
13201320
}
13211321

1322+
static String maskRbdKey(String message) {
1323+
return message == null ? null : message.replaceAll("key=[^:\\s]*", "key=******");
1324+
}
1325+
1326+
private Rados connectToCeph(KVMStoragePool pool) throws RadosException {
1327+
Rados r = new Rados(pool.getAuthUserName());
1328+
r.confSet("mon_host", pool.getSourceHost() + ":" + pool.getSourcePort());
1329+
r.confSet("key", pool.getAuthSecret());
1330+
r.confSet("client_mount_timeout", "30");
1331+
r.connect();
1332+
return r;
1333+
}
1334+
1335+
private boolean rbdImageExists(KVMStoragePool pool, String imageName) throws RadosException, RbdException {
1336+
Rados r = connectToCeph(pool);
1337+
try {
1338+
IoCTX io = r.ioCtxCreate(pool.getSourceDir());
1339+
try {
1340+
return Arrays.asList(new Rbd(io).list()).contains(imageName);
1341+
} finally {
1342+
r.ioCtxDestroy(io);
1343+
}
1344+
} finally {
1345+
r.shutDown();
1346+
}
1347+
}
1348+
1349+
/**
1350+
* Removes an RBD image left behind by a failed copy, so that it does not make every later copy fail with "File exists"
1351+
*/
1352+
private void removeRbdImageQuietly(KVMStoragePool pool, String imageName) {
1353+
try {
1354+
Rados r = connectToCeph(pool);
1355+
try {
1356+
IoCTX io = r.ioCtxCreate(pool.getSourceDir());
1357+
try {
1358+
Rbd rbd = new Rbd(io);
1359+
if (Arrays.asList(rbd.list()).contains(imageName)) {
1360+
rbd.remove(imageName);
1361+
logger.info("Removed the incomplete RBD image {}/{} left by the failed copy", pool.getSourceDir(), imageName);
1362+
}
1363+
} finally {
1364+
r.ioCtxDestroy(io);
1365+
}
1366+
} finally {
1367+
r.shutDown();
1368+
}
1369+
} catch (RadosException | RbdException e) {
1370+
logger.warn("Failed to remove the incomplete RBD image {}/{} left by the failed copy, remove it manually: {}", pool.getSourceDir(), imageName, e.getMessage());
1371+
}
1372+
}
1373+
13221374
/**
13231375
* This function copies a physical disk from Secondary Storage to Primary Storage
13241376
* or from Primary to Primary Storage
@@ -1730,12 +1782,21 @@ to support snapshots(backuped) as qcow2 files. */
17301782
* To do so it's mandatory that librbd on the system is at least 0.67.7 (Ceph Dumpling)
17311783
*/
17321784
logger.debug("The source image is not RBD, but the destination is. We will convert into RBD format 2");
1785+
String rbdDestPath = destPool.getSourceDir() + "/" + name;
1786+
// Only an image created by this copy may be removed again when the copy fails
1787+
boolean destCreatedByCopy = false;
17331788
try {
17341789
srcFile = new QemuImgFile(sourcePath, sourceFormat);
1735-
String rbdDestPath = destPool.getSourceDir() + "/" + name;
17361790
String rbdDestFile = KVMPhysicalDisk.RBDStringBuilder(destPool, rbdDestPath);
17371791
destFile = new QemuImgFile(rbdDestFile, destFormat);
17381792

1793+
if (rbdImageExists(destPool, name)) {
1794+
// qemu-img can't convert into an existing image, and it can't be told if it is complete, so don't touch it
1795+
logger.error("RBD image {} already exists, it will not be overwritten by the copy of {}", rbdDestPath, srcFile.getFileName());
1796+
return null;
1797+
}
1798+
destCreatedByCopy = true;
1799+
17391800
logger.debug("Starting copy from source image " + srcFile.getFileName() + " to RBD image " + rbdDestPath);
17401801
qemu.convert(srcFile, destFile);
17411802
logger.debug("Successfully converted source image " + srcFile.getFileName() + " to RBD image " + rbdDestPath);
@@ -1761,15 +1822,23 @@ to support snapshots(backuped) as qcow2 files. */
17611822
r.ioCtxDestroy(io);
17621823
} catch (QemuImgException | LibvirtException e) {
17631824
String srcFilename = srcFile != null ? srcFile.getFileName() : null;
1764-
String destFilename = destFile != null ? destFile.getFileName() : null;
1765-
logger.error(String.format("Failed to convert from %s to %s the error was: %s", srcFilename, destFilename, e.getMessage()));
1825+
logger.error("Failed to convert from {} to RBD image {} the error was: {}", srcFilename, rbdDestPath, maskRbdKey(e.getMessage()));
17661826
newDisk = null;
1827+
if (destCreatedByCopy) {
1828+
removeRbdImageQuietly(destPool, name);
1829+
}
17671830
} catch (RadosException e) {
17681831
logger.error("A Ceph RADOS operation failed (" + e.getReturnValue() + "). The error was: " + e.getMessage());
17691832
newDisk = null;
1833+
if (destCreatedByCopy) {
1834+
removeRbdImageQuietly(destPool, name);
1835+
}
17701836
} catch (RbdException e) {
17711837
logger.error("A Ceph RBD operation failed (" + e.getReturnValue() + "). The error was: " + e.getMessage());
17721838
newDisk = null;
1839+
if (destCreatedByCopy) {
1840+
removeRbdImageQuietly(destPool, name);
1841+
}
17731842
}
17741843
} else {
17751844
/**

‎plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptorTest.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.UUID;
2828

2929
import org.junit.After;
30+
import org.junit.Assert;
3031
import org.junit.Before;
3132
import org.junit.Test;
3233
import org.junit.runner.RunWith;
@@ -176,4 +177,19 @@ public void testUpdateLocalPoolIops_NullResultFromScript() {
176177

177178
Mockito.verify(mockPool, never()).setUsedIops(anyLong());
178179
}
180+
181+
@Test
182+
public void maskRbdKeyHidesTheCephKey() {
183+
String message = "qemu-img: rbd:pool/image:mon_host=10.0.0.1\\:6789:auth_client_required=cephx:id=pool:key=AQCsecret==:rbd_default_format=2: error while converting raw: error rbd create: File exists";
184+
String masked = LibvirtStorageAdaptor.maskRbdKey(message);
185+
Assert.assertFalse(masked.contains("AQCsecret"));
186+
Assert.assertTrue(masked.contains("key=******:rbd_default_format=2"));
187+
Assert.assertTrue(masked.endsWith("File exists"));
188+
}
189+
190+
@Test
191+
public void maskRbdKeyKeepsMessagesWithoutKey() {
192+
Assert.assertNull(LibvirtStorageAdaptor.maskRbdKey(null));
193+
Assert.assertEquals("timeout", LibvirtStorageAdaptor.maskRbdKey("timeout"));
194+
}
179195
}

0 commit comments

Comments
 (0)