Monday, June 13, 2011

How to extend Volume Groups and Logical Volumes on Linux

In the previous part of this article, we had a look at how to configure LVM, and we had successfully setup a Volume Group and two Logical Volumes. Now we are going to have a look at extending the group and volumes.
So let’s start the story from the previous part of this article!
Because, the mysql volume was filling up quickly so I decided to add a new disk of size 10 GB, and I really want to extend the mysql volume quickly, before the MySQL server stalls. With LVM, thats no longer an issue.

Partitioning the new disk

First let’s have a look at the partition table,
$ fdisk -l
The new disk is /dev/sdb, but its not partitioned, so lets create a single partition spanning the whole disk.
We will do so using the fdisk utility,
$ fdisk /dev/sdb
which will provide you with an interactive console, that you will use to create the partition.
Enter the commands in the following sequence:
Command (m for help): n

Command action
   e   extended
   p   primary partition (1-4)
p

Partition number (1-4): 1

First cylinder (1-1305, default 1): 1

Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): 1305

Command (m for help): w
Note that, in the above example, I have entered n, p, 1, 1, 1305 and w on the prompts.
Now executing the following will show you that our changes have been made to the disk:
$ fdisk -l /dev/sdb
The output will be something like the following,
Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa5081455

Device      Boot      Start      End      Blocks    Id    System
/dev/sdb1               1        1305    10482381   83    Linux

Initializing the new partition for use with LVM

Now that we have a new partition, lets initialize it for use with LVM:
$ pvcreate /dev/sdb1
The output will be similar to the following:
Physical volume "/dev/sdb1" successfully created

Extending the Volume Group

Extending the volume group is as simple as executing the following command (remember that the volume group name in our case is “system” and the partition that we want to add to the volume group is “/dev/sdb1″:
$ vgextend system /dev/sdb1
The output will be similar to the following:
Volume group "system" successfully extended
Now lets see the details of the volume group:
$ vgs
The output will be something like the following,
VG     #PV #LV #SN Attr   VSize  VFree
  system   2   2   0 wz--n- 15.31g 11.31g
See the volume group size is now extended to 15.31 GB.

Extending the Logical Volume

Now we can safely extend the logical volume mysql, so we can store more data on it now.
Before resizing the logical volume, because we will be making changes to its filesystem, we have to unmount the volume,
$ umount /var/lib/mysql/
Extending the logical volume is as simple as executing the following:
$ lvextend -L 6g /dev/system/mysql
The output will be something similar to the following
Extending logical volume mysql to 6.00 GiB
  Logical volume mysql successfully resized
Now lets see the details about the logical volumes:
$ lvs
The output will be something similar to the following,
LV    VG     Attr   LSize Origin Snap%  Move Log Copy%  Convert
  logs  system -wi-ao 2.00g
  mysql system -wi-a- 6.00g
As you can see the logical volume mysql has been extended to the required size of 6 GB

Notifying the file system about change in size of the volume

But simply extending the logical volume to a new size is not enough, we have to resize the filesystem as well,
$ e2fsck -f /dev/system/mysql
$ resize2fs /dev/system/mysql
Now that the filesystem is resized we are ready to mount the volume again,
$ mount /dev/system/mysql /var/lib/mysql/
Lets see the filesystem details now again,
$ df -h
The output is going to be something similar to the following,
Filesystem                Size  Used Avail Use% Mounted on
/dev/sda3                 3.7G  696M  2.9G  20% /
none                      116M  224K  116M   1% /dev
none                      122M     0  122M   0% /dev/shm
none                      122M   36K  122M   1% /var/run
none                      122M     0  122M   0% /var/lock
none                      3.7G  696M  2.9G  20% /var/lib/ureadahead/debugfs
/dev/sda2                 473M   30M  419M   7% /boot
/dev/mapper/system-logs   2.0G   67M  1.9G   4% /var/logs
/dev/mapper/system-mysql  6.0G   68M  5.6G   2% /var/lib/mysql
See that mysql volume has been mounted to the correct size.
See how easy it is to extend volumes and groups.

No comments:

Post a Comment