L.V.
M (Logical Volume Manager)
Now a day every Server needs more space for day to day requirement for that we need
to extend depending on our needs.
LVM is a method of allocating hard drive space into logical volumes that can be easily
resized as we wish instead of partitions and makes it easier to manage disk space. With
LVM the hard drives (or) set of hard drives are converted into one or more physical
volumes. The physical volumes are combined into volume groups. Each volume group is
divided into logical volumes which are assigned to mountpoints such as /home and
filesystem types such as ext3 or ext4 or xfs
While using logical volumes we can extend across multiple disks, logical volumes or
reduce logical volumes in size without reformatting and re-partitioning the current disk.
Volumes can stripes data across multiple disks this can increase the I/O stats.
Logical volumes can be used in RAID, SAN.
LVM Features
It is flexible to expand the space at any time.
Any file systems can be installed and handle.
Migration can be used to recover faulty disk without data loss.
Restore the file system using Snapshot features to earlier stage. etc..
To configure LVM
1) Create LVM enable partitions
2) Convert them as physical volumes
3) Create volume groups from physical volumes
4) Create logical volumes from volume groups and assign mountpoints
Create LVM enable partitions
Ex: - create 3 lvm enables partitions
#fdisk -l
#fdisk /dev/sda
n
+200M
t
<partition number>
8e
w
# partx a /dev/sda
To convert LVM partitions as physical volumes
#pvcreate /dev/sda<partition numbers>
Ex: pvcreate /dev/sda{5,6,7}
A&N Teck Solutions 1
Suresh Reddy (RHCE,RHCSS,CCNA)
[Link]@[Link]
To view physical volumes
#pvdisplay (or) #pvs
To create volume group
#vgcreate <vg name><pvs or partitions>
ex: vgcreate vg1 /dev/sda{5,6,7}
To view volume groups
#vgdisplay (or) #vgs -v
Output:
VG Name A Volume Group name.
Format LVM Architecture Used LVM2.
VG Access Volume Group is in Read and Write and ready to use.
VG Status VG can be re-sized, we can expand more if we need more space.
Cur LV Currently created Logical volumes in this Volume Group.
CurPV and Act PV Currently using Physical Disk and its active, so what we can
use this volume group.
PE Size Physical Extends. Size for a disk can be defined using PE or GB size,
4MB is the Default PE size of LVM. For example, if we need to create 5 GB size of
logical volume we can use sum of 1280 PE, i.e 5G/4
Total PE This Volume Group have.
Alloc PE Total PE Used, full PE already Used
Free PE
Note: we can also create vg with specified PE size by using -s option
Ex: #vgcreate -s <PE size 32M> <vgname> <PVs>
Understanding vgs command output:
VG: Volume Group name.
PV: Number of Physical Volumes used in this Volume Group.
LV: Number of Logical Volumes
SN: Number of Snapshots the volume group contains
Attr: Status of the Volume group as Writeable, readable, resizeable, exported,
partial and clustered, Here it is wzn- that means w = Writable, z = resizeable..
Vsize: Total Size of the Volume Group.
Vfree: Shows free space available in this volume group.
To create a logical volume
#lvcreate -L <+size><vg name> -n <LV name>
Ex: lvcreate -L +500M /dev/vg1 -n lv1
Creating Logical Volumes using PE Size
#lvcreate -l (Extend size) -n (name_of_logical_volume) (volume_group)
To view logical volumes
A&N Teck Solutions 2
Suresh Reddy (RHCE,RHCSS,CCNA)
[Link]@[Link]
#lvdisplay (or) #lvs
To make filesystem on logical volumes
#mkfs.ext4 /dev/vg1/lv1
Create a mountpoint and mount logical volume on it
#mkdir /vinuthna
#mount /dev/vg1/lv1 /vinuthna
#cd /vinuthna
Permanent Mounting
Its now temporarily mounted, for permanent mount we need to add the entry in fstab,
for that let us get the mount entry from mtab using
#cat /etc/mtab
We need to make slight changes in fstab entry while entering the mount entry contents
copies from mtab; we need to change the rw to defaults
#vim /etc/fstab
Our fstab Entry want to be similar to below sample. Save and exit from fstab using wq!.
/dev/mapper/vg1 /vinuthna ext4 defaults 0 0
Note: Execute the below command to check for the fstab entry systax errors.
#mount -av
To extend, reduce a Logical Volume and Volume Group:
To extend size of logical volume
#lvextend -L <size><lv name>
Ex: lvextend -L +500M /dev/vg1/lv1
To make filesystem for extended size
#resize2fs <logical volume>
Ex: resize2fs /dev/vg1/lv1
Ex: xfs_growfs /dev/vg1/lv1 --> if you formatted with xfs use this command to reformat
To resize or reduce a logical volume
Note: Before reducing a logical volume, it is always good to backup the data, so that it
will not be a headache if something goes wrong and umount the logical volume.
#mkdir /vinuthna_backup
#cp -rf /vinuthna/* /vinuthna_backup
#umount -v /vinuthna
Checks the file system after unmount.
# df h
Then check for the file-system error using following command.
A&N Teck Solutions 3
Suresh Reddy (RHCE,RHCSS,CCNA)
[Link]@[Link]
# e2fsck -ff /dev/vg1/lv1
#xfs_repair -n device --> Then check for the file-system error in centos 7
Reduce the file system.
# resize2fs /dev/vg1/lv1 900M
Reduce the Logical Volume size than Current size.
#lvreduce -L <-size><LVM>
ex: lvreduce -L -100M /dev/vg1/lv1
Recheck the file system for error.
# e2fsck -ff /dev/vg1/lv1
Remount the file-system back to stage.
#mount /dev/vg1/lv1 /vinuthna
To remove an LVM
#umount <mountpoint>
#lvremove <logical volume>
Ex: #lvremove /dev/vg1/lv1
To extend volume group
1) Create another LVM enabled partition
2) Convert into physical volume
#vgextend <vg name><partition name>
Ex: #vgextend /dev/vg1 /dev/sda8
To reduce volume group
#vgreduce <vgname><partition name>
Ex: #vgreduce /dev/vg1 /dev/sda8
To remove volume group
#vgremove <vg name>
Ex: #vgremove /dev/vg1
To delete physical volumes
#pvremove <partitions>
Ex: #pvremove /dev/sda{5,6,7,8}
How to Take Snapshot of Logical Volume and Restore in LVM
LVM Snapshots are space efficient pointing time copies of lvm volumes. It works only
with lvm and consumes the space only when changes are made to the source logical
volume to snapshot volume. If source volume has a huge changes made to sum of 1GB
the same changes will be made to the snapshot volume. Its best to always have a small
size of changes for space efficient. In case the snapshot runs out of storage, we can use
lvextend to grow. And if we need to shrink the snapshot we can use lvreduce.
A&N Teck Solutions 4
Suresh Reddy (RHCE,RHCSS,CCNA)
[Link]@[Link]
If we have accidentally deleted any file after creating a Snapshot we dont have to worry
because the snapshot has the original file which we have deleted. It is possible if the file
was there when the snapshot was created. Dont alter the snapshot volume; keep as it
while snapshot used to do a fast recovery.
Snapshots cant be use for backup option. Backups are Primary Copy of some datas, so
we cant use snapshot as a backup option.
Creating LVM Snapshot:
First, check for free space in volume group to create a new snapshot.
#vgs
#lvcreate -L 500M -s -n vinuthna_data_snap /dev/vg1/lv_vinuthna
OR
#lvcreate --size 500M --snapshot --name vinuthna_data_snap /dev/vg1/ lv_vinuthna
-L or size: (500M) Size of snapshot.
-s: Creates snapshot.
-n: Creates name for the snapshot.
vinuthna_data_snap: New snapshots name.
/dev/vg1/lv1: Volume which we are going to create a snapshot.
#lvs --> check the newly created lvm snapshot volume
#df Th --> check the disk usage
#lvdisplay /dev/vg1/vinuthna_data_snap
Note: when copy more than 500MB of files in lv_vinuthna, you will get error message like
Input/output error, it means out of space in snapshot. If the logical volume becomes full it will
get dropped automatically and we cant use it any more, even though if we extend the size of
snapshot volume. It is the best idea to crate a snapshot with same size of source volume. So it
will never over flow, because it has enough space to take snap of your volume.
Extend Snapshot in LVM
If we need to extend the snapshot size before overflow we can do it using.
#lvextend -L +1G /dev/vg1/vinuthna_data_snap
To verify the new size and COW table using following command.
#lvdisplay /dev/vg1/vinuthna_data_snap
To know the size of snap volume and usage in %
#lvs
Restoring Snapshot or Merging
A&N Teck Solutions 5
Suresh Reddy (RHCE,RHCSS,CCNA)
[Link]@[Link]
To restore the snapshot, we need to un-mount the file system first.
# umount /mnt/lv_vinuthna/
To restore the snap using command lvconvert.
# lvconvert --merge /dev/vg1/vinuthna_data_snap
Note: After merge is completed, snapshot volume will be removed automatically. Now
we can see the space of our partition using df command.
# df -Th
# lvs
To Extend the Snapshots automatically
Open the lvm configuration file and Search for word autoextend
#vim /etc/lvm/[Link]
Change the auto extend threshold value 100 to 75 here and auto extend percent is 20. If
the snapshot volume reach 75% it will automatically expand the size of snap volume by
20% more. Thus, we can expand automatically. Save and exit.
A&N Teck Solutions 6
Suresh Reddy (RHCE,RHCSS,CCNA)
[Link]@[Link]