qemu: escape ipv6 for rbd network disk hosts

Hosts for rbd are ceph monitor daemons. These have fixed IP addresses,
so they are often referenced by IP rather than hostname for
convenience, or to avoid relying on DNS. Using IPv4 addresses as the
host name works already, but IPv6 addresses require rbd-specific
escaping because the colon is used as an option separator in the
string passed to qemu.

Escape these colons, and enclose the IPv6 address in square brackets
so it is distinguished from the port, which is currently mandatory.

Acked-by: Osier Yang <jyang@redhat.com>
Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
This commit is contained in:
Josh Durgin 2013-01-24 18:45:31 -08:00 committed by Osier Yang
parent 339bdd99a1
commit c1509ab47e
6 changed files with 78 additions and 8 deletions

View File

@ -1099,7 +1099,10 @@
</attribute>
</optional>
<attribute name="name">
<ref name="dnsName"/>
<choice>
<ref name="dnsName"/>
<ref name="ipAddr"/>
</choice>
</attribute>
<attribute name="port">
<ref name="unsignedInt"/>

View File

@ -34,6 +34,7 @@
#include "virerror.h"
#include "virutil.h"
#include "virfile.h"
#include "virstring.h"
#include "viruuid.h"
#include "c-ctype.h"
#include "domain_nwfilter.h"
@ -1937,13 +1938,16 @@ qemuBuildRBDString(virConnectPtr conn,
if (i) {
virBufferAddLit(opt, "\\;");
}
if (disk->hosts[i].port) {
virBufferAsprintf(opt, "%s\\:%s",
disk->hosts[i].name,
disk->hosts[i].port);
/* assume host containing : is ipv6 */
if (strchr(disk->hosts[i].name, ':')) {
virBufferEscape(opt, '\\', ":", "[%s]", disk->hosts[i].name);
} else {
virBufferAsprintf(opt, "%s", disk->hosts[i].name);
}
if (disk->hosts[i].port) {
virBufferAsprintf(opt, "\\:%s", disk->hosts[i].port);
}
}
}
@ -1961,15 +1965,25 @@ error:
static int qemuAddRBDHost(virDomainDiskDefPtr disk, char *hostport)
{
char *port;
size_t skip;
char **parts;
disk->nhosts++;
if (VIR_REALLOC_N(disk->hosts, disk->nhosts) < 0)
goto no_memory;
port = strstr(hostport, "\\:");
if ((port = strchr(hostport, ']'))) {
/* ipv6, strip brackets */
hostport += 1;
skip = 3;
} else {
port = strstr(hostport, "\\:");
skip = 2;
}
if (port) {
*port = '\0';
port += 2;
port += skip;
disk->hosts[disk->nhosts-1].port = strdup(port);
if (!disk->hosts[disk->nhosts-1].port)
goto no_memory;
@ -1978,7 +1992,12 @@ static int qemuAddRBDHost(virDomainDiskDefPtr disk, char *hostport)
if (!disk->hosts[disk->nhosts-1].port)
goto no_memory;
}
disk->hosts[disk->nhosts-1].name = strdup(hostport);
parts = virStringSplit(hostport, "\\:", 0);
if (!parts)
goto no_memory;
disk->hosts[disk->nhosts-1].name = virStringJoin((const char **)parts, ":");
virStringFreeList(parts);
if (!disk->hosts[disk->nhosts-1].name)
goto no_memory;

View File

@ -185,6 +185,7 @@ mymain(void)
DO_TEST("disk-drive-network-nbd");
DO_TEST("disk-drive-network-gluster");
DO_TEST("disk-drive-network-rbd");
DO_TEST("disk-drive-network-rbd-ipv6");
/* older format using CEPH_ARGS env var */
DO_TEST("disk-drive-network-rbd-ceph-env");
DO_TEST("disk-drive-network-sheepdog");

View File

@ -0,0 +1,9 @@
LC_ALL=C PATH=/bin HOME=/home/test USER=test LOGNAME=test \
/usr/bin/qemu -S -M pc -m 214 -smp 1 -nographic -monitor \
unix:/tmp/test-monitor,server,nowait -no-acpi -boot c -usb -drive \
file=/dev/HostVG/QEMUGuest1,if=ide,bus=0,unit=0 -drive \
'file=rbd:pool/image:auth_supported=none:\
mon_host=[\:\:1]\:6321\;example.org\:6789\;\
[ffff\:1234\:567\:abc\:\:0f]\:6322\;\
[2001\:db8\:\:ff00\:42\:8329]\:6322,\
if=virtio,format=raw' -net none -serial none -parallel none

View File

@ -0,0 +1,36 @@
<domain type='qemu'>
<name>QEMUGuest1</name>
<uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
<memory unit='KiB'>219136</memory>
<currentMemory unit='KiB'>219136</currentMemory>
<vcpu placement='static'>1</vcpu>
<os>
<type arch='i686' machine='pc'>hvm</type>
<boot dev='hd'/>
</os>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/bin/qemu</emulator>
<disk type='block' device='disk'>
<source dev='/dev/HostVG/QEMUGuest1'/>
<target dev='hda' bus='ide'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<disk type='network' device='disk'>
<driver name='qemu' type='raw'/>
<source protocol='rbd' name='pool/image'>
<host name='::1' port='6321'/>
<host name='example.org' port='6789'/>
<host name='ffff:1234:567:abc::0f' port='6322'/>
<host name='2001:db8::ff00:42:8329' port='6322'/>
</source>
<target dev='vda' bus='virtio'/>
</disk>
<controller type='usb' index='0'/>
<controller type='ide' index='0'/>
<memballoon model='virtio'/>
</devices>
</domain>

View File

@ -478,6 +478,8 @@ mymain(void)
QEMU_CAPS_DRIVE, QEMU_CAPS_DRIVE_FORMAT);
DO_TEST("disk-drive-network-rbd-auth",
QEMU_CAPS_DRIVE, QEMU_CAPS_DRIVE_FORMAT);
DO_TEST("disk-drive-network-rbd-ipv6",
QEMU_CAPS_DRIVE, QEMU_CAPS_DRIVE_FORMAT);
DO_TEST("disk-drive-no-boot",
QEMU_CAPS_DRIVE, QEMU_CAPS_DEVICE, QEMU_CAPS_BOOTINDEX);
DO_TEST("disk-usb", NONE);