Introduction
In Linux system administration, disk partitioning is a fundamental task that allows organizing storage, improving performance, and facilitating backups. Although graphical tools like GParted exist, the parted command offers precise and scriptable control from the terminal, ideal for servers and environments without a graphical interface.
What is parted?
parted (Partition Editor) is a command-line utility that allows creating, deleting, resizing, and manipulating partitions on hard drives and storage devices. It supports various partition tables, including MSDOS, GPT, Apple, and more, and works directly with the block device, making it powerful but also requiring care.
Installation and verification
In most Linux distributions, parted comes preinstalled. If it is not present, it can be installed via the package manager:
- Debian/Ubuntu:
sudo apt-get install parted - Fedora:
sudo dnf install parted - Arch Linux:
sudo pacman -S parted
To verify the version, run parted -v.
Basic concepts
Before manipulating a disk, it is essential to identify the correct device using lsblk or fdisk -l. Suppose we are working with /dev/sdb. When starting parted, an interactive prompt opens:
sudo parted /dev/sdb
Within this environment, commands are written without the sudo prefix, as superuser privileges are already held.
Creating a new partition table
The first step is usually to define the partition table. For a new disk, GPT is chosen for its flexibility and support for disks larger than 2 TB:
mklabel gpt
If compatibility with older BIOS systems is needed, mklabel msdos can be used. This operation erases any existing table, so it must be done with caution.
Creating partitions
With the table ready, partitions can be created by specifying start, end, and filesystem type. For example, to create a 20 GB ext4 partition at the beginning of the disk:
mkpart primary ext4 0% 20GB
The number 0% indicates the start of the disk and 20GB the size. After creating the partition, it is possible to assign a label:
name 1 root
To format the partition, exit parted and use mkfs.ext4 /dev/sdb1.
Resizing partitions
One of the advantages of parted is the ability to resize partitions without losing data, provided the filesystem supports it. To enlarge partition 1 to 30 GB:
resizepart 1 30GB
After changing the size, you must run resize2fs /dev/sdb1 (for ext4) so the filesystem can take advantage of the additional space.
Deleting partitions
If a partition is no longer needed, it is deleted with:
rm 2
where 2 is the number of the partition to delete. This action is immediate and irreversible, so it is recommended to double-check the number before executing it.
Labels and filesystems
In addition to partitioning operations, parted allows assigning names and checking alignment. Proper alignment (for example, to multiples of 1 MiB) improves performance on SSDs and advanced disks. It can be checked with:
align-check opt 1
A result of 1 indicates that the partition is correctly aligned.
Best practices
- Always back up critical data before modifying the partition table.
- Work in a screen or tmux session to avoid loss due to disconnection.
- Frequently use
printinsidepartedto review the current state. - Prefer GPT for new disks, reserving MSDOS only for legacy.
- Document each change in a changelog or script for reproducibility.
Conclusion
The parted command is an indispensable tool for advanced partitioning in Linux. Its command-line interface provides flexibility, precision, and the ability to automate tasks via scripts. With knowledge of its basic operations and appropriate precautions, administrators can manage storage safely and efficiently, whether on production servers or personal workstations.
This post is also available in ESPAÑOL.