* [PATCH 0/3] parted: add command to create partitions specifying the size
@ 2025-05-22 13:40 Sascha Hauer
2025-05-22 13:40 ` [PATCH 1/3] partitions: add function to find free space on partition table Sascha Hauer
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Sascha Hauer @ 2025-05-22 13:40 UTC (permalink / raw)
To: BAREBOX
So far to create partitions we have to specify both start and end of a
partition. Create a mkpart_size command to conveniently create a
partition by specifying only the size.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
Sascha Hauer (3):
partitions: add function to find free space on partition table
parted: align partitions to 1MiB
parted: implement mkpart_size command
commands/parted.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++--
common/partitions.c | 42 +++++++++++++++++++++++++++++++++
include/partitions.h | 7 +++++-
3 files changed, 111 insertions(+), 3 deletions(-)
---
base-commit: dbc28aa3aad573f6ed0199c4fb85295b95e3bd11
change-id: 20250522-parted-size-9dbf0ee60661
Best regards,
--
Sascha Hauer <s.hauer@pengutronix.de>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/3] partitions: add function to find free space on partition table
2025-05-22 13:40 [PATCH 0/3] parted: add command to create partitions specifying the size Sascha Hauer
@ 2025-05-22 13:40 ` Sascha Hauer
2025-05-22 13:40 ` [PATCH 2/3] parted: align partitions to 1MiB Sascha Hauer
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2025-05-22 13:40 UTC (permalink / raw)
To: BAREBOX
We currently only support creating partitions by specifying the exact
start and end position. Add some functions to find free space in a
partition table so that we can implement creating partitions with only
specifying the size.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
common/partitions.c | 42 ++++++++++++++++++++++++++++++++++++++++++
include/partitions.h | 3 ++-
2 files changed, 44 insertions(+), 1 deletion(-)
diff --git a/common/partitions.c b/common/partitions.c
index bc90f51f611223a59a28812b0f9854b342b30ad3..25d5f15721fc5980c927863b2745c23d7149da92 100644
--- a/common/partitions.c
+++ b/common/partitions.c
@@ -163,6 +163,48 @@ int partition_table_write(struct partition_desc *pdesc)
return pdesc->parser->write(pdesc);
}
+bool partition_is_free(struct partition_desc *pdesc, uint64_t start, uint64_t size)
+{
+ struct partition *p;
+
+ if (start < PARTITION_ALIGN_SECTORS)
+ return false;
+
+ if (start + size >= pdesc->blk->num_blocks)
+ return false;
+
+ list_for_each_entry(p, &pdesc->partitions, list) {
+ if (region_overlap_size(p->first_sec, p->size, start, size))
+ return false;
+ }
+
+ return true;
+}
+
+int partition_find_free_space(struct partition_desc *pdesc, uint64_t sectors, uint64_t *start)
+{
+ struct partition *p;
+ uint64_t min_sec = PARTITION_ALIGN_SECTORS;
+
+ min_sec = ALIGN(min_sec, PARTITION_ALIGN_SECTORS);
+
+ if (partition_is_free(pdesc, min_sec, sectors)) {
+ *start = min_sec;
+ return 0;
+ }
+
+ list_for_each_entry(p, &pdesc->partitions, list) {
+ uint64_t s = ALIGN(p->first_sec + p->size, PARTITION_ALIGN_SECTORS);
+
+ if (partition_is_free(pdesc, s, sectors)) {
+ *start = s;
+ return 0;
+ }
+ }
+
+ return -ENOSPC;
+}
+
int partition_create(struct partition_desc *pdesc, const char *name,
const char *fs_type, uint64_t lba_start, uint64_t lba_end)
{
diff --git a/include/partitions.h b/include/partitions.h
index 785fb77ab1674d92fdbac5f5df03910b1e2196af..7fd6899bd3b88f691385c330bb7900d9ec1547ac 100644
--- a/include/partitions.h
+++ b/include/partitions.h
@@ -64,6 +64,7 @@ int partition_create(struct partition_desc *pdesc, const char *name,
const char *fs_type, uint64_t lba_start, uint64_t lba_end);
int partition_remove(struct partition_desc *pdesc, int num);
void partition_table_free(struct partition_desc *pdesc);
-
+bool partition_is_free(struct partition_desc *pdesc, uint64_t start, uint64_t size);
+int partition_find_free_space(struct partition_desc *pdesc, uint64_t sectors, uint64_t *start);
#endif /* __PARTITIONS_PARSER_H__ */
--
2.39.5
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/3] parted: align partitions to 1MiB
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 ` Sascha Hauer
2025-05-22 13:40 ` [PATCH 3/3] parted: implement mkpart_size command Sascha Hauer
2025-05-26 13:48 ` [PATCH 0/3] parted: add command to create partitions specifying the size Sascha Hauer
3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2025-05-22 13:40 UTC (permalink / raw)
To: BAREBOX
Common partitioning tools align partition start offsets and sizes to
1MiB. Do likewise in barebox parted to avoid unnecessary write
amplification due to unaligned partition starts.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
commands/parted.c | 10 ++++++++--
include/partitions.h | 4 ++++
2 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/commands/parted.c b/commands/parted.c
index 68b056a0758a14e597d72f76cdc4ba6781a06051..221c6fc98601f1532ea85e000d1e2147f7a5af63 100644
--- a/commands/parted.c
+++ b/commands/parted.c
@@ -8,6 +8,7 @@
#include <linux/sizes.h>
#include <partitions.h>
#include <linux/math64.h>
+#include <range.h>
static struct partition_desc *gpdesc;
static bool table_needs_write;
@@ -160,13 +161,18 @@ static int do_mkpart(struct block_device *blk, int argc, char *argv[])
end *= mult;
/* If not on sector boundaries move start up and end down */
- start = ALIGN(start, SECTOR_SIZE);
- end = ALIGN_DOWN(end, SECTOR_SIZE);
+ start = ALIGN(start, SZ_1M);
+ end = ALIGN_DOWN(end, SZ_1M);
/* convert to LBA */
start >>= SECTOR_SHIFT;
end >>= SECTOR_SHIFT;
+ 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
diff --git a/include/partitions.h b/include/partitions.h
index 7fd6899bd3b88f691385c330bb7900d9ec1547ac..dd3e631c5aa337d752312cd94be5e7dbb4f527a5 100644
--- a/include/partitions.h
+++ b/include/partitions.h
@@ -11,6 +11,7 @@
#include <filetype.h>
#include <linux/uuid.h>
#include <linux/list.h>
+#include <linux/sizes.h>
#define MAX_PARTITION 128
#define MAX_PARTITION_NAME 38
@@ -55,6 +56,9 @@ struct partition_parser {
const char *name;
};
+#define PARTITION_ALIGN_SIZE SZ_1M
+#define PARTITION_ALIGN_SECTORS (PARTITION_ALIGN_SIZE >> SECTOR_SHIFT)
+
void partition_desc_init(struct partition_desc *pd, struct block_device *blk);
int partition_parser_register(struct partition_parser *p);
struct partition_desc *partition_table_read(struct block_device *blk);
--
2.39.5
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 3/3] parted: implement mkpart_size command
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
2025-05-26 13:48 ` [PATCH 0/3] parted: add command to create partitions specifying the size Sascha Hauer
3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2025-05-22 13:40 UTC (permalink / raw)
To: BAREBOX
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 0/3] parted: add command to create partitions specifying the size
2025-05-22 13:40 [PATCH 0/3] parted: add command to create partitions specifying the size Sascha Hauer
` (2 preceding siblings ...)
2025-05-22 13:40 ` [PATCH 3/3] parted: implement mkpart_size command Sascha Hauer
@ 2025-05-26 13:48 ` Sascha Hauer
3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2025-05-26 13:48 UTC (permalink / raw)
To: BAREBOX, Sascha Hauer
On Thu, 22 May 2025 15:40:21 +0200, Sascha Hauer wrote:
> So far to create partitions we have to specify both start and end of a
> partition. Create a mkpart_size command to conveniently create a
> partition by specifying only the size.
>
>
Applied, thanks!
[1/3] partitions: add function to find free space on partition table
https://git.pengutronix.de/cgit/barebox/commit/?id=f19d76f0dad8 (link may not be stable)
[2/3] parted: align partitions to 1MiB
https://git.pengutronix.de/cgit/barebox/commit/?id=541a8f5c5e43 (link may not be stable)
[3/3] parted: implement mkpart_size command
https://git.pengutronix.de/cgit/barebox/commit/?id=be5e1d5106a0 (link may not be stable)
Best regards,
--
Sascha Hauer <s.hauer@pengutronix.de>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-05-26 13:49 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH 3/3] parted: implement mkpart_size command Sascha Hauer
2025-05-26 13:48 ` [PATCH 0/3] parted: add command to create partitions specifying the size Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox