LVM, or Logical Volume Management, is a storage device management technology that gives users the power to pool and abstract the physical layout of component storage devices for easier and flexible administration. Utilizing the device mapper Linux kernel framework, the current iteration, LVM2, can be used to gather existing storage devices into groups and allocate logical units from the combined space as needed.
Benefits of LVM
- If you have more than one hard-disk, logical volumes can extend over more than one disk.
- You can set up “striped” LVs, so that I/O can be distributed to all disks hosting the LV in parallel.
- You can create a (read only) snapshot of any LV. You can revert the original LV to the snapshot or delete the snapshot at a later time or delete the snapshot if you no longer need it.
- To use the device for an LVM the device must be initialized as a physical volume (PV).
- Initializing a block device as a physical volume places a label near the start of the device.
- By default, the LVM label is placed in the second 512 byte sector. You can overwrite this default label by placing the label on any of the first 4 sectors.
- PVs (Physical Volumes) are combined into VG. This creates a pool of disk space out of which logical volumes can be allocated.
- Within a VG, the disk space available for allocation is divided into units of a fixed size called extents.
- An extent is the smallest unit of space that can be allocated within a PV. The extents are referred to as physical extents.
Logical Volume Types
In LVM, a volume group is divided up into logical volumes. There are three types of LVM logical volumes:
- Linear volumes
- Striped volumes
- Mirrored volumes
Linear logical volumes
A linear volume aggregates multiple physical volumes into one logical volume. For example, if you have two 10GB disks, you can create 20GB logical volume. The physical storage is concatenated. In linear volume the data gets written into the first disk and then the second disk.
Striped logical volume
When you write data into an LVM logical volume, the file system lays the data out across the underlying physical volume. You can control the way the data is written into the physical volumes by creating a striped logical volume.
- For large sequential reads and writes, this can improve the efficiency of the data I/O.
- Striping enhances performance by writing the data to a predetermined number of volumes in round robin fashion.
- With striping I/O can be done in parallel.
Mirrored logical volume
A mirror maintains identical copies of the data in different devices. When data is written to one device then it is written to the second device as well, i.e. mirroring the data. This provides protection for a device failure. When one leg of the mirror fails, the logical volume becomes a linear volume and can still be accessed.
Best Practice for Creating LVM
# echo "- - -" > /sys/class/scsi_host/hostn/scan # The three dash ("- - -") of the command act as wildcards meaning rescan everything. Remember that the three values normally stand for channel, SCSI target ID, and LUN (Logical Unit Number). # More explanation below # echo "c t l" > /sys/class/scsi_host/hosth/scan Where, -> h is for the HBA card number -> c is the channel on the HBA -> t is the SCSI target ID -> l is the LUN Id. # fdisk -l | grep -i disk # fdisk /dev/sdb n (n for new disk creation) p (p for primary partition) 1 (1 is number of the partition) 2048 (It starts from 2048 sector because the first 512 bytes arereserved for the lvm label) enter for last t (t for type of the partition) 8e (8e for lvm partition type) w (w for write the changes) # pvcreate /dev/sdb1 # vgcreate vg01 /dev/sdb1 # lvcreate -L 100M -n lv01 vg01
Create striped volume when we have 3 disk:
# lvcreate -L 300M -i3 -I64 -n lvstrp /dev /vg01 Where -i3 -> Taking 3 disk or number of disk -I64 -> Striped size # mkfs.xfs /dev/vg01/lvstrp # mkdir /striped # mount /dev/vg01/lvstrp /striped
Create Mirrored logical volume:
# lvcreate -L +200M -m1 -n mirrorlv /dev/vg01 Where m1 -> For mirroring I want a single copy
Note: 200M space will be created and of the same size another 200M size will be created (i.e. mirrored). It will take another 200MB space from the other disk for the redundancy purpose.
# mkfs.xfs /dev/vg01/mirrorlv
LVM configuration files:
# ls /etc/lvm (To get the list of lvm configuration files)
- Metadata backups and archives are automatically created on every volume group and logical volume configuration changes unless disabled in the lvm.conf file.
- By default, the metadata backup is stored in /etc/lvm/backup file and the metadata archives are stored in /etc/lvm/archive file.
You can manually backup the metadata to the
backup file with the vgcfgbackup command. You can restore metadata with the vgcfgrestore command.
LVM volume group metadata:
- The configuration details of a volume group are referred to as the metadata. By default, an identical copy of the metadata is maintained in every metadata area in every physical volume within the volume group. LVM volume group metadata is stored as ASCII.
- If a volume group contains many physical volumes, having many redundant copies of the metadata is insufficient. It is possible to create a physical volume without any metadata copies by using
--metadatacopies 0 option of the pvcreate command
- Once you have selected the number of metadata copies the physical volume will contain, you cannot change that at a later point. Selecting 0 copies can result in faster updates on configuration changes. If you intend to split the volume group in future, every volume group needs at least one metadata copy.
The physical volume label:
By default, the pvcreate command places the physical volume label in the 2nd 512-byte sector. This label can optionally be placed in any of the first four sectors, since the LVM tools that scan for a physical volume label check the first 4 sectors. The physical volume label begins with the string LABELONE.
The physical volume label contains:
- Physical volume UUID
- Size of block device in bytes.
The volume group metadata contains:
- Information about how and when it was created.
- Information about the volume group.
The volume group information contains:
- Name and unique id
- A version number which is incremented whenever the metadata gets updated.
- Any properties: Read/Write? Resizable?
- Any administrative limit on the number of physical volumes and logical volumes it may contain.
- The extent size (in units of sectors which are defined as 512 bytes)
Recovering metadata of physical volume:
- Sometimes, the VG metadata area of a PV could be overwritten or deleted. An error message indicating that the metadata area is incorrect or the system was unable to find a PV with a particular UUID is displayed.
- The following example shows the kind of output that might be displayed if the metadata area is missing or corrupted.
# lvs -a -o +devices
Couldn’t find device with uuid ################
Now taking a practical scenario:
# vgs # dd if=/dev/zero of=/dev/sdc1 bs=1024 count=1 (It will erase the first sector PV id has been removed) # vgs -v /dev/sdc # lvs -a -o +devices
Now after the replacement of the disk
# pvcreate --uuid "##################" --restorefile /etc/lvm/archive/vg01_$#$##$$$$ /dev/sdc1
Now the metadata is restored and it is getting error then first unmount and then inactive:
# vgchange -an --partial /dev/vg01 # vgdisplay -v /dev/sdc # vgcfgrestore /dev/vg01 (Now the configuration is also restored) # lvs -a -o +devices # vgchange -ay /dev/vg01
# mount -a
That’s it, Let us know your opinion on this article using the comment section. Thank you.