Sunday, March 21, 2021

Create Logical Volume Manager (LVM) in Linux

In Linux, Logical Volume Manager (LVM) is a device mapper framework that provides logical volume management for the Linux kernel. Most modern Linux distributions are LVM-aware to the point of being able to have their root file systems on a logical volume. Heinz Mauelshagen wrote the original LVM code in 1998 (Source: wiki)

Advantages of LVM: (Source: wiki)

  • Volume groups (VGs) can be resized online by absorbing new physical volumes (PVs) or ejecting existing ones.
  • Logical volumes (LVs) can be resized online by concatenating extents onto them or truncating extents from them.
  • LVs can be moved between PVs.
  • Creation of read-only snapshots of logical volumes (LVM1), leveraging a copy on write (CoW) feature, or read/write snapshots (LVM2)
  • VGs can be split or merged in situ as long as no LVs span the split. This can be useful when migrating whole LVs to or from offline storage.
  • LVM objects can be tagged for administrative convenience.
  • VGs and LVs can be made active as the underlying devices become available through use of the lvmetad daemon
LVM Elements: (Source: wiki)


Physical volumes (pv) are regular storage devices. LVM writes a header to the device to allocate it for management.

Volume Groups (vg) is the combination of physical volumes into storage pools known as volume groups.

Logical Volumes (lv) is the sliced portion of volume group

Create LVM:
Usually fdisk is used for drives that are smaller than 2TB and either parted or gdisk is used for disks that are lasger than 2TB. Here is a very good article that covers the differences, titled: The Differences Between MBR and GPT.

Find out the harddisk:
server1:~# fdisk -l

As there is no partitions yet on /dev/sdb, let's create a partition on it and assume the harddisk is larger than 2TB, we will use parted tool

parted /dev/sdb
(parted) mklabel gpt
(parted) unit TB
(parted) print
(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]? xfs # other known filesystems are ext3, ext4
Start? 0
End? 11TB
(parted) print # print newly created disk
(parted) quit

Now check the changes again:
server1:~# fdisk -l

Now create PV, VG and LV. Once this is done, format the partition in desired partition type and mount the volume in under a mount point. For persistent, update fstab file accordingly.

server1:~# pvcreate /dev/sdb1        # create physical volume
server1:~# pvs                         # view physical volume
server1:~# vgcreate vg_data /dev/sdb1  # create volume group
server1:~# vgs                         # view volume group
server1:~# vgdisplay -v vg_data        # display volume group details with FREE PE (Physical Extent) number
server1:~# lvcreate -l +2621437 -n lv_data vg_data    # create logical volume group with FREE Physical Entent (PE) number
server1:~# vgdisplay -v vg_data        # view the volume group
server1:~# lvs                         # view the logical volume
server1:~# mkfs.ext4 /dev/vg_data/lv_data # format partition with ext4
OR
server1:~# mkfs.xfs /dev/vg_data/lv_data # format partition with xfs
server1:~# mkdir /data     # create a mount point
server1:~# mount /dev/vg_data/lv_data /data/ # mount newly created partition

server1:~# df -Th

server1:~# vim /etc/fstab

/dev/vg_data/lv_data /data/ default 0 0 # add fstab entry for a persistent automount after reboot, first 0 option is for skip backup and second 0 option is for no filesystem check (fsck) at boot time

Let's assume the harddisk /dev/sdb is < 2TB. So for this, we will use fdisk instead of parted

server1:~# fdisk -l
server1:~# fdisk /dev/sdb
server1:~# fdisk> m # for help
fdisk> n # create new partition
fdisk> Primary/ Extended? Primary
fdisk> Partition ID: 1
fdisk> Start: [enter for default]
fdisk> End: [enter for default]
fdisk> p       # print newly created partition
fdisk> t                              # change the partition ID
fdisk> L [Print all types]       # change the partition ID
fdisk> 8e           # 8e is the partition code for Linux LVM
fdisk> w       # write and exit
server1:~# fdisk -l

Repeat the same process again starting from creation of PV, VG and LV. Format the partition and mount as above process.

Cheers :-)

No comments:

Restore Archived Log into VMware Aria Operations for Logs (formerly known as vRealize Log Insight - vRLI)

As we cannot keep all logs in searchable space in vRLI production system due to performance and slowness issue, it is always recommended to ...