parted Command in Linux

Introduction

This lab provides a comprehensive guide on leveraging the parted command for effective disk partition management within a Linux environment. You'll gain practical experience in understanding and utilizing parted for creating, managing, resizing, and deleting partitions. This hands-on tutorial utilizes a virtual disk image, offering a secure and controlled setting to hone your skills. We'll break down each step with clear examples and explanations, empowering you to confidently manage disks and file systems using the parted command. This is essential knowledge for any systemadmin.

Understand the parted Command

This section introduces the parted command, a powerful and versatile tool for systemadmins to manage disk partitions in Linux. With parted, you can create, resize, and delete partitions on various storage devices directly from the command line.

First, let's verify the version of parted that is currently installed in the Ubuntu 22.04 Docker container:

sudo parted --version

Example output:

parted (GNU parted) 3.4
Copyright (C) 2023 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

The parted command boasts a range of subcommands, allowing granular control over disk partitions. Key subcommands include:

  • print: Shows the partition table of a specified device.
  • unit: Configures the default unit of measurement.
  • mkpart: Creates a brand new partition.
  • rm: Deletes an existing partition.
  • resizepart: Changes the size of a partition.
  • rescue: Aims to recover lost or damaged partitions.

For in-depth information regarding the parted command and its subcommands, utilize the built-in help functionality:

sudo parted --help

This command will display a comprehensive listing of all available parted subcommands and their respective syntax.

Create and Manage Partitions with parted

This segment will guide you through the process of creating and managing partitions on a storage device using the parted command. You'll learn essential skills for any systemadmin.

Firstly, let's generate a new virtual disk image file for our partitioning practice:

sudo dd if=/dev/zero of=~/project/disk.img bs=1M count=1024

This command generates a 1GB disk image file named disk.img located in the ~/project directory.

Now, initiate the parted interactive shell to interact with the newly created disk image:

sudo parted ~/project/disk.img

You should encounter the parted prompt:

(parted)

To create a new partition, the mkpart command is used. We'll create a primary partition occupying the entire disk space:

(parted) mkpart primary 0% 100%

This action will create a single, primary partition that spans the disk entirely.

To validate the created partition table, utilize the print command:

(parted) print
Model: (file) ~/project/disk.img
Disk /home/labex/project/disk.img: 1024MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      0.00GB  1.00GB  1.00GB  primary

(parted) quit

The output confirms the successful creation of a single primary partition on the disk image. Mastering this is crucial for any aspiring root user or systemadmin.

Resize and Delete Partitions with parted

Here, you'll discover how to resize and delete partitions using the parted command. These are essential skills for any Linux systemadmin managing storage.

Begin by launching the parted interactive shell, working with the disk image created in the preceding step:

sudo parted ~/project/disk.img

To resize an existing partition, the resizepart command is employed. Let's reduce the partition size to 512MB:

(parted) resizepart 1 512MB

This will resize the first (and only) partition to a size of 512MB.

To confirm the changes, re-execute the print command:

(parted) print
Model: (file) ~/project/disk.img
Disk /home/labex/project/disk.img: 1024MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      0.00GB  0.50GB  0.50GB  primary

(parted) quit

Now, let's proceed to delete the partition utilizing the rm command:

[object Object]

The output verifies that the partition was successfully removed. Understanding these commands is vital for effective Linux system administration and partition management.

Summary

This lab introduced the parted command, a vital tool for systemadmins to manage disk partitions within Linux. You explored core subcommands like print, unit, mkpart, rm, resizepart, and rescue, enabling you to create, resize, and delete partitions on storage devices. This tutorial also covered how to check the parted version and leverage the built-in help system.

Furthermore, you created a virtual disk image for hands-on practice, learning how to create and manage partitions effectively using the parted command, including resizing and deleting partitions as necessary. These skills are foundational for any individual working with Linux, especially root users and systemadmins needing to manage storage resources.

400+ Linux Commands