Home / Alt manpages / partx(8)

  • partx(8)
  • Admin command
  • linux

Inspect Linux Partition Tables Safely with partx

You will finish with a repeatable way to read a disk or disk image's partition table, select one partition, and produce output suitable for a script. You will also know exactly what partx changes when you use its administrative options. Allow about fifteen minutes. You need a shell and the util-linux package; a test image is useful if you want to reproduce the examples without touching a real disk.

This guide follows the installed manual page, which documents util-linux 2.39.3. On this machine, the package is util-linux 2.39.3-9ubuntu6.6, while partx resolves to a util-linux 2.41.3 binary in /home/linuxbrew/.linuxbrew/sbin. Check your own path and version before relying on small output or option differences.

1. Confirm which partx you are running

Start with read-only checks. No elevated privileges are needed:

$ command -v partx
/home/linuxbrew/.linuxbrew/sbin/partx
$ partx --version
partx from util-linux 2.41.3
$ partx --help

The command accepts a disk, or a partition together with its parent disk. The main read-only mode is --show, also written -s. The older --list form is documented as deprecated in the local manual, so use --show in new commands and scripts.

Checkpoint

If command -v points at an unexpected installation, stop and decide whether your shell path is intentional. Do not mix the help output from one binary with results from another.

2. List a real disk or image

Replace the placeholder with a path you have permission to read. Listing is normally unprivileged, although device permissions and the host's security policy can require 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, and SECTORS is the length. Except for SIZE, the displayed numbers are 512-byte sectors. The human-readable size uses binary units and may display 8M rather than the longer 8 MiB form.

For a block device, use its exact path, such as /dev/sdb. Do not guess a device name. Confirm it with a separate inventory command such as lsblk, and check the mount points before using any command that can change kernel bookkeeping.

3. Select one partition and stable columns

Use --nr when you want a particular partition or range. A single number selects one partition; 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 forms are also supported: 2: means partition 2 onwards and :4 means through partition 4. Negative numbers count from the end, so --nr -1:-1 selects the last partition. Quote a range if your shell could interpret a leading hyphen as an option, or use the long option exactly as shown.

For scripts, choose columns explicitly and remove headings. This avoids depending on the default display:

$ partx --show --noheadings --output NR,START,SECTORS,SIZE --nr 2 /path/to/disk-image.img
2 18432 16384 8M

Use --bytes when a byte count is more useful than the default human-readable size. Use --pairs for KEY="value" records, or --raw for the raw format. Inspect partx --help on the actual binary to see the output columns it supports.

Checkpoint

Verify that the selected partition number, start sector and size match an independent view such as lsblk or the partitioning tool that created the image. A successful parse does not prove that you selected the intended disk.

4. Scan a partition as a nested disk

If a partition contains another partition table, pass a hyphen as the disk argument. This tells partx to treat the partition as a whole disk rather than as one entry inside its parent:

$ partx --show - /dev/sda3

That command is a read-only scan, but the device path still matters. Confirm that /dev/sda3 is the intended partition before running it. If the partition is ordinary filesystem data, an empty result or a parse error is expected; do not initialise it merely to make partx print rows.

5. Understand add, delete and update

--add, --delete and --update are different from partitioning. They tell the kernel about partitions that exist in the on-disk table; they do not rewrite that table. They can still affect device nodes and processes using the device, so treat them as privileged, service-disrupting operations.

Warning

Do not run these against a mounted or production device as a first test. Confirm the device, unmount anything that must be unmounted, check for active users, and arrange a recovery path. A wrong device path can cause a real outage even though partx itself is not 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 commands is an undo for a changed partition table, because none changes the table in the first place. If you need to restore kernel bookkeeping after a mistaken delete, use --add for the partition that still exists on disk, after checking that the device is safe to rescan.

Do not use --add as a substitute for fdisk, parted or another partition editor. If the table itself is wrong, stop and take a backup before choosing a tool that writes it.

6. Diagnose an unexpected result

First rerun the harmless checks:

$ stat /path/to/disk-image.img
$ partx --show --verbose /path/to/disk-image.img
$ printf 'partx exit status: %s\n' "$?"

A non-zero status means the command did not complete successfully. Check that the path is readable, that it is the intended disk or image, and that the partition table type is one the command recognises. --type TYPE can select a partition table type when autodetection is not suitable, and --sector-size SIZE can override the assumed sector size. Use those options only when the image format or device documentation justifies them; they are not generic fixes for a wrong path.

If you are inspecting a partition as a nested disk, remember the hyphen form from step 4. If you only want partition 3 from /dev/sdb, use either partx --show --nr 3 /dev/sdb or provide the partition and its parent, partx --show /dev/sdb3 /dev/sdb. These identify the same partition under the documented syntax.

Done means

  • You confirmed the binary path and version before interpreting its output.
  • You listed the intended disk or image with --show.
  • You can select a partition or range with --nr.
  • Your script examples choose output columns and handle sectors explicitly.
  • You understand that add, delete and update change kernel bookkeeping, not the on-disk partition table.
  • You will verify the device and service impact before using an elevated command.