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
_______________________________________________
Libguestfs mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to