Using the Partx Command (with examples)

Using the Partx Command (with examples)

The partx command is a powerful tool for managing partitions in Linux. It allows you to parse a partition table and interact with the kernel to recognize and manage partitions on block devices or disk images. This article will provide code examples for different use cases of the partx command and explain the motivations and arguments for each use case.

Use Case 1: Listing Partitions on a Block Device or Disk Image

To list the partitions on a block device or disk image, you can use the following command:

sudo partx --list path/to/device_or_disk_image

Motivation: This use case is helpful when you need to identify the partitions present on a block device or disk image. It provides a convenient way to obtain information about the partitions without manually examining the partition table.

Explanation: The --list option instructs partx to list the partitions found on the specified device or disk image. The path/to/device_or_disk_image argument should be replaced with the actual path to the device or disk image you want to query.

Example Output:

NR     START    END SECTORS SIZE    NAME    UUID
1      2048     4095  2048    1M  primary  00000000-1111-2222-3333-444444444444
2      4096     8191  4096    2M  primary  11111111-2222-3333-4444-555555555555
3      8192     12287 4096    2M  primary  22222222-3333-4444-5555-666666666666

Use Case 2: Adding Partitions to the Kernel

To add all the partitions found in a given block device to the kernel, you can use the following command:

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

Motivation: This use case is useful when you have made changes to the partition table and want to inform the kernel about the new partitions. By adding the partitions to the kernel, you enable them to be recognized and accessed by the system.

Explanation: The --add option tells partx to add the partitions found on the specified device or disk image to the kernel. The --verbose option provides detailed output, showing the progress and status of the operation. Again, replace path/to/device_or_disk_image with the actual path to the device or disk image you want to manipulate.

Example Output:

partx: /dev/sda: adding partition #1
partx: /dev/sda: adding partition #2
partx: /dev/sda: adding partition #3

Use Case 3: Deleting Partitions from the Kernel

To delete all the partitions found from the kernel (without altering the partitions on disk), you can use the following command:

sudo partx --delete path/to/device_or_disk_image

Motivation: This use case is relevant when you need to remove partitions from the kernel’s recognition, usually after making changes to the partition table or when working with temporary disk images. Deleting the partitions from the kernel allows you to refresh the partition information and ensures that old or invalid partitions are not visible or accessible.

Explanation: The --delete option directs partx to delete the partitions found on the specified device or disk image from the kernel. Again, replace path/to/device_or_disk_image with the actual path to the device or disk image you want to manipulate.

Example Output:

partx: /dev/sda: deleting partition #1
partx: /dev/sda: deleting partition #2
partx: /dev/sda: deleting partition #3

Conclusion

The partx command is a powerful tool for managing partitions in Linux. By using it, you can easily list the partitions on a block device or disk image, add them to the kernel for recognition, and delete them from the kernel when necessary. The provided code examples and explanations should help you understand how to utilize the partx command in various scenarios.

Related Posts

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

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

The spark command-line tool is part of Laravel Spark, a powerful scaffolding system for Laravel applications.

Read More
How to use the command "history expansion" (with examples)

How to use the command "history expansion" (with examples)

History expansion is a feature in various Unix-like shells, such as sh, bash, zsh, rbash, and ksh, that allows users to reuse and expand their command history.

Read More
How to use the command reptyr (with examples)

How to use the command reptyr (with examples)

The reptyr command is used to move a running process to a new terminal.

Read More