mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 3/3] parted: implement mkpart_size command
Date: Thu, 22 May 2025 15:40:24 +0200	[thread overview]
Message-ID: <20250522-parted-size-v1-3-369d146aa100@pengutronix.de> (raw)
In-Reply-To: <20250522-parted-size-v1-0-369d146aa100@pengutronix.de>

This creates a command which creates a partition with specified size.
The command finds a suitable place for the partition itself.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/parted.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/commands/parted.c b/commands/parted.c
index 221c6fc98601f1532ea85e000d1e2147f7a5af63..6872a8414c79e8e116c9ecce864888cec542ad18 100644
--- a/commands/parted.c
+++ b/commands/parted.c
@@ -192,6 +192,57 @@ static int do_mkpart(struct block_device *blk, int argc, char *argv[])
 	return ret < 0 ? ret : 5;
 }
 
+static int do_mkpart_size(struct block_device *blk, int argc, char *argv[])
+{
+	struct partition_desc *pdesc;
+	uint64_t size, start;
+	const char *name, *fs_type;
+	int ret;
+	uint64_t mult;
+
+	if (argc < 4) {
+		printf("Error: Missing required arguments\n");
+		return -EINVAL;
+	}
+
+	name = argv[1];
+	fs_type = argv[2];
+
+	ret = parted_strtoull(argv[3], &size, &mult);
+	if (ret)
+		return ret;
+
+	if (!mult)
+		mult = gunit;
+
+	size *= mult;
+
+	/* If not on sector boundaries move start up and end down */
+	size = ALIGN(size, PARTITION_ALIGN_SIZE);
+
+	/* convert to LBA */
+	size >>= SECTOR_SHIFT;
+
+	pdesc = pdesc_get(blk);
+	if (!pdesc)
+		return -EINVAL;
+
+	ret = partition_find_free_space(pdesc, size, &start);
+	if (ret) {
+		printf("No free space for %llu sectors found\n", size);
+		return ret;
+	}
+
+	printf("%s: creating partition with %llu blocks at %llu\n", __func__, size, start);
+
+	ret = partition_create(pdesc, name, fs_type, start, start + size - 1);
+
+	if (!ret)
+		table_needs_write = true;
+
+	return ret < 0 ? ret : 4;
+}
+
 static int do_rmpart(struct block_device *blk, int argc, char *argv[])
 {
 	struct partition_desc *pdesc;
@@ -267,6 +318,9 @@ struct parted_command parted_commands[] = {
 		.name = "mkpart",
 		.command = do_mkpart,
 	}, {
+		.name = "mkpart_size",
+		.command = do_mkpart_size,
+	},  {
 		.name = "print",
 		.command = do_print,
 	}, {
@@ -360,6 +414,7 @@ BAREBOX_CMD_HELP_OPT ("print", "print partitions")
 BAREBOX_CMD_HELP_OPT ("mklabel <type>", "create a new partition table")
 BAREBOX_CMD_HELP_OPT ("rm <num>", "remove a partition")
 BAREBOX_CMD_HELP_OPT ("mkpart <name> <fstype> <start> <end>", "create a new partition")
+BAREBOX_CMD_HELP_OPT ("mkpart_size <name> <fstype> <size>", "create a new partition")
 BAREBOX_CMD_HELP_OPT ("unit <unit>", "change display/input units")
 BAREBOX_CMD_HELP_OPT ("refresh", "refresh a partition table")
 BAREBOX_CMD_HELP_TEXT("")

-- 
2.39.5




  parent reply	other threads:[~2025-05-22 13:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-22 13:40 [PATCH 0/3] parted: add command to create partitions specifying the size Sascha Hauer
2025-05-22 13:40 ` [PATCH 1/3] partitions: add function to find free space on partition table Sascha Hauer
2025-05-22 13:40 ` [PATCH 2/3] parted: align partitions to 1MiB Sascha Hauer
2025-05-22 13:40 ` Sascha Hauer [this message]
2025-05-26 13:48 ` [PATCH 0/3] parted: add command to create partitions specifying the size Sascha Hauer

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=20250522-parted-size-v1-3-369d146aa100@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox