[libvirt] [PATCH v2] util: Set backing file name for LOOP_GET_STATUS64 queries.

jcfaracco@gmail.com posted 1 patch 4 years, 6 months ago
Test syntax-check passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20190902170027.11922-1-jcfaracco@gmail.com
src/util/virfile.c | 8 ++++++++
1 file changed, 8 insertions(+)
[libvirt] [PATCH v2] util: Set backing file name for LOOP_GET_STATUS64 queries.
Posted by jcfaracco@gmail.com 4 years, 6 months ago
From: Julio Faracco <jcfaracco@gmail.com>

This is an issue for LXC loop devices when you are trying to get loop
devices info using `ioctl`. Modern apps uses `/sys/dev/block` to grab
information about devices, but if you use the method mention you won't
be able to retrive the associated file with that loop device. See
example below from cryptsetup sources:

    static char *_ioctl_backing_file(const char *loop)
    {
        struct loop_info64 lo64 = {0};
        int loop_fd;

        loop_fd = open(loop, O_RDONLY);
        if (loop_fd < 0)
            return NULL;

        if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
            close(loop_fd);
            return NULL;
        }

        lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
        lo64.lo_file_name[LO_NAME_SIZE-1] = 0;

        close(loop_fd);
        return strdup((char*)lo64.lo_file_name);
    }

It will return an empty string because lo_file_name was not set.
Function `virFileLoopDeviceOpenSearch()` is using `ioctl` to query data,
but it is not checking `lo_file_name` field.

v1: I accidentally committed two wrong lines.

Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
---
 src/util/virfile.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/util/virfile.c b/src/util/virfile.c
index 81a3c096eb..dbfe74e24f 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -781,6 +781,14 @@ int virFileLoopDeviceAssociate(const char *file,
     memset(&lo, 0, sizeof(lo));
     lo.lo_flags = LO_FLAGS_AUTOCLEAR;
 
+    /* Set backing file name for LOOP_GET_STATUS64 queries */
+    if (virStrncpy((char *) lo.lo_file_name, file,
+                   strlen(file), LO_NAME_SIZE) < 0) {
+        virReportSystemError(errno,
+                             _("Unable to set backing file %s"), file);
+        goto cleanup;
+    }
+
     if ((fsfd = open(file, O_RDWR)) < 0) {
         virReportSystemError(errno,
                              _("Unable to open %s"), file);
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2] util: Set backing file name for LOOP_GET_STATUS64 queries.
Posted by Julio Faracco 4 years, 6 months ago
For further reference:
https://github.com/lxc/lxc/commit/a70c9e85a6d8ac1b75d6705d2373fd9c7b567240

Em seg, 2 de set de 2019 às 14:00, <jcfaracco@gmail.com> escreveu:
>
> From: Julio Faracco <jcfaracco@gmail.com>
>
> This is an issue for LXC loop devices when you are trying to get loop
> devices info using `ioctl`. Modern apps uses `/sys/dev/block` to grab
> information about devices, but if you use the method mention you won't
> be able to retrive the associated file with that loop device. See
> example below from cryptsetup sources:
>
>     static char *_ioctl_backing_file(const char *loop)
>     {
>         struct loop_info64 lo64 = {0};
>         int loop_fd;
>
>         loop_fd = open(loop, O_RDONLY);
>         if (loop_fd < 0)
>             return NULL;
>
>         if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
>             close(loop_fd);
>             return NULL;
>         }
>
>         lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
>         lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
>
>         close(loop_fd);
>         return strdup((char*)lo64.lo_file_name);
>     }
>
> It will return an empty string because lo_file_name was not set.
> Function `virFileLoopDeviceOpenSearch()` is using `ioctl` to query data,
> but it is not checking `lo_file_name` field.
>
> v1: I accidentally committed two wrong lines.
>
> Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
> ---
>  src/util/virfile.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
>
> diff --git a/src/util/virfile.c b/src/util/virfile.c
> index 81a3c096eb..dbfe74e24f 100644
> --- a/src/util/virfile.c
> +++ b/src/util/virfile.c
> @@ -781,6 +781,14 @@ int virFileLoopDeviceAssociate(const char *file,
>      memset(&lo, 0, sizeof(lo));
>      lo.lo_flags = LO_FLAGS_AUTOCLEAR;
>
> +    /* Set backing file name for LOOP_GET_STATUS64 queries */
> +    if (virStrncpy((char *) lo.lo_file_name, file,
> +                   strlen(file), LO_NAME_SIZE) < 0) {
> +        virReportSystemError(errno,
> +                             _("Unable to set backing file %s"), file);
> +        goto cleanup;
> +    }
> +
>      if ((fsfd = open(file, O_RDWR)) < 0) {
>          virReportSystemError(errno,
>                               _("Unable to open %s"), file);
> --
> 2.20.1
>

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2] util: Set backing file name for LOOP_GET_STATUS64 queries.
Posted by Daniel P. Berrangé 4 years, 6 months ago
On Mon, Sep 02, 2019 at 02:00:27PM -0300, jcfaracco@gmail.com wrote:
> From: Julio Faracco <jcfaracco@gmail.com>
> 
> This is an issue for LXC loop devices when you are trying to get loop
> devices info using `ioctl`. Modern apps uses `/sys/dev/block` to grab
> information about devices, but if you use the method mention you won't
> be able to retrive the associated file with that loop device. See
> example below from cryptsetup sources:
> 
>     static char *_ioctl_backing_file(const char *loop)
>     {
>         struct loop_info64 lo64 = {0};
>         int loop_fd;
> 
>         loop_fd = open(loop, O_RDONLY);
>         if (loop_fd < 0)
>             return NULL;
> 
>         if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
>             close(loop_fd);
>             return NULL;
>         }
> 
>         lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
>         lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
> 
>         close(loop_fd);
>         return strdup((char*)lo64.lo_file_name);
>     }
> 
> It will return an empty string because lo_file_name was not set.
> Function `virFileLoopDeviceOpenSearch()` is using `ioctl` to query data,
> but it is not checking `lo_file_name` field.
> 
> v1: I accidentally committed two wrong lines.

This type of message should be put after the "---" lines...

> 
> Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
> ---

Here, so that it doesn't get into git history when the patch
is applied

>  src/util/virfile.c | 8 ++++++++
>  1 file changed, 8 insertions(+)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>

and pushed to git


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list