Showing posts with label lvm on linux. Show all posts
Showing posts with label lvm on linux. Show all posts

Monday, June 17, 2013

How to reduce the root partition in LVM

In this tutorial, I am using the CentOS6(I believe that its also applicable on other Linux distro but didn’t try 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 destory your system. You are responsible for your own actions!

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

0 Boot from CentOS 6 DVD (or any other Linux distro that you are using) and select “rescue” option: 1 Select the Skip, so that it will not mount the filesystem: 2 Run these commands:
pvscan
vgscan
vgchange -a y
lvscan

3 Display the lv_root:
lvdisplay /dev/vg_centos6/lv_root

4 fsck on the large root filesystem (lv_root):
fsck.ext4 /dev/vg_centos6/lv_root

5 Run e2fsck with -f (force) option:
e2fsck -f /dev/vg_centos6/lv_root

6

Issue the resize2fs command to reduce the filesystem (Important: The size here is the actual/total size of the lv_root after reduce, not the size that we want to decrease):
resize2fs -p /dev/vg_centos6/lv_root 65G

7

Now, issue the lvreduce command to reduce the logical volume size:
lvreduce -L 65G /dev/vg_centos6/lv_root

8

Run lvdisplay command to confirm the change:
lvdisplay /dev/vg_centos6/lv_root

9

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

sudo pvremove /dev/sdb1

10

Check the size of lv_root after all these changes:
df -h

11

Success!

Hope this will help you!

How to extend the root partition in LVM

In this tutorial, I am using the CentOS6(but also applicable on other Linux distro) that has ext4 partition lv_root mounted as / and lv_swap as swap from the volume group vg_centos6 (which is default), we pretend that we are running out of space in lv_root(/) and volume group (vg_centos6) doesn’t have any free space. We added a brand new drive with 20 GB space. Now, we need to assign this 20 GB space to volume group and then extend lv_root(/).

WARNING: Backup your data before attempting this.

First, we’ll check the file system disk space usage and logical volume information:
sudo df -h
sudo lvs

1

Get the information about newly added hard disk using the following command:
sudo fdisk -l

2Note: /dev/sdb is the newly added hard disk!

To create the partition on the second Hard disk, use the following command and follow the “on screen” instructions and change the partition type from Linux to LVM (8e):
sudo fdisk /dev/sdb

3

Identify the already mounted lvm file system type:
df -T

4

Format the newly created partition using the following command:
sudo mkfs.ext4 /dev/sdb1

5

Initialize the newly created partition as a physical volume:
sudo pvcreate /dev/sdb1

6

Check the volume groups using the following command:
sudo vgs

7Note: vg_centos6 is the volume group that I want to extend, you can change according to your volume group.

Extend the VG (vg_centos6) with new PV (/dev/sdb1):
sudo vgextend vg_centos6 /dev/sdb1

8

Extend the logical volume (lv_root) with all the free space of the VG(vg_centos6):
sudo lvextend -l +100%FREE /dev/vg_centos6/lv_root

9

Finally, resize the file-system:
sudo resize2fs /dev/vg_centos6/lv_root

10

Verify the file-system is larger using the following commands:
sudo df -h
sudo lvs
sudo vgs

11

Hope this will help you!