Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

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

May 10, 2023 CentOSLinuxQuestions and AnswersUbuntu

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
previous post
Troubleshooting “RPC Server Unavailable” Errors on Windows
next post
Could not Reconnect All Mapped Network Drives on Windows 10

Related Reading

How to Increase Size of Disk Partition in...

October 5, 2023

How to Use Ansible to Manage Windows Machines

September 25, 2023

Fixing ‘The Network Path Was Not Found’ 0x80070035...

August 30, 2023

How to Install and Configure Ansible on Linux

August 27, 2023

Computer Doesn’t Turn Off After Shutting Down Windows...

August 26, 2023

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023
  • How to Use Ansible to Manage Windows Machines

    September 25, 2023
  • Installing Language Pack in Windows 10/11 with PowerShell

    September 15, 2023
  • Configure Email Forwarding for Mailbox on Exchange Server/Microsoft 365

    September 14, 2023
  • How to View and Change BIOS (UEFI) Settings with PowerShell

    September 13, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • How to Configure MariaDB Master-Master/Slave Replication?
  • How to Mount Google Drive or OneDrive in Linux?
  • KVM: How to Expand or Shrink a Virtual Machine Disk Size?
  • Install and Configure SNMP on RHEL/CentOS/Fedor
  • Configuring High Performance NGINX and PHP-FPM Web Server
  • Adding VLAN Interface in CentOS/Fedora/RHEL
  • Configuring Routing on Linux (RHEL/CentOS)
Footer Logo

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


Back To Top