How to configure LVM in Linux cpanel server.

LVM or Logical Volume Management is a tool used by Unix/Linux administrators to manage disk resources.  LVM provides a layer of abstraction between the underlying physical disk/volume and the host operating system.  LVM partitions can span across physical hard drives and can be re-sized.

We can use the following steps to create LVM.

    Partitioning
    Physical volume(s) creation
    Volume group(s) creation
    Logical volume(s) creation
    Formatting of the file system
    Mounting of file system
    Updating fstab for automatic volume mounting

lvm setup in linux

Create Partition

Use this LVM configuration in Linux cpanel server, LVM partitions must be of type 8e (Linux LVM.) Create new partition and make sure with ID 8e.

[[email protected] ~]# fdisk -l

Disk /dev/sda: 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: 0x000d5460

Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1        1306    10484736   8e  Linux LVM

if not then you can create partition with the type of the file system to 8e using t option.

[[email protected] ~]# fdisk /dev/sda

Physical Volume (PV)

physical storage unit of an LVM logical volume is a block device such as a partition or whole disk. To use the device for an LVM logical volume the device must be initialized as a physical volume (PV).

Check whether any physical volumes (PV’s) already exist on the system.

[[email protected] ~]# pvdisplay

Use the following command to create Physical Volume

[[email protected] ~]#pvcreate /dev/sda1
Physical volume “/dev/sda1” successfully created
[[email protected] ~]#

Volume Group (VG)

Physical volumes are combined into volume groups (VGs). This creates a pool of disk space out of which logical volumes can be allocated. Within a volume group, the disk space available for allocation is divided into units of a fixed-size called extents.

Check Volume Groups the are already present on the system

[[email protected] ~]# vgdisplay

Create Volume Group

[[email protected] ~]# vgcreate VolGroup /dev/sda1
Volume group “VolGroup” successfully created

[[email protected] ~]# vgdisplay
--- Volume group ---
VG Name               VolGroup
System ID
Format                lvm2
Metadata Areas        2
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                2
Act PV                2
VG Size               10.70 GiB
PE Size               4.00 MiB
Total PE              4481
Alloc PE / Size       4481 / 10.70 GiB
Free  PE / Size       0 / 0
VG UUID               TGFbtz-ysqf-XE8e-fDb0-dRzh-hfzE-Jx484g

Logical Volume

we have a Volume Group created that has free physical extents, we are now ready to create our new Logical Volume (LV).  LV’s can be created using a number of extents (with the -l command line switch) or by total size (with the -L command line switch followed by KB, MB, GB or TB.)

just use this command to create LV with all the space from that VG.

lvcreate <lvname> <vgname>

[[email protected] ~]#lvcreate lv_home VolGroup
Logical volume “lv_home” created

[[email protected] ~]# lvdisplay
--- Logical volume ---
LV Name                /dev/VolGroup/lv_home
VG Name                VolGroup
LV UUID                trHeKm-Cxbu-cApC-IaHd-gxRW-RMdC-dw4msV
LV Write Access        read/write
LV Status              available
# open                 1
LV Size                10.54 GiB
Current LE             4233
Segments               2
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           253:0

If you need 4G of LV then define a new LV that is 4GB in size

[[email protected] ~]#lvcreate -L 4GB -n lv_home VolGroup
Logical volume “lv_home” created

To create LV with full disk space

[[email protected] ~]# lvcreate -n lv_home -l 100%FREE VolGroup
Logical volume “lv-home” created

Formatting a File System

Linux offers many different file systems, some distributions of Linux/Unix will tend to recommend EXT3 while others might recommend XFS or ReiserFS.

[[email protected] ~]#mke2fs -j /dev/VolGroup/lv_home

Mounting a File System

create and mount that drive for your directory.

[[email protected] ~]#mkdir /home

[[email protected] ~]#mount /dev/VolGroup/lv_home /home

Once completed view your mounted device.

[[email protected] ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_home
10G  789M   8G   7% /home

Thats all!!!

To remove LV, you can use this command.

[[email protected] ~]# lvremove /dev/VolGroup/lv_home
Do you really want to remove active logical volume lv_home? [y/n]: y
Logical volume “lv_home” successfully removed

Using LVextend Command

Change the size of the logical volumes Using lvextend Command

We can extend the size of the logical volumes after creating it by using lvextend utility.

For example logical volume extend size of 100M

[[email protected] ~]# umount /dev/VolGroup/lv_home

Volume group free space will add to LV.

[[email protected] ~]#lvextend -L+100 /dev/VolGroup/lv_home
Extending logical volume lv_home to 100.00 MB
Logical volume lv_home successfully resized

[[email protected] ~]# e2fsck -f /dev/VolGroup/lv_home
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/VolGroup/lv_home: 12/131072 files (0.0% non-contiguous), 25388/524288 blocks

[[email protected] ~]# resize2fs /dev/VolGroup/lv_home
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/home2lvm-lv_home2 to 786432 (4k) blocks.
The filesystem on /dev/mapper/home2lvm-lv_home2 is now 786432 blocks long.

Mount that partition now.

[[email protected] ~]# mount /dev/VolGroup/lv_home /home2

/dev/mapper/home2lvm-lv_home2
3.0G   67M  2.8G   3% /home2

That’s All….

 

 

How to increase mounted partition size in LVM

Its is possible to grow a partition safely without unmounting and remounting your volume. These steps presume you have extra disk available on your volume which is currently unallocated.

Run pvscan to verify you have allocatable space in your target Logical Volume:

pvscan

PV /dev/sda2 VG VolGroup00 lvm2 [19.88 GB / 1020.00 MB free]
PV /dev/sdb VG VolGroup01 lvm2 [100.00 GB / 20.00 GB free]

From the output, you can see VolGroup01, which contains the partition want to grow, had some free space.

Run lvresize to add free space to your partition:

VG name : home2lvm

LVname :  lv_home2

[[email protected] ~]# lvresize -L+1.9G -n /dev/mapper/home2lvm-lv_home2
Rounding size to boundary between physical extents: 1.90 GiB
Extending logical volume lv_home2 to 4.90 GiB
Logical volume lv_home2 successfully resized

[[email protected] ~]# resize2fs /dev/home2lvm/lv_home2
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/home2lvm/lv_home2 is mounted on /home2; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/home2lvm/lv_home2 to 1285120 (4k) blocks.
The filesystem on /dev/home2lvm/lv_home2 is now 1285120 blocks long.

Verify the disk space added in your current volume.

df -h

How to use VGExtend ?

When you add new drive, you can extend Volume Group and resize LV with out unmount.

After new drive attached, create partition with LVM code 8e.

Create PV

[[email protected] ~]# pvcreate /dev/xvdc1
Physical volume “/dev/xvdc1” successfully created
[[email protected] ~]# vgextend home2lvm /dev/xvdc1
Volume group “home2lvm” successfully extended

VGExtend

[[email protected] ~]# vgextend home2lvm /dev/xvdc1
Volume group “home2lvm” successfully extended

LVresize

[[email protected] ~]# lvresize -L+2.05G -n /dev/mapper/home2lvm-lv_home2
Rounding size to boundary between physical extents: 2.05 GiB
Extending logical volume lv_home2 to 6.95 GiB
Logical volume lv_home2 successfully resized

[[email protected] ~]# resize2fs /dev/mapper/home2lvm-lv_home2
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/home2lvm-lv_home2 is mounted on /home2; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/mapper/home2lvm-lv_home2 to 1822720 (4k) blocks.
The filesystem on /dev/mapper/home2lvm-lv_home2 is now 1822720 blocks long.

You are Done!!! Check with drive size

df -h

 

5.00 avg. rating (95% score) - 1 vote
Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive