From: Tobias Stoeckmann <tobias@stoeckmann.org>
To: util-linux@vger.kernel.org
Subject: [PATCH 2/2] Distinguish between first LBA sector and partition
Date: Sat, 27 Aug 2016 19:28:48 +0200 [thread overview]
Message-ID: <20160827172848.GB11886@localhost> (raw)
An insufficient check leads to an invalid free space output, as seen here:
$ dd if=/dev/zero of=cfdisk.iso bs=1M count=1
$ losetup -f cfdisk.iso
$ echo w | fdisk /dev/loop0
$ echo '1,1' | sfdisk /dev/loop0 --append
$ echo '3,' | sfdisk /dev/loop0 --append
$ sfdisk --list-free /dev/loop0
Start End Sectors Size
1 2 2 1K
$ _
In this case, libfdisk fails to notice that it tries to calculate space
between two partitions, not between start of disk and first partition.
Currently, the code tries to achieve that by checking the address of the
last "partition", which is the first_lba block. Now if the first
partition is merely 1 block in size, the "last" address is still equal
to the first_lba block, which renders the check in libfdisk for the next
partition invalid.
I chose to use "nparts == 0" for this check, because the partitions are
properly sorted before iterating over them.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
---
libfdisk/src/table.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libfdisk/src/table.c b/libfdisk/src/table.c
index bf30d1b..f975168 100644
--- a/libfdisk/src/table.c
+++ b/libfdisk/src/table.c
@@ -617,9 +617,9 @@ int fdisk_get_freespaces(struct fdisk_context *cxt, struct fdisk_table **tb)
* aligned, the exception is space before the first partition where
* we assume that cxt->first_lba is aligned. */
if (last + grain < pa->start
- || (last < pa->start && last == cxt->first_lba)) {
+ || (last < pa->start && nparts == 0)) {
rc = table_add_freespace(cxt, *tb,
- last + (last > cxt->first_lba ? 1 : 0),
+ last + (nparts == 0 ? 0 : 1),
pa->start - 1, NULL);
}
/* add gaps between logical partitions */
--
2.9.3
next reply other threads:[~2016-08-27 17:28 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-08-27 17:28 Tobias Stoeckmann [this message]
2016-08-29 11:35 ` [PATCH 2/2] Distinguish between first LBA sector and partition Karel Zak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160827172848.GB11886@localhost \
--to=tobias@stoeckmann.org \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.