From: Maud Spierings via B4 Relay <devnull+maud_spierings.hotmail.com@kernel.org>
To: Sascha Hauer <s.hauer@pengutronix.de>,
BAREBOX <barebox@lists.infradead.org>
Cc: Maud Spierings <maud_spierings@hotmail.com>
Subject: [PATCH] parted: add max option to mkpart <end>
Date: Sun, 19 Oct 2025 17:07:06 +0200 [thread overview]
Message-ID: <20251019-parted-v1-1-93b0501b40d9@hotmail.com> (raw)
From: Maud Spierings <maud_spierings@hotmail.com>
Add the option to specify "max" as the end location, this will fill the
block device up to the end of its available space.
A secondary effect is that it is now possible to use two different size
units for start and end
mkpart root ext4 66MiB 3866111KiB
previously it would read 66MiB as 66KiB as it only used the last read
unit.
Signed-off-by: Maud Spierings <maud_spierings@hotmail.com>
---
At first it was considered to make <end> an optional argument. But this
would make chaining parted subcommands very difficult to manage.
Therefore I chose to make "max" an option so this can still be done like
so:
parted /dev/mmc0 mklabel gpt mkpart env bbenv 1MiB 2MiB mkpart boot fat32 2MiB 66MiB mkpart root ext4 66MiB max print
I get a checkpatch error about assignment in an if statement, the issue
is that there is an error print in parted_strtoull(), so if it checks
for a regular value but it is "max" it will print an error even though
there is none.
removing that print and adding it to every place where it is used seems
excessive too. What would be a good alternative approach?
Another option I have considered is making end=0 mean fill to the end,
if that is preferred I will make it so in v2
---
commands/parted.c | 56 +++++++++++++++++++++++++++++++++++--------------------
1 file changed, 36 insertions(+), 20 deletions(-)
diff --git a/commands/parted.c b/commands/parted.c
index 7ec56da4c15f..0e858eb881ba 100644
--- a/commands/parted.c
+++ b/commands/parted.c
@@ -138,6 +138,10 @@ static int do_mkpart(struct block_device *blk, int argc, char *argv[])
int ret;
uint64_t mult;
+ pdesc = pdesc_get(blk);
+ if (!pdesc)
+ return -EINVAL;
+
if (argc < 5) {
printf("Error: Missing required arguments\n");
return -EINVAL;
@@ -150,40 +154,51 @@ static int do_mkpart(struct block_device *blk, int argc, char *argv[])
if (ret)
return ret;
- ret = parted_strtoull(argv[4], &end, &mult);
- if (ret)
- return ret;
-
if (!mult)
mult = gunit;
-
start *= mult;
- end *= mult;
- /* If not on sector boundaries move start up and end down */
+ /* If not on sector boundaries round start up */
start = ALIGN(start, SZ_1M);
- end = ALIGN_DOWN(end, SZ_1M);
/* convert to LBA */
start >>= SECTOR_SHIFT;
- end >>= SECTOR_SHIFT;
+
+ if (!strcmp(argv[4], "max")) {
+ /* gpt requires 34 blocks at the end */
+ if (pdesc->parser->type == filetype_gpt)
+ end = blk->num_blocks - 35;
+ else if (pdesc->parser->type == filetype_mbr)
+ end = blk->num_blocks - 1;
+ else
+ return -ENOSYS;
+ } else if (!(ret = parted_strtoull(argv[4], &end, &mult))) {
+ if (!mult)
+ mult = gunit;
+ end *= mult;
+
+ /* If not on sector boundaries round end down */
+ end = ALIGN_DOWN(end, SZ_1M);
+
+ /* convert to LBA */
+ end >>= SECTOR_SHIFT;
+
+ /*
+ * When unit is >= KB then subtract one sector for user
+ * convenience. It allows to start the next partition where the
+ * previous ends
+ */
+ if (mult >= 1000)
+ end -= 1;
+ } else {
+ return ret;
+ }
if (end == start) {
printf("Error: After alignment the partition has zero size\n");
return -EINVAL;
}
- /*
- * When unit is >= KB then substract one sector for user convenience.
- * It allows to start the next partition where the previous ends
- */
- if (mult >= 1000)
- end -= 1;
-
- pdesc = pdesc_get(blk);
- if (!pdesc)
- return -EINVAL;
-
ret = partition_create(pdesc, name, fs_type, start, end);
if (!ret)
@@ -424,6 +439,7 @@ BAREBOX_CMD_HELP_TEXT("<type> must be \"gpt\" or \"msdos\"")
BAREBOX_CMD_HELP_TEXT("<fstype> can be one of \"ext2\", \"ext3\", \"ext4\", \"fat16\", \"fat32\" or \"bbenv\"")
BAREBOX_CMD_HELP_TEXT("<name> for MBR partition tables can be one of \"primary\", \"extended\" or")
BAREBOX_CMD_HELP_TEXT("\"logical\". For GPT this is a name string.")
+BAREBOX_CMD_HELP_TEXT("<end> can be \"max\" it will take all remaining space")
BAREBOX_CMD_HELP_END
BAREBOX_CMD_START(parted)
---
base-commit: 8defba1d0ab1aef9dd5d57710e18d0d02e2c48e2
change-id: 20251019-parted-b0d637580e80
Best regards,
--
Maud Spierings <maud_spierings@hotmail.com>
next reply other threads:[~2025-10-20 16:24 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-19 15:07 Maud Spierings via B4 Relay [this message]
2025-10-20 8:16 ` Sascha Hauer
2025-10-20 8:23 ` Maud Spierings
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=20251019-parted-v1-1-93b0501b40d9@hotmail.com \
--to=devnull+maud_spierings.hotmail.com@kernel.org \
--cc=barebox@lists.infradead.org \
--cc=maud_spierings@hotmail.com \
--cc=s.hauer@pengutronix.de \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox