Monday, July 7, 2008

Logical Volumes LVM

Logical Volumes are the greatest thing since sliced bread
if your data is stored on a logical volume and you run out of disk space, you can simply add more hard drives to your system and then grow your logical volume onto the new physical hard drives giving your data the extra space it needs, logical volumes can also work on top of Raid since logical volumes by them selves gives you no redundancy at all it is a good idea to have them setup on top of a Raid array.

To set up a logical volume partitions must first be converted into physical volumes using the pvcreate command. once you have added your new hard drives to the system, use fdisk to tag the drives as LVM's the tag hex code for LVM is 8e once tagged you can convert the drives to physical volumes to convert /dev/hda and /dev/hdb into physical volumes type:

pvcreate /dev/hda
pvcreate /dev/hdb

NOTE : to setup a LVM on top of a linux software Raid array you simply convert your software raid device to a physical volume eg:
pvcreate /dev/md0

Once you have one or more physical volumes created, you can create a volume group from these PVs using the vgcreate command.

vgcreate vg1 /dev/hda /dev/hdb

this creates a VG called vg1 from the two disks, /dev/hda and /dev/hdb
more PVs can be added to this volume group using the vgextend command.

pvcreate /dev/hdc
vgextend vg1 /dev/hdc

this will add /dev/hdc to the volume group "vg1"
to remove the PV from vg1 use the vgreduce command

vgreduce vg1 /dev/hdc
you can add as many disks to the volume as you like, think as the volume
as your cache of storage.
Use lvcreate command to create a logical volume which will use the free capacity in the volume group pool. (ie your storage cache)

lvcreate -n lv1 -L 100G vg1

or you can specify the capacity in PE's "physical extents" to find out how many PE's are available
in your volume group type.

vgdisplay vg1 | grep "Total PE"

which returns

Total PE 34456

Then use lvcreate to create a logical volume with 34456 extents

lvcreate -n lg1 -L 34456 vg1

you can also allocate a logical volume from a specific physical partition in the volume group by specifying the partition at the end of the lvcreate command.

lvcreate -L100G -n lg1 /dev/hda /dev/hdb

A logical volume can be removed from a Volume group using lvremove
you must first umount the logical volume.

umount /dev/vg1/lg1
lvremove /dev/vg1/lg1

when you add more hard drives to your system and add them to the Volume group you can then extend the logical volume to make use of the added storage by using the lvextend command. You can specify either an absolute size for the extended LV or how much additional storage you want to add to the LV.

lvextend -L120G /dev/vg1/lg1

will extend the logical volume to 120 GB, while

lvextend -L+20G /dev/vg1/lg1

will extend the logical volume by an additional 20 GB. Once a logical volume has been extended, the file system must be expanded to use the added storage.
to do this use the resize2fs command.

resize2fs /dev/vg1/lg1your done...

other usefull commands
pvs will list all your partitions that you have assigned to a volume group
lvs will list all of your configured logical Volumes
vgs will show you how much available or free space you have available in your volume group
lvscan will scan for active logical volumes
vgdisplay will display the information of your volume group
lvdisplay will display currently configured logical volumes

For a good guide to grow a Virtual Machine checkout the following link

No comments: