Skip to main content

gptsum

A tool to make disk images using GPT partitions self-verifiable, like isomd5sum.

Note this only works for read-only, immutable images!

Quickstart

First, create an empty disk image:

$ truncate -s64M image.raw

Then, create a GPT partition table in it. Note, below we set an explicit label-id. In general, this should not be done and a random GUID will be generated. Simply remove the line.

$ sfdisk image.raw << EOF
label: gpt
label-id: 132e3631-1ec9-4411-ab25-9b95b54b0903
first-lba: 2048
EOF

Checking that no-one is using this disk right now ... OK

Disk image.raw: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

>>> Script header accepted.
>>> Script header accepted.
>>> Script header accepted.
>>> Done.
Created a new GPT disklabel (GUID: 132E3631-1EC9-4411-AB25-9B95B54B0903).

New situation:
Disklabel type: gpt
Disk identifier: 132E3631-1EC9-4411-AB25-9B95B54B0903

The partition table has been altered.
Syncing disks.

We can retrieve the current disk GUID using gptsum:

$ gptsum get-guid image.raw
132e3631-1ec9-4411-ab25-9b95b54b0903

Verification should fail:

$ gptsum verify image.raw || echo "Verification failed!"
Disk GUID doesn't match expected checksum, got 132e3631-1ec9-4411-ab25-9b95b54b0903, expected 6190f5bb-1967-14ec-9fbd-a7d213a45461
Verification failed!

Embed the disk checksum as the label GUID:

$ gptsum embed image.raw

Verification should now succeed:

$ gptsum verify image.raw && echo "Verification succeeded!"
Verification succeeded!

Indeed, the GUID was changed:

$ gptsum get-guid image.raw
6190f5bb-1967-14ec-9fbd-a7d213a45461

How It Works

Generally, when checksums are used to validate the integrity of a file, this checksum needs to be provided out-of-band, e.g., in a separate file. This complicates the process a bit, since now multiple files need to be kept around (and potentially in sync).

Being able to verify a file’s integrity without any external information would be great. However, if we embed the checksum of a file in the file itself, we change its checksum and verification would fail. So we need to apply some tricks.

The isomd5sum tool is often used to verify the integrity of ISO files, e.g., Linux distribution releases. It uses an unused location in the ISO9660 file format to embed an MD5 checksum of the actual data segments of said file. As such, this MD5 checksum does not represent the complete file contents but only the pieces of data we’re interested in.

We can translate this to GPT-partitioned disk images as well. In the GPT format, there’s no room to embed any arbitrary blobs (unless we’d use the reserved or padding sections of the headers, which should be zeroed out so we shouldn’t). However, GPT disks are identified by a GUID which is stored in the two metadata sections stored on the disk, at LBA 1 and as the last LBA on the disk (the so-called primary and secondary GPT headers). This leaves room for 16 bytes of semi-arbitrary data.

Furthermore, the GPT headers themselves, including the GUID, are protected using a CRC32 checksum.

As such, we can apply the following procedure:

  • Zero out the CRC32 and GUID fields in both GPT headers

  • Calculate a 16 byte checksum of the resulting image (covering all data, except for the CRC32 and GUID fields)

  • Embed the checksum as the GUID field in both GPT headers (now becoming the disk GUID)

  • Update the CRC32 fields in both GPT headers

At this point we have a fully valid GPT disk image with a GUID representing the actual data contained in the image. One could argue this is no longer a valid GUID (indeed it’s not), but since it’s generated using a secure hashing algorithm over a (potentially large) file, we can assume the entropy is sufficient to avoid collisions. Essentially, if two disk images were to get the same GUID, they’re very likely the exact same disk image, content-wise.

Verifying an image file is roughly the same procedure:

  • Zero out the CRC32 and GUID fields in both GPT headers (in-memory)

  • Calculate a 16 byte checksum of the resulting image

  • Verify this calculated checksum equals the actual GUID embedded in the image

Implementation Details

gptsum is implemented in Python, and compatible with Python 3.8 and later. It uses the Blake2b checksum algorithm to construct a 16 byte digest of the disk image.

Various subcommands are exposed by the CLI, refer to the documentation for more details.

Release files for gptsum 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gptsum 0.5.0
File Size Uploaded
gptsum-0.5.0.tar.gz 75.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gptsum 0.5.0
File Interpreter ABI Platform
gptsum-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 91.6 kB

Release files / gptsum-0.5.0.tar.gz

Download URL gptsum-0.5.0.tar.gz
Size 75.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d51434998789fcacd46092813abc0a3ce1e2e8656197a2014e3651bbd7f7087d
BLAKE2b-256 checksum
How to use checksums
b2d815b5e5ea219755b3fa90c71a5cb44824b1133eba030056af998fbb1a37e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release files / gptsum-0.5.0-py3-none-any.whl

Download URL gptsum-0.5.0-py3-none-any.whl
Size 16.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9f1c927145c191835cecb4d87e9f00bba4e3bc98c6108d7d72746bbc4a9e2924
BLAKE2b-256 checksum
How to use checksums
f474b14e290b09be75de834f3c88b1393741ee0c0dd66e59338f0bc32009b181
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page