LVM (Logical volume management) tool is used for creating multiple Logical volumes which means allocating disk space, mirroring and resize volume to anyway without data loss. LVM is very flexibility in managing our storage device.

Multiple physical hard disk combine into one volume groups and create number of logical volumes from group with any size.

Physical volume : A physical volume is typically a hard disk

Volume Group : Its a collection physical volume and logical volume manage into one administrative.

Logical Volume : It’s standard block device such as Logical volume contain file system ( /home or /devicename  )

The below disk space / (root) and /home LVM partitioned and don’t confused with below partitions.

#  df -h

Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vol0-root
                       18G  7.5G  9.4G  45% /
tmpfs                 1.8G  260K  1.8G   1% /dev/shm
/dev/sda1             248M   69M  167M  30% /boot
/dev/mapper/vol0-home
                      504M   17M  462M   4% /home

On this post create three physical volume ( /dev/sda5  /dev/sda6  and /dev/sda7 ) each device have allocated 10 GB  and combine into two different Volume groups (redhat, centos) then create two logical volumes (admin, user). Finally mount this volume to directory /admin and /user.



Assume that, I would like to create three device like /dev/sda5 /dev/sda6 and /dev/sda7 using fdisk command.

One time read the below command action before creating partition.

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
#  fdisk -cu /dev/sda

Command (m for help):  p

Disk /dev/sda: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488397168 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: 0xf9b75ce8

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      526335      262144   83  Linux
/dev/sda2          526336    57870335    28672000   8e  Linux LVM
/dev/sda3        57870336    58918911      524288   82  Linux swap / Solaris

Extend partition:

On Linux we can create maximum four partition so first go to extend the balance space.

a)  Press ‘n’ new partition
b)  Press ‘e’ to extended partition
c)  Press ‘Enter’ on First sector and Last sector
d)  Press ‘p’ Print the partition
 
Command (m for help):  n
Command action
   e   extended
   p   primary partition (1-4)
 e
Selected partition    4
First sector (58918912-488397167, default 58918912):
Using default value 58918912
Last sector, +sectors or +size{K,M,G} (58918912-488397167, default 488397167):
Using default value 488397167

Command (m for help):    p

Disk /dev/sda: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488397168 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: 0xf9b75ce8

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      526335      262144   83  Linux
/dev/sda2          526336    57870335    28672000   8e  Linux LVM
/dev/sda3        57870336    58918911      524288   82  Linux swap / Solaris
/dev/sda4        58918912   488397167   214739128    5  Extended

Now create new partition from extended device (/dev/sda4).

a) Press ‘n’ new partition
b) Press ‘Enter’ on First sector
c) Give value ‘+10G’ on Last sector
d) Press ‘t’ to change partition types
e) Give value ‘5’ partition number
f) Enter partition type ‘8e’ for Linux LVM

Note : Just follow the steps on next two partition
 
Command (m for help): n
First sector (58920960-488397167, default 58920960):
Using default value 58920960
Last sector, +sectors or +size{K,M,G} (58920960-488397167, default 488397167): +10G

Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)

Command (m for help): n
First sector (79894528-488397167, default 79894528):
Using default value 79894528
Last sector, +sectors or +size{K,M,G} (79894528-488397167, default 488397167): +10G

Command (m for help): t
Partition number (1-6): 6
Hex code (type L to list codes): 8e
Changed system type of partition 6 to 8e (Linux LVM)

Command (m for help): n
First sector (100868096-488397167, default 100868096):
Using default value 100868096
Last sector, +sectors or +size{K,M,G} (100868096-488397167, default 488397167): +10G

Command (m for help): t
Partition number (1-7): 7
Hex code (type L to list codes): 8e
Changed system type of partition 7 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sda: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488397168 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: 0xf9b75ce8

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      526335      262144   83  Linux
/dev/sda2          526336    57870335    28672000   8e  Linux LVM
/dev/sda3        57870336    58918911      524288   82  Linux swap / Solaris
/dev/sda4        58918912   488397167   214739128    5  Extended
/dev/sda5        58920960    79892479    10485760   8e  Linux LVM
/dev/sda6        79894528   100866047    10485760   8e  Linux LVM
/dev/sda7       100868096   121839615    10485760   8e  Linux LVM

a) Finally press ‘w’ write table to disk and exit
 
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 or after you run partprobe(8) or kpartx(8)
Syncing disks.

Once you have completed all the steps reboot the system,
 
# init 6

Create PV, VG, LV:
 
Create physical volume

 
#   pvcreate /dev/sda5

  Writing physical volume data to disk "/dev/sda5"
  Physical volume "/dev/sda5" successfully created

or
 
#  pvcreate /dev/sda6 /dev/sda7

  Writing physical volume data to disk "/dev/sda6"
  Physical volume "/dev/sda6" successfully created
  Writing physical volume data to disk "/dev/sda7"
  Physical volume "/dev/sda7" successfully created

To check your physical volume,
 
# pvs

PV         VG   Fmt  Attr PSize  PFree
  /dev/sda2  vol0 lvm2 a--  27.31g  2.81g
  /dev/sda5       lvm2 a--  10.00g 10.00g
  /dev/sda6       lvm2 a--  10.00g 10.00g
  /dev/sda7       lvm2 a--  10.00g 10.00g

To get full configuration each partition,
 
#  pvdisplay  /dev/sda5 /dev/sda6 /dev/sda7

  "/dev/sda5" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda5
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               SMscjD-Dttf-9zzw-SYGg-TAyE-73Hd-t9oUnx
   
  "/dev/sda6" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda6
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               Z1g7PI-WqxM-LdVh-spAE-Vess-FMPv-pQgS16
   
  "/dev/sda7" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda7
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               xeNbeB-2aqf-Glan-TFMx-iJn9-YECn-91c39O

Create Volume Group:

We have already decided as per the above diagram /dev/sda5 create Volume group name of redhat and /dev/sda6, sda7 combine into group name of centos
 
# vgcreate redhat /dev/sda5

  Volume group "redhat" successfully created
# vgcreate centos /dev/sda6 /dev/sda7
 

Volume group "centos" successfully created

To verify the volume groups name, size and free space.
 
# vgs

VG     #PV #LV #SN Attr   VSize  VFree
  centos   2   0   0 wz--n- 19.99g 19.99g
  redhat   1   0   0 wz--n- 10.00g 10.00g
  vol0     1   3   0 wz--n- 27.31g  2.81g

or
 
#  vgdisplay centos

--- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               19.99 GiB
  PE Size               4.00 MiB
  Total PE              5118
  Alloc PE / Size       0 / 0   
  Free  PE / Size       5118 / 19.99 GiB
  VG UUID               j4HlRp-2AK7-6FiA-ivUi-LyLd-Wciw-pcfGBo


Create Logical Volume:

Create logical volume name of admin with 5 GB space from redhat group
 
# lvcreate -n admin  -L +5G redhat

Logical volume "admin" created

Create logical volume name of user with 5 GB space from centos group
 
# lvcreate -n user -L +5G centos

Logical volume "user" created

Now, you can check with commnad vgs and lvs
 
#  vgs

  VG     #PV #LV #SN Attr   VSize  VFree
  centos   2   1   0 wz--n- 19.99g 14.99g
  redhat   1   1   0 wz--n- 10.00g  5.00g
  vol0     1   3   0 wz--n- 27.31g  2.81g
#  lvs

  LV      VG     Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  user    centos -wi-a-   5.00g                                      
  admin   redhat -wi-a-   5.00g                                      
  root    vol0   -wi-ao  18.00g       

LVM Path:
 
# cd /dev/mapper/

# ls
 centos-user  control  redhat-admin  vol0-home  vol0-root  vol0-vserver 

Make file system to ext4
 
# mkfs.ext4  /dev/mapper/redhat-admin

mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

 
# mkfs.ext4 /dev/mapper/centos-user

Mount the Logical:

Now, mount the logical volume redhat-admin to /admin directory and centos-user /user directory

Create two new directory
 
# mkdir /admin
# mkdir /user

Mount the volumes to these directory
 
# mount /dev/mapper/redhat-admin /admin/

# mount /dev/mapper/centos-user /user/

Finally check the partition using df -h command,
 
# df  -h

Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vol0-root
                       18G  7.5G  9.4G  45% /
tmpfs                 1.8G  348K  1.8G   1% /dev/shm
/dev/sda1             248M   69M  167M  30% /boot
/dev/mapper/vol0-home
                      504M   17M  462M   4% /home
/dev/mapper/redhat-admin
                      5.0G  138M  4.6G   3% /admin
/dev/mapper/centos-user
                      5.0G  138M  4.6G   3% /user

set on after reboot add the line in /etc/fstab file.