bug#50168: Inconsistent behavior creating partitions with 'Xmib' and 'X%' (off-by-1 error?)

Diederik de Haas <[email protected]> Sun, 22 Aug 2021 19:59:06 +0200
Newsgroups gmane.comp.gnu.parted.bugs
Organization Connecting Knowledge
Message-ID <5866920.Mn9LVKsFZs@bagend>
[I'm not sure this is the appropriate place/way and if not, apologies, and can 
you point me to the right place/way]

Hi,

This is a forward of https://bugs.debian.org/988146 where I reported that 
partitions were created differently when using 'mib' unit vs '%' unit.

To demonstrate it, I created 3 scripts which creates a 100MB image and do the 
partitioning within that. 
When reporting the Debian bug, I only had the mixed test and 'parted-bug-test-
mixed.sh' is identical to the one attached here, apart from an 'else' clause 
which explicitly deletes a prior created image.

In parted-bug-test-mixed.sh, I mixed 'mib' and '%' and due to the 100MB, that 
should've worked, but it did not.

When using only 'mib' then the script fails too.
When using only '%' then the script succeeds.

I think parted does the right thing when using '%'.

Relevant portion of output when running the mixed script:
=======================================================
Creating partition table ... Done
Creating 1st partition ('4mib' '20%') ... Done
Creating 2nd partition ('20%' '40%' ... Done
Creating 3rd partition ('40mib' '60mib') ... Done

Showing partition layout
Disk temp/parted-test.img: 100 MiB, 104857600 bytes, 204800 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x03f77810

Device                Boot Start    End Sectors Size Id Type
temp/parted-test.img1       8192  40959   32768  16M  c W95 FAT32 (LBA)
temp/parted-test.img2      40960  81919   40960  20M 83 Linux
temp/parted-test.img3      81920 122880   40961  20M 83 Linux

Creating 4th partition ('60mib' '100%' ... Error: You requested a partition 
from 62,9MB to 105MB (sectors 122880..204799).
The closest location we can manage is 62,9MB to 105MB (sectors 
122881..204799).
=======================================================

Cheers,
  Diederik
parted-bug-test-mb.sh (application/x-shellscript, 1.3 KB)
#!/bin/sh
BUILD_DIR="temp"
IMAGE_NAME="parted-test-mb.img"
IMAGE_PATH="$BUILD_DIR/$IMAGE_NAME"
IMAGE_SIZE="100M"

echo "BUILD_DIR: $BUILD_DIR"
echo "IMAGE_NAME: $IMAGE_NAME"
echo "IMAGE_PATH: $IMAGE_PATH"
echo "IMAGE_SIZE: $IMAGE_SIZE"

if [ ! -d "$BUILD_DIR" ] ; then
    echo "directory $BUILD_DIR doesn't exist; create it"
    mkdir $BUILD_DIR
else
    if [ -f "$IMAGE_PATH" ] ; then
        echo "Removing previous image"
        rm "$IMAGE_PATH"
    fi
fi

echo "Creating image at '$IMAGE_PATH' of size '$IMAGE_SIZE'"
qemu-img create -f raw "$IMAGE_PATH" "$IMAGE_SIZE"
echo "Image file created"

echo -n "Creating partition table ... "
/sbin/parted -s $IMAGE_PATH mklabel msdos
echo "Done"

echo -n "Creating 1st partition ('4mib' '20mib') ... "
/sbin/parted -s $IMAGE_PATH mkpart primary fat32 '4mib' '20mib'
echo "Done"

echo -n "Creating 2nd partition ('20mib' '40mib' ... "
/sbin/parted -s $IMAGE_PATH mkpart primary ext4 '20mib' '40mib'
echo "Done"

echo -n "Creating 3rd partition ('40mib' '60mib') ... "
/sbin/parted -s $IMAGE_PATH mkpart primary ext4 '40mib' '60mib'
echo "Done"

echo ""
echo "Showing partition layout"
/sbin/fdisk -l $IMAGE_PATH

echo ""

echo -n "Creating 4th partition ('60mib' '100mib' ... "
/sbin/parted -s $IMAGE_PATH mkpart primary ext4 '60mib' '100mib'
echo "Done"
parted-bug-test-perc.sh (application/x-shellscript, 1.2 KB)
#!/bin/sh
BUILD_DIR="temp"
IMAGE_NAME="parted-test-perc.img"
IMAGE_PATH="$BUILD_DIR/$IMAGE_NAME"
IMAGE_SIZE="100M"

echo "BUILD_DIR: $BUILD_DIR"
echo "IMAGE_NAME: $IMAGE_NAME"
echo "IMAGE_PATH: $IMAGE_PATH"
echo "IMAGE_SIZE: $IMAGE_SIZE"

if [ ! -d "$BUILD_DIR" ] ; then
    echo "directory $BUILD_DIR doesn't exist; create it"
    mkdir $BUILD_DIR
else
    if [ -f "$IMAGE_PATH" ] ; then
        echo "Removing previous image"
        rm "$IMAGE_PATH"
    fi
fi

echo "Creating image at '$IMAGE_PATH' of size '$IMAGE_SIZE'"
qemu-img create -f raw "$IMAGE_PATH" "$IMAGE_SIZE"
echo "Image file created"

echo -n "Creating partition table ... "
/sbin/parted -s $IMAGE_PATH mklabel msdos
echo "Done"

echo -n "Creating 1st partition ('4%' '20%') ... "
/sbin/parted -s $IMAGE_PATH mkpart primary fat32 '4%' '20%'
echo "Done"

echo -n "Creating 2nd partition ('20%' '40%' ... "
/sbin/parted -s $IMAGE_PATH mkpart primary ext4 '20%' '40%'
echo "Done"

echo -n "Creating 3rd partition ('40%' '60%') ... "
/sbin/parted -s $IMAGE_PATH mkpart primary ext4 '40%' '60%'
echo "Done"

echo ""
echo "Showing partition layout"
/sbin/fdisk -l $IMAGE_PATH

echo ""

echo -n "Creating 4th partition ('60%' '100%' ... "
/sbin/parted -s $IMAGE_PATH mkpart primary ext4 '60%' '100%'
echo "Done"
parted-bug-test-mixed.sh (application/x-shellscript, 1.3 KB)
#!/bin/sh
BUILD_DIR="temp"
IMAGE_NAME="parted-test-mixed.img"
IMAGE_PATH="$BUILD_DIR/$IMAGE_NAME"
IMAGE_SIZE="100M"

echo "BUILD_DIR: $BUILD_DIR"
echo "IMAGE_NAME: $IMAGE_NAME"
echo "IMAGE_PATH: $IMAGE_PATH"
echo "IMAGE_SIZE: $IMAGE_SIZE"

if [ ! -d "$BUILD_DIR" ] ; then
    echo "directory $BUILD_DIR doesn't exist; create it"
    mkdir $BUILD_DIR
else
    if [ -f "$IMAGE_PATH" ] ; then
        echo "Removing previous image"
        rm "$IMAGE_PATH"
    fi
fi

echo "Creating image at '$IMAGE_PATH' of size '$IMAGE_SIZE'"
qemu-img create -f raw "$IMAGE_PATH" "$IMAGE_SIZE"
echo "Image file created"

echo -n "Creating partition table ... "
/sbin/parted -s $IMAGE_PATH mklabel msdos
echo "Done"

echo -n "Creating 1st partition ('4mib' '20%') ... "
/sbin/parted -s $IMAGE_PATH mkpart primary fat32 '4mib' '20%'
echo "Done"

echo -n "Creating 2nd partition ('20%' '40%' ... "
/sbin/parted -s $IMAGE_PATH mkpart primary ext4 '20%' '40%'
echo "Done"

echo -n "Creating 3rd partition ('40mib' '60mib') ... "
/sbin/parted -s $IMAGE_PATH mkpart primary ext4 '40mib' '60mib'
echo "Done"

echo ""
echo "Showing partition layout"
/sbin/fdisk -l $IMAGE_PATH

echo ""

echo -n "Creating 4th partition ('60mib' '100%' ... "
/sbin/parted -s $IMAGE_PATH mkpart primary ext4 '60mib' '100%'
echo "Done"
signature.asc (application/pgp-signature, 228 B)
-----BEGIN PGP SIGNATURE-----

iHUEABYKAB0WIQT1sUPBYsyGmi4usy/XblvOeH7bbgUCYSKQagAKCRDXblvOeH7b
broHAQCNoLuZ0bO9HeSvjcTwv0y+GMOWbJqJLfI9Hh5z1rMmRgEAiFCVHHzFOtU5
+koENhKra8By5F/CeoRTtMs+TH6s2QA=
=P+9O
-----END PGP SIGNATURE-----