Re-sizing partitions in Centos7

Update: I have updated the post with some great tips from CertDepot

 

By default in a CentOS7 install we get a couple of partitions created for the root user and one for home usually something like this:

# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0               2:0    1    4K  0 disk 
sda               8:0    0   70G  0 disk 
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0 69.5G  0 part 
  ├─centos-swap 253:0    0    2G  0 lvm  [SWAP]
  ├─centos-root 253:1    0 45.3G  0 lvm  /
  └─centos-home 253:2    0 22.1G  0 lvm  /home
sr0              11:0    1 1024M  0 rom

Note that the root partition is 45.3Gb and the home partition is 22.1Gb

Often I find myself wanting or needing to remove the centos-home partition and expand the centos-root partition.

It is a pretty straight forward exercise, but one that I often forget the steps involved.

So here they are:

• First backup any data that might exist in /home so you can restore it later.

There are many ways to do this and depending upon how much data you have and where you need to store your backup. So I will leave this up to you to decide how to go about this

• Unmount the centos-home partition.

# umount /home/

• Next show the logical volumes.

# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                H8tAWA-f5nF-iBXR-Ew3L-djcI-4Vpg-zcjsPK
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-07-20 05:28:54 -0400
  LV Status              available
  # open                 1
  LV Size                45.34 GiB
  Current LE             11607
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/home
  LV Name                home
  VG Name                centos
  LV UUID                VvIe9h-ZdOF-KvhU-PDLZ-zpXD-rrMV-MTAwSA
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-07-20 05:28:54 -0400
  LV Status              available
  # open                 0
  LV Size                22.14 GiB
  Current LE             5667
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                1C22cf-Mq0r-5cDY-YX5T-d7sh-tMLu-zbAxAh
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-07-20 05:28:55 -0400
  LV Status              available
  # open                 2
  LV Size                2.03 GiB
  Current LE             520
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

•Now remove the logical volume for centos-home.

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

•You should now have the free space available in VFree when you have a look using vgs.

# vgs
  VG     #PV #LV #SN Attr   VSize  VFree 
  centos   1   2   0 wz--n- 69.51g 22.14g

• Now resize the centos-root partition.

# lvextend --size +22.13GB -r /dev/mapper/centos-root 
  Rounding size to boundary between physical extents: 22.13 GiB
  Extending logical volume root to 67.47 GiB
  Logical volume root successfully resized
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=2971392 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=11885568, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=5803, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 11885568 to 17687552

Note that I expanded the partition by slightly less than the available space, 22.13Gb instead of 22.14Gb, this is just to make sure you avoid hitting an insufficient free space error.

• Confirm your new partition size.

# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0               2:0    1    4K  0 disk 
sda               8:0    0   70G  0 disk 
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0 69.5G  0 part 
  ├─centos-swap 253:0    0    2G  0 lvm  [SWAP]
  └─centos-root 253:1    0 67.5G  0 lvm  /
sr0              11:0    1 1024M  0 rom  

• Remove the centos-home mount from fstab so that the system does not try to mount it at startup

# sed -i '/centos-home/d' /etc/fstab

Reboot and you’re done!

Easy!

37 comments

  1. Hi all. Good description, but I got the same error. After these steps the system could’t find the /home during boot, so it starts recovery mode. My system is a Centos7 in a VM.

    Error logs:
    Dependency failed for /home
    Dependency failed for File System Check on /dev/mapper/centos-home

    Thanks your reply.

    Like

  2. Yes as mentioned above i forget to note that you will need to edit the fstab file to remove the centos-home mount. Will update the post when i have a moment

    Like

  3. 5 points:
    – don’t use “df -h” when dealing with partitions and logical volumes, use “lsblk”, it’s much clearer,
    – explain how to backup your /home directory, for example: cd /; tar –selinux -czvf /root/home.tgz home
    – explain how to restore your data, for example: cd /; tar –selinux -xzvf /root/home.tgz
    – remove the /home entry in the /etc/fstab file, otherwise you will have some trouble when booting,
    – use this command to resize your logical volume in only one step (same command for ext4 and xfs): lvextend –size +28.47GB -r /dev/mapper/centos-root

    Like

  4. I followed the steps exactly and it still fails.

    No entries for /home in /etc/fstab. No entries for /home under lvdisplay. No entries for /home in lsblk.
    For some reason systemd continues to try and mount it and the boot is failing.

    Like

    1. You’ll need to see what your volume group is called then. Follow the steps and note where in my example it says centos. On your machine note the name and use that instead

      Like

  5. Great post, thanks very much. I can’t believe that the default installation on a server gives the majority of the space to /home.

    Like

  6. I guess problem may arise if there are users added in /home directory before removing the lvm. So best to create a backup of /home directory in the root filesystem.

    cd /
    mkdir homebak
    cp -a /home /homebak

    Then proceed in removing lvm. Once root is extended cp -a the backup back to home directory. Also don’t forget to comment out /home in your /etc/fstab

    Like

    1. Pretty sure the post says to make a backup of /home?
      >
      “• First backup any data that might exist in /home so you can restore it later.
      There are many ways to do this and depending upon how much data you have and where you need to store your backup. So I will leave this up to you to decide how to go about this”

      I also mention to remove the entry from fstab
      >
      “• Remove the centos-home mount from fstab so that the system does not try to mount it at startup
      # sed -i ‘/centos-home/d’ /etc/fstab”

      Like

  7. Thanks a lot Sir Calum Hunter for this great post on RE-SIZING PARTITIONS IN CENTOS7, and successfully implemented in my VM’s Project, now my root size was extended to a higher capacity… 100% works for me your good job sir…

    Like

  8. After i did all the steps and rebooted i found out that my xampp was not working anymore after that i found out that the file system did become read-only i get the error: Cannot create temp file for here-document: Read-only file system

    now i am pretty stuck any help?

    Like

    1. Filesystem Size Used Avail Use% Mounted on
      /dev/mapper/cl_server14-root 927G 50G 877G 6% /
      devtmpfs 896M 0 896M 0% /dev
      tmpfs 911M 4.0K 911M 1% /dev/shm
      tmpfs 911M 8.9M 903M 1% /run
      tmpfs 911M 0 911M 0% /sys/fs/cgroup
      /dev/sda2 1014M 166M 849M 17% /boot
      /dev/sda1 200M 9.5M 191M 5% /boot/efi
      tmpfs 183M 0 183M 0% /run/user/0

      Like

      1. After i do: mount -o remount,rw /dev/mapper/cl_server14-root

        the Read-only file system disapairs and apache,proftpd is running but mysql is still dead.. and after restarting the Read-only file system error again popping up. Then i can remount again to solve it but i hope there is a better solution and also one that hopefully get mysql also running again:D

        Like

  9. Great Post Thank you

    Only problem i had was that df wouldn’t recognize the extra size added to / – lsblk showed it but file copy would halt at the original size etc

    “xfs_growfs /” did the trick for me !

    thanks folks

    Will

    Like

  10. Thanks for this post! I didn’t actually need to take the space from centos-home but I did this process after expanding my virtual disk capacity and it worked smoothly!

    Like

Leave a reply to CertDepot Cancel reply