Subject: bug#15352: [PATCH 04/19] libparted: preserve the uuid on dm partitions (#832145)

From: "Brian C. Lane" <[email-masked]>
Date: Wed, 11 Sep 2013 12:24:54 -0700

From: "Brian C. Lane" <[email-masked]>

* libparted/arch/linux.c (_dm_add_partition): Set the uuid if there was
  one.
---
 libparted/arch/linux.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index 1c5faa5..375be83 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -2711,9 +2711,12 @@ static int
 _dm_add_partition (PedDisk* disk, const PedPartition* part)
 {
         LinuxSpecific*  arch_specific = LINUX_SPECIFIC (disk->dev);
-        char *params = NULL;
-        char *vol_name = NULL;
-        uint32_t cookie = 0;
+        char*           params = NULL;
+        char*           vol_name = NULL;
+        const char*     dev_name = NULL;
+        char*           vol_uuid = NULL;
+        const char*     dev_uuid = NULL;
+        uint32_t        cookie = 0;
 
         /* Get map name from devicemapper */
         struct dm_task *task = dm_task_create (DM_DEVICE_INFO);
@@ -2727,7 +2730,7 @@ _dm_add_partition (PedDisk* disk, const PedPartition* part)
         if (!dm_task_run (task))
                 goto err;
 
-        const char *dev_name = dm_task_get_name (task);
+        dev_name = dm_task_get_name (task);
         size_t name_len = strlen (dev_name);
         vol_name = zasprintf ("%s%s%d",
                               dev_name,
@@ -2736,6 +2739,11 @@ _dm_add_partition (PedDisk* disk, const PedPartition* part)
         if (vol_name == NULL)
                 goto err;
 
+        dev_uuid = dm_task_get_uuid (task);
+        if (dev_uuid && (strlen(dev_uuid) > 0)
+             && !(vol_uuid = zasprintf ("part%d-%s", part->num, dev_uuid)))
+            goto err;
+
         /* Caution: dm_task_destroy frees dev_name.  */
         dm_task_destroy (task);
         task = NULL;
@@ -2748,6 +2756,8 @@ _dm_add_partition (PedDisk* disk, const PedPartition* part)
                 goto err;
 
         dm_task_set_name (task, vol_name);
+        if (vol_uuid)
+                dm_task_set_uuid (task, vol_uuid);
         dm_task_add_target (task, 0, part->geom.length,
                 "linear", params);
         if (!dm_task_set_cookie (task, &cookie, 0))
@@ -2756,6 +2766,7 @@ _dm_add_partition (PedDisk* disk, const PedPartition* part)
                 dm_task_update_nodes ();
                 dm_task_destroy (task);
                 free (params);
+                free (vol_uuid);
                 free (vol_name);
                 return 1;
         } else {
@@ -2766,6 +2777,7 @@ err:
         if (task)
                 dm_task_destroy (task);
         free (params);
+        free (vol_uuid);
         free (vol_name);
         return 0;
 }
-- 
1.8.3.1