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!

Advertisements

26 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s