Nov 8, 2018

How to create LVM in Amazon AWS EC2 / CentOS

LVM stands for Logical Volume Manager. With LVM, we can create logical partitions that can span across one or more physical hard drives. First, the hard drives are divided into physical volumes, then those physical volumes are combined together to create the volume group and finally the logical volumes are created from volume group.

To create a LVM, we need to run through the following steps.
  1. Select the physical storage devices for LVM
  2. Create the Volume Group from Physical Volumes
  3. Create Logical Volumes from Volume Group
You create your new volume from the "Volumes" menu in your EC2 management console.

You attach your new volume to your ec2 instance by right clicking the new volume and choose attach.

From the "Attachment Information", you can see where it is being attached, such as "/dev/sdf".

Or you can then see your new volume inside your EC2 instance:

$ cat /proc/partitions
major minor  #blocks  name

202        1    8388608 xvda1
202       80   18874368 xvdf

Select the Physical Storage Devices for LVM – Use pvcreate, pvscan, pvdisplay Commands

$ sudo pvcreate /dev/xvdf
 Physical volume "/dev/sdf" successfully created

You can view physical volumes created using the pvscan command

$ sudo pvscan
 PV /dev/sdf                      lvm2 [18.00 GiB]
 Total: 1 [18.00 GiB] / in use: 0 [0   ] / in no VG: 1 [18.00 GiB]

You can view the list of physical volumes with attributes like size, physical extent size, total physical extent size, the free space, etc., using pvdisplay command as shown below.

$ sudo pvdisplay
 "/dev/sdf" is a new physical volume of "18.00 GiB"
 --- NEW Physical volume ---
 PV Name               /dev/sdf
 VG Name               
 PV Size               18.00 GiB
 Allocatable           NO
 PE Size               0   
 Total PE              0
 Free PE               0
 Allocated PE          0
 PV UUID               tlrKSp-LN51-98jM-HJYp-fuW5-5fme-I1nZRd

Create the Volume Group – Use vgcreate, vgdisplay Commands

$ sudo vgcreate vol_grp1 /dev/sdf
 Volume group "vol_grp1" successfully created

LVM Create: Create Logical Volumes – Use lvcreate, lvdisplay command

$ sudo lvcreate -l +100%FREE  -n logical_vol1 vol_grp1
 Logical volume "logical_vol1" created

vgdisplay command lists the created volume groups.

$  sudo lvdisplay
 --- Logical volume ---
 LV Path                /dev/vol_grp1/logical_vol1
 LV Name                logical_vol1
 VG Name                vol_grp1
 LV UUID                3DcMk6-dGIm-EN0H-x4Ou-WYbD-c3Be-bsxTyS
 LV Write Access        read/write
 LV Creation host, time ip-10-73-133-174, 2014-09-22 17:50:08 +0000
 LV Status              available
 # open                 0
 LV Size                18.00 GiB
 Current LE             4607
 Segments               1
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           253:0

$ sudo  mkfs.ext4 /dev/vol_grp1/logical_vol1
mke2fs 1.42.8 (20-Jun-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1179648 inodes, 4717568 blocks
235878 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
144 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, 1605632, 2654208,
4096000

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

$ sudo mkdir /home2

$ sudo mount  /dev/vol_grp1/logical_vol1   /home2

$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
/dev/xvda1                         7.8G  7.6G  130M  99% /
devtmpfs                           281M   20K  281M   1% /dev
tmpfs                              297M     0  297M   0% /dev/shm
/dev/mapper/vol_grp1-logical_vol1   18G   44M   17G   1% /home2

See also:

How to add / attach extra EBS volume to your AWS EC2 instance