Skip to content

Check Qcow2 version before using --bitmaps #10896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.cloudstack.storage.formatinspector.Qcow2Inspector;
import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang3.StringUtils;
import org.libvirt.LibvirtException;
Expand Down Expand Up @@ -53,6 +54,8 @@ public class QemuImg {
public static final long QEMU_2_10 = 2010000;
public static final long QEMU_5_10 = 5010000;

public static final int MIN_BITMAP_VERSION = 3;

/* The qemu-img binary. We expect this to be in $PATH */
public String _qemuImgPath = "qemu-img";
private String cloudQemuImgPath = "cloud-qemu-img";
Expand Down Expand Up @@ -466,7 +469,7 @@ public void convert(final QemuImgFile srcFile, final QemuImgFile destFile,
script.add(srcFile.getFileName());
}

if (this.version >= QEMU_5_10 && keepBitmaps) {
if (this.version >= QEMU_5_10 && keepBitmaps && Qcow2Inspector.validateQcow2Version(srcFile.getFileName(), MIN_BITMAP_VERSION)) {
script.add("--bitmaps");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
package org.apache.cloudstack.storage.formatinspector;

import com.cloud.utils.NumbersUtil;
import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -108,6 +110,32 @@
return result;
}


/**
* Validates if the file has a minimum version.
* @param filePath Path of the file to be validated.
* @param minVersion the minimum version that it should contain.
* @throws RuntimeException If a IOException is thrown.
* @return true if file version is >= minVersion, false otherwise.
*/
public static boolean validateQcow2Version(String filePath, int minVersion) {
try (InputStream inputStream = new FileInputStream(filePath)) {

Check warning on line 122 in services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/formatinspector/Qcow2Inspector.java

View check run for this annotation

Codecov / codecov/patch

services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/formatinspector/Qcow2Inspector.java#L121-L122

Added lines #L121 - L122 were not covered by tests
for (Qcow2HeaderField qcow2Header : Qcow2HeaderField.values()) {
if (qcow2Header != Qcow2HeaderField.VERSION) {
skipHeader(inputStream, qcow2Header, filePath);
continue;

Check warning on line 126 in services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/formatinspector/Qcow2Inspector.java

View check run for this annotation

Codecov / codecov/patch

services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/formatinspector/Qcow2Inspector.java#L125-L126

Added lines #L125 - L126 were not covered by tests
}

byte[] headerValue = readHeader(inputStream, qcow2Header, filePath);
int version = new BigInteger(headerValue).intValue();

Check warning on line 130 in services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/formatinspector/Qcow2Inspector.java

View check run for this annotation

Codecov / codecov/patch

services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/formatinspector/Qcow2Inspector.java#L129-L130

Added lines #L129 - L130 were not covered by tests
return version >= minVersion;
}
} catch (IOException ex) {
throw new CloudRuntimeException(String.format("Unable to validate file [%s] due to: ", filePath), ex);
}
return false;
}

Check warning on line 137 in services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/formatinspector/Qcow2Inspector.java

View check run for this annotation

Codecov / codecov/patch

services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/formatinspector/Qcow2Inspector.java#L133-L137

Added lines #L133 - L137 were not covered by tests

/**
* Skips the field's length in the InputStream.
* @param qcow2InputStream InputStream of the QCOW2 being unraveled.
Expand Down
Loading