Resize disks in a Centos 7 Install

The default layout for disks in a Centos deployment may make sense for the average use case, but not for using the machine as a Tripleo all-in-one development box. I have 500 GB of Disk space, and the default installer puts 400GB into /home and 50 GB into /. However, since most of the work here is going to be done in virtual machines, the majority of the /home space is wasted, and I found I have filled up the 50 GB partition on / on a regular basis. So, I want to remove /home and put all the space under /.

Here is my start state.

# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G   18G   33G  35% /
devtmpfs                  16G     0   16G   0% /dev
tmpfs                     16G     0   16G   0% /dev/shm
tmpfs                     16G   33M   16G   1% /run
tmpfs                     16G     0   16G   0% /sys/fs/cgroup
/dev/mapper/centos-home  411G  1.9G  409G   1% /home
/dev/sda1                497M  167M  331M  34% /boot
tmpfs                    3.2G     0  3.2G   0% /run/user/0

Thus far, I only have 1.9 GB under /home, and 33 out of 50 GB under /, so I have enough space to work with. I start by backing up the /home subdirectories to space on the partition that holds /.

mkdir /home-alt
df -h
mv /home/stack/ /home-alt/
umount /home

Edit the Filesystem table to remove the home directory in the future.

#
# /etc/fstab
# Created by anaconda on Wed Jan 20 14:27:36 2016
#
# 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=3347d9ba-bb62-44cf-8dfc-1b961279f428 /boot                   xfs     defaults        0 0
#/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

From the above, we can see that the partition for / and /home are /dev/mapper/centos-root and /dev/mapper/centos-home.

using the pvs command, I can see one physical volume:

  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  476.45g 64.00m

Using vgs, I can see a singe volume group:

  VG     #PV #LV #SN Attr   VSize   VFree 
  centos   1   3   0 wz--n- 476.45g 64.00m

And finally, using lvs I see the three logical volumes that appeared in my fstab;

  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos -wi-a----- 410.70g                                                    
  root centos -wi-ao----  50.00g                                                    
  swap centos -wi-ao----  15.69g 

Remove the centos-home volume:

lvremove /dev/mapper/centos-home
Do you really want to remove active logical volume home? [y/n]: y
  Logical volume "home" successfully removed

Extend the centos-root volume by 410GB. I can resize the underlying file system at the same time by passing -r.

lvextend -r /dev/mapper/centos-root /dev/sda2

Check if it worked:

# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  461G   20G  442G   5% /
devtmpfs                  16G     0   16G   0% /dev
tmpfs                     16G     0   16G   0% /dev/shm
tmpfs                     16G   33M   16G   1% /run
tmpfs                     16G     0   16G   0% /sys/fs/cgroup
/dev/sda1                497M  167M  331M  34% /boot
tmpfs                    3.2G     0  3.2G   0% /run/user/0

I’ll admit, that was easier than I expected.

Return the /home subdirectories to their correct positions in the directory tree.

# mv /home-alt/ayoung/ /home/
# mv /home-alt/stack/ /home/
# rmdir /home-alt/

For references I used:

  1. How to Extend/Reduce LVM’s (Logical Volume Management) in Linux – Part II
  2. Resize your disks on the fly with LVM
  3. and the man pages for the commands listed.

6 thoughts on “Resize disks in a Centos 7 Install

  1. Thanks for this 🙂

    I was running out of space on a production server and this was the first post I came across on a google search.

    Much easier that I thought it was going to be.

  2. You are quite welcome. I keep notes like these public with the hope that they will help others, but also so I can find them myself in a pinch.

  3. After following these steps I can’t boot in default mode. It always goes to emergency mode

  4. Not enough information in your response for me to provide anything helpful. Many things you could have done that triggers that.

  5. Hi there,

    I have VM on ESXI host with Centos7 core.

    I made a four primary partitions (maybe it was mistake, maybe better that last /dev/sda4 is extended). I wanted to add more partition, but it is restricted with four. It says that I need to convert one primary partition with the extended.

    Here is from fdisk -l (There is on /dev/sda 21.5Gb, so 7Gb available) :

    Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x00072bb8

    Device Boot Start End Blocks Id System
    /dev/sda1 * 2048 1026047 512000 83 Linux
    /dev/sda2 1026048 20971519 9972736 8e Linux LVM
    /dev/sda3 20971520 27262975 3145728 83 Linux
    /dev/sda4 27262976 31457279 2097152 8e Linux LVM

    Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 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: 14.4 GB, 14399045632 bytes, 28123136 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

    I think of these solutions :
    1. Add one more virtual HDD in Sphere (then is it posible to extend /dev/sda1 – boot partition ?

    2. Add one usb flash drive and move date there, delete /dev/sda4 and create bigger from the pool?

    What is your suggestion?

    Tnx in advance

    Mladen

  6. Although I did work on vSphere, it was a long time ago, and I don’t remember anything about the Disk management functions. It depends on why you want an additional partition what you should do: add another disk, or resize the existing ones and rebalance. I don’t think you are going to get beyond the 4 partition limit with the disk you have now.

Leave a Reply

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