Using the `parted` command.

Hey everyone, I'm trying to get a handle on the `parted` command for managing disk partitions on my Linux server. I've seen it mentioned for creating, resizing, and deleting partitions, but I'm a bit lost on the syntax and best practices. Can anyone share some common use cases or tips to avoid messing up my drives?

1 Answers

✓ Best Answer

Using the `parted` Command: A Step-by-Step Guide

The parted command is a powerful tool for managing disk partitions in Linux and other Unix-like systems. Unlike some other partitioning tools, parted supports GPT (GUID Partition Table) and is suitable for disks larger than 2TB. This guide provides a comprehensive walkthrough on how to use parted effectively.

Step 1: Identify the Disk

First, identify the disk you want to partition. Use the lsblk command to list available block devices:

lsblk

This command will display a list of disks and their partitions. Note the device name (e.g., /dev/sdb).

Step 2: Start `parted`

Start parted with the identified disk. Replace /dev/sdb with your disk's device name:

sudo parted /dev/sdb

You'll enter the parted command-line interface.

Step 3: Set the Disk Label

If the disk is new or doesn't have a partition table, set one. GPT is recommended for modern systems:

mklabel gpt

Alternatively, for older systems, you can use:

mklabel msdos

Step 4: Create a New Partition

To create a new partition, use the mkpart command. You need to specify the partition type, start, and end points.

mkpart primary ext4 0% 100%
  • primary: Partition type (primary, extended, logical).
  • ext4: Filesystem type (ext4, xfs, fat32, etc.).
  • 0%: Start of the partition (0% of the disk).
  • 100%: End of the partition (100% of the disk).

For more complex setups, you might create multiple partitions:

mkpart primary ext4 0% 50%
mkpart primary xfs 50% 100%

Step 5: Set Flags (Optional)

You can set flags for a partition, such as boot for a bootable partition:

set 1 boot on

Here, 1 is the partition number.

Step 6: Verify the Partition Table

To verify the partition table, use the print command:

print

This will display the current partition table with all partitions and their details.

Step 7: Quit `parted`

To exit parted, type:

quit

Step 8: Format the Partition

After creating the partition, format it with the specified filesystem. Replace /dev/sdb1 with your partition's device name:

sudo mkfs.ext4 /dev/sdb1

Or, for XFS:

sudo mkfs.xfs /dev/sdb1

Step 9: Mount the Partition

Finally, mount the partition to a directory:

sudo mount /dev/sdb1 /mnt

To make the mount permanent, add an entry to /etc/fstab.

By following these steps, you can effectively use the parted command to manage disk partitions.

Know the answer? Login to help.