Resizing CentOS 7 disk

Couldn’t find any article that explained step by step how to resize the main disks volume in a CentOS 7 vm. In my example I started with a single disk of 8GB and expanded to 20GB. I have two partitions on disk sda

  • sda1 type Linux for boot
  • sda2 type Linux LVM for the remaining logical volumes

Space utilisation as following:

[root@vcd-c1-s1 ~]# df -h
 Filesystem               Size  Used Avail Use% Mounted on
 devtmpfs                 3.9G     0  3.9G   0% /dev
 tmpfs                    3.9G     0  3.9G   0% /dev/shm
 tmpfs                    3.9G  8.8M  3.9G   1% /run
 tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
 /dev/mapper/centos-root  6.2G  1.9G  4.4G  31% /
 /dev/sda1               1014M  179M  836M  18% /boot
 tmpfs                    783M     0  783M   0% /run/user/0

We basically need to:

1) Check and print the existing partitions

fdisk /dev/sda
Command (m for help): p

2) Delete the existing lvm partition (sda2)

Command (m for help): d
Partition number (1-4): 2

3) Create a new LVM partition including the extended new size

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2

You will be prompted for the first and last sector, accept the default settings (type enter) and it will automatically grab all the available extra space

4) Set the new partition to be of type 8e (Linux LVM)

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

5) Check again the changes before writing them to disk

Command (m for help): p
<REMOVED OUTPUT>

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.

6) Reboot the vm

reboot

7) Resize the physical volume to match the size of the partition (sda2)

 pvresize /dev/sda2

  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized 

8) Resize the logical volume to use the new space available

 lvresize /dev/mapper/centos-root /dev/sda2
   Size of logical volume centos/root changed from <6.20 GiB (1586 extents) to <18.20 GiB (4658 extents).
   Logical volume centos/root successfully resized 

9) Finally, resize the file system.
Using resize2fs I was getting the following error:

resize2fs /dev/mapper/centos-root
 resize2fs 1.42.9 (28-Dec-2013)
 resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-root
 Couldn't find valid filesystem superblock.

So after a bit of Googling found out I had to use xfs_growfs instead, as following:

xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=406016 blks
         =     sectsz=512  attr=2, projid32bit=1
         =     crc=1       finobt=0 spinodes=0
data     =     bsize=4096  blocks=1624064, imaxpct=25
         =     sunit=0     swidth=0 blks
naming   =version 2        bsize=4096   blocks=2560, version=2
         =                sectsz=512   sunit=0 blks, lazy-count=1
realtime =none            extsz=4096   blocks=0, rtextents=0
data blocks changed from 1624064 to 4769792

10) Finally check again the file system disk space

df -h
 Filesystem               Size  Used Avail Use% Mounted on
 devtmpfs                 3.9G     0  3.9G   0% /dev
 tmpfs                    3.9G     0  3.9G   0% /dev/shm
 tmpfs                    3.9G  8.8M  3.9G   1% /run
 tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
 /dev/mapper/centos-root   19G  1.9G   17G  11% /
 /dev/sda1               1014M  179M  836M  18% /boot
 tmpfs                    783M     0  783M   0% /run/user/0

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

18 Trackbacks