Skip to content
Merged
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 @@ -83,7 +83,7 @@ public boolean parseDomainXML(String domXML) {
String protocol = getAttrValue("source", "protocol", disk);
String authUserName = getAttrValue("auth", "username", disk);
String poolUuid = getAttrValue("secret", "uuid", disk);
String host = getAttrValue("host", "name", disk);
String host = LibvirtStoragePoolXMLParser.getStorageHosts(disk);
int port = 0;
String xmlPort = getAttrValue("host", "port", disk);
if (StringUtils.isNotBlank(xmlPort)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,14 @@ public String toString() {
}
if (_poolType == PoolType.RBD) {
storagePoolBuilder.append("<source>\n");
if (_sourcePort > 0) {
storagePoolBuilder.append("<host name='" + _sourceHost + "' port='" + _sourcePort + "'/>\n");
} else {
storagePoolBuilder.append("<host name='" + _sourceHost + "'/>\n");
for (String sourceHost : _sourceHost.split(",")) {
storagePoolBuilder.append("<host name='");
storagePoolBuilder.append(sourceHost);
if (_sourcePort != 0) {
storagePoolBuilder.append("' port='");
storagePoolBuilder.append(_sourcePort);
}
storagePoolBuilder.append("'/>\n");
}

storagePoolBuilder.append("<name>" + _sourceDir + "</name>\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -52,7 +54,7 @@ public LibvirtStoragePoolDef parseStoragePoolXML(String poolXML) {
String poolName = getTagValue("name", rootElement);

Element source = (Element)rootElement.getElementsByTagName("source").item(0);
String host = getAttrValue("host", "name", source);
String host = getStorageHosts(source);
String format = getAttrValue("format", "type", source);

if (type.equalsIgnoreCase("rbd") || type.equalsIgnoreCase("powerflex")) {
Expand Down Expand Up @@ -123,4 +125,13 @@ private static String getAttrValue(String tag, String attr, Element eElement) {
Element node = (Element)tagNode.item(0);
return node.getAttribute(attr);
}

protected static String getStorageHosts(Element parentElement) {
List<String> storageHosts = new ArrayList<>();
NodeList hosts = parentElement.getElementsByTagName("host");
for (int j = 0; j < hosts.getLength(); j++) {
storageHosts.add(((Element) hosts.item(j)).getAttribute("name"));
}
return StringUtils.join(storageHosts, ",");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1093,13 +1093,15 @@ public String toString() {
diskBuilder.append(" protocol='" + _diskProtocol + "'");
diskBuilder.append(" name='" + _sourcePath + "'");
diskBuilder.append(">\n");
diskBuilder.append("<host name='");
diskBuilder.append(_sourceHost);
if (_sourcePort != 0) {
diskBuilder.append("' port='");
diskBuilder.append(_sourcePort);
for (String sourceHost : _sourceHost.split(",")) {
diskBuilder.append("<host name='");
diskBuilder.append(sourceHost.replace("[", "").replace("]", ""));
if (_sourcePort != 0) {
diskBuilder.append("' port='");
diskBuilder.append(_sourcePort);
}
diskBuilder.append("'/>\n");
}
diskBuilder.append("'/>\n");
diskBuilder.append("</source>\n");
if (_authUserName != null) {
diskBuilder.append("<auth username='" + _authUserName + "'>\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
import org.apache.cloudstack.utils.qemu.QemuObject;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class KVMPhysicalDisk {
private String path;
Expand All @@ -29,10 +33,7 @@ public static String RBDStringBuilder(String monHost, int monPort, String authUs
String rbdOpts;

rbdOpts = "rbd:" + image;
rbdOpts += ":mon_host=" + monHost;
if (monPort > 0) {
rbdOpts += "\\:" + monPort;
}
rbdOpts += ":mon_host=" + composeOptionForMonHosts(monHost, monPort);

if (authUserName == null) {
rbdOpts += ":auth_supported=none";
Expand All @@ -48,6 +49,25 @@ public static String RBDStringBuilder(String monHost, int monPort, String authUs
return rbdOpts;
}

private static String composeOptionForMonHosts(String monHost, int monPort) {
List<String> hosts = new ArrayList<>();
for (String host : monHost.split(",")) {
if (monPort > 0) {
hosts.add(replaceHostAddress(host) + "\\:" + monPort);
} else {
hosts.add(replaceHostAddress(host));
}
}
return StringUtils.join(hosts, "\\;");
}

private static String replaceHostAddress(String hostIp) {
if (hostIp != null && hostIp.startsWith("[") && hostIp.endsWith("]")) {
return hostIp.replaceAll("\\:", "\\\\:");
}
return hostIp;
}

private PhysicalDiskFormat format;
private long size;
private long virtualSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import com.google.common.base.Joiner;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

Expand All @@ -27,6 +29,10 @@ public class QemuImageOptions {
private static final String FILENAME_PARAM_KEY = "file.filename";
private static final String LUKS_KEY_SECRET_PARAM_KEY = "key-secret";
private static final String QCOW2_KEY_SECRET_PARAM_KEY = "encrypt.key-secret";
private static final String DRIVER = "driver";

private QemuImg.PhysicalDiskFormat format;
private static final List<QemuImg.PhysicalDiskFormat> DISK_FORMATS_THAT_SUPPORT_OPTION_IMAGE_OPTS = Arrays.asList(QemuImg.PhysicalDiskFormat.QCOW2, QemuImg.PhysicalDiskFormat.LUKS);

public QemuImageOptions(String filePath) {
params.put(FILENAME_PARAM_KEY, filePath);
Expand Down Expand Up @@ -55,14 +61,13 @@ public QemuImageOptions(QemuImg.PhysicalDiskFormat format, String filePath, Stri
params.put(LUKS_KEY_SECRET_PARAM_KEY, secretName);
}
}
if (format != null) {
params.put("driver", format.toString());
}
setFormat(format);
}

public void setFormat(QemuImg.PhysicalDiskFormat format) {
if (format != null) {
params.put("driver", format.toString());
params.put(DRIVER, format.toString());
this.format = format;
}
}

Expand All @@ -71,6 +76,9 @@ public void setFormat(QemuImg.PhysicalDiskFormat format) {
* @return array of strings representing command flag and value (--image-opts)
*/
public String[] toCommandFlag() {
if (format == null || !DISK_FORMATS_THAT_SUPPORT_OPTION_IMAGE_OPTS.contains(format)) {
return new String[] { params.get(FILENAME_PARAM_KEY) };
}
Map<String, String> sorted = new TreeMap<>(params);
String paramString = Joiner.on(",").withKeyValueSeparator("=").join(sorted);
return new String[] {"--image-opts", paramString};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import junit.framework.TestCase;
import com.cloud.hypervisor.kvm.resource.LibvirtStoragePoolDef.PoolType;
import com.cloud.hypervisor.kvm.resource.LibvirtStoragePoolDef.AuthenticationType;
import org.junit.Test;

public class LibvirtStoragePoolDefTest extends TestCase {

Expand Down Expand Up @@ -102,4 +103,35 @@ public void testRbdStoragePoolWithoutPort() {

assertEquals(expectedXml, pool.toString());
}

@Test
public void testRbdStoragePoolWithMultipleHostsIpv6() {
PoolType type = PoolType.RBD;
String name = "myRBDPool";
String uuid = "1583a25a-b192-436c-93e6-0ef60b198a32";
String host = "[fc00:1234::1],[fc00:1234::2],[fc00:1234::3]";
int port = 3300;
String authUsername = "admin";
AuthenticationType auth = AuthenticationType.CEPH;
String dir = "rbd";
String secretUuid = "28909c4f-314e-4db7-a6b3-5eccd9dcf973";

LibvirtStoragePoolDef pool = new LibvirtStoragePoolDef(type, name, uuid, host, port, dir, authUsername, auth, secretUuid);

String expected = "<pool type='rbd'>\n" +
"<name>myRBDPool</name>\n" +
"<uuid>1583a25a-b192-436c-93e6-0ef60b198a32</uuid>\n" +
"<source>\n" +
"<host name='[fc00:1234::1]' port='3300'/>\n" +
"<host name='[fc00:1234::2]' port='3300'/>\n" +
"<host name='[fc00:1234::3]' port='3300'/>\n" +
"<name>rbd</name>\n" +
"<auth username='admin' type='ceph'>\n" +
"<secret uuid='28909c4f-314e-4db7-a6b3-5eccd9dcf973'/>\n" +
"</auth>\n" +
"</source>\n" +
"</pool>\n";

assertEquals(expected, pool.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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.hypervisor.kvm.resource;

import junit.framework.TestCase;
import org.junit.Assert;

public class LibvirtStoragePoolXMLParserTest extends TestCase {

public void testParseNfsStoragePoolXML() {
String poolXML = "<pool type='netfs'>\n" +
" <name>feff06b5-84b2-3258-b5f9-1953217295de</name>\n" +
" <uuid>feff06b5-84b2-3258-b5f9-1953217295de</uuid>\n" +
" <capacity unit='bytes'>111111111</capacity>\n" +
" <allocation unit='bytes'>2222222</allocation>\n" +
" <available unit='bytes'>3333333</available>\n" +
" <source>\n" +
" <host name='10.11.12.13'/>\n" +
" <dir path='/mnt/primary1'/>\n" +
" <format type='auto'/>\n" +
" </source>\n" +
" <target>\n" +
" <path>/mnt/feff06b5-84b2-3258-b5f9-1953217295de</path>\n" +
" <permissions>\n" +
" <mode>0755</mode>\n" +
" <owner>0</owner>\n" +
" <group>0</group>\n" +
" </permissions>\n" +
" </target>\n" +
"</pool>";

LibvirtStoragePoolXMLParser parser = new LibvirtStoragePoolXMLParser();
LibvirtStoragePoolDef pool = parser.parseStoragePoolXML(poolXML);

Assert.assertEquals("10.11.12.13", pool.getSourceHost());
}

public void testParseRbdStoragePoolXMLWithMultipleHosts() {
String poolXML = "<pool type='rbd'>\n" +
" <name>feff06b5-84b2-3258-b5f9-1953217295de</name>\n" +
" <uuid>feff06b5-84b2-3258-b5f9-1953217295de</uuid>\n" +
" <source>\n" +
" <name>rbdpool</name>\n" +
" <host name='10.11.12.13' port='6789'/>\n" +
" <host name='10.11.12.14' port='6789'/>\n" +
" <host name='10.11.12.15' port='6789'/>\n" +
" <format type='auto'/>\n" +
" <auth username='admin' type='ceph'>\n" +
" <secret uuid='262f743a-3726-11ed-aaee-93e90b39d5c4'/>\n" +
" </auth>\n" +
" </source>\n" +
"</pool>";

LibvirtStoragePoolXMLParser parser = new LibvirtStoragePoolXMLParser();
LibvirtStoragePoolDef pool = parser.parseStoragePoolXML(poolXML);

Assert.assertEquals(LibvirtStoragePoolDef.PoolType.RBD, pool.getPoolType());
Assert.assertEquals(LibvirtStoragePoolDef.AuthenticationType.CEPH, pool.getAuthType());
Assert.assertEquals("10.11.12.13,10.11.12.14,10.11.12.15", pool.getSourceHost());
Assert.assertEquals(6789, pool.getSourcePort());
}

public void testParseRbdStoragePoolXMLWithMultipleHostsIpv6() {
String poolXML = "<pool type='rbd'>\n" +
" <name>feff06b5-84b2-3258-b5f9-1953217295de</name>\n" +
" <uuid>feff06b5-84b2-3258-b5f9-1953217295de</uuid>\n" +
" <source>\n" +
" <name>rbdpool</name>\n" +
" <host name='[fc00:aa:bb:cc::1]' port='6789'/>\n" +
" <host name='[fc00:aa:bb:cc::2]' port='6789'/>\n" +
" <host name='[fc00:aa:bb:cc::3]' port='6789'/>\n" +
" <format type='auto'/>\n" +
" <auth username='admin' type='ceph'>\n" +
" <secret uuid='262f743a-3726-11ed-aaee-93e90b39d5c4'/>\n" +
" </auth>\n" +
" </source>\n" +
"</pool>";

LibvirtStoragePoolXMLParser parser = new LibvirtStoragePoolXMLParser();
LibvirtStoragePoolDef pool = parser.parseStoragePoolXML(poolXML);

Assert.assertEquals(LibvirtStoragePoolDef.PoolType.RBD, pool.getPoolType());
Assert.assertEquals(LibvirtStoragePoolDef.AuthenticationType.CEPH, pool.getAuthType());
Assert.assertEquals("[fc00:aa:bb:cc::1],[fc00:aa:bb:cc::2],[fc00:aa:bb:cc::3]", pool.getSourceHost());
Assert.assertEquals(6789, pool.getSourcePort());
}
}
Loading