Resizing Disk in CentOS 7

Resizing Disk in CentOS 7

By: ZebraHost

This article will show you how to resize a disk in Centos

  1. First ssh to your server as root or as a user that can gain root access.
ssh root@<ip-address>
  1. Once you are on the server you will list the disk and partitions:

Using df -h will result in the following:

[root@zhwhm ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G   12K  1.9G   1% /dev/shm
tmpfs                    1.9G  188M  1.7G  10% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root  146G  123G   23G  85% /
/dev/sda1               1014M  193M  822M  19% /boot
/dev/loop0               2.0G   90M  1.9G   5% /var/tmp
/dev/loop1               3.8G  8.0M  3.6G   1% /tmp
tmpfs                    379M     0  379M   0% /run/user/0

Using fdisk -l will result in the following:

[root@zhwhm ~]# fdisk -l

Disk /dev/sda: 161.1 GB, 161061273600 bytes, 314572800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 65536 bytes
Disk label type: dos
Disk identifier: 0x000a8ae8

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   314572799   156236800   8e  Linux LVM

Find drive you want to extend and fdisk <drivename> to it

[root@zhwhm ~]# fdisk /dev/sda
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): 

Using p here will print the partition table:

Command (m for help): p

Disk /dev/sda: 161.1 GB, 161061273600 bytes, 314572800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 65536 bytes
Disk label type: dos
Disk identifier: 0x000a8ae8

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   314572799   156236800   8e  Linux LVM

Command (m for help): 

If the above partition table does NOT show the LVM as the last partition. STOP! Please contact us for assistance.

We want to delete and recreate the Linux LVM partition here. We will use the d option to delete that partition:

Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted

Command (m for help): 

That partition has been deleted, we will now use n to recreate a new partition with bigger size:

Leaving the defaults to fill the rest of the drive.

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

Now we have to set the partition type using t and the code of 8e

Command (m for help): t
Partition number (1,2, default 2):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): 

Now we can review the partition table again using p to see our new size:

The above cli snippets were for reference, the size didn’t change but the process is the same.

Command (m for help): p

Disk /dev/sda: 161.1 GB, 161061273600 bytes, 314572800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 65536 bytes
Disk label type: dos
Disk identifier: 0x000a8ae8

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   314572799   156236800   8e  Linux LVM

Command (m for help): 

Finally use the w command to write the partition table and exit fdisk

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

At this point the partition is changed but the kernel still has the old table in memory. We have two options here

A. We can reboot the server

OR

B. We can use partprobe to refresh the table in memory without a reboot.

We will refresh the table with partprobe:

[root@zhwhm ~]# partprobe /dev/sda
[root@zhwhm ~]#

Now that the table is refreshed we need to resize the pvs volume, we can do this with pvresize.

[root@zhwhm ~]# pvresize /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized

After resizing the physical volume, we need to resize the mapped logical volume to the free spaceo the newly expanded disk. We can do this with lvresize

[root@zhwhm ~]#  lvresize /dev/mapper/centos-root /dev/sda2

Now we have to grow the filesystem on that drive. We have two options depending on the filesystem type:

If it is XFS we will use xfs_growfs.

[root@zhwhm ~]# xfs_growfs /dev/centos/root

Otherwise we would use resize2fs.

[root@zhwhm ~]#resize2fs /dev/mapper/centos-root

We have now resized the disk. We can check with df

[root@zhwhm ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G   12K  1.9G   1% /dev/shm
tmpfs                    1.9G  188M  1.7G  10% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root  146G  123G   23G  85% /
/dev/sda1               1014M  193M  822M  19% /boot
/dev/loop0               2.0G   90M  1.9G   5% /var/tmp
/dev/loop1               3.8G  8.0M  3.6G   1% /tmp
tmpfs                    379M     0  379M   0% /run/user/0

Congratulations! We have now resized the disk.