Reduce LVM Partition size in 5 steps after OS Provisioning

Rakesh Jain
4 min readSep 18, 2020

--

Sometimes while provisioning a new Linux server here for example CentOS7 , the drive get partioned with the root, boot and swap, and then all the rest of the space is given to the home directory. But lets say we don’t want that much space allocated for home instead root should be given more.

Here, we will learn about after OS provisioning how we can reduce the size of the /home partition and allocate the remaining space back to the root partition.

Step 1: List current Block Devices

List the current block devices present on the system

root@centos7-kernel-test ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 64G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 63G 0 part
├─centos-root 253:0 0 41G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 20G 0 lvm /home
sdb 8:16 0 20G 0 disk

Step 2: Backup /home partition content

Backup all the contents of the /home directory -

[root@centos7-kernel-test ~]# tar -czvf /root/home.tgz -C /home .
./
./vagrant/
./vagrant/.bash_logout
./vagrant/.bash_profile
./vagrant/.bashrc
./vagrant/.vbox_version
./vagrant/.ssh/
./vagrant/.ssh/authorized_keys
./rakesh/
./rakesh/.bash_logout
./rakesh/.bash_profile
./rakesh/.bashrc
./data_backup/
./data_backup/test1
./data_backup/test2
./data_backup/test3
[root@centos7-kernel-test ~]#

Test the backup taken -

[root@centos7-kernel-test ~]# tar -tvf /root/home.tgzdrwxr-xr-x root/root         0 2020-09-18 04:44 ./
drwx------ vagrant/vagrant 0 2020-07-17 06:22 ./vagrant/
-rw-r--r-- vagrant/vagrant 18 2020-04-01 02:17 ./vagrant/.bash_logout
-rw-r--r-- vagrant/vagrant 193 2020-04-01 02:17 ./vagrant/.bash_profile
-rw-r--r-- vagrant/vagrant 231 2020-04-01 02:17 ./vagrant/.bashrc
-rw-r--r-- vagrant/vagrant 6 2020-07-17 06:16 ./vagrant/.vbox_version
drwx------ vagrant/root 0 2020-09-18 04:06 ./vagrant/.ssh/
-rw------- vagrant/vagrant 389 2020-09-18 04:06 ./vagrant/.ssh/authorized_keys
drwx------ rakesh/rakesh 0 2020-09-18 04:43 ./rakesh/
-rw-r--r-- rakesh/rakesh 18 2020-04-01 02:17 ./rakesh/.bash_logout
-rw-r--r-- rakesh/rakesh 193 2020-04-01 02:17 ./rakesh/.bash_profile
-rw-r--r-- rakesh/rakesh 231 2020-04-01 02:17 ./rakesh/.bashrc
drwxr-xr-x root/root 0 2020-09-18 04:44 ./data_backup/
-rw-r--r-- root/root 0 2020-09-18 04:44 ./data_backup/test1
-rw-r--r-- root/root 0 2020-09-18 04:44 ./data_backup/test2
-rw-r--r-- root/root 0 2020-09-18 04:44 ./data_backup/test3

Step 3: Reduce the Size of the /home Partition

Unmount the home volume

[root@centos7-kernel-test ~]# umount /dev/mapper/centos-home[root@centos7-kernel-test ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 908M 0 908M 0% /dev
tmpfs tmpfs 919M 0 919M 0% /dev/shm
tmpfs tmpfs 919M 17M 903M 2% /run
tmpfs tmpfs 919M 0 919M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 41G 1.3G 40G 3% /
/dev/sda1 xfs 1014M 240M 775M 24% /boot
vagrant vboxsf 2.0T 457G 1.6T 23% /vagrant
tmpfs tmpfs 184M 0 184M 0% /run/user/0

Now remove the home logical volume

[root@centos7-kernel-test ~]# lvremove /dev/mapper/centos-home
Do you really want to remove active logical volume centos/home? [y/n]: y
Logical volume "home" successfully removed

Recreate a new 10GB logical volume for /home

[root@centos7-kernel-test ~]# lvcreate -L 10GB -n home centos
WARNING: xfs signature detected on /dev/centos/home at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/centos/home.
Logical volume "home" created.

Format it with xfs filesystem type

[root@centos7-kernel-test ~]# mkfs.xfs /dev/centos/home
meta-data=/dev/centos/home isize=512 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2621440, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

Mount it

[root@centos7-kernel-test ~]# mount /dev/mapper/centos-home[root@centos7-kernel-test ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 908M 0 908M 0% /dev
tmpfs tmpfs 919M 0 919M 0% /dev/shm
tmpfs tmpfs 919M 17M 903M 2% /run
tmpfs tmpfs 919M 0 919M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 41G 1.3G 40G 3% /
/dev/sda1 xfs 1014M 240M 775M 24% /boot
vagrant vboxsf 2.0T 457G 1.6T 23% /vagrant
tmpfs tmpfs 184M 0 184M 0% /run/user/0
/dev/mapper/centos-home xfs 10G 33M 10G 1% /home

Step 4: Extend the /root volume

Now lets extend the /root volume with all of the remaining space and resize the filesystem

[root@centos7-kernel-test ~]# lvextend -r -l +100%FREE /dev/mapper/centos-root
Size of logical volume centos/root changed from 40.98 GiB (10492 extents) to <51.00 GiB (13055 extents).
Logical volume centos/root successfully resized.
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=2685952 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=10743808, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=5246, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 10743808 to 13368320

Step 5: Restore /home partition content

Restore the contents of the /home directory

[root@centos7-kernel-test ~]# tar -xzvf /root/home.tgz -C /home
./
./vagrant/
./vagrant/.bash_logout
./vagrant/.bash_profile
./vagrant/.bashrc
./vagrant/.vbox_version
./vagrant/.ssh/
./vagrant/.ssh/authorized_keys
./rakesh/
./rakesh/.bash_logout
./rakesh/.bash_profile
./rakesh/.bashrc
./data_backup/
./data_backup/test1
./data_backup/test2
./data_backup/test3

Step 6: List all Block Devices

List the block devices again to make sure it was updated

NAME            MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 64G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 63G 0 part
├─centos-root 253:0 0 51G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 10G 0 lvm /home
sdb 8:16 0 20G 0 disk 11:0 1 1024M 0 rom

verify the filesystem type

[root@centos7-kernel-test ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 xfs c8d9181d-7720-4abf-a277-e1d2bc3e9452 /boot
└─sda2 LVM2_member 3k3TuQ-E1Bm-tMcl-d1ck-x6Pp-uooR-6TNa2r
├─centos-root xfs 1396c2eb-dacf-472d-a1c3-c6ae52ed28dc /
├─centos-swap swap 1866250b-23ba-48d2-bf7b-d4df22217b48 [SWAP]
└─centos-home xfs 70728904-0ed5-4604-8597-a1b7f426530d /home
sdb

Hope you like the tutorial. Please let me know your feedback in the response section.

--

--

Rakesh Jain
Rakesh Jain

Written by Rakesh Jain

DevOps Professional | Technical writer

Responses (1)