Skip to content

Conversation

@mkshevetskiy
Copy link
Contributor

@mkshevetskiy mkshevetskiy commented Jan 6, 2026

This MR is a collection of small improvements to ptgen utility that touch mostly the same code - they are not split into different MRs to avoid unnecessary merge conflicts.

The improvements are as follows:

  • Fixed a bug where a broken stub partition will be created if the "-e <gpt_entry_offs>" option was used and there was no gap between the GPT entry table and the first partition.
  • Add option to specify GPT partition index
  • Add option to disable creation of GPT stub partition
  • Add getopt_long() versions of options and expand the help text to be more verbose about them

@mkshevetskiy
Copy link
Contributor Author

mkshevetskiy commented Jan 6, 2026

@dangowrt could you look on it?

@mkshevetskiy
Copy link
Contributor Author

@Rondom

The commit 0782d24 (Revert "ptgen: do not create stub partition to
fill a gap if gap caused by alignment") fixes one issue but introduce
another.

If "-e <gpt_entry_offs>" option is used and there is no gap between GPT
Entry Table and 1-st partition, then

 1) A GPT stub partition will be created, but this should not be done
    because of no space for it.
 2) A stub partition will be incorrect (start_sector > end_sector)

This patch fixes an issue.

Fixes: 0782d24 (Revert "ptgen: do not create stub partition to fill a gap if gap caused by alignment")
Signed-off-by: Mikhail Kshevetskiy <[email protected]>
This patch allows to specify the GPT entry index to use. Subsequent
partitions will use sequential indexes, starting with the specified one.
This will continue until a new starting index is specified.

The patch ensures that the same index will never be used twice.

[Explanation within example]

ptgen creates gpt partitions using consecutive indexes starting from
zero. For example the following command

  ptgen -o mmc.img -g -d 32M \
	-p 2M \			# describe /dev/mmcblk0p1 (index=0)
	-p 4M \			# describe /dev/mmcblk0p2 (index=1)
	-p 25M			# describe /dev/mmcblk0p3 (index=2)

will create

  $ /sbin/fdisk mmc.img
  ...
  Device     Start   End Sectors Size Type
  mmc.img1      34  4129    4096   2M Linux filesystem  <= see here
  mmc.img2    4130 12321    8192   4M Linux filesystem
  mmc.img3   12322 63521   51200  25M Linux filesystem

Sometimes it's necessary to create one or more partitions whose indexes
don't follow above rule. For example, we might want the partition located
at the very beginning of the mmc flash to be named as /dev/mmcblk0p7.
Within a patch this can be achieved by a command

  ptgen -o mmc.img -g -d 32M \
	-i 6 -p 2M \		# describe /dev/mmcblk0p7 (index=6)
	-i 0 -p 4M \		# describe /dev/mmcblk0p1 (index=0)
	     -p 20M 		# describe /dev/mmcblk0p2 (index=1)

  $ /sbin/fdisk mmc.img
  ...
  Device     Start   End Sectors Size Type
  mmc.img1    4130 12321    8192   4M Linux filesystem
  mmc.img2   12322 63521   51200  25M Linux filesystem
  mmc.img7      34  4129    4096   2M Linux filesystem  <== see here

So we done it.

Signed-off-by: Mikhail Kshevetskiy <[email protected]>
ptgen tends to create stub partitions between the end of GPT entry table
and the start of the first disk. This patch adds an option to disable
this behavior.

Signed-off-by: Mikhail Kshevetskiy <[email protected]>
ptgen help is bad:
 - there are a lot of single letter options (so the option name tells
   nothing)
 - some options does not mentioned in the ptgen help
 - there are no any options explanations

This patch
 - adds long option support
 - provides more usefull help
 - add description for missed options

Signed-off-by: Mikhail Kshevetskiy <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant