Disk partitions and mounting in CentOS 7 and RHEL 7

Dear friends in this tutorial I am going to show you step by steps hard disk partitions creation and mounting hard disk partitions in CentOS7. Here I am using one Virtual machine where I have added one additional disk of 4GB. Now I will show you how to create a partition on this disk and mount to use this disk.

I am going to use fdisk Linux tool to creating hard disk partitions. We can use fdisk -l to display disk information. So first of all run this commands on your system to check disk information. Make sure you have one additional row disk where you want to create a partitions.

You can also extend your exiting OS disk partitions if you want to create one additional partitions. Then you can use fdisk command to create a new partitions with your extended last partitions of OS disk. Suppose if you have 6 partitions where 3 disk partitions are used by OS and 3 partitions is created for as a normal use then you can use your last 6 partition to extend one more partitions as like partition number 7.

But first you have to make sure your last 6 partitions have no any data and you have to delete this partition then you can create again with 6 and 7 partitions as per your disk space requirement.

In this tutorial we are going to cover this steps:-

  1. Creating Hard Disk partition
  2. Formatting Hard Disk partition
  3. Mounting Hard Disk partition
  4. Checking mounted file system
  5. Making permanent mount file system

Creating Hard Disk partition

Here I am going to create hard disk partitions on one additional disk where there is no any partitions created. Let’s start.

[[email protected] ~]# fdisk -l

Disk /dev/sda: 16.1 GB, 16106127360 bytes, 31457280 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00063127

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 31457279 15215616 8e Linux LVM

Disk /dev/sdb: 4294 MB, 4294967296 bytes, 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-root: 13.9 GB, 13925089280 bytes, 27197440 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 1610 MB, 1610612736 bytes, 3145728 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[[email protected] ~]#

In above commands output you can see I have 2 hard disk, first is /dev/sda: 16.1 GB and second is /dev/sdb: 4294 MB. OS is installed in /dev/sda disk and I want to create disk partition on second disk.

To create partitions we have to run this commands:- fdisk /dev/sdb

[[email protected] ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x2c6fc8a9.

Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes, 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x2c6fc8a9

Device Boot Start End Blocks Id System

Command (m for help): q

[[email protected] ~]#

You can see in above commands output m option will provide help and p option provide disk partitions information. As per output you can see we don’t have any current partitions of selected disk. For new partitions we have to select n option. So let’s start create new partitions.

[[email protected] ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x08d7b0f1.

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-8388607, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-8388607, default 8388607):
Using default value 8388607
Partition 1 of type Linux and of size 4 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] ~]#

In above output you can see we have created one primary partitions of 4GB. Default partitions is 1 and default we have selected all space for partition 1. After that we have selected w option to write this partitions information. Now you can check your partitions with this command fdisk /dev/sdb  with p option. Then you will get output like below.

[[email protected] ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes, 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x08d7b0f1

Device Boot Start End Blocks Id System
/dev/sdb1 2048 8388607 4193280 83 Linux

Command (m for help): q

[[email protected] ~]#

You can see in above output we have successfully /dev/sdb1 partitions created.

Formatting Hard Disk  partition

After partition creation we need to format this partition to use this disk. So let’s start the format this partition with mkfs.xfs Linux commands.

[[email protected] ~]# mkfs.xfs /dev/sdb1

meta-data=/dev/sdb1              isize=512    agcount=4, agsize=262080 blks

         =                       sectsz=512   attr=2, projid32bit=1

         =                       crc=1        finobt=0, sparse=0

data     =                       bsize=4096   blocks=1048320, imaxpct=25

         =                       sunit=0      swidth=0 blks

naming   =version 2              bsize=4096   ascii-ci=0 ftype=1

log      =internal log           bsize=4096   blocks=2560, version=2

         =                       sectsz=512   sunit=0 blks, lazy-count=1

realtime =none                   extsz=4096   blocks=0, rtextents=0

[[email protected] ~]#

You can see we have successfully create xfs file system. Now you can check our file system with blkid Linux commands. Run this commands and see your partitions should be look like this:-

[[email protected] ~]# blkid
/dev/sda1: UUID="9b1b6c8c-a702-4654-8b65-3ea79c368a84" TYPE="xfs"
/dev/sda2: UUID="5xJdNh-wNQI-68UB-Mgty-kAau-0xjs-PZvplY" TYPE="LVM2_member"
/dev/sdb1: UUID="182cd151-60e6-473a-8f77-fc963ccc9aa8" TYPE="xfs"
/dev/mapper/centos-root: UUID="1c83edd9-d686-4b30-9749-cb5efcfeac8b" TYPE="xfs"
/dev/mapper/centos-swap: UUID="f5b799a2-5536-4ee0-afbf-74ce51591a61" TYPE="swap"
[[email protected] ~]#

You can see in above output we have successfully file system has been created (/dev/sdb1: UUID=”182cd151-60e6-473a-8f77-fc963ccc9aa8″ TYPE=”xfs”)

Mounting Hard Disk partition

Now we need to mount this hard disk partitions to use this disk partitions. we can use mount Linux command to mount this partition. Let’s start mount this partitions. First of all we need to create one directory where we wont to mount this partition.

Create /backup1 directory using mkdir command.

[[email protected] ~]# mkdir /backup1
[[email protected] ~]# ls -l /backup1/
total 0
[[email protected] ~]#

Mount the disk partition using mount commands on /backup1 directory.

[[email protected] ~]# mount /dev/sdb1 /backup1
[[email protected] ~]#

You can check our mounted partition using df -h commands

[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 13G 6.3G 6.8G 48% /
devtmpfs 904M 0 904M 0% /dev
tmpfs 920M 0 920M 0% /dev/shm
tmpfs 920M 8.9M 911M 1% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
/dev/sda1 497M 214M 283M 44% /boot
tmpfs 184M 24K 184M 1% /run/user/1001
tmpfs 184M 28K 184M 1% /run/user/1000
tmpfs 184M 0 184M 0% /run/user/0
/dev/sdb1 4.0G 33M 4.0G 1% /backup1
[[email protected] ~]#

In above output you can see we have successfully created one /backup1 directory and mount /dev/sdb1 partition on this directory. Our mounted directory has been successfully displaying using df -h command.

Checking mounted file system

Now we need to check our mounted file system working properly or not. So create one file in mounted file system, and check its allow to create or not.

[[email protected] ~]# cd /backup1/
[[email protected] backup1]#
[[email protected] backup1]# mkdir test
[[email protected] backup1]#
[[email protected] backup1]# touch test1.sh
[[email protected] backup1]# ls -l
total 0
drwxr-xr-x 2 root root 6 Jul 29 17:45 test
-rw-r--r-- 1 root root 0 Jul 29 17:45 test1.sh
[[email protected] backup1]# cd
[[email protected] ~]#

In above output you can see we have successfully created directory and file in mounted file system. That means our mounted disk partitions is working successfully working.

Making permanent mount file system

Now we can make permanent mount file system, so that file system available after reboot our system. We need to add file system information in /etc/fstab file. like below:-

[[email protected] ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue Sep 26 12:44:35 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=9b1b6c8c-a702-4654-8b65-3ea79c368a84 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
UUID=182cd151-60e6-473a-8f77-fc963ccc9aa8 /backup1 xfs defaults 0 0
[[email protected] ~]#

You can see we have updated /etc/fstab file. Now we are going to reboot this machine and check our file system is properly mounted after reboot or not. So let’s start to check this steps.

[ro[email protected] ~]# reboot
login as: root
[email protected]'s password:
Last login: Sun Jul 29 17:44:45 2018 from 192.168.43.104
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 13G 6.4G 6.7G 49% /
devtmpfs 904M 0 904M 0% /dev
tmpfs 920M 0 920M 0% /dev/shm
tmpfs 920M 8.9M 911M 1% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
/dev/sdb1 4.0G 33M 4.0G 1% /backup1
/dev/sda1 497M 214M 283M 44% /boot
tmpfs 184M 20K 184M 1% /run/user/1001
tmpfs 184M 8.0K 184M 1% /run/user/42
tmpfs 184M 0 184M 0% /run/user/0
[[email protected] ~]#

In above output you can see our /backup1 mounted file system has been successfully automatically mounted.

That’s all we have successfully created hard disk partitions and mounted in CentOS7.

Leave a Reply

Your email address will not be published. Required fields are marked *