Inspect Partition Tables Safely with partx
Before you reach for fdisk or a script that parses partition rows by hand, check whether partx already gives you exactly the columns you need. You'll finish with a repeatable way to read a disk or disk image's partition table, select one partition, and produce script-friendly output, plus a clear picture of what the administrative options actually change. Allow about fifteen minutes.
The route
Jump straight to the step you need, or tick off Done means at the end.
- Needs: a shell and the util-linux package. A test image is handy if you don't want to touch a real disk.
- Versions matter: this follows the installed manual for util-linux 2.39.3. On this machine, the Ubuntu package is
util-linux 2.39.3-9ubuntu6.6, butpartxitself resolves to a util-linux 2.41.3 binary in/home/linuxbrew/.linuxbrew/sbin. Check your own path and version before trusting a small output difference.
1. Confirm which partx you are running
Start with read-only checks, no elevated privileges needed:
$ command -v partx
/home/linuxbrew/.linuxbrew/sbin/partx
$ partx --version
partx from util-linux 2.41.3
$ partx --help
The command takes a disk, or a partition together with its parent disk. The main read-only mode is --show (short form -s). The older --list form is documented as deprecated, so use --show in anything new.
Checkpoint
If command -v points at an unexpected install, stop and decide whether that shell path is intentional. Don't mix help text from one binary with output from another.
2. List a real disk or image
Swap in a path you have permission to read. Listing is normally unprivileged, though device permissions or host policy can still demand sudo:
$ partx --show /path/to/disk-image.img
NR START END SECTORS SIZE NAME UUID
1 2048 18431 16384 8M 45cfca7a-01
2 18432 34815 16384 8M 45cfca7a-02
Your rows will differ. NR is the partition number, START and END are sector positions, SECTORS is the length. Every displayed number is a 512-byte sector except SIZE, which is human-readable and uses binary units, so it shows 8M rather than the longer 8 MiB form.
For a block device, use its exact path such as /dev/sdb, confirmed with lsblk rather than guessed, and check its mount points before running anything that changes kernel bookkeeping.
3. Select one partition with stable columns
Use --nr to pick a particular partition or range. A single number selects one; a colon range selects an inclusive interval:
$ partx --show --nr 2 /path/to/disk-image.img
NR START END SECTORS SIZE NAME UUID
2 18432 34815 16384 8M 45cfca7a-02
$ partx --show --nr 2:4 /path/to/disk-image.img
- Open-ended ranges work too:
2:means partition 2 onwards,:4means through partition 4. - Negative numbers count from the end:
--nr -1:-1selects the last partition. - Quote a range if your shell might read a leading hyphen as an option, or use the long option exactly as shown.
For scripts, choose columns explicitly and drop the headings rather than depending on the default display:
$ partx --show --noheadings --output NR,START,SECTORS,SIZE --nr 2 /path/to/disk-image.img
2 18432 16384 8M
Reach for --bytes when a byte count beats a human-readable size, --pairs for KEY="value" records, or --raw for the raw format. Run partx --help on your actual binary to see the columns it supports.
Checkpoint
Check the selected partition number, start sector and size against an independent view such as lsblk or the tool that created the image. A successful parse doesn't prove you picked the right disk.
4. Scan a partition as a nested disk
If a partition contains its own partition table, pass a hyphen as the disk argument. This tells partx to treat that partition as a whole disk rather than one entry inside its parent:
$ partx --show - /dev/sda3
It's a read-only scan, but the device path still matters, confirm /dev/sda3 is the intended partition first. If it's ordinary filesystem data, an empty result or a parse error is expected: don't initialise it just to make partx print rows.
5. Understand add, delete and update
--add, --delete and --update are not partitioning. They tell the kernel about partitions that already exist in the on-disk table; they never rewrite that table. They can still affect device nodes and processes using the device, so treat them as privileged and service-disrupting.
Warning
Don't run these against a mounted or production device as a first test. Confirm the device, unmount whatever needs it, check for active users and arrange a recovery path first. A wrong device path can cause a real outage even though partx is not itself a partition editor.
$ sudo partx --add --nr 3:5 /dev/sdd
$ sudo partx --delete --nr 5 /dev/sdd
$ sudo partx --update --nr 3:5 /dev/sdd
The first adds available partitions 3 through 5 to the kernel's view. The second removes partition 5 from that view. The third asks the kernel to update the specified partitions. None of these undoes a changed partition table, because none of them changes the table in the first place. To restore kernel bookkeeping after a mistaken delete, use --add for the partition that still exists on disk, once you've confirmed the device is safe to rescan.
Don't use --add as a substitute for fdisk, parted or another partition editor. If the table itself is wrong, take a backup before reaching for a tool that writes it.
6. Diagnose an unexpected result
Rerun the harmless checks first:
$ stat /path/to/disk-image.img
$ partx --show --verbose /path/to/disk-image.img
$ printf 'partx exit status: %s\n' "$?"
- Non-zero status: check the path is readable, is the intended disk or image, and has a table type the command recognises.
- Wrong table type or sector size:
--type TYPEselects a partition table type when autodetection fails, and--sector-size SIZEoverrides the assumed sector size. Use these only when the image format or device documentation justifies them, not as generic fixes for a wrong path. - Nested partition confusion: revisit the hyphen form from step 4. For just partition 3 of
/dev/sdb, use eitherpartx --show --nr 3 /dev/sdborpartx --show /dev/sdb3 /dev/sdb, both identify the same partition.
Done means
- Confirmed the binary path and version before trusting its output.
- Listed the intended disk or image with
--show. - Selected a partition or range with
--nr. - Scripted output with explicit columns rather than the default display.
- Understand that add, delete and update touch kernel bookkeeping, not the on-disk table.
- Verified the device and service impact before running an elevated command.