We use LVM to create partitions, physical volumes, a virtual group, logical volumes, and filesystems on a hard disk. We’ll also show how to mount, extend, and remove our newly created logical volumes. By the end of this tutorial, you’ll have a full understanding of how to use LVM and apply your configurations.
In this tutorial you will learn:
How to install LVM on major Linux distros
How to create partitions
How to create physical volumes
How to create a virtual group
How to create logical volumes
How to create a filesystem on logical volumes
How to edit fstab to automatically mount partitions
How to mount logical volumes
How to extend a logical volume
How to remove a logical volume
Identify the new hard disks
This is achieved using the lsblk
command. lsblk
lists information about all available or specified block devices. It reads the sysfs filesystem and udev db to gather information.
lsblk
Here, sdb
and sdc
is the new hard disks that we have added.
Create physical volumes
This is done with pvcreate
. This initializes physical volume(s) for later use by the Logical Volume Manager (LVM). Each physical volume can be a disk partition, whole disk, meta-device, or loopback file.
pvcreate /dev/sdb /dev/sdc
pvdisplay command for view volumes
pvdisplay
Create a volume group
vgcreate
creates a new volume group named DEMO on physical volumes /dev/sdb and /dev/sdc.
vgcreate DEMO /dev/sdb /dev/sdc
vgdisplay to view
vgdisplay
Create a logical volume
lvcreate
creates a logical volume in a volume group. In this case, it's creating a logical volume named demo_lab
with a size of 1G in the volume group DEMO
.
lvcreate -n demo_lab --size 1G DEMO
lvdisplay to view
lvdisplay
Partition of the new logical volume
fdisk
is a dialogue-driven command-line utility that creates and manipulates partition tables and partitions on a hard disk.
fdisk /dev/mapper/DEMO-demo_lab
Here you're asked to press n
to create a new partition. then press p
for primary then mention size or press enter, enter.
Then again will ask for the option press w
to write and exit
Error:
WARNING: Re-reading the partition table failed with error 22: Invalid argument. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.
then put the command below
partprobe /dev/DEMO/demo_lab
check if you are able to view the partition with
lsblk
Create a filesystem
mkfs.ext4
is used to create an ext4 filesystem on the partition.
mkfs.ext4 /dev/mapper/DEMO-demo_lab
Create a mount point and mount the logical volume
Create a directory that will serve as the mount point, and then use the mount
command to mount the logical volume.
mkdir our-demo
mount /dev/mapper/DEMO-demo_lab our-demo
Extend the logical volume
lvextend
allows you to extend the size of a logical volume. Here, you're extending the logical volume demo_lab
by an additional 2G.
lvextend -L +2G /dev/mapper/DEMO-demo_lab
Resize the filesystem
resizefs
resize the filesystem on the logical volume to use all of the available space.
resize2fs /dev/mapper/DEMO-demo_lab
Create a snapshot
lvcreate
with -s
creates a snapshot logical volume, which is a read-only copy of another logical volume.
lvcreate -L 1GB -s -n demo_lab_snap /dev/mapper/DEMO-demo_lab
Merge the snapshot
lvconvert
with --merge
will merge the snapshot back into its origin volume. If both the origin and snapshot volumes are not open the merge will start immediately, otherwise, it will be delayed until the origin volume becomes inactive.
lvconvert --merge /dev/mapper/DEMO-demo_lab_snap
Reboot and you can see the status
Mirror
It creates a mirror of data in all physical drives
lvcreate -L 2GB -m1 -n testmirror DEMO
Remove a logical volume
The command lvremove
can be used to remove logical volumes. We should make sure a logical volume does not have any valuable data stored on it before we attempt to remove it. Moreover, we should make sure the volume is not mounted.
lvremove /dev/mapper/DEMO-demo_lab
Now to remove the DEMO
volume
lvremove /dev/mapper/DEMO
In the above ways we can perform multiple tasks using Logical volume manager.