Thursday, July 6, 2017

How to Create (LVM) Logical Volume in Linux - (PVCreate, VGCreate, LVCreate,LVextend)

LVM - Logical Volume Manager: -
LVM is a tool for logical volume management which includes allocating disks, striping, mirroring and resizing logical volumes. With LVM, a hard drive or set of hard drives is allocated to one or more physical volumes.LVM physical volumes can be placed on other block devices which might span two or more disks.

The advantage of Using LVM: - 

1- Resize Storage Pool
2- Flexible storage capacity 
3- Use striped, Mirror and snapshot volumes

 LVM :-
LVM has 3 concepts:

1- PV (Physical Volume)- It is Whole Disk or Partition 
2- VG (Volume Group)-  Corresponding to one or more PV
3- LV (Logical Volume)- represents a portion of a VG. An LV can only belong to one VG. It’s on an LV that we can create a file system.



Prerequisites: -
1- One running Linux Server (In my case I am using Ubuntu 16.04 LTS)
2- Server IP- 192.168.102.11
3- 3 Hard Disk- sda, sdb, sdc.




















.












Step 1: - Formate raw disk-2 and create Primary partition as Logical Volume Manager (LVM).

Create PV(Physical Volume) using command below:
root@Server16:~# fdisk /dev/sdb















































Step 2: - Formate raw disk-3 and create primary partition as logical Volume manager (LVM).
Do same exercise for Disk-3 also as we did in above picture; 
root@Server16:~# fdisk /dev/sdc
















































Step 3: - Create PV Physical Volume:
Let's Create Physical Volume using both the partition sdb1 and sdc1

root@Server16:~# pvcreate /dev/sdb1 /dev/sdc1
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdc1" successfully created
root@Server16:~#
Let's view created the physical volume using below command.

root@Server16:~# pvs
  PV         VG          Fmt  Attr PSize  PFree
   /dev/sdb1              lvm2 ---  10.00g 10.00g
  /dev/sdc1              lvm2 ---  10.00g 10.00g

Step 4: - Create VG Volume Groups
Create Volume Group in the name of tecmint_add_vg using available free PV

root@Server16:~# vgcreate VGroup /dev/sdb1 /dev/sdc1
  Volume group "VGroup" successfully created
root@Server16:~#

Let's Display created Volume Group using the command below:

root@Server16:~# vgs
  VG          #PV #LV #SN Attr   VSize  VFree
  VGroup       2   0   0 wz--n- 19.99g  19.99g

Step 5: - Create LV  Logical Volume

Now Create Logical Volume using the command below:

root@Server16:~# lvcreate -L 5GB -n firstLV VGroup
  Logical volume "firstLV" created.
root@Server16:~#



Where 

lvcreate       =           command to create LV

-L                  =           Syntax to give size

5GB             =           Size of LV

first LV        =            Name of Logical Volume
VGroup       =           Name of Volume Group



Display LV using the command below:



















Step 6: - Create File System and format LV
For using the logical volumes we need to format. Here I am using ext4 file-system to create the volumes and going to mount under /mnt/.
root@Server16:~# mkfs.ext4 /dev/VGroup/firstLV
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 1310720 4k blocks and 327680 inodes
Filesystem UUID: c9788493-b938-47bd-9f62-39d1f1b89b6b
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736

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

Step 7: - Create mount point and make fstab entry to mount volume permanently
7a- Create mount point using below command

root@Server16:~# mkdir /mnt/FLV
7b-Let's make fstab entry to mount Logical volume permanently. append line below in the end of fstab file.

/dev/VGroup/firstLV     /mnt/FLV        ext4    errors=remount-ro       0       0

Save and exit from file.
7c- apply made changes without rebooting PC using below command.

root@Server16:~# mount -a
Let's check partition using below command.
root@Server16:~# df -h
Filesystem                                                                        Size  Used Avail Use% Mounted on
udev                                                                              351M     0  351M   0% /dev
tmpfs                                                                              75M  3.6M   71M   5% /run
/dev/mapper/Server16--vg-root                                                     6.6G  5.8G  477M  93% /
tmpfs                                                                             371M     0  371M   0% /dev/shm
tmpfs                                                                             5.0M     0  5.0M   0% /run/lock
tmpfs                                                                             371M     0  371M   0% /sys/fs/cgroup
/dev/sda1                                                                         472M  248M  200M  56% /boot
/dev/mapper/VGroup-firstLV                                                        4.8G   10M  4.6G   1% /mnt/FLV

!!!Logical Volume created Successfully!!!

Additional helpful command and task:

Check Volume Group Total Size and available free space
root@Server16:~# vgs
  VG          #PV #LV #SN Attr   VSize  VFree
 VGroup        2   1   0 wz--n- 19.99g 14.99g
Check Logical Volume Total Size and available free space

root@Server16:~# lvs
  LV       VG         Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
 firstLV VGroup      -wi-ao----   5.00g
Check Logical Volume Total Size and available free space


Extend Logical Volume:

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

root@Server16:~# lvextend -L+2000 /dev/VGroup/firstLV
  Size of logical volume VGroup/firstLV changed from 4.98 GiB (1275 extents) to 6.93 GiB (1775 extents).
  Logical volume firstLV successfully resized.
root@Server16:~#
Let's Apply Changes:

root@Server16:~# resize2fs /dev/VGroup/firstLV
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/VGroup/firstLV is mounted on /mnt/FLV; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/VGroup/firstLV is now 1817600 (4k) blocks long.
Check new Size:
Rename VG:
We can rename Volume Group name latter whenever need to change it. Use commadn below to change VG name.

root@Server16:~# vgrename VGroup VG01
Volume group "VGroup" successfully renamed to "VG01"
In the above command Command where

vgrename                    =               it is a command
VGgroup                     =               it is old VG name
VG01                          =                it is new VG name.
Rename LV:
We can rename Logical Volume name latter wh the never need to change it. Use commadn below to change LV name.

root@Server16:~# lvrename /dev/VG01/firstLV /dev/VG01/LV01
Renamed "firstLV" to "LV01" in volume group "VG01"


!!!!That's All, Thank you for Reading this Article!!!

No comments:

Post a Comment