As a system administrator, sometimes you may require to manage partitions of your CentOS/Red Hat Linux Operating System. Partition management is always a risky task. But there are some useful tools those are so helpful to manage CentOS/Red Hat Linux disk partition easily. In my previous article, I discussed how to manage CentOS/Red Hat 7 HDD partitions with the fdisk utility. The fdisk tool is only eligible to manage MBR partition table. But GPT partition table is now becoming more popular. On the other hand, the fdisk tool cannot resize (shrink or extend) a partition. To overcome these limitations, we can use another partition management tool named parted which can handle GPT partition table as well as can resize disk partitions. In this article, we will know how to create, delete, resize and rescue partitions in CentOS 7/Red Hat 7 Linux with the parted partition manager tool.
The parted Utility
The parted is open source and command line utility that is used to manage hard disk partitions. It supports multiple partition table formats including MS-DOS (MBR) and GPT. It is useful for creating space for new operating systems, reorganizing disk usage, and copying data to new hard disks.
Installing parted in CentOS 7/Red Hat 7 Linux
The parted tool is by default installed and enabled in CentOS 7/Red Hat 7 Linux. But you may not get all the features in the default parted tool sometimes. So, it is better to install it again. Login to your CentOS 7/Red Hat 7 Linux and issue the following command to install it.
After installing parted utility, just type parted command from your CLI and you will find the below output.
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
(parted)
The above output is showing the parted version and the disk is being used by the parted. If the device is not mentioned as argument, the parted command by default uses first hard disk drive. To show available disk drive, issue the following command.
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 525MB 524MB primary xfs boot
2 525MB 2673MB 2147MB primary linux-swap(v1)
3 2673MB 21.5GB 18.8GB primary xfs
Error: /dev/sdb: unrecognised disk label
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
(parted)
Now we can see that there are two hard disk drives (/dev/sda and /dev/sdb) in our system. The /dev/sda is being used by the system partition. So, to switch the second disk drive, issue the following command.
Using /dev/sdb
(parted)
Now second hard disk is being used by the parted program. To get available commands in parted program, issue the help command and you will get the command list with short description.
The help command will show the commands that can be used in parted program to create, delete, rescue or resize partitions.
Creating Partition with the parted Tool in CentOS 7 Linux
To create a new partition with the parted tool, the disk drive must have free space. As the /dev/sda has no free space, we will create a new partition in the /dev/sdb disk. So, start the parted program with selecting the /dev/sdb disk and issue print command to show disk status.
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
(parted) print
Error: /dev/sdb: unrecognised disk label
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
(parted)
The above output shows that the /dev/sdb is a raw disk and there is now partition in it. The output also shows that the /dev/sdb disk has no partition table assigned. So, first we will define partition table and then create new partition. Issue the following command to define partition table for the /dev/sdb disk.
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
(parted)
GPT partition table is now defined for the /dev/sdb disk. If you wish to use MBR partition, provide msdos argument for the mktable command.
To create new partitions, issue the mkpart command. [The mkpart command will ask for partition type, file system, start and end location for the new partition. You will find available options for the arguments with the help mkpart command].
Partition name? []? primary
File system type? [ext2]? xfs
Start? 0
End? 2GB
(parted)
First, the mkpart command will ask for the partition type or name. As we want to create primary partition, type primary here.
Second, it will ask for the File System type. We want to create xfs file system.
Third, it will ask for starting location. Default unit used by the parted program is MB. We want to create a 2 GB partition. As it is our first partition, we have started from 1MB location. We can also provide starting location with percentage (%) such as 1%.
Fourth, it will ask for ending location. For 2GB partition we have provided 2GB. So, partition started from 1MB to 2GB. We can also provide ending location with percentage (%) such as 10%.
With print command, we can see the status of our new created partition.
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 2000MB 1999MB primary
We can also run the mkpart command with its four arguments at a time. The following command will create another 2GB partition that will start from 2GB to 4GB.
(parted)
We can see our created partition with the print command.
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 2000MB 1999MB primary
2 2000MB 4000MB 2001MB primary
To return from the parted program, issue the quit command.
Now we have two newly created partitions. After creating partition, it is now required to inform the operating system to update partition table. Issue the following command to update partition table permanently.
Our partition table now has been updated. Now it is required to format our partition to use. The recent Linux supported file format system is xfs. So, issue the following commands to format sdb1 and sdb2 partitions with xfs.
[root@localhost ~]# mkfs.xfs /dev/sdb2
We have successfully formatted our newly created partitions with xfs file system. Now these partitions are ready to keep data. But before keeping data, we have to mount partition to a mount point (folder or directory).
Mounting New Partition
For practice, we will now mount sdb1 to data directory and sdb2 to backup directory. So, first create these folders with the following command.
Now mount data to sdb1 and backup to sdb2 with the following command.
[root@localhost ~]# mount /dev/sdb2 /backup
Browsing these folders, you can keep any data now. You can see usage of disk with the following command.
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 xfs 19G 5.3G 14G 28% /
devtmpfs devtmpfs 506M 0 506M 0% /dev
tmpfs tmpfs 514M 82k 514M 1% /dev/shm
tmpfs tmpfs 514M 7.4M 507M 2% /run
tmpfs tmpfs 514M 0 514M 0% /sys/fs/cgroup
/dev/sda1 xfs 521M 122M 400M 24% /boot
/dev/sdb1 xfs 2.0G 34M 2.0G 2% /data
/dev/sdb2 xfs 2.0G 34M 2.0G 2% /backup
At this stage, our mounted partitions are temporary. If our Operating System gets restarted, these mounted directories will be lost. So, we need to do permanent mount. To do permanent mount, we have to put entry in fstab file.
So, open fstab file and put two entries for sdb1 and sdb2 like the following example and save fstab file.
UUID=bee34176-dd55-479e-95c5-9545912d14b9 / xfs defaults 1 1
UUID=dede0f3e-6c9a-4185-afb4-f129fb873246 /boot xfs defaults 1 2
UUID=40464ff6-29e5-4492-9698-1b8229ca58f6 swap swap defaults 0 0
/dev/sdb1 /data xfs defaults 0 0
/dev/sdb2 /backup xfs defaults 0 0
To mount all file systems in fstab file, issue the following command. If any error is occurred in fstab file, the following command will also show that.
New partition creation with permanent mounting using the parted utility has been completed. Now we will show how to delete any partition in CentOS/Red Hat 7 Linux if required.
Deleting a Partition with the parted Tool in CentOS 7 Linux
Sometimes we may require deleting any partition. Using parted tool, we can easily delete any partition at any time. But before deleting any partition, you have to unmount the partition and remove fastab entry. Say, we want to remove sdb2 partition. So, first remove fstab entry by opening with vim editor.
UUID=bee34176-dd55-479e-95c5-9545912d14b9 / xfs defaults 1 1
UUID=dede0f3e-6c9a-4185-afb4-f129fb873246 /boot xfs defaults 1 2
UUID=40464ff6-29e5-4492-9698-1b8229ca58f6 swap swap defaults 0 0
/dev/sdb1 /data xfs defaults 0 0
Now unmounts the backup directory mounted to sdb2 partition with the following command.
We are now ready delete partition sdb2 because we have removed the fstab entry and also unmounted the backup directory from sdb2 partition. Now issue the following command to delete the sdb2 partition. To delete a partition with the parted tool, we need to know the partition number. From the print command, we can find the partition number easily.
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 2000MB 1999MB xfs primary
2 2000MB 4000MB 2001MB xfs primary
Getting partition number, we can remove any partition with the rm command. Issue the following command to remove partition number 2.
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
(parted) rm 2
Partition number 2 has been removed. Now run partprobe command to update partition table.
How to Rescue a Removed Partition
Sometimes we may remove any partition unfortunately. The parted tool is able to rescue any deleted partition within a Start and End location. For example, we want to rescue the partition number 2 that we removed before. The starting location of partition 2 was 2GB and the ending location was 4GB. So, issue the rescue command with these START and END locations.
Information: A xfs primary partition was found at 2000MB -> 4000MB. Do you want to add it to the partition table?
Yes/No/Cancel? Y
(parted)
With the print command, you will find that the removed partition has been recovered.
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 2000MB 1999MB xfs primary
2 2000MB 4000MB 2001MB xfs
(parted)
Resize (Shrink or Extend) Partition with the parted Tool
The parted tool is able to resize a disk partition so easily. But before resizing a disk, it will be better to keep your data backup as well as you have to unmount the partition because a busy disk cannot be resized.
The resizepart command is used to shrink or to extend a partition. The resizepart command accepts two arguments. The first argument is the partition number and the second argument is the ending location. If the ending location is less than the current ending location, the disk will be shrunk and if the ending location is greater than the current ending location, the disk will be extended if free space is available.
We will first shrink our partition 2 whose ending location is 4GB. Issue the following command to shrink the /dev/sdb2.
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
(parted) resizepart 2 3GB
Warning: Shrinking a partition can cause data loss, are you sure you want to continue?
Yes/No? y
(parted)
So, partition 2 is now resized. With the print command you will find the current status of partition 2.
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 2000MB 1999MB xfs primary
2 2000MB 3000MB 1000MB xfs
(parted)
Now we will do another example where we will extend our partition 2. Issue the following resizepart command to extend partition 2 to 5GB ending location.
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 2000MB 1999MB xfs primary
2 2000MB 5000MB 3000MB xfs
(parted)
Now partition 2 is extended to ending location 5GB. With the print command, you will find the current status of partition 2(/dev/sdb2).
If you face any confusion to follow the above steps properly, watch the below video about how to manage CentOS/Red Hat 7 Linux disk partition carefully. I hope it will reduce your any confusion.
How to manage CentOS 7 or Red Hat 7 Linux partition with the parted utility has been discussed in this article. I hope you will now be able to create a new partition, to delete a partition, to rescue a deleted partition or to resize a partition with the parted tool. However, if you face any confusion to manage your Disk Drive with the parted tool, feel free to discuss in comment or contact with me from Contact page. I will try my best to stay with you.