Linux LVM | Brief description on few common day to day concepts

Linux LVM | Brief description on few common day to day concepts

Share Now
6 min read
54

Linux LVM creation

Logical Volume Manager is a device mapping technology that provides the flexibility to expand and shrink the size of storage as per the requirement.

To create LVM, there are certain steps. Unlike RAID, it does not require same sized disk.

It takes few physical volumes and make a volume groups out of it and then creates LVM from that volume group.

Physical Volumes (eg: /dev/sdb1, /dev/sdc1 etc)  —-> Volume Group  —-> LVM

Note : We can create LVM using a single disk or using multiple disks.

Lets gets started with some practical approach. You can follow the steps or if you are an advanced user, you can skip few steps and directly move to the command that interests you the most.

Linux LVM

Log in to the Linux Server

Check the disk attached on the server

fdisk -l

Based on the attached disk, you may get an output similar to the below

Where /dev/sdc is a disk attached, but note: It does not have any partition (Disk /dev/sdc doesn’t contain a valid partition table)

lvm concept in linux

Now, we have to create a partition and after that, we have to format that using appropriate filesystem type like ext3, ext4 etc but for LVM we have to make sure, we should NOT format the partition using any of the above filesystems.

We have to set type as LVM.

Process1: Make a partition of “/dev/sdc”

fdisk /dev/sdc

This will give you a prompt to enter some value.

For help simply type “m” and that would leave you with multiple options.

For making “new” partition, type “n”

Select “p” for Primary

You can select 4 primary partitions

After selecting “p” select 1 for the first partition.

Then give a size to the partition (+5G= 5GB)

This will create the 1st partition with 5GB storage.

If you have more space remained on the disk, you can create the 2nd partition the same way(p,2)

Now as the partition is created, we have to make those partitions as LVM

Now, we need to assign LVM to the partition, so we have to select that partition first.

Type “t” → Type

Select the partition → 1

List the items —> l

From the above, we find the LVM has hexacode as 8e.

So, we will select that.

That will give us the below message “Changed system type of partition 1 to 8e (Linux LVM)”

Now, we have to write those changes to the disk, so we will use the option “w”

Now, if you type fdisk -l, you can see these two LVM partitions for /dev/sdc

Now, we can use these disk partitions to create LVM.

LVM creation process includes

  • Pvcreate — > This creates physical volume from those physical partitions
  • Vgcreate —> This will combine all these physical volumes and make it look like a single volume.
  • Ivcreate → This will create logical volumes from that volume group.

Now, we are creating a physical volume.

pvcreate /dev/sdb1 /dev/sda3 /dev/sda4 /dev/sdc1 /dev/sdc2

This creates successfully and later to see the list of physical volumes, execute the command pvs

Now, create a volume group using these physical volumes.

Vgcreate is used to create a volume group.

vgcreate volumegroup_name physical volumes

vgcreate DemoVG /dev/sdb1 /dev/sda3 /dev/sda4 /dev/sdc1 /dev/sdc2

This creates a volume group successfully.

To add more information, you can always use -v or -vvv

Some important & useful command

Pvdisplay

Vgdisplay

Pvs

Vgs

lv
Use Case1:

You have created a volume group by mistake and you want to delete that.

  •       Use the command vgremove name
Use Case2:

Suppose you have created a new physical volume with some new disk partition to increase the pvcreate /dev/sdd1 for an example.

Now, how can you add that physical volume into the existing volume group “DemoVG”

That can be achieved using the command “vgextend”vgextend DemoVG /dev/sdd1
Use case3:
You want to remove one physical volume from the volume group to reduce the size of the volume group.
vgreduce volumegroup_name physical_volume

vgreduce DemoVG /dev/sdd1
Use case 4:

How can you remove a volume group

It needs to have some precaution: This can be only done if there is no LV attached to that VG.

vgremove DemoVG  - - > This command will remove the volume group altogether.
Use case 5:

You already have 3 physical device (/dev/sda5, /dev/sda6, /dev/sda7) as a part of a volume group named “vgone”.

Now, you want to create a new volume group named “vgtwo” but with physical volume from other volume groups. ( Eg: You want to create vgtwo using /dev/sda7, where /dev/sd7 is a part of vgone or spliting one physical volume from one VG to other)

Vgsplit vgone vgtwo /dev/sda7
Use Case6:

You want to combine two volume groups into one. (merge)

Vgmerge vgone vgtwo ( 1st one is retained and 2nd one gets merged with 1st one)

Renaming a volume group : vgrename vgone new_vg_name
Process : 3: Creating LV or Logical Volume:
Command : lvcreate -L (size) -n (name of LV) volume_group_name 
lvcreate -L 1G -n new_lv onevg

How to create a LV using %tage size of VG.

Type “vgs” look for total free space available.

Then to create a LV using 70% of free space. The command would be

lvcreate -l 70%VG -n new_lv onevg

If you want 100% of VG to be used as a LV -- > lvcreate -l 100%FREE lv_again vgone

Removing an LV :

 lvremove /dev/vgone/lv_again

Physical Extent (PE)

Vgdisplay | grep -i ‘total pe’

You can create LV using the PE value also

Lvcreate -l 200 vgone lv_1

Concept

LV is always a linear physical volume creation.

When you create a LV, it first uses the space on the first physical volume on the VG.

However, there is a way, you can define from which PV you want your LV to be created

lvcreate -L 190M -n lv_2 DemoVG /dev/sda3

Creating LV using extents from different PVs

  • lvcreate -l 50 -n lv_3 DemoVG /dev/sdc1:0-25 /dev/sdc2:0-25

Creating LV using extents from same PV but split Extents.

  • lvcreate -l 50 -n lv_4 DemoVG /dev/sda4:0-10:50-

Create LV lv_4 with for only 50 extents from Volume Group DemoVG and use the physical volume

/dev/sda4 but use split extents, use first 10 extents and then use extents from 50 onwards

Reduce the size of LV:

  • Lvreduce -l -2 /dev/DemoVG/lv_3

Changing the permission of LV from RW to Read-only.

  • Lvcgange -pr /dev/DemoVG/lv_2

How to grow the LVM:

Lvextend -L2G /dev/DemoVG/lv_1   — > This will change the size of lv1 from 192Mb to 2GB

Lvextend -L+200M  /dev/DemoVG/lv_1 — > This will add 200MB to its existing size.

Few Reporting parameters:
  • Pvs
  • Pvs -v
  • pvs –separator =      This gives output in human readable
  • pvs –separator = –aligned
  • vgs -o +pv_name   (-o is option)
  • lvs –segments   ( This shows if it is a linear lvm or stripped LVM or mirrored)
  • pvs -o pv_name,pv_size,pv_free,pv_pe_count
  • pvs -o pv_name,pv_size,pv_free,pv_pe_count -O pv_size ( -O to sort with pv_size)
  • pvs –unit M ( It will show the units in MB)   

To Know more about linux visit our page http://liainfraservices.com/

Leave comment

Your email address will not be published. Required fields are marked with *.