Learn Logical Volume Manager in 15 minutes

Logical Volume Manager

I am working as SystemAdmin and have created Logical Volume Partitions on many servers's. After a long time, I realize that I should write about Logical Volume Manager and should share on an own blog which system admins can use easy and can understand the working of Logical Volume Manager.

In this article, i have explaining Logical Volume Manager commands which we need to create LVM partitions easily without affecting our old partitions data.

Before start with Commands let's understand what Logical Volume Manager is and why we need Logical Volume Manager?

LVM [ Logical Volume Manager ]

LVM is a tool for logical volume management which is used to allocating disks and resizing logical volumes.

With the help of Logical Volume Manager, you will be able to increase and decrease the size of your disks or hard drives and also allocate it to one or more physical volumes.

Why we need LVM?

LVM is very useful in a various scenario where your hard disk is getting full, and you want to increase hard drive size without losing data, or also you can reduce as per the requirement.

Commands use in LVM

$ pvcreate: - is used to create a physical volume on the harddrive.

$ pvdisplay :- is used to display created a physical volume on the harddisk.

$ pvremove :- is used to remove a physical volume from harddisk.

$ vgcreate :- is used to create volume group by adding created physical volume in a single group.

$ vgextend :- we can also extend the already created volume group with the help of vgextend command.

$ vgdisplay :- is used to display volume groups on harddrives.

$ vgremove :- we can also remove created volume groups from harddrives with the help of vgremove command.

$ lvcreate :- with the support of this command, we can create logical volume as per the user required a size from the Volume group.

$ lvdisplay :- is used to display created volumes.

$ lvreduce :- we can also reduce the size of already created logical volumes with the help of lvreduce command without losing data.

$ lvremove :- we can also remove created logical volumes if we don’t require.

$ lvextend :- is used to extend the size of the already created logical volume.

Let's learn command mentioned above one by one so you will get an idea to build Logical volumes on your server or system

Before start making logical volume check your system disks storage and make sure on which drive you to want to add logical volume.

If you want to start the fresh creation of logical volume disk without touching already existing drive then follow below steps:

Here I have created three new raw physical partitions with the help of Fdisk command,

Check available disk space

Run below fdisk command to check available disk space on system,

fdisk -l

 

fdisk -l

o/p will look like this :

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes

255 heads, 63 sectors/track, 121601 cylinders, total 1953525168 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk identifier: 0x00077cf4

Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *        2048      499711      248832   83  Linux

/dev/sda2          501758  1953523711   976510977    5  Extended

Partition 2 does not start on physical sector boundary.

/dev/sda5          501760  1953523711   976510976   8e  Linux LVM

Create new partitions

Here, I have created new partitions which I used further for logical volumes. If you want to build new partitions, then follow this steps.

Run below fdisk command with the primary partition of the system.

fdisk  /dev/sda

The device presents a logical sector size that is smaller than

the physical sector size. Aligning to a physical sector (or optimal

I/O) size boundary recommended, or performance may impact.

Command (m for help): m  --> To check fdisk options

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)

Now select 'n' option which helps to create a new partition.

Command (m for help): n

   p   primary (1 primary, 1 extended, 2 free)

   l   logical (numbered from 5)

Then choose the type of partition you want. Linux only provides 4 primary partitions. If you have already 4 partitions on your system, then you need to create an extended partition.

Here I have chosen 'p' then it will prompt you to provide size for your partitions. Three way you can provide size for partition like +5000K, +500M or +5G or use default value which adds remaining available disk space in your partition. I have used default value

Select (default p): p

Partition number (1-4, default 3):

Using default value 3

First sector (1953523712-1953525167, default 1953523712):

Using default value 1953523712

Last sector, +sectors or +size{K,M,G} (1953523712-1953525167, default 1953525167):

Using default value 1953525167

Now save changes with 'w' option and you will come out of fdisk.

Command (m for help): w

 

After saving changes run command partprobe will help to take affect changes we made in fdisk for creating a partition.

prashant-pc ~ # partprobe

Format new partition

Now to use new partition you need to format that partition with ext4 filesystem.

Use below command to format partitions.

mkfs.ext4   /dev/sda3

Mount Partition

Now create a directory and then mount /dev/sda3 on that directory.

Run below commands for mounting partition.

  • mkdir /test
  • mount  /dev/sda3  /test

Add partition in fstab file

After adding any new partition on system or server, you need to add an entry of that partition under /etc/fstab file. If you will not give or add an entry in fstab then after rebooting system your partition or disk will unmount automatically.

vim /etc/fstab

 /dev/sda3    /test    ext4    defaults    0    0

After successful creation of partitions on the system, let's start with the LVM.

Create Physical Volume

When you start LVM process first, you need to convert your disk with physical volume. For creating physical volume use 'pvcreate' command.

Here I have creating a physical volume on /dev/sda1, /dev/sda2, /dev/sda3 partitions.

# pvcreate  /dev/sda1 /dev/sda2 /dev /sda3

Output:  

  Writing physical volume data to disk "/dev/sda1"

   Physical volume "/dev/sda1" successfully created

   Writing physical volume data to disk "/dev/sda2"

   Physical volume "/dev/sda2" successfully created

   Writing physical volume data to disk "/dev/sda3"

   Physical volume "/dev/sda3" successfully created

After running above command check, physical volumes are created using 'pvdisplay' command,

# pvdisplay

Output:

"/dev/sda1" is a new physical volume of "1.00 GB"

--- NEW Physical volume ---

PV Name /dev/sda1

VG Name

PV Size 1.00 GiB

Allocatable NO

PE Size 0

Total PE 0

Free PE 0

Allocated PE 0

PV UUID FJGgGD-c6h3-88v3-mIh5-Y9XL-oPbe-YUlgAl

 

"/dev/sda2" is a new physical volume of "1.00 GB"

--- NEW Physical volume ---

PV Name /dev/sda2

VG Name

PV Size 1.00 GiB

Allocatable NO

PE Size 0

Total PE 0

Free PE 0

Allocated PE 0

PV UUID Nsb7iS-uR6f-eBe2-KmjQ-U1Iy-lnrp-pKbZwX

 

"/dev/sda3" is a new physical volume of "1.00 GB"

--- NEW Physical volume ---

PV Name /dev/sda3

VG Name

PV Size 1.00 GiB

Allocatable NO

PE Size 0

Total PE 0

Free PE 0

Allocated PE 0

PV UUID Nsb7iS-uR6f-eBe2-KmjQ-U1Iy-lnrp-pKbZwX

Create Volume Group

After successful creation of Physical volume, we are going to add all this physical volume to the single Volume Group.

# vgdisplay

Output:

--- Volume group ---

VG Name mynewvg

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 2.99 GiB

PE Size 4.00 MiB

Total PE 510

Alloc PE / Size 0 / 0

Free PE / Size 510 / 2.99 GiB

VG UUID W8G3vJ-ImqE-nR0z-e5ND-fiWe-rHCo-VB9KV1

Create Logical Volume

Now from the volume group, you can create logical volumes as per your requirement of size for each partition.

For creating logical volume use 'lvcreate' command.

# lvcreate   -L   700M  /dev/mynewvg   -n  lvol1

     Logical volume "lvol1" created

# lvcreate   -L   400M  /dev/mynewvg   -n  lvol2

   Logical volume "lvol2" created

 # lvcreate   -L   400M  /dev/mynewvg   -n  lvol3

   Logical volume "lvol3" created

Display all these logical volumes use below command,

   #  lvdisplay

With the help of these three simple commands [pvcreate,vgcreate,lvcreate], you can create partitions on the system as per your requirement.

Remove LVM 

If you want to remove lvm partitions from the system, then follow steps like lvremove,vgremove, and last pvremove.

Logical Volume remove

 

   #  lvremove  /dev/mynewvg/lvol1

   Do you really want to remove active logical volume "lvol1"? [y/n]: y

   Logical volume "lvol1" successfully removed

   #  lvremove  /dev/mynewvg/lvol2

   Do you really want to remove active logical volume "lvol2"? [y/n]: y

   Logical volume "lvol2" successfully removed

   #  lvremove  /dev/mynewvg/lvol3

   Do you really want to remove active logical volume "lvol3"? [y/n]: y

   Logical volume "lvol3" successfully removed

Volume Group Remove

# vgremove   /dev/mynewvg

Volume group "mynewvg" successfully removed

Physical Volume Remove

# pvremove   /dev/sda1  /dev/sda2   /dev/sda3

   Labels on physical volume "/dev/sda1" successfully wiped

   Labels on physical volume "/dev/sda2" successfully wiped

   Labels on physical volume "/dev/sda3" successfully wiped

Extend Volume Size

Sometimes we need some extra space to store data, and we found no space left on disk that time you can use 'lvextend' or 'lvresize' command to increase or extend volume size without affecting on your existing data.

# lvextend  -L   +500M /dev/mynewvg/lvol1

    Extending logical volume lvol1 to 1.5 GiB

    Logical volume lvol1 successfully resized

OR

# lvresize   -L  +500M  /dev/mynewvg/lvol

# resize2fs  /dev/mynewvg/lvol1  [ To take affect of above command ]

Reduce Volume Size

if you want to reduce the size of  Logical volume use below commands,

  • umount   /dev/mynewvg/lvol1
  • e2fsck  -f   /dev/mynewvg/lvol1
  • resize2fs  /dev/mynewvg/lvol1  600M
  • lvreduce  -L  -500M  /dev/mynewvg/lvol1

Extend Volume Group

if you want to increase the Volume group, you need to add another physical volume in volume group use below command,

#  vgextend  /dev/mynewvg    /dev/sda4

   Extending logical volume lvol1 to 3.99 GiB

   Logical volume mynewvg successfully resized

I hope you like the article if you find any difficulties while creating Logical volume then please do comment your queries or problem via the comment section, till then stay tuned to techthings.org for more such valuable articles.

Leave a Reply

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