Linux parted Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the powerful parted command to manage disk partitions in Linux. The lab covers understanding the parted command, creating and managing partitions, and resizing or deleting existing partitions. You will practice these tasks on a virtual disk image file, ensuring a safe and controlled environment for your learning. The lab provides step-by-step guidance, including examples and explanations, to help you become proficient in using the parted command for your disk and file system management needs.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") subgraph Lab Skills linux/rm -.-> lab-422850{{"`Linux parted Command with Practical Examples`"}} linux/dd -.-> lab-422850{{"`Linux parted Command with Practical Examples`"}} end

Understand the parted Command

In this step, you will learn about the parted command, which is a powerful tool for managing disk partitions in Linux. The parted command allows you to create, resize, and delete partitions on your storage devices.

First, let's check the version of parted 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 provides several subcommands that allow you to manage disk partitions. Some of the commonly used subcommands are:

  • print: Displays the partition table of a device.
  • unit: Sets the default unit to be used.
  • mkpart: Creates a new partition.
  • rm: Removes a partition.
  • resizepart: Resizes a partition.
  • rescue: Attempts to recover lost partitions.

To get more information about the parted command and its available subcommands, you can use the built-in help:

sudo parted --help

This will display a comprehensive list of all the available parted subcommands and their usage.

Create and Manage Partitions with parted

In this step, you will learn how to create and manage partitions on a storage device using the parted command.

First, let's create a new virtual disk image file that we can use for our partitioning exercises:

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

This will create a 1GB disk image file named disk.img in the ~/project directory.

Now, let's start the parted interactive shell and work with the newly created disk image:

sudo parted ~/project/disk.img

You should see the parted prompt:

(parted)

To create a new partition, we can use the mkpart command. Let's create a primary partition that takes up the entire disk:

(parted) mkpart primary 0% 100%

This will create a single primary partition that spans the entire disk.

To verify the partition table, we can use 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 shows that we have successfully created a single primary partition on the disk image.

Resize and Delete Partitions with parted

In this step, you will learn how to resize and delete partitions using the parted command.

First, let's start the parted interactive shell and work with the disk image we created in the previous step:

sudo parted ~/project/disk.img

To resize the existing partition, we can use the resizepart command. Let's resize the partition to 512MB:

(parted) resizepart 1 512MB

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

To verify the changes, we can use the print command again:

(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 delete the partition using the rm command:

sudo parted ~/project/disk.img
(parted) print
(parted) rm 1
(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

(parted) quit

The output shows that the partition has been successfully deleted.

Summary

In this lab, you first learned about the parted command, which is a powerful tool for managing disk partitions in Linux. You explored the various subcommands available in parted, such as print, unit, mkpart, rm, resizepart, and rescue, which allow you to create, resize, and delete partitions on your storage devices. You also learned how to check the version of parted installed on your system and how to access the built-in help to get more information about the command and its usage.

Next, you created a new virtual disk image file that you can use for your partitioning exercises. You then learned how to create and manage partitions on this storage device using the parted command, including resizing and deleting partitions as needed.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like