Skip to content
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 @@ -20,6 +20,9 @@
package com.cloud.storage.template;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import javax.naming.ConfigurationException;
Expand All @@ -31,6 +34,8 @@
import com.cloud.utils.component.AdapterBase;

public class RawImageProcessor extends AdapterBase implements Processor {
private static final byte[] QCOW2_MAGIC = {'Q', 'F', 'I', (byte) 0xfb};

StorageLayer _storage;

@Override
Expand Down Expand Up @@ -60,6 +65,10 @@ public FormatInfo process(String templatePath, ImageFormat format, String templa
logger.debug("Unable to find raw image:" + imgPath);
return null;
}
if (isQcow2(imgPath)) {
throw new InternalErrorException("Template " + templateName + " was registered as RAW but the image is QCOW2, "
+ "register it again with format QCOW2");
}
FormatInfo info = new FormatInfo();
info.format = ImageFormat.RAW;
info.filename = templateName + "." + ImageFormat.RAW.getFileExtension();
Expand All @@ -74,4 +83,15 @@ public long getVirtualSize(File file) {
return file.length();
}

// Taking a QCOW2 file's length as its size makes block storage create a volume that is too small
boolean isQcow2(String path) {
byte[] magic = new byte[QCOW2_MAGIC.length];
try (FileInputStream in = new FileInputStream(path)) {
return in.read(magic) == magic.length && Arrays.equals(magic, QCOW2_MAGIC);
} catch (IOException e) {
logger.debug("Unable to read the header of " + path + ": " + e.getMessage());
return false;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.storage.template;

import java.io.File;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import com.cloud.exception.InternalErrorException;
import com.cloud.storage.StorageLayer;

@RunWith(MockitoJUnitRunner.class)
public class RawImageProcessorTest {

@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Mock
StorageLayer storage;

private RawImageProcessor processor;

@Before
public void setUp() throws Exception {
processor = new RawImageProcessor();
Map<String, Object> params = new HashMap<>();
params.put(StorageLayer.InstanceConfigKey, storage);
processor.configure("Raw Image Processor", params);
}

private File image(byte[] header) throws Exception {
File file = folder.newFile("template.raw");
byte[] content = new byte[4096];
System.arraycopy(header, 0, content, 0, header.length);
Files.write(file.toPath(), content);
Mockito.when(storage.exists(file.getPath())).thenReturn(true);
Mockito.lenient().when(storage.getSize(file.getPath())).thenReturn(file.length());
return file;
}

@Test
public void processAcceptsRawImage() throws Exception {
image(new byte[] {(byte) 0xeb, 0x63, (byte) 0x90});

Processor.FormatInfo info = processor.process(folder.getRoot().getPath(), null, "template");

Assert.assertEquals(4096L, info.virtualSize);
}

@Test(expected = InternalErrorException.class)
public void processRejectsQcow2RegisteredAsRaw() throws Exception {
image(new byte[] {'Q', 'F', 'I', (byte) 0xfb, 0, 0, 0, 3});

processor.process(folder.getRoot().getPath(), null, "template");
}
}
Loading