How to Create and Manage Partitions Using `parted` on Debian 12 Bookworm

Learn how to create and manage partitions using parted on a Debian 12 system.

Introduction

Partitioning a hard disk is a crucial step in managing storage efficiently, especially in Linux-based operating systems like Debian 12 (Bookworm). The parted utility is a powerful tool that provides flexible disk partitioning options, supporting both MBR (Master Boot Record) and GPT (GUID Partition Table) partitioning schemes.

This guide will cover how to create and manage partitions using parted on a Debian 12 system, including installation, partition creation, modification, and deletion.


1. Installing parted

In most cases, parted is pre-installed in Debian 12. However, if it is missing, you can install it using:

sudo apt update && sudo apt install parted -y

To verify the installation, check the version:

parted --version

If installed successfully, it should display version details.


2. Listing Available Disks

To view all available storage devices and partitions, run:

sudo parted -l

This command provides details about disk partitions, file systems, and partitioning schemes.


3. Creating a New Partition Table

Before creating partitions, ensure the disk has the appropriate partition table. To create a new partition table:

sudo parted /dev/sdX mklabel gpt

Replace /dev/sdX with the correct disk name (e.g., /dev/sdb). Use gpt for modern systems and msdos for older systems.

Warning: This command erases all data on the disk.


4. Creating a New Partition

4.1 Creating a Primary Partition

To create a new partition:

sudo parted /dev/sdX mkpart primary ext4 1MiB 10GiB
  • primary specifies the partition type.
  • ext4 is the intended file system.
  • 1MiB is the starting point.
  • 10GiB is the partition size.

To create additional partitions, adjust the start and end positions accordingly.

4.2 Creating a Swap Partition

To create a swap partition:

sudo parted /dev/sdX mkpart primary linux-swap 10GiB 20GiB

After creation, enable swap:

sudo mkswap /dev/sdX2
sudo swapon /dev/sdX2

To make it persistent, add it to /etc/fstab:

echo '/dev/sdX2 none swap sw 0 0' | sudo tee -a /etc/fstab

5. Formatting the Partition

Once a partition is created, it needs to be formatted with a file system:

sudo mkfs.ext4 /dev/sdX1

Replace ext4 with the desired file system (xfs, btrfs, etc.).


6. Mounting and Using the Partition

Create a mount point and mount the partition:

sudo mkdir -p /mnt/data
sudo mount /dev/sdX1 /mnt/data

To make it persistent after reboot, add it to /etc/fstab:

echo '/dev/sdX1 /mnt/data ext4 defaults 0 2' | sudo tee -a /etc/fstab

7. Resizing a Partition

Before resizing, unmount the partition:

sudo umount /dev/sdX1

Resize the partition:

sudo parted /dev/sdX resizepart 1 15GiB

After resizing, resize the file system:

sudo resize2fs /dev/sdX1

8. Deleting a Partition

To delete a partition:

sudo parted /dev/sdX rm 1

This permanently removes the partition, so proceed with caution.


9. Checking Disk and Partition Status

To check disk space usage:

df -h

To check partition consistency:

sudo fsck /dev/sdX1

Conclusion

Using parted in Debian 12 provides a flexible way to create, manage, and modify partitions efficiently. Whether setting up a new disk, resizing partitions, or managing swap space, parted offers an intuitive yet powerful command-line interface. Always ensure you have backups before making changes to avoid data loss.