How to use the command 'partx' (with examples)

How to use the command 'partx' (with examples)

The partx command is a powerful utility primarily used for parsing partition tables and transferring the details to the Linux kernel. It allows administrators to efficiently interact with partition tables, which are essential for dividing physical storage into manageable sections. partx provides capabilities such as listing, adding, or deleting partition details, making it a handy tool in system management and maintenance.

Use case 1: List the partitions on a block device or disk image

Code:

sudo partx --list path/to/device_or_disk_image

Motivation:

Listing partitions on a block device or disk image is a fundamental task for system administrators. It provides a clear view of the partitions configured on a device, helping in system diagnostics, space allocation, and verification of partition configurations. Without this information, managing storage devices effectively would be challenging, as identifying the layout and structure of a storage device is crucial in troubleshooting or optimizing system storage.

Explanation:

  • sudo: This command is prefixed with sudo to run with superuser privileges, which is typically necessary for accessing hardware details and making low-level changes.
  • partx: The utility being called, responsible for handling partition table information.
  • --list: An option provided to list the current set of partitions from the block device or disk image.
  • path/to/device_or_disk_image: This specifies the location of the block device or disk image whose partitions are to be listed.

Example Output:

NR START    END        SECTORS  SIZE NAME PARTUUID
 1 2048  1050623  1048576  512M  part1 12345678-01
 2 1050624  2097151  1046528  512M  part2 12345678-02

Use case 2: Add all the partitions found in a given block device to the kernel

Code:

sudo partx --add --verbose path/to/device_or_disk_image

Motivation:

The action of adding partition details to the kernel is often needed when new devices are attached or partitions are created, modified, or removed. When a disk’s partitions change, the kernel doesn’t automatically become aware of these changes. Using partx to add partitions ensures that the kernel recognizes the current partition scheme, which is crucial for accessing and managing storage correctly and preventing data access errors.

Explanation:

  • sudo: Used to execute the command with administrative privileges.
  • partx: The command responsible for interfacing with partition tables.
  • --add: This option indicates that the partitions found should be added to the kernel’s table.
  • --verbose: A flag for providing detailed output, useful for understanding every action taken during command execution.
  • path/to/device_or_disk_image: The path to the device or image file containing the partitions to be added to the kernel.

Example Output:

partx: adding partition 1
partx: adding partition 2

Use case 3: Delete all the partitions found from the kernel (does not alter partitions on disk)

Code:

sudo partx --delete path/to/device_or_disk_image

Motivation:

Deleting partition information from the kernel might be necessary when a device is being removed or when partition configurations change and require refreshing. This action is useful during system reconfiguration, ensuring the kernel’s partition table does not contain stale or outdated information, which could lead to incorrect access decisions or device utilization issues.

Explanation:

  • sudo: Ensures the command is executed with the required permissions for altering device details.
  • partx: The utility called upon to manage partition data.
  • --delete: Specifies that the partitions should be removed from the kernel’s current table.
  • path/to/device_or_disk_image: Identifies the block device or disk image whose partition information needs to be removed from the kernel.

Example Output:

partx: deleting partition 1
partx: deleting partition 2

Conclusion:

The partx command is an essential tool for managing partition tables and keeping the kernel aware of the storage structure of your devices. By using partx, administrators can ensure that partition data is accurately represented, aiding in effective system management and preventing potential issues related to outdated or incorrect partition information.

Related Posts

How to Use the Command 'pkgctl diff' (with examples)

How to Use the Command 'pkgctl diff' (with examples)

pkgctl diff is a versatile command designed to compare package files in various modes.

Read More
Mastering Nix Flakes: Key Use Cases and Examples (with examples)

Mastering Nix Flakes: Key Use Cases and Examples (with examples)

Nix flakes represent a new paradigm in the Nix package management ecosystem, offering a more structured and reproducible way of managing packages, dependencies, and configurations.

Read More
How to Use the 'cdrdao' Command (with Examples)

How to Use the 'cdrdao' Command (with Examples)

cdrdao is a command-line utility specifically designed for reading and writing CDs in a mode known as disc-at-once.

Read More