How to Manage partitions in CentOS 8 / RHEL 8

0
199

Disk partitioning or disk slicing is the creation of one or more regions on secondary storage, so that each region can be managed separately. These regions are called partitions. It is typically the first step of preparing a newly installed disk, before any file system is created. So, in this post, you will learn about manage partitions in centOS 8.

The disk stores the information about the partition locations and sizes in an area known as the partition table that the operating system reads before any other part of the disk. Each partition then appears to the operating system as a distinct “logical” disk that uses part of the actual disk. System administrators use different programs to create, resize, delete, and manipulate the partitions.

In this article we will use fdisk program to create partition. Partitioning allows the use of different filesystems to be installed for different kinds of files. Separating user data from system data can prevent the system partition from becoming full and rendering the system unusable. Partitioning can also make backing up easier.

List/view all Disk Partitions in CentOS 8.

fdisk   -l  

In linux all hard disks names show as sda, sdb, sdc etc.

sda means hard disk 1
sdb means hard disk 2
sdc means hard disk 3 and so on..

Hard Disk Partitions names:

Sda> sda1, sda2 etc

sda1 mean partitions 1 of sda(Hard disk 1)
So, sda2 mean partitions 2 of sda(Hard disk 1)

To Check File System Disk Space Usage and mount point of partitions.

df -h    

We will create and configure partition in four steps.

1- Create partition
2- Partprobe
3- Format Partition (Filesystem)
4- Mount Partition

Then we will also see that how to unmount and delete a created partition.

Manage partitions in CentOS 8. Step 1- Create partition

a- Run fdisk command to see disks and their partitions.

In fdisk -l command you have seen that a hard drive /dev/sdb has no partitions, so we will create a new partition on it.

 fdisk –l  

b- Choose desired hard disk

fdisk    /dev/sdb

c- Press m for help.
d- Press p to print partition table.
e- Press n to create new partition.
f- Press p to make primary partition.
g- Press 1 is created first partition on selected disk.
There are 1-4 partition number. We can type any one number from them.
h- Press enter to leave first cylinder as default.
i- Now assign space to partition. Use + to assign space e.g.
+1024M and press enter.
j- Press p to print partition table.


k- Press w to write/save partition.

Step 2- Partprobe

We have created partition but Linux kernel/Operating system did not know that a new partition has been added. To inform operating system about partition table changes. We can do two things. We will use second method it is recommended.

Restart machine (It is not recommended)
Run partprobe command (Recommended)

The partprobe command is used to inform the operating system of partition table changes without restarting the machine.

Note: Always run partprobe command after partition creation.

partprobe
fdisk -l

Step 3- Format Partition (Filesystem)

We can set file system on partition in two different commands. We can use any of the following.

First Command

mkfs    -t  ext4   /dev/sdb1

mkfs make filesystem
-t to set filesystem type
ext4 It is a filesystem
/dev/sdb1 partition name

OR

Now, second Command

mkfs.ext4  /dev/sdb1

When we press Tab after mkfs we will see all file systems.

Manage partititions on CentOS. Step 4- Mount Partition

First create a mount point (a directory) /disk1 and use mount command to mount /dev/sdb1

a- Make a directory where we mount partition.

mkdir /disk1

b- Now mount partition on created directory.

mount  /dev/sdb1   /disk1

c- See mounted partitions

df –h

d- Mount partition permanently so it can also mount after system reboot.

The configuration file /etc/fstab contains the necessary information to automate the process of mounting partitions. In simple words /etc/fstab file is used to auto mount partitions.

syntax to add partition in /etc/fstab file:

Add partition in /etc/fstab to mount it permanent so if system will reboot partition will not unmount. Run following command.

vi /etc/fstab

then add below lines in in this file.

/dev/sdb1            /disk1      ext4            defaults        0     0

Save and quite.

Above two zero means filesystem will not run repair command on boot time on that partition.

Unmount a partition

If you want to unmount a partition use following command, where /dev/sdb1 will be replaced with your parition.

umount    /dev/sdb1

Mount all partitions. It will read /etc/fstab file and mount all partitions which are configured in that file.

mount    -a

Delete a partition:

df –h

a- Unmount partition. If we does not un-mount, partition will not delete.

umount    /dev/sdb1

b- Choose disk/drive.

fdisk    /dev/sdb

c- Press p to print partitions.
d- Press d to delete partition.
e- Now select partition number, we want to delete. E.g 1 is sdb1
Press 1 and enter
f- Press w to write/save changes.
g- Now run partprobe command.

partprobe

That’s it, You have briefly learned how to create, mount and set filesystems on a partition. We have also mounted, unmount created a partition and also check how to delete it. So, you know how to manage partitions in CentOS 8.

Also, you can read our posts about CentOS 8 in this link.

Please share this post and join our Telegram channel.

LEAVE A REPLY

Please enter your comment!
Please enter your name here