Subject: bug#16231: [PATCH 7/9] libparted: give correct partition device name on loop labels

From: Phillip Susi <[email-masked]>
Date: Fri, 2 May 2014 21:50:49 -0400

ped_partition_get_path() was returning "/dev/foo1" instead of
"/dev/foo" on loop labels.  This caused gparted to run tools like mkfs on
a device node that did not actually exist.
---
 NEWS                   |  3 +++
 libparted/arch/linux.c | 10 ++++++++--
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index f99c6fe..c97f4eb 100644
--- a/NEWS
+++ b/NEWS
@@ -23,6 +23,9 @@ GNU parted NEWS                                    -*- outline -*-
 
 ** Bug Fixes
 
+  libparted: ped_partition_get_path() was returning "/dev/foo1" instead
+  of "/dev/foo" for loop labels.
+
   partprobe: when called on a disk that has become a loop label,
   remove any partitions left over from a previous label.
 
diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index 9ae6d64..f2e2abc 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -48,6 +48,7 @@
 #include "../architecture.h"
 #include "dirname.h"
 #include "xstrtol.h"
+#include "xalloc.h"
 
 #if ENABLE_NLS
 #  include <libintl.h>
@@ -2356,6 +2357,9 @@ _device_get_part_path (PedDevice const *dev, int num)
 static char*
 linux_partition_get_path (const PedPartition* part)
 {
+        /* loop label means use the whole disk */
+        if (strcmp (part->disk->type->name, "loop") == 0)
+                return xstrdup (part->disk->dev->path);
         return _device_get_part_path (part->disk->dev, part->num);
 }
 
@@ -2424,6 +2428,8 @@ linux_partition_is_busy (const PedPartition* part)
 
         PED_ASSERT (part != NULL);
 
+        if (strcmp (part->disk->type->name, "loop") == 0)
+                return linux_is_busy (part->disk->dev);
         if (_partition_is_mounted (part))
                 return 1;
         if (part->type == PED_PARTITION_EXTENDED) {
@@ -2546,7 +2552,7 @@ _sysfs_ull_entry_from_part(PedPartition const* part, const char *entry,
                            unsigned long long *val)
 {
         char path[128];
-        char *part_name = linux_partition_get_path(part);
+        char *part_name = _device_get_part_path (part->disk->dev, part->num);
         if (!part_name)
                 return false;
 
@@ -2581,7 +2587,7 @@ _kernel_get_partition_start_and_length(PedPartition const *part,
         PED_ASSERT(start);
         PED_ASSERT(length);
 
-        char *dev_name = linux_partition_get_path (part);
+        char *dev_name = _device_get_part_path (part->disk->dev, part->num);
         if (!dev_name)
                 return false;
 
-- 
1.9.1