QEmu: support disk filenames with comma
Hi!
This patch fixes RHBZ #801036 adding support to filenames with comma using QEmu. As this is my first patch to libvirt, I'm not completely sure if I should have also changed other parts of the code, or if I have used the wrong memory allocation functions/macros, or if I shouldn't have created a separate commit to add my name to the AUTHORS file... :-)
I also need to make it clear that this patch only fixes the problem in the "libvirt data -> QEmu args" way (e.g., when QEmu reads an XML file to start QEmu, or when using virsh domxml-to-native). The other way, "QEmu args -> libvirt data" is *not* implemented (e.g., virsh domxml-from-native). I would like to know if I'm going in the right way so I can continue working on this patch, and if that half of the feature is also needed.
Any feedback is welcome, thanks!
Best regards,
Crístian.
PATCH 1/2 QEmu: support disk filenames with comma
If there is a disk file with a comma in the name, QEmu expects a double comma instead of a single one (e.g., the file "virtual,disk.img" needs to be specified as "virtual,,disk.img" in QEmu's command line). This patch fixes libvirt to work with that feature. Fix RHBZ #801036.
src/qemu/qemu_command.c +48 -7
@@ -1628,6 +1628,48 @@ qemuSafeSerialParamValue(const char *value)return 0 ; } +static const char * +qemuEscapeCommas (const char *name) +{ +const char *name_ptr = name; +char *escaped_name, *escaped_name_ptr; +size_t escaped_name_max_size = (strlen (name) *2 ) +1 ; + +/* If there is no comma in the string, return a copy of the original one. */ +if (!strchr (name,',' )) { +if (!(escaped_name =strdup (name))) { +virReportOOMError (); +return NULL ; + } +return escaped_name; + } + +/* In the worst case the string will have a sequence of only commas, +* which will require double the space for the escaped string. */ +if (VIR_ALLOC_N (escaped_name, escaped_name_max_size) <0 ) { +virReportOOMError (); +return NULL ; + } + +/* Insert a comma after each comma in the string. */ + escaped_name_ptr = escaped_name; +while (*name_ptr !='\0' ) { +if (*name_ptr ==',' ) { + *escaped_name_ptr++ =',' ; + } + + *escaped_name_ptr++ = *name_ptr++; + } + + escaped_name_ptr ='\0' ; + +/* Reduce the string memory size to its current length. */ +VIR_SHRINK_N (escaped_name, escaped_name_max_size, +strlen (escaped_name) -strlen (name)); + +return escaped_name; +} + static int qemuBuildRBDString(virConnectPtr conn, virDomainDiskDefPtr disk,
@@ -1638,7 +1680,7 @@ qemuBuildRBDString(virConnectPtr conn,char *secret =NULL ;size_t secret_size; -virBufferAsprintf (opt,"rbd:%s" , disk->src); +virBufferAsprintf (opt,"rbd:%s" ,qemuEscapeCommas (disk->src));if (disk->auth.username) {virBufferEscape (opt,":" ,":id=%s" , disk->auth.username);/* look up secret */
@@ -1922,9 +1964,9 @@ qemuBuildDriveStr(virConnectPtr conn ATTRIBUTE_UNUSED, goto error; }if (disk->device ==VIR_DOMAIN_DISK_DEVICE_FLOPPY ) -virBufferAsprintf (&opt,"file=fat:floppy:%s," , disk->src); +virBufferAsprintf (&opt,"file=fat:floppy:%s," ,qemuEscapeCommas (disk->src));else -virBufferAsprintf (&opt,"file=fat:%s," , disk->src); +virBufferAsprintf (&opt,"file=fat:%s," ,qemuEscapeCommas (disk->src)); }else if (disk->type ==VIR_DOMAIN_DISK_TYPE_NETWORK ) {switch (disk->protocol) {case VIR_DOMAIN_DISK_PROTOCOL_NBD :
@@ -1944,16 +1986,16 @@ qemuBuildDriveStr(virConnectPtr conn ATTRIBUTE_UNUSED,break ;case VIR_DOMAIN_DISK_PROTOCOL_SHEEPDOG :if (disk->nhosts ==0 ) -virBufferAsprintf (&opt,"file=sheepdog:%s," , disk->src); +virBufferAsprintf (&opt,"file=sheepdog:%s," ,qemuEscapeCommas (disk->src));else /* only one host is supported now */ virBufferAsprintf (&opt,"file=sheepdog:%s:%s:%s," , disk->hosts->name, disk->hosts->port, - disk->src); +qemuEscapeCommas (disk->src));break ; } }else { -virBufferAsprintf (&opt,"file=%s," , disk->src); +virBufferAsprintf (&opt,"file=%s," ,qemuEscapeCommas (disk->src)); } }if (qemuCapsGet (qemuCaps,QEMU_CAPS_DEVICE ))
@@ -2134,7 +2176,6 @@ error:return NULL ; } -char * qemuBuildDriveDevStr(virDomainDefPtr def, virDomainDiskDefPtr disk,
Signature · 1 line
1.7.8.5
Quoted 4 lines · Crístian Viana
If there is a disk file with a comma in the name, QEmu expects a double comma instead of a single one (e.g., the file "virtual,disk.img" needs to be specified as "virtual,,disk.img" in QEmu's command line). This patch fixes libvirt to work with that feature. Fix RHBZ #801036.
I think you meant to send this to libvir-devel.
Regards,
Anthony Liguori
Quoted 103 lines
src/qemu/qemu_command.c +48
-7@@ -1628,6 +1628,48 @@ qemuSafeSerialParamValue(const char *value)return 0 ; } +static const char * +qemuEscapeCommas (const char *name) +{ +const char *name_ptr = name; +char *escaped_name, *escaped_name_ptr; +size_t escaped_name_max_size = (strlen (name) *2 ) +1 ; + +/* If there is no comma in the string, return a copy of the original one. */ +if (!strchr (name,',' )) { +if (!(escaped_name =strdup (name))) { +virReportOOMError (); +return NULL ; + } +return escaped_name; + } + +/* In the worst case the string will have a sequence of only commas, +* which will require double the space for the escaped string. */ +if (VIR_ALLOC_N (escaped_name, escaped_name_max_size)<0 ) { +virReportOOMError (); +return NULL ; + } + +/* Insert a comma after each comma in the string. */ + escaped_name_ptr = escaped_name; +while (*name_ptr !='\0' ) { +if (*name_ptr ==',' ) { + *escaped_name_ptr++ =',' ; + } + + *escaped_name_ptr++ = *name_ptr++; + } + + escaped_name_ptr ='\0' ; + +/* Reduce the string memory size to its current length. */ +VIR_SHRINK_N (escaped_name, escaped_name_max_size, +strlen (escaped_name) -strlen (name)); + +return escaped_name; +} + static int qemuBuildRBDString(virConnectPtr conn, virDomainDiskDefPtr disk,@@ -1638,7 +1680,7 @@ qemuBuildRBDString(virConnectPtr conn,char *secret =NULL ;size_t secret_size; -virBufferAsprintf (opt,"rbd:%s" , disk->src); +virBufferAsprintf (opt,"rbd:%s" ,qemuEscapeCommas (disk->src));if (disk->auth.username) {virBufferEscape (opt,":" ,":id=%s" , disk->auth.username);/* look up secret */ @@ -1922,9 +1964,9 @@ qemuBuildDriveStr(virConnectPtr conn ATTRIBUTE_UNUSED, goto error; }if (disk->device ==VIR_DOMAIN_DISK_DEVICE_FLOPPY ) -virBufferAsprintf (&opt,"file=fat:floppy:%s," , disk->src); +virBufferAsprintf (&opt,"file=fat:floppy:%s," ,qemuEscapeCommas (disk->src));else -virBufferAsprintf (&opt,"file=fat:%s," , disk->src); +virBufferAsprintf (&opt,"file=fat:%s," ,qemuEscapeCommas (disk->src)); }else if (disk->type ==VIR_DOMAIN_DISK_TYPE_NETWORK ) {switch (disk->protocol) {case VIR_DOMAIN_DISK_PROTOCOL_NBD :@@ -1944,16 +1986,16 @@ qemuBuildDriveStr(virConnectPtr conn ATTRIBUTE_UNUSED,break ;case VIR_DOMAIN_DISK_PROTOCOL_SHEEPDOG :if (disk->nhosts ==0 ) -virBufferAsprintf (&opt,"file=sheepdog:%s," , disk->src); +virBufferAsprintf (&opt,"file=sheepdog:%s," ,qemuEscapeCommas (disk->src));else /* only one host is supported now */ virBufferAsprintf (&opt,"file=sheepdog:%s:%s:%s," , disk->hosts->name, disk->hosts->port, - disk->src); +qemuEscapeCommas (disk->src));break ; } }else { -virBufferAsprintf (&opt,"file=%s," , disk->src); +virBufferAsprintf (&opt,"file=%s," ,qemuEscapeCommas (disk->src)); } }if (qemuCapsGet (qemuCaps,QEMU_CAPS_DEVICE ))@@ -2134,7 +2176,6 @@ error:return NULL ; } -char * qemuBuildDriveDevStr(virDomainDefPtr def, virDomainDiskDefPtr disk,
PATCH 2/2 Add myself to the AUTHORS file
In reply to Crístian VianaAUTHORS +1 -0
@@ -224,6 +224,7 @@ Patches have also been contributed by: Peter Robinson <[email protected]> Benjamin Cama <[email protected]> Duncan Rance <[email protected]> + Crístian Viana <[email protected]> [....send patches to get your name here....]
Signature · 1 line
1.7.8.5
Quoted 4 lines · Crístian Viana
Hi!
This patch fixes RHBZ #801036 adding support to filenames with comma using QEmu. As this is my first patch to libvirt,
...
Quoted 2 lines
Any feedback is welcome, thanks!
For starters, you mailed the qemu list. This patch should be sent instead to [email protected], so that it will actually reach the libvirt folks.
Signature · 2 lines
Eric Blake [email protected] +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 4 lines · Eric Blake
For starters, you mailed the qemu list. This patch should be sent instead to [email protected], so that it will actually reach the libvirt folks.
Oh, sorry, wrong list :-) I'll mail the right one then.
Thanks!