Monday, February 9, 2015

How to reduce the root partition in LVM


So at work I ended up being told two different things. First I was told use all of the disk space so that we can get an LVM with as much size on it as possible. Then I was told oh we need 20 percent or so so that we can use it for backing up. With that I set out on a mission to find out how to resize LVM space. We come to find out we put the root partition in the LVM. This means we have a few more steps to do.

A few sites mention how to do this but I found one step by step guide with pictures the rest were not as informative as this site and we were using the same distro so I chose to use this for my base documentation. Other changes include a few grammer tweaks.

Best practice would be to make sure you don't have to shrink your LVM as it can become corrupt, and it is much easier to enlarge it.

Modified from:

https://rbgeek.wordpress.com/2013/02/11/how-to-reduce-the-root-partition-in-lvm/

My additions are in blue comments and have brackets
In this tutorial, I am using the CentOS 6 (I believe that its also applicable on other Linux distro but haven't tried yet) that has ext4 partition lv_root mounted as / and lv_swap as swap from the volume group vg_centos6 (which is default), that has two hard drives (66GB & 25GB). Due to some reasons, I want to remove the 25GB hard drive from my computer and want to add new 50 GB hard drive. Before, removing the hard drive from the computer, we need to resize the lv_root, then remove it from volume group and at the end from the physical volume.

WARNING: It’s really dangerous, so backup your data before attempting this. Please don’t blame me, if you destroy your system. You are responsible for your own actions!

Check the size of lv_root before starting this process:
df -h



Boot from CentOS 6 DVD (or any other Linux distro that you are using) and select “rescue” option:



[The next thing I chose to do was I selected to enable the em0 (the ethernet device adapter) incase I needed it for any reason. This was not a step mentioned in the tutorial I took this from.]

Select the Skip, so that it will not mount the filesystem:



Run these commands:

pvscan
vgscan
vgchange -a y
lvscan



Display the lv_root:

lvdisplay /dev/vg_centos6/lv_root



[This section would not run] fsck on the large root filesystem (lv_root):

fsck.ext4 /dev/vg_centos6/lv_root

]

Run e2fsck with -f (force) option:

e2fsck -f /dev/vg_centos6/lv_root



Issue the resize2fs command to reduce the filesystem (Important: The size here is the actual/total size of thelv_root after reduce, not the size that we want to decrease):

resize2fs -p /dev/vg_centos6/lv_root 65G [I chose to use 2750G as we have 4 terabytes and that is what I calculated to be 20 % ish]



Now, issue the lvreduce command to reduce the logical volume size:

lvreduce -L 65G /dev/vg_centos6/lv_root [Once again I chose to use 2750G]



Run lvdisplay command to confirm the change:

lvdisplay /dev/vg_centos6/lv_root



Reboot the system and login. Remove the disk (in my case it is, /dev/sdb1) from volume group and then from physical volume:

[These next two commands would also not run]

sudo vgreduce vg_centos6 /dev/sdb1

sudo pvremove /dev/sdb1

]

Check the size of lv_root after all these changes:

df -h



Success!

2 comments: