Skip to content

Linux LVM Guide you should never miss

Linux LVM Guide you should never miss
Share

Reading Time: 6 minutes

This tutorial will guide you on Linux LVM (Logical Volume Manager). LVM Provides better flexibility than traditional approval hence we recommend you follow the complete tutorial Linux LVM Guide you should never miss

What is LVM

In the traditional approach, storage capacity is used as per actual individual disk capacity. LVM uses a separate concept compared to the Traditional approach. In LVM storage capacity is handled by combining disk space and pooling the total capacity of the available hard disk (drives). In traditional storage each disk is managed separately, With LVM these disks are aggregated and, for example, if you have 3 drives having 1 TB each then using LVM you can aggregate hence you will have 3 TB storage. You will learn more about how to achieve the same.

Linux LVM Guide you should never miss

Check disk and Associated File System


lsblk is a very useful command and you can find disks and associated file systems in Linux.

[root@tetertechie ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 3.9G 0 lvm [SWAP]
└─centos-home 253:2 0 45.1G 0 lvm /home
sr0 11:0 1 1024M 0 rom
[root@tetertechie ~]#


Find PV ( Physical Volumes )
$pvs – You can find Physical Volumes using pvs command

[root@tetertechie ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <99.00g 4.00m
[root@tetertechie ~]#

Find Free/Unused Disks

$ fdisk -l – You can use this command to find free / Unused disks.
You must have free disks in order to proceed further or plan to add disks in your VMs/ Servers. I will cover detail in a separate post on how to add disks.

Before the Addition of New disks, Please look at all currently attached disks.

[root@tetertechie ~]# fdisk -l

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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 label type: dos
Disk identifier: 0x000b0e53

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 209715199 103808000 8e Linux LVM

Disk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 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 /dev/mapper/centos-swap: 4160 MB, 4160749568 bytes, 8126464 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 /dev/mapper/centos-home: 48.4 GB, 48444211200 bytes, 94617600 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

[root@tetertechie ~]#

Add Disks and Check New Disks

[root@tetertechie ~]# fdisk -l

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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 label type: dos
Disk identifier: 0x000b0e53

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 209715199 103808000 8e Linux LVM

Disk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 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 /dev/mapper/centos-swap: 4160 MB, 4160749568 bytes, 8126464 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 /dev/mapper/centos-home: 48.4 GB, 48444211200 bytes, 94617600 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 /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 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

[root@tetertechie ~]#

Note: You can notice New disks /dev/sdb has been added

How to create Physical Volume

We recommend you follow the complete tutorial Linux LVM Guide you should never miss.
[root@tetertechie ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <99.00g 4.00m
[root@tetertechie ~]#

Create a Partition and LVM disks. You can sub-partition newly added /dev/sdb based on your file system need. In this example, I am taking 20G out of 50GB of /dev/sdb.

[root@tetertechie ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xecfbc9a1.

Command (m for help): p

Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 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 label type: dos
Disk identifier: 0xecfbc9a1

Device Boot Start End Blocks Id System

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-104857599, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): +20G
Partition 1 of type Linux and of size 20 GiB is set

Command (m for help): p

Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 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 label type: dos
Disk identifier: 0xecfbc9a1

Device Boot Start End Blocks Id System
/dev/sdb1 2048 41945087 20971520 83 Linux

Command (m for help):

Change the “System” type to Linux LVM
type “t” and 8e t convert in Linux LVM. You type “L” to view all available types


Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 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 label type: dos
Disk identifier: 0xecfbc9a1

Device Boot Start End Blocks Id System
/dev/sdb1 2048 41945087 20971520 8e Linux LVM

Command (m for help):

Note: use “w” to save the changes

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@tetertechie ~]#

Validate Disk and partition

[root@tetertechie ~]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 50G 0 disk
└─sdb1 8:17 0 20G 0 part
[root@tetertechie ~]#


Create Physical Volume
$ pvcreate /dev/sdb1

[root@tetertechie ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
[root@tetertechie ~]#


Validate Newly Create PV

[root@tetertechie ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <99.00g 4.00m
/dev/sdb1 lvm2 --- 20.00g 20.00g

Now, We have successfully created PV and we are all set to create a Volume Group (VG)

How to create a Volume Group

We recommend you follow the complete tutorial Linux LVM Guide you should never miss
Find Existing VG using $vgs command
$ vgs

root@tetertechie ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 3 0 wz--n- <99.00g 4.00m
[root@tetertechie ~]#


$ vgcreate vg01 /dev/sdb1
vgcreate is the command to create VG , vg01 is the name of VG, /dev/sdb1 are actual disks on which VG has been created.

[root@tetertechie ~]# vgcreate vg01 /dev/sdb1
Volume group "vg01" successfully created
[root@tetertechie ~]#


Validate New VG
Look at the size and name of the VG

[root@tetertechie ~]# vgs vg01
VG #PV #LV #SN Attr VSize VFree
vg01 1 0 0 wz--n- <20.00g <20.00g
[root@tetertechie ~]#

How to create Logical Volume

We recommend you follow the complete tutorial Linux LVM Guide you should never miss.
$ lvs
$lvcreate -l 100%FREE -n lvol01 vg01
Option -l 100%FREE will use complete VG size, however, if you want to use a lesser size like 10GB out of 20 then use the -L option, Example: lvcreate -L 10G -n lvol01 vg01

-n = New Name of LV
vg01 = This is the size of VG which we created before.

[root@tetertechie ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home centos -wi-ao---- <45.12g
root centos -wi-ao---- 50.00g
swap centos -wi-ao---- <3.88g
lvol01 vg01 -wi-a----- <20.00g.
New LV

How to create File System

Find the actual path of LV.
You can use the display command to find the actual path of the Logical Volume.

[root@tetertechie ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg01/lvol01


$ mkfs -t ext4 /dev/vg01/lvol1
In this tutorial of “Linux LVM Guide you should never miss” we are using File System type as ext4 however you can use xfs, ext3, etc based on your need. You can read more on different FS types here

[root@tetertechie ~]# mkfs -t ext4 /dev/vg01/lvol01
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5241856 blocks
262092 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
160 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

How to mount File System

Create a new directory where we will mount the newly created file system.
$ mkdir /mountdir
$mount /dev/vg01/lvol1 /mountdir
$ df -hP
/mountdir

[root@tetertechie ~]# mkdir /mountdir

[root@tetertechie ~]# mount /dev/vg01/lvol01 /mountdir
[root@tetertechie ~]#


[root@tetertechie ~]# df -hP //mountdir
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg01-lvol01 20G 45M 19G 1% /mountdir
[root@tetertechie ~]#

How to mount File system persistently

Take Backup of /etc/fstab file
$ cp /etc/fstab /etc/fstab.backup
$ vi /etc/fstab
Go to the last line and add the below content
/dev/vg01/lvol01 /mountdir ext4 defaults 0 0
Save the File and quit

Conclusion

Read More about LVM

You can read more on how to create Linux Users here
You also read more about LVM here
We hope this tutorial “Linux LVM Guide you should never miss” has helped you to learn and practice Linux LVM. If you have any questions on “Linux LVM Guide you should never miss” or any other related topics please do contact us.


Share

Leave a Reply

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

?>