Home » » Re-Sizing Partitions In Centos7

Re-Sizing Partitions In Centos7

Written By Psychic Zero on Wednesday, August 16, 2017 | 2:54 PM

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!

0 comments:

Post a Comment