Extend Linux LVM Drive

In this post I am going to take you through how to extend a drive (either VM or SAN) on a Linux server using LVM.

First lets see our current disk layout:

sudo su -
fdisk /dev/sda

Type p to print the partition layout

command (m for help): p

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00069a2d

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 499711 248832 83 Linux
/dev/sda2 501758 41940991 20719617 5 Extended
/dev/sda5 501760 41940991 20719616 8e Linux LVM
We can see that disk a is 21.5GB in size,  and has it a boot partition (sda1) and an extended partition (sda2), which contains our LVM volume (sda5).

We can see that /dev/sda is 21.5GB.  There is a boot partition (sda1).  There is an extended partition (sda2), which has an LVM partition (sda5).  Now lets quit fdisk

Command (m for help): q

I am now going to extend this disk by 10GB, how you do this will depend if the disk is a virtual or SAN disk.  Once the disk has been extended you need to get the OS to see this change.  There are two options, ether reboot or rescan the disk:

To rescan the disk:

sudo su -
cd /sys/class/scsi_disk/
ls

You should see somthing like this:
2:0:0:0

cd 2\:0\:0\:0/
echo '1' > device/rescan

Now lets take a look at our disk layout again:

fdisk /dev/sda

Type p to print the partition layout

command (m for help): p

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00069a2d

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 499711 248832 83 Linux
/dev/sda2 501758 41940991 20719617 5 Extended
/dev/sda5 501760 41940991 20719616 8e Linux LVM

You can see that disk now shows 32.2GB (the extra 10GB now shows)
We now need to create new partition with the extra 10GB. To do this type n:

Command (m for help): n

Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)

Enter p for primary partition:

Select (default p):

Use the next available partition number (3)

Partition number (1-4, default 3):

We now need to enter the First sector. You can see when we printed the partition table, that the last sector for sda5 was 4194991. We are going to use the next available sector which is 41940992.

First sector (499712-62914559, default 499712): 41940992

For the last sector, accept the default:

Last sector, +sectors or +size{K,M,G} (41940992-62914559, default 62914559):

Now we need to select the partition type for our new partition. Enter t as a command:

Command (m for help): t

Select our new partition number:

Partition number (1-5): 3

Enter Hex code 8e for Linux LVM

Hex code (type L to list codes): 8e

Now type w to write the partition table

Command (m for help): w

The partition table has been altered!
We now need to get the OS to see this partition table change:

partprobe -s

/dev/sda: msdos partitions 1 2 <5> 3
You can see from the above, this now shows our new partition 3.  If this doesn’t work as you get an error saying the kernel could not re-read the partition table, then you may need to reboot.

We now need to get our volume group name.  To do this type:

vgdisplay

This will show you all of your volume groups:
-- Volume group ---

VG Name ubuntu
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.76 GiB
PE Size 4.00 MiB
Total PE 5058
Alloc PE / Size 5053 / 19.74 GiB
Free PE / Size 5 / 20.00 MiB
VG UUID QdrSSz-7QgO-xHji-jXzR-dB4J-usg1-1ABQtZ

In this case our volume group is called ubuntu

Now we need to add /dev/sda3 to our volume group:

vgextend ubuntu /dev/sda3

No physical volume label read from /dev/sda3
Physical volume "/dev/sda3" successfully created
Volume group "ubuntu" successfully extended

If we look at our root disk, we will see it is still 17GB

df -h /

Filesystem Size Used Avail Use% Mounted on
/dev/mapper/ubuntu-root 19G 1020M 17G 6% /

Now lets extend our root drive by 10GB

lvextend -L+10G /dev/mapper/ubuntu-root

Extending logical volume root to 28.74 GiB
Logical volume root successfully resized

 

Now lets resize the filesystem on the root disk

resize2fs /dev/mapper/ubuntu-root

resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/mapper/ubuntu-root is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/mapper/ubuntu-root is now 7534592 blocks long.

Now if we look at out root disk, we can see this has been increased from 19 to 29GB

df -h /

Filesystem Size Used Avail Use% Mounted on
/dev/mapper/ubuntu-root   29G 1020M   26G   4% /

 

Reference: http://www.trainsignal.com/blog/resize-vmware-linux-drives