Wednesday, October 30, 2013

Linux -- Extend Logical Volume Size -- LVM

In this example, we will extend the size of a Linux Logical Volume.
Before starting, I want to give a general information about Linux LVM..
LVM is a logical volume manager for the Linux kernel. It manages disk drives and similar mass-storage devices.
In LVM, disk partitions are assigned to the physical volumes, physical volumes are assigned to Volume Groups and Logical Volumes are related to Logical Volumes, which placed on top of the filesystems.
Any type of filesystem can be created on the logical volumes, including as swap space.
Physical Volumes -> Volume Groups -> Logical Volumes -> Filesystem

Now, lets get to work;
We will extend the size of the logical volume which is mounted to root. We will do this online..

We first create a new partition(sda3) on the disk /dev/sda using fdisk .. We will use the extra space that our storage admin have assigned to our linux server..
If the OS can not see the newly added disk, you can scan the scsi bus to make the OS see the new disk/new size of the disk ..
echo "- - -" > /sys/class/scsi_host/host0/scan
[root@erman ~]# fdisk /dev/sda
The number of cylinders for this disk is set to 52216.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Disk /dev/sda: 429.4 GB, 429496729600 bytes
255 heads, 63 sectors/track, 52216 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 33418 268325662+ 8e Linux LVM
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (33419-52216, default 33419):
Using default value 33419
Last cylinder or +size or +sizeM or +sizeK (33419-52216, default 52216):
Using default value 52216
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

So , we created a new partition (/dev/sda3) in the disk /dev/sda..

We use partprobe to make the OS recognize the newly created partition. partprobe is a program that informs the operating system kernel of partition table changes, by requesting that the operating system re-read the partition table.

lvextend -L +144G /dev/VolGroup00/LogVol00 (I used +144G to extend the logical volume, as I know there is 144 GB more to extend.. you can use other argument with lvextend command something like extend using all the available space etc..)

Finally, we extend the filesystem on the logical volume we just extend using the resize2fs..