[PATCH] daemon: Fix btrfs volume device reverse-translation
Devices associated with btrfs volumes are not reverse-translated (e.g., btrfsvol:/dev/sdX to sdY). Forward translation occurs, creating a path mismatch. This causes errors in subsequent btrfs commands. --- daemon/device-name-translation.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/daemon/device-name-translation.c b/daemon/device-name-translation.c index cfebc6495..eaf9a4686 100644 --- a/daemon/device-name-translation.c +++ b/daemon/device-name-translation.c @@ -251,9 +251,28 @@ device_name_translation (const char *device) char * reverse_device_name_translation (const char *device) { + const char *original_device = device; char *ret = NULL; size_t i; + const char *prefix = ""; + const char *suffix = ""; + bool btrfsvol = STRPREFIX (device, "btrfsvol:"); + if (btrfsvol) { + prefix = "btrfsvol:"; + + const char *device_start = device + strlen (prefix); + const char *device_end = strchr (device_start + strlen ("/dev/"), '/'); + device = strndup(device_start, device_end - device_start); + if (device == NULL) { + perror ("strndup"); + return NULL; + } + + suffix = device_end; + + } + /* Look it up in the cache, and if found return the canonical name. * If not found return a copy of the original string. */ @@ -265,13 +284,19 @@ reverse_device_name_translation (const char *device) char drv[16]; guestfs_int_drive_name (i, drv); - if (asprintf (&ret, "/dev/sd%s%s", drv, &device[len]) == -1) { + if (asprintf (&ret, "%s/dev/sd%s%s%s", prefix, drv, &device[len], suffix) == -1) { reply_with_perror ("asprintf"); + if (btrfsvol) + free (device); return NULL; } break; } } + if (btrfsvol) { + free (device); + device = original_device; + } if (ret == NULL) { ret = strdup (device); -- 2.52.0.460.gd25c4c69ec-goog
On Tue, Nov 25, 2025 at 03:28:30PM +0200, Arye Yurkovsky wrote:
Devices associated with btrfs volumes are not reverse-translated (e.g., btrfsvol:/dev/sdX to sdY). Forward translation occurs, creating a path mismatch. This causes errors in subsequent btrfs commands.
Sorry for missing this patch last week. Can you try the attached version instead? There are some problems with the patch as posted, including that it isn't const correct, but the attached version should fix those things. Rich.
daemon/device-name-translation.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/daemon/device-name-translation.c b/daemon/device-name-translation.c index cfebc6495..eaf9a4686 100644 --- a/daemon/device-name-translation.c +++ b/daemon/device-name-translation.c @@ -251,9 +251,28 @@ device_name_translation (const char *device) char * reverse_device_name_translation (const char *device) { + const char *original_device = device; char *ret = NULL; size_t i;
+ const char *prefix = ""; + const char *suffix = ""; + bool btrfsvol = STRPREFIX (device, "btrfsvol:"); + if (btrfsvol) { + prefix = "btrfsvol:"; + + const char *device_start = device + strlen (prefix); + const char *device_end = strchr (device_start + strlen ("/dev/"), '/'); + device = strndup(device_start, device_end - device_start); + if (device == NULL) { + perror ("strndup"); + return NULL; + } + + suffix = device_end; + + } + /* Look it up in the cache, and if found return the canonical name. * If not found return a copy of the original string. */ @@ -265,13 +284,19 @@ reverse_device_name_translation (const char *device) char drv[16];
guestfs_int_drive_name (i, drv); - if (asprintf (&ret, "/dev/sd%s%s", drv, &device[len]) == -1) { + if (asprintf (&ret, "%s/dev/sd%s%s%s", prefix, drv, &device[len], suffix) == -1) { reply_with_perror ("asprintf"); + if (btrfsvol) + free (device); return NULL; } break; } } + if (btrfsvol) { + free (device); + device = original_device; + }
if (ret == NULL) { ret = strdup (device); -- 2.52.0.460.gd25c4c69ec-goog
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
On Wed, Dec 03, 2025 at 11:18:29AM +0000, Richard W.M. Jones via Libguestfs wrote:
+/* btrfsvol:/dev/sdX also needs reversing. */ +static char * +reverse_btrfsvol (const char *device) +{ + const char prefix[] = "btrfsvol:"; + const char *device_start, *device_end; + CLEANUP_FREE char *device_name = NULL; + CLEANUP_FREE char *reversed_device = NULL; + char *ret; + + device_start = device + strlen (prefix); + device_end = strchr (device_start + strlen ("/dev/"), '/'); + device_name = strndup (device_start, device_end - device_start);
^^ Missing an error check here. I added this in my copy: if (device_name == NULL) { reply_with_perror ("strndup"); return NULL; }
+ + reversed_device = reverse_device_name_translation (device_name); + if (reversed_device == NULL) + return NULL; + + /* Construct the final btrfsvol: and return it, caller frees. */ + if (asprintf (&ret, "%s%s%s", prefix, reversed_device, device_end) == -1) { + reply_with_perror ("asprintf"); + return NULL; + } + + return ret; +}
Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
I tested your patch & it works as-well. On Wed, Dec 3, 2025 at 1:25 PM Richard W.M. Jones <rjones@redhat.com> wrote:
On Wed, Dec 03, 2025 at 11:18:29AM +0000, Richard W.M. Jones via Libguestfs wrote:
+/* btrfsvol:/dev/sdX also needs reversing. */ +static char * +reverse_btrfsvol (const char *device) +{ + const char prefix[] = "btrfsvol:"; + const char *device_start, *device_end; + CLEANUP_FREE char *device_name = NULL; + CLEANUP_FREE char *reversed_device = NULL; + char *ret; + + device_start = device + strlen (prefix); + device_end = strchr (device_start + strlen ("/dev/"), '/'); + device_name = strndup (device_start, device_end - device_start);
^^ Missing an error check here.
I added this in my copy:
if (device_name == NULL) { reply_with_perror ("strndup"); return NULL; }
+ + reversed_device = reverse_device_name_translation (device_name); + if (reversed_device == NULL) + return NULL; + + /* Construct the final btrfsvol: and return it, caller frees. */ + if (asprintf (&ret, "%s%s%s", prefix, reversed_device, device_end) == -1) { + reply_with_perror ("asprintf"); + return NULL; + } + + return ret; +}
Rich.
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
On Wed, Dec 10, 2025 at 01:22:33PM +0200, Arye Yurkovsky wrote:
I tested your patch & it works as-well.
Thanks for testing, This is upstream in commit c7b204bce3c860c10663fcb9250dd934eaf3390a. Rich.
On Wed, Dec 3, 2025 at 1:25 PM Richard W.M. Jones <rjones@redhat.com> wrote:
On Wed, Dec 03, 2025 at 11:18:29AM +0000, Richard W.M. Jones via Libguestfs wrote: > +/* btrfsvol:/dev/sdX also needs reversing. */ > +static char * > +reverse_btrfsvol (const char *device) > +{ > + const char prefix[] = "btrfsvol:"; > + const char *device_start, *device_end; > + CLEANUP_FREE char *device_name = NULL; > + CLEANUP_FREE char *reversed_device = NULL; > + char *ret; > + > + device_start = device + strlen (prefix); > + device_end = strchr (device_start + strlen ("/dev/"), '/'); > + device_name = strndup (device_start, device_end - device_start);
^^ Missing an error check here.
I added this in my copy:
if (device_name == NULL) { reply_with_perror ("strndup"); return NULL; }
> + > + reversed_device = reverse_device_name_translation (device_name); > + if (reversed_device == NULL) > + return NULL; > + > + /* Construct the final btrfsvol: and return it, caller frees. */ > + if (asprintf (&ret, "%s%s%s", prefix, reversed_device, device_end) == -1) { > + reply_with_perror ("asprintf"); > + return NULL; > + } > + > + return ret; > +}
Rich.
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/ ~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
participants (2)
-
Arye Yurkovsky -
Richard W.M. Jones