/blkpg-part

Partition table and disk geometry handling utility.

Primary LanguageShellGNU Lesser General Public License v2.1LGPL-2.1

blkpg-part

Partition table and disk geometry handling utility

TL;DR;

blkpg-part(1) creates temporary partitions that are not part of the MBR. It makes a partition block device from any consecutive blocks that are not partitioned.

DESCRIPTION

blkpg-part(1) creates, resizes and deletes partitions on the fly without writing back the changes to the partition table.

Under the hood, blkpg-part(1) uses the three following ioctl(3P) from the header linux/blkpg.h:

  • BLKPG_ADD_PARTITION
  • BLKPG_DEL_PARTITION
  • BLKPG_RESIZE_PARTITION

Thanks to blkpg-part(1), it is possible to export any consecutive blocks, that are not already part of a partition, as a temporary partitioned block device.

A typically use case in embedded systems is to export hidden blobs that are stored in raw in block devices (i.e. blobs that are not stored into a file-system).

CREATE PARTITION

The creation of a temporary partition takes:

  1. the block device (ex. /dev/mmcblk0)
  2. an arbitrary partition number (ex. 100)
  3. the offset and the length of the desired partition in bytes[*].

Only consecutive blocks that are not a part of an existing partition can create a partition.

[*]: Both offsets and sizes are expressed in bytes and should be a multiple of block size (512 Bytes).

CREATE EXAMPLE

A dummy example for DOS partition scheme devices is to create a partition that exports the MBR:

# blkpg-part add /dev/mmcblk0 100 0 512
# hexdump -C /dev/mmcblk0p100
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 xx xx  |................|
000001c0  xx xx xx xx xx xx xx xx  xx xx xx xx xx xx xx xx  |................|
000001d0  xx xx xx xx xx xx xx xx  xx xx xx xx xx xx xx xx  |................|
000001e0  xx xx xx xx xx xx xx xx  xx xx xx xx xx xx xx xx  |................|
000001f0  xx xx xx xx xx xx xx xx  xx xx xx xx xx xx 55 aa  |..............U.|
00000200

DELETE PARTITION

The deletion of an existing partition takes:

  1. the block device (ex. /dev/mmcblk0)
  2. the partition number (ex. 1 or 100)

Both temporary partition and partition from the partition table can be deleted.

DELETE EXAMPLES

A dummy example for DOS partition scheme devices is to delete the recently created temporary partition that exports the MBR:

# blkpg-part delete /dev/mmcblk0 100
# hexdump -C /dev/mmcblk0p100
hexdump: /dev/mmcblk0p100: No such file or directory
hexdump: all input file arguments failed

Another example is to delete the existing first partition from the partition table:

# blkpg-part delete /dev/mmcblk0 1
# hexdump -C /dev/mmcblk0p1
hexdump: /dev/mmcblk0p1: No such file or directory
hexdump: all input file arguments failed

CONCRETE EXAMPLE

Here is a simple DOS partition scheme on an SD-Card where there is space that is not partitioned at the beginning of the disk; between the MBR and the first partition (Boot).

Partition Offset Length Device name
MBR 0 1 (512B) -
(empty) 1 8191 (4MB) -
Boot 8192 (4M) 8192 (4MB) /dev/mmcblkp1
RootFS 16384 (8M) 16384 (8MB) /dev/mmcblkp2
Data 32768 (16M) - /dev/mmcblkp3

This kind of scheme is common on embedded devices. This empty space usually hides blobs specific to the SoC such as bootloaders stored in raw format [1] into the block device.

Those blobs are generally dd'ed at a very specific offset which depends on the ROMCode[2] of the SoC in use.

The ROMCode of TI AM335x SoCs loads a second stage bootloader that loads a third stage bootloader that loads the operating system.

This very first piece of software is called MLO and must be stored at offset 0 on the booting device. It can also be duplicated at offsets 0x20000, 0x40000 and 0x60000[3].

Note: Because of this example is using a DOS partition scheme, the MLO cannot be placed at offset 0, otherwise it overrides the DOS partition table.

# dd if=MLO of=/dev/mmcblk0 skip=$((0x20000))
# dd if=MLO of=/dev/mmcblk0 skip=$((0x40000))
# dd if=MLO of=/dev/mmcblk0 skip=$((0x60000))

It offers two advantages:

  1. It is not necessary to remember the precise offset: the new created block device knows where to start.
  2. If the blob to dd is bigger than expected, the copy does not override the following partition: the new created block device knows where to stop.
# dd if=MLO of=/dev/mmcblk0p101
# dd if=MLO of=/dev/mmcblk0p102
# dd if=MLO of=/dev/mmcblk0p103

[1]: The raw format is to opposed to file-system where the blob is easily identifiable using a human readable identifier (filename).

[2]: The ROMCode is the bootloader that is hardcoded inside the SoC. Its goal is to load the first piece of software that will be run by the CPU. It is usually referenced as the first stage bootloader.

[3]: The ROMCode of TI AM335x SoCs allows the MLO to be stored in a FAT partition. But this case does not illustrate how blkpg-part can be useful in such situation.

UDEV RULES

udev(7) automates the creation of a temporary partition via static rules by RUN-ing blkpg-part(1) matching the name of a non-deleted block device.

An example of an udev rule is available in in the in the support directory.

SYSTEMD GENERATOR

systemd-generator(7) is another alternative to automate the creation of temporary partitions.

blkpgtab-generator(8) generates systemd.service(5)-s to create temporary partitions at early boot via the static blkpgtab(5) file.

An example of a blkpgtab is available in in the in the support directory.

EMBEDDED BUILD SYSTEMS

blkpg-part(1) is part of the rtone-br2-external's Buildroot br2-external and the meta-blkpg-part's OpenEmbedded layer.

PATCHES

Sumbit patches at https://github.com/gportay/blkpg-part/pulls

BUGS

Report bugs at https://github.com/gportay/blkpg-part/issues

AUTHOR

Written by Gaël PORTAY gael.portay@gmail.com

COPYRIGHT

Copyright 2018,2021,2023-2024 Gaël PORTAY 2023-2024 Rtone. 2021 Collabora Ltd. 2018 Savoir-Faire Linux Inc.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 2.1 of the License, or (at your option) any later version.

SEE ALSO

ioctl(3P)