When creating a partition, ensure size is a multiple of 512
authorDavid Zeuthen <davidz@redhat.com>
Wed, 9 Nov 2011 16:40:22 +0000 (11:40 -0500)
committerDavid Zeuthen <davidz@redhat.com>
Wed, 9 Nov 2011 16:40:22 +0000 (11:40 -0500)
Also account for parted(8) expecting the @end parameter to be
inclusive...

Signed-off-by: David Zeuthen <davidz@redhat.com>
data/org.freedesktop.UDisks2.xml
src/udiskslinuxpartitiontable.c

index 288a43b..7afa94c 100644 (file)
 
         Note that the created partition won't necessarily be created
         at the exact @offset due to disk geometry and other alignment
-        constraints. The new partition may also end up being slightly
+        constraints (e.g. 1MiB alignment).
+
+        The newly created partition may also end up being slightly
         larger or smaller than the requested @size bytes for the same
         reasons.
 
index 8a46f61..c675fb6 100644 (file)
@@ -338,12 +338,14 @@ handle_create_partition (UDisksPartitionTable   *table,
       escaped_name = g_strescape (name, NULL);
       escaped_escaped_name = g_strescape (escaped_name, NULL);
 
-      /* round to MiB for alignment purposes and round up so we _start_ at MiB granularity
-       * since that ensures optimal IO
+      /* Ensure we _start_ at MiB granularity since that ensures optimal IO...
+       * Also round up size to nearest multiple of 512
        */
       start_mib = offset / MIB_SIZE + 1L;
-      end_bytes = start_mib * MIB_SIZE + size;
-      /* reduce size until we are not overlapping
+      end_bytes = start_mib * MIB_SIZE + ((size + 511L) & (~511L));
+
+      /* Now reduce size until we are not overlapping
+       *
        *  - neighboring partitions; or
        *  - the end of the disk (note: the 33 LBAs is the Secondary GPT)
        */
@@ -357,14 +359,13 @@ handle_create_partition (UDisksPartitionTable   *table,
            */
           end_bytes -= 512L;
         }
-
       wait_data->pos_to_wait_for = (start_mib*MIB_SIZE + end_bytes) / 2L;
       command_line = g_strdup_printf ("parted --align optimal --script \"%s\" "
                                       "\"mkpart \\\"%s\\\" ext2 %" G_GUINT64_FORMAT "MiB %" G_GUINT64_FORMAT "b\"",
                                       escaped_device,
                                       escaped_escaped_name,
                                       start_mib,
-                                      end_bytes);
+                                      end_bytes - 1); /* end_bytes is *INCLUSIVE* (!) */
       g_free (escaped_escaped_name);
       g_free (escaped_name);
     }