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

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

The parted command is a widely used tool for managing disk partitions in Linux systems. It provides both an interactive and non-interactive mode, allowing users to create, delete, resize, and list partitions on various disk devices. With support for different partition table types and file systems, parted is particularly valuable for users wanting to manage disk storage effectively without needing multiple separate utilities.

Use case 1: List partitions on all block devices

Code:

sudo parted --list

Motivation:

Listing all partitions on block devices is fundamental for disk management. It provides quick insight into available partitions, their sizes, and types, helping administrators or users understand the current disk layout, especially before undertaking any operation like creating or resizing partitions.

Explanation:

  • sudo: Grants superuser privileges necessary for accessing disk-related data.
  • parted: The command-line tool used for partition manipulation.
  • --list: An option that directs parted to list partitions on all detected block devices.

Example Output:

Model: ATA Disk (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Number  Start   End     Size    File system  Name     Flags
 1      1049kB  9437MB  9435MB  ext4         primary
 2      9437MB  500GB   491GB   ext4         primary
 3      500GB   1000GB  500GB   ext4         backup   lvm

Use case 2: Start interactive mode with the specified disk selected

Code:

sudo parted /dev/sdX

Motivation:

Interactive mode in parted allows for a more controlled and user-guided interaction with the disk. When you specify the disk device, it pre-selects that device for any subsequent partition operations, making it easier to avoid accidental changes to the wrong device.

Explanation:

  • /dev/sdX: Path to the block device you wish to manipulate.
  • sudo: Required to grant necessary permissions.
  • parted: Initiates the parted tool for partitioning tasks.

Example Output:

GNU Parted 3.3
Using /dev/sdX
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

Use case 3: Create a new partition table of the specified label-type

Code:

sudo parted --script /dev/sdX mklabel gpt

Motivation:

Creating a new partition table on a disk is often the first step when preparing a raw disk for use. By specifying the label type, such as GPT (GUID Partition Table) over the older MBR (Master Boot Record), users can utilize modern features like larger partition sizes and more partitions on a disk.

Explanation:

  • --script: Runs the command in non-interactive mode.
  • /dev/sdX: Specifies the disk to operate on.
  • mklabel: Command to create a new partition label.
  • gpt: Example label type specifying the partition table format.

Example Output:

No direct output, but on success, the disk has a new GPT partition table created.

Use case 4: Show partition information in interactive mode

Code:

(parted) print

Motivation:

Displaying partition information is essential to verify current configurations and planned changes. In interactive mode, it provides immediate and detailed information about the selected disk and its partitions.

Explanation:

  • print: The command within parted’s interactive mode that lists partition details of the currently selected disk.

Example Output:

Model: ATA Disk (scsi)
Disk /dev/sdX: 1000GB
Partition Table: gpt
...

Use case 5: Select a disk in interactive mode

Code:

(parted) select /dev/sdX

Motivation:

In scenarios where you need to manage multiple disks, you can select a different disk without exiting parted interactive mode. This flexibility enhances user efficiency by allowing quick transitions between devices.

Explanation:

  • select: Part of parted’s interactive commands to change the disk context.
  • /dev/sdX: Path to the new disk to select for operations.

Example Output:

Using /dev/sdX

Use case 6: Create a 16 GB partition with the specified filesystem in interactive mode

Code:

(parted) mkpart primary ext4 0% 16G

Motivation:

Creating a specific sized partition with a particular filesystem type, like ext4, is useful during disk setup, ensuring the partitions align with storage requirements for different applications or services.

Explanation:

  • mkpart: Command to make a new partition.
  • primary: Partition type indicating this is a primary partition.
  • ext4: File system type assignable to the partition.
  • 0%: Start point of the partition.
  • 16G: End size for the partition.

Example Output:

(parted) mkpart primary ext4 0% 16G
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore

Use case 7: Resize a partition in interactive mode

Code:

(parted) resizepart /dev/sdXN 20G

Motivation:

Resizing partitions is a common task when reallocating storage space to meet changing requirements. It can help optimize disk space utilization by increasing or decreasing the size of active partitions without data loss.

Explanation:

  • resizepart: Parted command for resizing a partition.
  • /dev/sdXN: The partition to resize, where N is the partition number.
  • 20G: New size or endpoint of the partition.

Example Output:

(parted) resizepart /dev/sdXN 20G

Use case 8: Remove a partition in interactive mode

Code:

(parted) rm /dev/sdXN

Motivation:

Removing a partition frees up space for reallocation and is often necessary when restructuring disk storage. It is part of routine maintenance and restructuring operations.

Explanation:

  • rm: Command to remove a partition.
  • /dev/sdXN: Specifies the partition to delete, where N is the partition number.

Example Output:

(parted) rm 1

Conclusion:

The parted command offers robust and versatile tools essential for managing disk partitions efficiently. Each use case demonstrates the capability to perform critical operations—from listing and creating partitions to interactive and script-based management. Understanding these use cases is invaluable for system administrators and users seeking to optimize their system’s disk usage and perform disk management tasks effectively.

Related Posts

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

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

ausyscall is a command-line tool used for mapping syscall names and numbers, providing a crucial bridge between human-readable syscall names and their corresponding numeric codes that are used at the kernel level.

Read More
How to Use the AWS S3 Command (with Examples)

How to Use the AWS S3 Command (with Examples)

Amazon Web Services (AWS) Simple Storage Service (S3) provides scalable, high-speed, web-based cloud storage services.

Read More
How to use the command 'namei' (with examples)

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

The namei utility is a command-line tool used in Unix-like operating systems to deconstruct and display the components of a given pathname.

Read More