bug-parted
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

treat "1MiB" like "1048576B"


From: Jim Meyering
Subject: treat "1MiB" like "1048576B"
Date: Sun, 16 Jan 2011 13:25:56 +0100

[ I noticed that using a starting address of "1MiB" evokes a warning,
  along with the obviously unintended start sector of 34:

    parted -s -- $dev mklabel gpt mkpart PARTITION-NAME 1MiB -34s
    Warning: The resulting partition is not properly aligned for best 
performance.

  While the following, with "1048576B", works fine and creates the partition
  starting at the desired 1MiB address:

    parted -s -- $dev mklabel gpt mkpart PARTITION-NAME 1048576B -34s
  ]

I made parted tell me why it was treating "1MiB" differently from
"1048576B".  Bottom line is that if you use a large unit, like MiB, it
assumes you're being sloppy, but if you specify bytes or sectors, you
require exactness (radius = 0).  That may have made sense when people
used sloppy units like MB (1,000,000), but obviously does not hold for
most people who bother to type "MiB".

Here's a proposed patch to make the command I gave work the way I
intended.  Opinions welcome.  On one hand, I don't particularly like
treating 1MiB differently from 1MB, but if someone is using 1MB and
intends 1MiB, they need a little wake up call (or some RTFM).
[the full patch will include a documentation update as well as
a couple of tests to illustrate/exercise the difference. ]

In the mean time, to get the proper 1MiB alignment, you'll have to
use an explicit byte or sector count.  I prefer byte counts, in spite of
the greater number of digits, because that works the same regardless
of a disk's sector size:

    dev=...
    k=1024 m=$((k*k))
    parted -s -- $dev mklabel gpt mkpart P-NAME ${m}B -34s


diff --git a/libparted/unit.c b/libparted/unit.c
index 2670c38..59a9644 100644
--- a/libparted/unit.c
+++ b/libparted/unit.c
@@ -480,6 +480,12 @@ parse_unit_suffix (const char* suffix, PedUnit 
suggested_unit)
        return suggested_unit;
 }

+static bool
+is_power_of_2 (long long n)
+{
+  return (n & (n - 1)) == 0;
+}
+
 /**
  * If \p str contains a valid description of a location on \p dev, then
  * \p *sector is modified to describe the location and a geometry is created
@@ -530,6 +536,13 @@ ped_unit_parse_custom (const char* str, const PedDevice* 
dev, PedUnit unit,
        radius = ped_div_round_up (unit_size, dev->sector_size) - 1;
        if (radius < 0)
                radius = 0;
+       /* If the user specified units in a power of 2, e.g., 1MiB, as in
+              parted -s -- $dev mklabel gpt mkpart P-NAME 1MiB -34s
+          do not use 1MiB as the range.  Rather, presume that they
+          are specifying precisely the starting or ending number,
+          and treat "1MiB" just as we would treat "1048576B".  */
+       if (is_power_of_2 (unit_size))
+               radius = 0;

        *sector = num * unit_size / dev->sector_size;
        /* negative numbers count from the end */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]