Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / Linux / Parted: Create and Manage Disk Partitions on Linux

March 11, 2024

Parted: Create and Manage Disk Partitions on Linux

Parted is a PARTition EDitor for Linux to create, format, delete, shrink and extend disk partitions. The tool is easy to use and available in all Unix/Linux distros. A GUI version is also available, Gparted. In this article we’ll show you how to manage disk partitions using parted on CentOS Linux (it works in the same way in other Linux distributions). Parted is the Linux equivalent of the Windows diskpart tool.

Contents:
  • How to Install Parted on Linux?
  • Managing Partition Tables with Parted
  • How to Create a New Partition with Parted?
  • How to Resize (Extend or Shrink) Partition with Parted?
  • Removing a Partition with Parted
  • How to Restore Accidentally Deleted Disk Partition with Rescue?

How to Install Parted on Linux?

Update software on your Linux host and install the parted package using the package manager in your Linux distribution. In CentOS 8 with the dnf package manager (that replaced yum), you can install parted from the basic repository with the commands:

# dnf update -y
# dnf install parted -y

install parted on linux

Or in Debian/Ubuntu:

# apt-get install parted

To check the tool version, run the following command:

# parted –v

parted (GNU parted) 3.2

parted get version

To use parted, enter:

# parted

GNU Parted 3.2
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.

Managing Partition Tables with Parted

Display the list of available disks:

# print

Or using parted:

$ sudo parted -l

parted unrecognized disk label

There is a 21 GB disk /dev/vdb without an assigned label (error /dev/vdb: unrecognized disk label).

You can create an msdos partition table (MBR) on the disk:

# mklabel msdos

Or a gpt partition table (GUID partition table supports the partition size over 2 TB):

# mklabel gpt

Note that unlike fdisk, parted doesn’t have a command to write changes. All changes are applied immediately.

Then parted will show the type of a partition table (layout) on the disk:

(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 21.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags

As you can see, there is the MBR partition table on the disk, but no disk partitions have been created yet.
If you have several disks on your server, you can switch between them using this command:

# select /dev/diskname

How to Create a New Partition with Parted?

The mkpart command is used to create a new partition in parted. After running this command in interactive mode, questions about the parameters of the new partition will appear.

parted - create new primary partition with mkpart

  • Partition type — specify a partition type (primary or extended)
  • File system type — set a file system. ext2 is offered by default (we will change it later);
  • Start is the initial partition sector;
  • End this is the last sector of the partition (in megabytes). In our example, we have entered 5,000, it means that a 5 GB partition will be created.

To display the amount of free space left on a disk, use the following command:

(parted) print free

You can create a partition that spans the whole disk:

# (parted) mkpart primary 0 0

or specify any partition size as follows:

# (parted) mkpart primary 0 1GB

You can also set the partition size in % and assign a label:

# (parted) mkpart "home part" ext4 2.5GiB 100%

To exit parted, run this command:

# quit

Let’s format the partition to ext4 file system:

# mkfs.ext4 /dev/vdb1

mke2fs 1.44.6 (5-Mar-2019)
Creating filesystem with 1220352 4k blocks and 305216 inodes
Filesystem UUID: 5c9daa97-c0f4-44bc-9cfa-f466ebd8895e
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

Check the file system of the partition and make sure it has changed (note that the print command displays the list of partitions on the disk, their numbers, type, size and file system).

print disk partition info with parted

You can create a partition and format it without going into the parted shell. Use the following one-liner:

# parted -a opt /dev/vdb mkpart primary ext4 0% 100% && mkfs.ext4 /dev/vda1

Using the command, we will create a partition on vdb disk and allocate all free space to it.

Thus, you can make your work easier or add similar commands to bash scripts or kickstart files.

How to Resize (Extend or Shrink) Partition with Parted?

To extend or shrink a partition size, the resizepart subcommand is used in parted. You can resize a partition interactively. Run the following command in parted:

# resizepart

Unmount the partitions with ext2/3/4 file systems before resizing.

The tool will prompt you to enter the partition number (you can take it from the print output) and the final size of the partition. In this example, the size of the partition will be extended from 5 to 10 GB:

(parted) resizepart
Partition number? 1
End? [5000MB]? 10000

resize disk partition on linux

First, extend the partition, and then expand the file system on it. If you shrinking the partition size, you have to reduce the file system size first, and then reduce your partition. Otherwise, you may lose your data.

To reduce the file system size, the following commands are used. For ext2/3/4 file systems:

resize2fs /dev/sdab size

For Btrfs:

btrfs filesystem resize /dev/sdab size

You can also change a partition flag in parted. You can set the one you want:

  • boot
  • root
  • swap
  • hidden
  • raid
  • lvm
  • lba
  • legacy_boot
  • irst
  • esp
  • palo

For example, let’s mark the partition as bootable:

# set 1 boot on

set boot flag for partition

Removing a Partition with Parted

If you want to remove a partition on a disk, you can use the rm command in parted:

# rm 1

The command will remove the partition with the number 1:

(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 21.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Be careful with the command, since it doesn’t require to confirm removal.

How to Restore Accidentally Deleted Disk Partition with Rescue?

You can restore a deleted partition using the rescue tool available in parted:

# rescue

The command will ask you to enter the start and end partition size. If there is some information about the partition in these positions, the command will try to restore the removed partition.

parted rescue - find an accidentally deleted disk partition in linux

As you can see, parted is easy to use and very convenient to create/modify the disk partitions.

0 comment
0
Facebook Twitter Google + Pinterest
LinuxQuestions and Answers
previous post
How to Upgrade from Windows 10/11 Home Edition to Pro or Enterprise
next post
Using Managed Service Accounts (MSA and gMSA) in Active Directory

Related Reading

Ubuntu/Mint/Kali Boots to Initramfs Prompt in BusyBox

March 11, 2024

Installing an Open Source KMS Server (Vlmcsd) on...

March 13, 2024

How to Check Disk Performance (IOPS and Latency)...

March 11, 2024

Install and Configure SNMP on RHEL/CentOS/Fedor

March 13, 2024

KVM: How to Expand or Shrink a Virtual...

March 11, 2024

Compress, Defrag and Optimize MariaDB/MySQL Database

March 11, 2024

How to Install and Use ClamAV Antivirus on...

March 13, 2024

How to Disable Microsoft Teams Auto Startup

March 12, 2024

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Installing an Open Source KMS Server (Vlmcsd) on Linux
  • How to Access VMFS Datastore from Linux, Windows, or ESXi
  • How to Configure MariaDB Master-Master/Slave Replication
  • Using iPerf to Test Network Speed and Bandwidth
  • Moving WSL to Another Drive in Windows
  • KVM: How to Expand or Shrink a Virtual Machine Disk Size?
  • How to Install Microsoft Teams Client on Linux
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top